<?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.chat.GroupChatPlanMapper">
	<resultMap type="com.gic.haoban.manage.service.entity.chat.TabGroupChatPlan"
		id="result-map-tabHaobanGroupChatPlan">
		<result column="plan_id" property="planId" />
		<result column="name" property="name" />
		<result column="remark" property="remark" />
		<result column="wx_enterprise_id" property="wxEnterpriseId" />
		<result column="enterprise_id" property="enterpriseId" />
		<result column="chat_content" property="chatContent" />
		<result column="creator_id" property="creatorId" />
		<result column="creator_name" property="creatorName" />
		<result column="modifier_id" property="modifierId" />
		<result column="modifier_name" property="modifierName" />
		<result column="create_time" property="createTime" />
		<result column="update_time" property="updateTime" />
		<result column="status_flag" property="statusFlag" />
		<result column="send_type" property="sendType" />
		<result column="send_time" property="sendTime" />
		<result column="start_time" property="startTime" />
		<result column="end_time" property="endTime" />
		<result column="expire_days" property="expireDays" />
		<result column="staff_count" property="staffCount" />
		<result column="send_count" property="sendCount" />
		<result column="success_chat_count" property="successChatCount" />
		<result column="fail_chat_count" property="failChatCount" />
		<result column="send_flag" property="sendFlag" />
		<result column="material_from" property="materialFrom" />
	</resultMap>
	<sql id="Base_Column_List">
		plan_id,
		name,
		remark,
		chat_content,
		creator_id,
		creator_name,
		modifier_id,
		modifier_name,
		create_time,
		update_time,
		status_flag,
		send_type,
		send_time,
		start_time,
		end_time,
		expire_days,
		staff_count,
		send_count,
		success_chat_count,
		fail_chat_count ,
		wx_enterprise_id , enterprise_id , send_flag ,
		material_from
	</sql>
	<!-- ===================== 新增 ======================== -->
	<insert id="insert"
		parameterType="com.gic.haoban.manage.service.entity.chat.TabGroupChatPlan">
  <![CDATA[
  INSERT INTO tab_haoban_group_chat_plan(
	  plan_id,
	  name,
	  remark,
	  chat_content,
	  creator_id,
	  creator_name,
	  modifier_id,
	  modifier_name,
	  create_time,
	  update_time,
	  status_flag,
	  send_type,
	  send_time,
	  start_time,
	  end_time,
	  expire_days,
	  staff_count,
	  send_count,
	  success_chat_count,
	  fail_chat_count , wx_enterprise_id , enterprise_id , delete_flag , send_flag,
	  material_from
  )VALUES(
	  #{planId},
	  #{name},
	  #{remark},
	  #{chatContent},
	  #{creatorId},
	  #{creatorName},
	  #{modifierId},
	  #{modifierName},
	  now(),
	  now(),
	  #{statusFlag},
	  #{sendType},
	  #{sendTime},
	  #{startTime},
	  #{endTime},
	  #{expireDays},
	  #{staffCount},
	  #{sendCount},
	  #{successChatCount},
	  #{failChatCount} , #{wxEnterpriseId} , #{enterpriseId} , 0 , 1,
	  #{materialFrom}
  )
  ]]>
	</insert>

	<!-- =====================删除==================== -->
	<update id="deleteById">
		UPDATE
		tab_haoban_group_chat_plan SET delete_flag = 1 , modifier_id =
		#{clerkId} , modifier_name = #{clerkName} , update_time=now()  WHERE plan_id = #{planId}
	</update>
	
	<update id="stopById">
		UPDATE
		tab_haoban_group_chat_plan SET status_flag = 0 , modifier_id =
		#{clerkId} , modifier_name = #{clerkName} , end_time = now() , update_time=now() 
		<if test="null != modifyStartTimeFlag">
			, startTime = now() 
		</if>
		 WHERE plan_id = #{planId}
	</update>
	
	<update id="updateSendFlag">
		UPDATE tab_haoban_group_chat_plan SET send_flag = 2 , send_time = now()  WHERE plan_id = #{planId}
	</update>
	
	<!-- ==================更新 ========== -->
	<update id="updateById"
		parameterType="com.gic.haoban.manage.service.entity.chat.TabGroupChatPlan">
		  <![CDATA[
		  UPDATE tab_haoban_group_chat_plan SET
			  name=#{name},
			  remark=#{remark},
			  chat_content=#{chatContent},
			  modifier_id=#{modifierId},
			  modifier_name=#{modifierName},
			  update_time=now(),
			  staff_count = #{staffCount},
			  send_type=#{sendType},
			  send_time=#{sendTime},
			  start_time=#{startTime},
			  end_time=#{endTime},
			  expire_days=#{expireDays},
			  material_from=#{materialFrom}
		  where plan_id = #{planId}
		  ]]>
	</update>
	
	<update id="updateRemark"
		parameterType="com.gic.haoban.manage.service.entity.chat.TabGroupChatPlan">
		  <![CDATA[
		  UPDATE tab_haoban_group_chat_plan SET
			  remark=#{remark},
			  modifier_id=#{modifierId},
			  modifier_name=#{modifierName},
			  update_time=now()
		  where plan_id = #{planId}
		  ]]>
	</update>
	
	
	<!-- ============ 查询============= -->
	<select id="selectById" parameterType="long"
		resultMap="result-map-tabHaobanGroupChatPlan">
		SELECT
		<include refid="Base_Column_List" />
		FROM tab_haoban_group_chat_plan WHERE plan_id = #{planId} and delete_flag = 0
	</select>

	<select id="list" parameterType="com.gic.haoban.manage.api.dto.qdto.chat.GroupChatPlanSearchQDTO" resultMap="result-map-tabHaobanGroupChatPlan">
		select
		<include refid="Base_Column_List" />
		from tab_haoban_group_chat_plan where wx_enterprise_id=#{wxEnterpriseId}
		and enterprise_id = #{enterpriseId} and delete_flag = 0
		<if test="null != startDate">
			and create_time <![CDATA[>=]]>	#{startDate} and create_time <![CDATA[<=]]> #{endDate}
		</if>
		<if test="null != status and status==1">
			and start_time <![CDATA[<= ]]>	now() and end_time >= now()
		</if>
		<if test="null != status and status==2">
			and start_time > now()
		</if>
		<if test="null != status and status==3">
			and end_time  <![CDATA[<]]> now()
		</if>
		<if test="null != searchParams and '' != searchParams">
			and name like #{searchParams}
		</if>
		<if test="null !=creatorIdList and creatorIdList.size>0">
			 and creator_id in 
			 <foreach collection="creatorIdList" item="id" close=")" index="index" open="(" separator=",">
			 	#{id}
			 </foreach>
		</if>
		order by create_time desc
	</select>
	
	<update id="updateData">
		update tab_haoban_group_chat_plan set send_count = send_count+ #{sendCount} , success_chat_count=success_chat_count+#{successChatCount} , fail_chat_count = fail_chat_count+#{failChatCount} , 
		update_time =now()  where plan_id = #{planId}
	</update>
</mapper> 