<?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.moment.QwMomentPlanAttendMapper">
  <resultMap type="com.gic.haoban.manage.service.entity.moment.TabQwMomentPlanAttend" id="result-map-tabHaobanQwMomentPlanAttend">
      <result column="attend_id" javaType="long" jdbcType="BIGINT" property="attendId"/>
      <result column="plan_id" javaType="long" jdbcType="BIGINT" property="planId"/>
      <result column="wx_enterprise_id" javaType="String" jdbcType="CHAR" property="wxEnterpriseId"/>
      <result column="enterprise_id" javaType="String" jdbcType="CHAR" property="enterpriseId"/>
      <result column="staff_id" javaType="String" jdbcType="CHAR" property="staffId"/>
      <result column="clerk_id" javaType="String" jdbcType="CHAR" property="clerkId"/>
      <result column="clerk_code" javaType="String" jdbcType="VARCHAR" property="clerkCode"/>
      <result column="clerk_name" javaType="String" jdbcType="VARCHAR" property="clerkName"/>
      <result column="create_time" javaType="java.util.Date" jdbcType="TIMESTAMP" property="createTime"/>
      <result column="update_time" javaType="java.util.Date" jdbcType="TIMESTAMP" property="updateTime"/>
      <result column="delete_flag" javaType="int" jdbcType="INTEGER" property="deleteFlag"/>
      <result column="comment_count" javaType="int" jdbcType="INTEGER" property="commentCount"/>
      <result column="like_count" javaType="int" jdbcType="INTEGER" property="likeCount"/>
      <result column="task_status" javaType="int" jdbcType="INTEGER" property="taskStatus"/>
      <result column="send_time" javaType="java.util.Date" jdbcType="TIMESTAMP" property="sendTime"/>
      <result column="moment_id" javaType="String" jdbcType="VARCHAR" property="momentId"/>
      <result column="qw_data_time" javaType="java.util.Date" jdbcType="TIMESTAMP" property="qwDataTime"/>
      <result column="store_id" javaType="String" jdbcType="VARCHAR" property="storeId"/>
      <result column="status_flag" property="statusFlag" />
  </resultMap>
  <sql id="Base_Column_List">
	  attend_id,
	  plan_id,
	  wx_enterprise_id,
	  enterprise_id,
	  staff_id,
	  clerk_id,
	  clerk_code,
	  clerk_name,
	  create_time,
	  update_time,
	  delete_flag,
	  comment_count,
	  like_count,
	  task_status,
	  send_time,
	  moment_id,
	  qw_data_time,
	  store_id , status_flag
  </sql>

    <select id="list" resultType="com.gic.haoban.manage.api.dto.moment.QwMomentPlanAttendDTO"
            parameterType="com.gic.haoban.manage.api.qdto.moment.PlanClerkListQDTO">
        select   a.attend_id attendId ,
                 a.plan_id planId,
                 a.wx_enterprise_id wxEnterpriseId,
                 a.enterprise_id enterpriseId,
                 a.staff_id staffId,
                 b.staff_name staffName ,
                 a.clerk_id clerkId ,
                 a.clerk_code clerkCode,
                 a.clerk_name clerkName ,
                 a.create_time createTime,
                 a.update_time updateTime,
                 a.comment_count commentCount ,
                 a.like_count likeCount,
                 a.task_status taskStatus,
                 a.send_time sendTime ,
                 a.moment_id momentId,
                 a.qw_data_time qwDataTime ,
                 a.store_id storeId , a.status_flag statusFlag , reason reason  from tab_haoban_qw_moment_plan_attend a left join tab_haoban_staff b on a.staff_id = b.staff_id
                where a.plan_id =#{planId} and a.delete_flag = 0
                <if test="null != staffNameLike">
                    and b.staff_name  like #{staffNameLike}
                </if>
                <if test="null != clerkParamsLike">
                    and (a.clerk_code like #{clerkParamsLike} or a.clerk_name like #{clerkParamsLike})
                </if>
                <if test="null != startTime">
                    and ( a.create_time >= #{startTime} and  a.create_time <![CDATA[ <= ]]> #{endTime} )
                </if>
                <if test="sendStatus==0">
                    and a.task_status = 1
                </if>
                <if test="sendStatus==1">
                    and a.task_status = 1
                </if>
                <if test="sendStatus==2">
                    and a.task_status = 2
                </if>
                and a.status_flag = #{statusFlag}
        order by a.${sortColumn}  ${sortType}
    </select>

    <select id="listExistClerkIds" resultType="java.lang.String">
        select clerk_id from tab_haoban_qw_moment_plan_attend where plan_id = #{planId} and clerk_id in (
        <foreach collection="list" separator="," item="item">
            #{item}
        </foreach>
        ) and delete_flag = 0
    </select>


    <select id="listClerk" resultType="com.gic.haoban.manage.api.dto.moment.QwMomentPlanAttendDTO">
        select clerk_id clerkId , clerk_code clerkCode , clerk_name clerkName , staff_id staffId , store_id storeId , attend_id attendId
        from tab_haoban_qw_moment_plan_attend where plan_id = #{planId}
            <if test="null != statusFlag">
                and status_flag = #{statusFlag}
            </if>
        and delete_flag = 0
    </select>

    <select id="taskDetail" resultType="com.gic.haoban.manage.api.dto.moment.TaskDetailDTO">
        select
            b.title name ,
            b.remark remark ,
            b.exec_type execType ,
            b.begin_date startTime ,
            b.end_date endTime ,
            b.media_type mediaType ,
            a.attend_id attendId ,
            a.plan_id planId ,
            a.clerk_id clerkId ,
            a.clerk_name clerkName ,
            a.send_time sendTime ,
            a.like_count likeCount ,
            a.comment_count commentCount ,
            a.qw_data_time qwDataTime ,
            a.task_status taskStatus
            from tab_haoban_qw_moment_plan_attend a left join tab_haoban_qw_moment_plan b on a.plan_id = b.plan_id
        where a.plan_id = #{planId} and a.clerk_id = #{clerkId} and a.delete_flag = 0 and a.status_flag = 1
    </select>

    <select id="taskListWxa" resultType="com.gic.haoban.manage.api.dto.moment.TaskListWxaDTO"
            parameterType="com.gic.haoban.manage.api.qdto.moment.TaskListWxaQDTO">
            select
            b.title name ,
            b.remark remark ,
            b.exec_type execType ,
            b.begin_date startTime ,
            b.end_date endTime ,
            a.plan_id planId ,
            a.clerk_id clerkId ,
            a.clerk_name clerkName ,
            a.send_time sendTime ,
            a.like_count likeCount ,
            a.comment_count commentCount ,
            a.qw_data_time qwDataTime ,
            a.task_status taskStatus
            from tab_haoban_qw_moment_plan_attend a left join tab_haoban_qw_moment_plan b on a.plan_id = b.plan_id
        where a.plan_id = #{planId} and a.store_id = #{storeId}
        <if test="null != execClerkId and execClerkId !='' ">
            and a.clerk_id = #{execClerkId}
        </if>
        <if test="null != clerkId and clerkId !='' ">
            and a.clerk_id = #{clerkId}
        </if>
        <if test="null != taskStatus and taskStatus==0">
            and a.task_status = 1 and b.end_date <![CDATA[ <= ]]> now()
        </if>
        <if test="null != taskStatus and taskStatus==1">
            and a.task_status = 1 and b.end_date > now()
        </if>
        <if test="null != taskStatus and taskStatus==2">
            and a.task_status = 2
        </if>
    </select>
    <select id="listForData" resultMap="result-map-tabHaobanQwMomentPlanAttend" parameterType="java.lang.Long">
        select <include refid="Base_Column_List" /> from tab_haoban_qw_moment_plan_attend where plan_id =#{planId} and delete_flag = 0 and task_status = 2
    </select>

    <select id="getQwCount" resultType="com.gic.haoban.manage.service.pojo.bo.moment.CountBO">
        select sum(like_count) likeCount , sum(comment_count) commentCount from tab_haoban_qw_moment_plan_attend where plan_id=#{planId} and delete_flag = 0 and
            like_count is not null    and comment_count is not null
    </select>

    <select id="getById" resultMap="result-map-tabHaobanQwMomentPlanAttend"
            parameterType="java.lang.Long">
        select <include refid="Base_Column_List" /> from tab_haoban_qw_moment_plan_attend where attend_id = #{attendId}
    </select>

    <select id="getByStaff" resultMap="result-map-tabHaobanQwMomentPlanAttend">
        select <include refid="Base_Column_List" /> from tab_haoban_qw_moment_plan_attend where plan_id = #{planId} and staff_id =#{staffId}
    </select>

    <select id="getTotalTask" resultType="int">
        select count(1) from tab_haoban_qw_moment_plan_attend a LEFT JOIN tab_haoban_qw_moment_plan b ON a.plan_id = b.plan_id
        where  a.enterprise_id =  #{enterpriseId}  and b.end_date <![CDATA[ > ]]> now()
          AND a.clerk_id = #{clerkId}  AND a.task_status = 1 and a.delete_flag = 0  and b.status_flag = 1
    </select>

    <!-- 查询失效任务数-->
    <select id="getInvalidTask" resultType="int">
        select count(1) from tab_haoban_qw_moment_plan_attend a LEFT JOIN tab_haoban_qw_moment_plan b ON a.plan_id = b.plan_id
        where  a.enterprise_id =  #{enterpriseId}  and b.end_date = #{time}
          AND a.clerk_id = #{clerkId}  AND a.task_status = 1 and a.delete_flag = 0 and b.status_flag = 1
    </select>

    <update id="delClerks">
        update  tab_haoban_qw_moment_plan_attend set delete_flag = 1 , update_time=now() where plan_id = #{planId}
        <if test="null != list">
            and clerk_id in (
            <foreach collection="list" item="item" separator=",">
                #{item}
            </foreach>
            )
        </if>
    </update>


    <update id="finishTask">
        update  tab_haoban_qw_moment_plan_attend set task_status = 2 , send_time= #{sendTime}
                                                 <if test="null != staffId">
                                                     , staff_id = #{staffId}
                                                 </if>
                                                 where attend_id = #{attendId}
    </update>

    <update id="updateMomentId">
        update  tab_haoban_qw_moment_plan_attend set moment_id = #{momentId} where attend_id = #{id}
    </update>

    <update id="updateFail">
        update  tab_haoban_qw_moment_plan_attend set status_flag = 2 , reason = #{reason} where plan_id = #{planId}
            and clerk_id in (
            <foreach collection="list" item="item" separator=",">
                #{item}
            </foreach>
            )
    </update>

    <update id="updateSuccess">
        update  tab_haoban_qw_moment_plan_attend set status_flag = 1 where plan_id = #{planId}
        and clerk_id in (
        <foreach collection="list" item="item" separator=",">
            #{item}
        </foreach>
        )
    </update>

    <update id="updateSuccessStaff">
        update  tab_haoban_qw_moment_plan_attend set status_flag = 1 , staff_id=#{staffId} where plan_id = #{planId} and clerk_id = #{clerkId}
    </update>

    <update id="updateQwData">
        update  tab_haoban_qw_moment_plan_attend set like_count = #{likeCount} , comment_count=#{commentCount}, qw_data_time = now() where plan_id = #{planId} and clerk_id = #{clerkId}
    </update>

    <!-- ===================== 新增 ======================== -->
  <insert id="batchInsert">
  INSERT INTO tab_haoban_qw_moment_plan_attend(
	  attend_id,
	  plan_id,
	  wx_enterprise_id,
	  enterprise_id,
	  staff_id,
	  clerk_id,
	  clerk_code,
	  clerk_name,
	  delete_flag,
	  comment_count,
	  like_count,
	  task_status,
	  send_time,
	  moment_id,
	  qw_data_time,
	  store_id , status_flag
  )VALUES
          <foreach collection="list" item="item" separator=","  index="index">
       (
	  #{item.attendId,jdbcType=BIGINT},
	  #{item.planId,jdbcType=BIGINT},
	  #{item.wxEnterpriseId,jdbcType=CHAR},
	  #{item.enterpriseId,jdbcType=CHAR},
	  #{item.staffId,jdbcType=CHAR},
	  #{item.clerkId,jdbcType=CHAR},
	  #{item.clerkCode,jdbcType=VARCHAR},
	  #{item.clerkName,jdbcType=VARCHAR},
	  0,
	  #{item.commentCount,jdbcType=INTEGER},
	  #{item.likeCount,jdbcType=INTEGER},
	  #{item.taskStatus,jdbcType=INTEGER},
	  #{item.sendTime,jdbcType=TIMESTAMP},
	  #{item.momentId,jdbcType=VARCHAR},
	  #{item.qwDataTime,jdbcType=TIMESTAMP},
	  #{item.storeId,jdbcType=VARCHAR} , #{item.statusFlag}
              )
          </foreach>

  </insert>

</mapper>