<?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.QwMomentPlanMapper">
    <resultMap type="com.gic.haoban.manage.service.entity.moment.TabQwMomentPlan" id="result-map-tabHaobanQwMomentPlan">
        <result column="plan_id" javaType="long" jdbcType="BIGINT" property="planId"/>
        <result column="title" javaType="String" jdbcType="VARCHAR" property="title"/>
        <result column="remark" javaType="String" jdbcType="VARCHAR" property="remark"/>
        <result column="begin_date" javaType="java.util.Date"  property="beginDate"/>
        <result column="end_date" javaType="java.util.Date"  property="endDate"/>
        <result column="expire_days" javaType="int" jdbcType="INTEGER" property="expireDays"/>
        <result column="exec_type" javaType="int" jdbcType="INTEGER" property="execType"/>
        <result column="wx_enterprise_id" javaType="String" jdbcType="CHAR" property="wxEnterpriseId"/>
        <result column="enterprise_id" javaType="String" jdbcType="CHAR" property="enterpriseId"/>
        <result column="creator_id" javaType="String" jdbcType="VARCHAR" property="creatorId"/>
        <result column="creator_name" javaType="String" jdbcType="VARCHAR" property="creatorName"/>
        <result column="status_flag" javaType="int" jdbcType="INTEGER" property="statusFlag"/>
        <result column="delete_flag" javaType="int" jdbcType="INTEGER" property="deleteFlag"/>
        <result column="publish_type" javaType="int" jdbcType="INTEGER" property="publishType"/>
        <result column="publish_time" javaType="java.util.Date" jdbcType="TIMESTAMP" property="publishTime"/>
        <result column="plan_clerk_type" javaType="int" jdbcType="INTEGER" property="planClerkType"/>
        <result column="total_num" javaType="int" jdbcType="INTEGER" property="totalNum"/>
        <result column="done_num" javaType="int" jdbcType="INTEGER" property="doneNum"/>
        <result column="stop_time" javaType="java.util.Date" jdbcType="TIMESTAMP" property="stopTime"/>
        <result column="media_type" javaType="int" jdbcType="INTEGER" property="mediaType"/>
        <result column="media_info" javaType="String"  property="mediaInfo"/>
        <result column="media_info_show" javaType="String"  property="mediaInfoShow"/>
        <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="total_like_count" javaType="int" jdbcType="INTEGER" property="totalLikeCount"/>
        <result column="total_comment_count" javaType="int" jdbcType="INTEGER" property="totalCommentCount"/>
        <result column="qw_jobid" javaType="String" jdbcType="VARCHAR" property="qwJobid"/>
        <result column="exec_rate" javaType="int" jdbcType="INTEGER" property="execRate"/>
        <result column="task_clerk_num" property="taskClerkNum" />
        <result column="moment_id" property="momentId" />
        <result column="media_link_flag" property="mediaLinkFlag" />
    </resultMap>
    <sql id="Base_Column_List">
        plan_id,
	  title,
	  remark,
	  begin_date,
	  end_date,
	  expire_days,
	  exec_type,
	  wx_enterprise_id,
	  enterprise_id,
	  creator_id,
	  creator_name,
	  status_flag,
	  delete_flag,
	  publish_type,
	  publish_time,
	  plan_clerk_type,
	  total_num,
	  done_num,
	  stop_time,
	  media_type,
	  media_info,
	  media_info_show,
	  create_time,
	  update_time,
	  total_like_count,
	  total_comment_count,
	  qw_jobid,
	  exec_rate , task_clerk_num , moment_id , media_link_flag
    </sql>
    <!-- ===================== 新增 ======================== -->
    <insert id="insert">
        INSERT INTO tab_haoban_qw_moment_plan(
            plan_id,
            title,
            remark,
            begin_date,
            end_date,
            expire_days,
            exec_type,
            wx_enterprise_id,
            enterprise_id,
            creator_id,
            creator_name,
            status_flag,
            publish_type,
            publish_time,
            plan_clerk_type,
            total_num,
            done_num,
            stop_time,
            media_type,
            media_info,
            media_info_show,
            total_like_count,
            total_comment_count,
            qw_jobid,
            exec_rate , media_link_flag
        )VALUES(
                   #{planId,jdbcType=BIGINT},
                   #{title,jdbcType=VARCHAR},
                   #{remark,jdbcType=VARCHAR},
                   #{beginDate},
                   #{endDate},
                   #{expireDays,jdbcType=INTEGER},
                   #{execType,jdbcType=INTEGER},
                   #{wxEnterpriseId,jdbcType=CHAR},
                   #{enterpriseId,jdbcType=CHAR},
                   #{creatorId,jdbcType=VARCHAR},
                   #{creatorName,jdbcType=VARCHAR},
                   #{statusFlag,jdbcType=INTEGER},
                   #{publishType,jdbcType=INTEGER},
                   #{publishTime,jdbcType=TIMESTAMP},
                   #{planClerkType,jdbcType=INTEGER},
                   #{totalNum,jdbcType=INTEGER},
                   #{doneNum,jdbcType=INTEGER},
                   #{stopTime,jdbcType=TIMESTAMP},
                   #{mediaType,jdbcType=INTEGER},
                   #{mediaInfo},
                   #{mediaInfoShow},
                   #{totalLikeCount,jdbcType=INTEGER},
                   #{totalCommentCount,jdbcType=INTEGER},
                   #{qwJobid,jdbcType=VARCHAR},
                   #{execRate,jdbcType=INTEGER} , #{mediaLinkFlag}
               )
  </insert>

    <update id="update" parameterType="com.gic.haoban.manage.service.entity.moment.TabQwMomentPlan">
        UPDATE tab_haoban_qw_moment_plan <set>

                <if test="null != title">
            title=#{title},
                </if>
                <if test="null != remark">
            remark=#{remark},
                </if>
                <if test="null != beginDate">
            begin_date=#{beginDate},
                </if>
                <if test="null != endDate">
            end_date=#{endDate},
                </if>
                <if test="null != expireDays">
            expire_days=#{expireDays},
                </if>
                <if test="null != execType">
            exec_type=#{execType},
                </if>
                <if test="null != publishType">
            publish_type=#{publishType},
                </if>
                <if test="null != publishTime">
            publish_time=#{publishTime},
                </if>
                <if test="null != planClerkType">
            plan_clerk_type=#{planClerkType},
                </if>
                <if test="null != stopTime">
            stop_time=#{stopTime},
                </if>
                <if test="null != mediaType">
            media_type=#{mediaType},
                </if>
                <if test="null != mediaInfo">
            media_info=#{mediaInfo},
                </if>
                <if test="null != mediaInfoShow">
            media_info_show=#{mediaInfoShow},
                </if>
            <if test="null != mediaLinkFlag">
                media_link_flag=#{mediaLinkFlag},
                </if>
    </set>
        where plan_id =#{planId}
  </update>

    <select id="getById" resultMap="result-map-tabHaobanQwMomentPlan">
        select <include refid="Base_Column_List" /> from tab_haoban_qw_moment_plan where plan_id =#{planId} and delete_flag = 0
    </select>

    <select id="planListWxa" resultType="com.gic.haoban.manage.api.dto.moment.QwMomentPlanWxaDTO"
            parameterType="com.gic.haoban.manage.api.qdto.moment.PlanListWxaQDTO">
        select a.plan_id planId ,
        a.title name ,
        a.begin_date startTime ,
        a.end_date endTime ,
        sum(ifnull(b.like_count,0)) likeCount ,
        sum(ifnull(b.comment_count,0)) commentCount ,
        count(1) totalNum,
        sum(IF(b.task_status = 2, 1, 0 )) AS execNum,
        SUM(IF( a.status_flag = 1 AND b.task_status = 1 AND end_date >= NOW() , 1, 0 )) AS notExecNum
        from tab_haoban_qw_moment_plan a left join tab_haoban_qw_moment_plan_attend b on a.plan_id = b.plan_id
        where a.wx_enterprise_id=#{wxEnterpriseId} and a.enterprise_id=#{enterpriseId} and b.store_id=#{storeId}
        and b.status_flag = 1 and a.delete_flag = 0
        <if test="null != clerkId">
            and b.clerk_id = #{clerkId}
        </if>
        <if test="null != nameLike">
            and a.title like #{nameLike}
        </if>
            group by a.plan_id
         <if test="planStatus==0">
             having notExecNum > 0
         </if>
         <if test="planStatus==1">
             having notExecNum = 0
         </if>
        ORDER BY a.publish_time DESC , a.plan_id DESC
    </select>

    <select id="getTaskNum" resultType="java.lang.Integer"
            parameterType="com.gic.haoban.manage.api.qdto.moment.TaskNumQDTO">
        select count(1) from (
        select  a.plan_id
        from tab_haoban_qw_moment_plan a left join tab_haoban_qw_moment_plan_attend b on a.plan_id = b.plan_id
        where a.wx_enterprise_id=#{wxEnterpriseId} and a.enterprise_id=#{enterpriseId} and b.store_id=#{storeId}
        and b.status_flag = 1 and a.end_date > now() and a.status_flag = 1  and b.task_status = 1
        <if test="null != clerkId">
            and b.clerk_id = #{clerkId}
        </if>
        group by a.plan_id ) t
    </select>

    <select id="list" resultMap="result-map-tabHaobanQwMomentPlan"
            parameterType="com.gic.haoban.manage.api.qdto.moment.QwMomentPlanQDTO">
        select <include refid="Base_Column_List"/> from tab_haoban_qw_moment_plan where  enterprise_id = #{enterpriseId}
        <if test="null != wxEnterpriseId">
           and wx_enterprise_id=#{wxEnterpriseId}
        </if>
        <if test="statusFlag ==0">
            and ( status_flag = 1 and begin_date  <![CDATA[ > ]]> NOW()  )
        </if>
        <if test="statusFlag ==1">
            and ( status_flag = 1   and end_date  <![CDATA[ >= ]]> NOW()  and begin_date  <![CDATA[ <= ]]> now()  )
        </if>
        <if test="statusFlag ==2">
            and ( status_flag = 2 )
        </if>
        <if test="statusFlag ==3">
            and ( status_flag = 1 and end_date  <![CDATA[ < ]]> NOW() )
        </if>

        <if test="null != startTime" >
            and create_time >= #{startTime} and create_time <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="null != nameParamsLike" >
            and ( title like #{nameParamsLike} or plan_id = #{nameParams} )
        </if>
        <if test="null != execType">
            and exec_type = #{execType}
        </if>
        <if test="null != creatorIdList and creatorIdList.size()>0">
            AND creator_id IN
            <foreach collection="creatorIdList" separator="," open="(" close=")" item="item">
                #{item}
            </foreach>
        </if>
        and delete_flag = 0 order by ${sortColumn}  ${sortType}
    </select>

    <select id="listForData" resultType="java.lang.Long">
        <if test="type==1">
            select plan_id from tab_haoban_qw_moment_plan where status_flag = 1 and begin_date <![CDATA[ < ]]> now() and end_date > now() and delete_flag = 0
        </if>
        <if test="type==2">
            select plan_id from tab_haoban_qw_moment_plan where status_flag = 1 and begin_date <![CDATA[ < ]]> now() and end_date > DATE_ADD(now(), INTERVAL -3 DAY) and delete_flag = 0 and done_num > 0
        </if>
    </select>

    <select id="listForStop" resultMap="result-map-tabHaobanQwMomentPlan">
        select <include refid="Base_Column_List" /> from tab_haoban_qw_moment_plan where end_date =#{time} and delete_flag =0 and status_flag = 1 and exec_type = 0
    </select>

    <update id="del" parameterType="java.lang.Long">
        update tab_haoban_qw_moment_plan set delete_flag = 1 where plan_id = #{planId}
    </update>

    <update id="stop" parameterType="java.lang.Long">
        update tab_haoban_qw_moment_plan set status_flag = 2 , stop_time = now() where plan_id = #{planId}
    </update>

    <update id="updateClerkNum">
        update tab_haoban_qw_moment_plan <set>
                         <if test="-1 != totalNum">
                             total_num = #{totalNum} ,
                         </if>
                            <if test="-1 != taskClerkNum">
                                task_clerk_num = #{taskClerkNum} ,
                            </if>
                            <if test="-1 != doneNum">
                                done_num = ifnull(done_num,0) + 1 , exec_rate = done_num *10000 / task_clerk_num
                            </if>
                          </set>
          where plan_id = #{planId}
    </update>

    <update id="updateJobid">
        update tab_haoban_qw_moment_plan set qw_jobid = #{jobId} where plan_id = #{planId}
    </update>

    <update id="updateMomentId">
        update tab_haoban_qw_moment_plan set moment_id = #{momentId} where plan_id = #{planId}
    </update>

    <update id="updateQwData">
        update tab_haoban_qw_moment_plan set total_like_count = #{like} , total_comment_count = #{comment} where plan_id = #{planId}
    </update>
</mapper>