<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.gic.haoban.manage.service.dao.mapper.TabHaobanMaterialMapper" >
  <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.TabHaobanMaterial" >
    <id column="material_id" property="materialId" jdbcType="VARCHAR" />
    <result column="material_title" property="materialTitle" jdbcType="VARCHAR" />
    <result column="from_material_id" property="fromMaterialId" jdbcType="VARCHAR"/>
    <result column="material_type" property="materialType" jdbcType="INTEGER" />
    <result column="category_id" property="categoryId" jdbcType="VARCHAR" />
    <result column="material_content" property="materialContent" jdbcType="VARCHAR" />
    <result column="wx_last_upload_time" property="wxLastUploadTime" jdbcType="TIMESTAMP" />
    <result column="media_id" property="mediaId" jdbcType="VARCHAR" />
    <result column="wx_enterprise_id" property="wxEnterpriseId" jdbcType="VARCHAR" />
    <result column="staff_id" property="staffId" jdbcType="VARCHAR" />
    <result column="staff_name" property="staffName" jdbcType="VARCHAR" />
    <result column="img_url" property="imgUrl" jdbcType="VARCHAR" />
    <result column="wx_img_url" property="wxImgUrl" jdbcType="VARCHAR" />
    <result column="material_desc" property="materialDesc" jdbcType="VARCHAR" />
    <result column="status_flag" property="statusFlag" jdbcType="INTEGER" />
    <result column="link" property="link" jdbcType="VARCHAR" />
    <result column="app_id" property="appId" jdbcType="VARCHAR"/>
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
  </resultMap>
  <sql id="Base_Column_List" >
    material_id,from_material_id, material_title, material_type, category_id, material_content, wx_last_upload_time,
    media_id, wx_enterprise_id, staff_id, staff_name, img_url, material_desc, status_flag,
    link,app_id, create_time, update_time,wx_img_url
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from tab_haoban_material
    where material_id = #{materialId,jdbcType=VARCHAR}
  </select>

    <select id="listByids" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_material
        where material_id in
        <foreach collection="materialIds" item="item" open="(" close=")" separator=",">
            #{item,jdbcType=VARCHAR}
        </foreach>
    </select>

  <insert id="insert" parameterType="com.gic.haoban.manage.service.entity.TabHaobanMaterial" >
    insert into tab_haoban_material (material_id, from_material_id, material_title, material_type,
      category_id, material_content, wx_last_upload_time, 
      media_id, wx_enterprise_id, staff_id, 
      staff_name, img_url,wx_img_url, material_desc,
    status_flag, link,app_id, create_time,
      update_time)
    values (#{materialId,jdbcType=VARCHAR},#{fromMaterialId}, #{materialTitle,jdbcType=VARCHAR},
    #{materialType,jdbcType=INTEGER},
      #{categoryId,jdbcType=VARCHAR}, #{materialContent,jdbcType=VARCHAR}, #{wxLastUploadTime,jdbcType=TIMESTAMP}, 
      #{mediaId,jdbcType=VARCHAR}, #{wxEnterpriseId,jdbcType=VARCHAR}, #{staffId,jdbcType=VARCHAR}, 
      #{staffName,jdbcType=VARCHAR}, #{imgUrl,jdbcType=VARCHAR},#{wxImgUrl,jdbcType=VARCHAR}, #{materialDesc,jdbcType=VARCHAR},
    #{statusFlag,jdbcType=INTEGER}, #{link,jdbcType=VARCHAR},#{appId,jdbcType=VARCHAR},
    #{createTime,jdbcType=TIMESTAMP},
      #{updateTime,jdbcType=TIMESTAMP})
  </insert>
  

  <insert id="batchInsert" parameterType="com.gic.haoban.manage.service.entity.TabHaobanMaterial">
    insert into tab_haoban_material
    (
        material_id,
        from_material_id,
        material_title,
        material_type,
        category_id,
        material_content,
        wx_last_upload_time,
        media_id,
        wx_enterprise_id,
        staff_id,
        staff_name,
        img_url,
        wx_img_url,
        material_desc,
        status_flag,
        link,
        app_id,
        create_time,
        update_time
    )
    VALUES
    <foreach collection="records" item="one" separator=",">
           (
            #{one.materialId,jdbcType=VARCHAR},
            #{one.fromMaterialId,jdbcType=VARCHAR},
            #{one.materialTitle,jdbcType=VARCHAR},
            #{one.materialType,jdbcType=INTEGER},
            #{one.categoryId,jdbcType=VARCHAR},
            #{one.materialContent,jdbcType=VARCHAR},
            #{one.wxLastUploadTime,jdbcType=TIMESTAMP},
            #{one.mediaId,jdbcType=VARCHAR},
            #{one.wxEnterpriseId,jdbcType=VARCHAR},
            #{one.staffId,jdbcType=VARCHAR},
            #{one.staffName,jdbcType=VARCHAR},
            #{one.imgUrl,jdbcType=VARCHAR},
            #{one.wxImgUrl,jdbcType=VARCHAR},
            #{one.materialDesc,jdbcType=VARCHAR},
            #{one.statusFlag,jdbcType=INTEGER},
            #{one.link,jdbcType=VARCHAR},
            #{one.appId,jdbcType=VARCHAR},
            #{one.createTime,jdbcType=TIMESTAMP},
            #{one.updateTime,jdbcType=TIMESTAMP}
            )
    </foreach>
  </insert>

  <update id="updateByPrimaryKeySelective" parameterType="com.gic.haoban.manage.service.entity.TabHaobanMaterial" >
    update tab_haoban_material
    <set >
      <if test="materialTitle != null" >
        material_title = #{materialTitle,jdbcType=VARCHAR},
      </if>
      <if test="materialType != null" >
        material_type = #{materialType,jdbcType=INTEGER},
      </if>
      <if test="categoryId != null" >
        category_id = #{categoryId,jdbcType=VARCHAR},
      </if>
      <if test="materialContent != null" >
        material_content = #{materialContent,jdbcType=VARCHAR},
      </if>
      <if test="wxLastUploadTime != null" >
        wx_last_upload_time = #{wxLastUploadTime,jdbcType=TIMESTAMP},
      </if>
      <if test="mediaId != null" >
        media_id = #{mediaId,jdbcType=VARCHAR},
      </if>
      <if test="wxEnterpriseId != null" >
        wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR},
      </if>
      <if test="staffId != null" >
        staff_id = #{staffId,jdbcType=VARCHAR},
      </if>
      <if test="staffName != null" >
        staff_name = #{staffName,jdbcType=VARCHAR},
      </if>
      <if test="imgUrl != null" >
        img_url = #{imgUrl,jdbcType=VARCHAR},
      </if>
      <if test="wxImgUrl != null" >
        wx_img_url = #{wxImgUrl,jdbcType=VARCHAR},
      </if>
      <if test="materialDesc != null" >
        material_desc = #{materialDesc,jdbcType=VARCHAR},
      </if>
      <if test="statusFlag != null" >
        status_flag = #{statusFlag,jdbcType=INTEGER},
      </if>
      <if test="link != null" >
        link = #{link,jdbcType=VARCHAR},
      </if>
      <if test="appId != null">
        app_id = #{appId,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null" >
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where material_id = #{materialId,jdbcType=VARCHAR}
  </update>
  
  <select id="listMaterialByCategoryId" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from tab_haoban_material
    where category_id = #{categoryId,jdbcType=VARCHAR}
    and status_flag = 1
    order by update_time desc 
  </select>
  
  <select id="listMaterial" resultMap="BaseResultMap" >
    select 
    <include refid="Base_Column_List" />
    from tab_haoban_material
    where status_flag = 1
    and wx_enterprise_id = #{wxEnterpriseId}
    <if test="keyword != null and keyword != ''">
   	and material_title like CONCAT('%',#{keyword},'%')
    </if> 
    <if test="materialType != null">
   	and material_type = #{materialType}
    </if> 
      <if test="categoryId != null">
   	and category_id = #{categoryId}
    </if> 
    
    order by update_time desc
  </select>

  <select id="listMaterialByCategoryIds" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from tab_haoban_material
    where status_flag = 1
    and wx_enterprise_id = #{wxEnterpriseId}
    <if test="keyword != null and keyword != ''">
      and material_title like CONCAT('%',#{keyword},'%')
    </if>
    <if test="materialType != null">
      and material_type = #{materialType}
    </if>
    <if test="categoryIds != null and categoryIds.size>0">
      and category_id in
      <foreach collection="categoryIds" item="item" open="(" separator="," close=")">
        #{item}
      </foreach>
    </if>

    order by update_time desc
  </select>

  <select id="selectByfromMaterialId" resultMap="BaseResultMap" parameterType="java.lang.String">
    select
    <include refid="Base_Column_List"/>
    from tab_haoban_material
    where from_material_id = #{fromMaterialId,jdbcType=VARCHAR}
    and status_flag=1
    and wx_enterprise_id=#{wxEnterpriseId}
  </select>

  <select id="listByfromMaterialId" resultMap="BaseResultMap" parameterType="java.lang.String">
    select
    <include refid="Base_Column_List"/>
    from tab_haoban_material
      where from_material_id = #{fromMaterialId,jdbcType=VARCHAR}
    and status_flag=1
  </select>

  <update id="delMaterialByIds">
    update tab_haoban_material
    set
      status_flag = 0,
    update_time = now()
    where material_id IN
    <foreach collection="ids" item="item" separator="," open="(" close=")">
      #{item}
    </foreach>
    or from_material_id in
    <foreach collection="ids" item="item" separator="," open="(" close=")">
      #{item}
    </foreach>
  </update>
</mapper>