<?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.yeejoin.amos.patrol.business.dao.mapper.MsgMapper">
    <!-- 根据ID获取点的项信息 -->

    <select id="getPushUserBo" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.PushTargetBo">
    	SELECT
		msc.user_id AS userId
	FROM
		v_msg_subscribe_check msc
	WHERE
		(
			FIND_IN_SET(user_id, #{userIds})>0
			OR FIND_IN_SET(#{routeId}, msc.route) > 0
		)
		<if test="type=='email'">
		and (
			msc.check_email = 'all'
			OR msc.check_email = #{checkType}
		)
		</if>
		<if test="type=='app'">
		and (
			msc.check_type = 'all'
			OR msc.check_type = #{checkType}
		)
		</if>
    </select>
    <select id="getAppPushTargetBo" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.PushTargetBo">

		SELECT
		msc.user_id AS userId
		FROM
		v_msg_subscribe_check msc
		<where>
			<if test="userId!=null">
				(
				(
				msc.check_type = 'all'
				<if test="checkType!=null">
					OR msc.check_type = #{checkType}
				</if>
				)
				AND msc.user_id = #{userId}
				)
			</if>
			<if test="routeId!=0 and routeId!=null ">
				OR (
				find_in_set(#{routeId}, msc.route) > 0
				AND (
				msc.check_type = 'all'
				<if test="checkType!=null">
					OR msc.check_type = #{checkType}
				</if>
				)
				)
			</if>
		</where>
    </select>

     <select id="getEmailPushTargetBo" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.PushTargetBo">
		SELECT
		msc.user_id AS userId
		FROM
		v_msg_subscribe_check msc
		<where>
			<if test="userId!=null">
				(
				(
				msc.check_email = 'all'
				<if test="checkEmail!=null">
					OR msc.check_email = #{checkEmail}
				</if>
				)
				AND msc.user_id = #{userId}
				)
			</if>
			<if test="routeId!=null">
				OR (
				find_in_set(#{routeId}, msc.route) > 0
				AND (
				msc.check_email = 'all'
				<if test="checkEmail!=null">
						OR msc.check_email = #{checkEmail}
				</if>
				)
				)
			</if>
		</where>
    </select>

    <select id="getCheckMsgBos" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.CheckMsgBo">
		  SELECT
				pc.id checkId,
				pc.is_ok AS 'status',
				pc.route_id AS 'routeId',
				<![CDATA[
				CONCAT(
					'巡检地点：',
					'address',
					'<br />点编号：',
					pp.point_no,
					'<br />当前状态：',
					CASE pc.is_ok WHEN 1 THEN '合格'
					WHEN 2 THEN '不合格'
					WHEN 3 THEN '漏检' END,
					'<br />是否合格：',
					CASE pc.is_ok WHEN 1 THEN '合格'
					WHEN 2 THEN '不合格'
					WHEN 3 THEN '漏检' END,
					'<br />不合格项：',
					IFNULL(pc.error,'无'),
					'<br />巡检人员：',
					pc.user_name ,
					'<br />巡检时间：',
					DATE_FORMAT(
						pc.check_time,
						'%Y-%m-%d %H:%i:%s'
					)
				)  ]]> saveMsg,
				CONCAT(
					'巡检地点：',
					'address',
					'；点编号：',
					pp.point_no,
					'；当前状态：',
					CASE pc.is_ok WHEN 1 THEN '合格'
					WHEN 2 THEN '不合格'
					WHEN 3 THEN '漏检' END,
					'；是否合格：',
					CASE pc.is_ok WHEN 1 THEN '合格'
					WHEN 2 THEN '不合格'
					WHEN 3 THEN '漏检' END,
					'；不合格项：',
					IFNULL(pc.error,'无'),
					' 巡检人员：',
					pc.user_name ,
					'；巡检时间：',
					DATE_FORMAT(
						pc.check_time,
						'%Y-%m-%d %H:%i:%s'
					)
				) pushMsg,
				pc.user_name name,
				pc.user_id userId,
				pc.org_code orgCode,
				pc.point_id pointId,
				pp.risk_source_id riskSourceId
			FROM
			p_check pc
			LEFT JOIN	p_point pp ON pp.id = pc.point_id
			<where>
				<if test="checkId!=null">
				AND pc.id = #{checkId}
				</if>
			</where>
    </select>

    <select id="getTaskMsgBos" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.TaskMsgBo">
		  SELECT
				pt.id taskId,
				pt.`status`,
				<![CDATA[
				CONCAT(
					'您好，您有一条名称为：',
					pt.title,
					' <br />内容为：',
					pt.remark,
					' 的任务单。<br />要求完成时间为：',
					DATE_FORMAT(
						pt.finish_time,
						'%Y-%m-%d %H:%i:%s'
					),
					'  请您及时处理！<br />任务下发人：',
					pt.publisher_name) saveMsg,
				]]>

				CONCAT(
					'您好，您有一条名称为：',
					pt.title,
					' 内容为：',
					pt.remark,
					' 的任务单。要求完成时间为：',
					DATE_FORMAT(
						pt.finish_time,
						'%Y-%m-%d %H:%i:%s'
					),
					' 请您及时处理！任务下发人：',
					pt.publisher_name
				) pushMsg,
				pt.executor_id AS reciver,
				pt.executor as reciverName,
				pt.org_code orgCode
			FROM
				p_task pt
			<where>
				<if test="taskId!=null">
				AND pt.id = #{taskId}
				</if>
			</where>
    </select>

    <select id="countMsgVoData" resultType="long">
	    SELECT
		    COUNT(1) AS total
		FROM
		    `p_msg` m
		<where>
		  <if test="orgCode!=null"> and (org_Code like concat (#{orgCode},"-%")or org_Code= #{orgCode})</if>
		  <if test="userId != null">AND m.user_id = #{userId}</if>
		  <if test="userName != null">AND m.`reciver_name` LIKE concat('%', #{userName}, '%')</if>
		  <if test="msgType != null">AND m.msg_type = #{msgType}</if>
          <if test="beginDate != null">AND m.create_date <![CDATA[>=]]> #{beginDate}</if>
          <if test="endDate != null">AND m.create_date <![CDATA[<=]]> #{endDate}</if>
		</where>
    </select>

    <select id="getMsgVoData" resultType="com.yeejoin.amos.patrol.business.vo.MsgVo">
        SELECT
            m.id,
            m.`reciver_name` AS userName,
            m.body AS msgBody,
            m.msg_type AS msgType,
            m.send_time AS sendTime,
            CASE m.is_immediately
        WHEN '1' THEN
            '是'
        ELSE
            '否'
        END AS isImmediately,
         m.fixed_time AS fixedTime
        FROM
            `p_msg` m
        <where>
		  <if test="orgCode!=null"> and (org_Code like concat (#{orgCode},"-%")or org_Code= #{orgCode})</if>
		  <if test="userId != null">AND m.user_id = #{userId}</if>
          <if test="userName != null">AND m.`reciver_name` LIKE concat('%', #{userName}, '%')</if>
          <if test="msgType != null">AND m.msg_type = #{msgType}</if>
          <if test="beginDate != null">AND m.create_date <![CDATA[>=]]> #{beginDate}</if>
          <if test="endDate != null">AND m.create_date <![CDATA[<=]]> #{endDate}</if>
        </where>
        ORDER BY m.id DESC
        <choose>
         <when test="pageSize==-1"></when>
         <when test="pageSize!=-1">limit #{offset},#{pageSize}</when>
        </choose>
    </select>

    <resultMap id="msgConfigParam" type="com.yeejoin.amos.patrol.business.param.MsgConfigParam">
       <result property="orgCode" column="orgCode" />
       <result property="userId" column="userId" />
       <result property="msgType" column="msgType" />
       <result property="msgTypeValue" column="attribute1" />
       <result property="value" column="attribute2" />
       <result property="name" column="name" />
       <result property="username" column="username" />
    </resultMap>
      <select id="getPlanTaskMsgConfigByUserIdAndStatus" resultMap="msgConfigParam">
	   SELECT
			pms.org_code orgCode,
			pms.user_id userId,
			pms.msg_type msgType,
			pms.attribute1,
			pms.attribute2
			<!-- su.`name`,
			su.user_name username -->
		FROM
			`p_msg_subscribe` pms
		<!-- LEFT JOIN s_user su ON su.id = pms.user_id -->

		WHERE
			pms.attribute1 = #{status}
		AND pms.user_id in
		<foreach item="userId" collection="userIds" index="index" open="(" separator="," close=")">
			#{userId}
		</foreach>
		AND (
			pms.msg_type = 'planBegin'
			OR pms.msg_type = 'planWarn'
			OR pms.msg_type = 'planEnd'
		)
    </select>
    <!-- 即将开始-->
     <select id="getPlanTaskWarnMsgBos" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.TaskMsgBo">
    	SELECT
				ppt.id taskId,
				ppt.finish_status status,
				<![CDATA[
				CONCAT(
					'您好，巡检工作即将开始！<br />计划名称：',
					pp.`name`,
					' <br />巡检路线：',
					pr.`name`,
					' <br />开始时间：',
					ppt.begin_time,
					' <br />结束时间：',
					ppt.end_time) saveMsg,
					]]>
				CONCAT(
					'您好，巡检工作即将开始！计划名称：',
					pp.`name`,
					' 巡检路线：',
					pr.`name`,
					' 开始时间：',
					ppt.begin_time,
					' 结束时间：',
					ppt.end_time
				) pushMsg,
				ppt.user_id AS reciver,
				ppt.user_id AS reciverName,
				ppt.org_code AS orgCode
			FROM
				p_plan_task ppt
				LEFT JOIN p_plan pp ON pp.id = ppt.plan_id
				LEFT JOIN p_route pr ON ppt.route_id = pr.id
			WHERE
				ppt.finish_status = 0
			AND ppt.id = #{planTaskId}
    </select>
    <!-- 开始尚未进行-->
     <select id="getPlanTaskBeginMsgBos" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.TaskMsgBo">
    	SELECT
			ppt.id taskId,
			ppt.finish_status status,
			<![CDATA[
			CONCAT(
				'您好，巡检工作未按计划开始！<br />计划名称：',
				pp.`name`,
				' <br />巡检路线：',
				pr.`name`,
				' <br />开始时间：',
				ppt.begin_time,
				' <br />结束时间：',
				ppt.end_time
			) saveMsg,
			]]>
			CONCAT(
				'您好，巡检工作未按计划开始！计划名称：',
				pp.`name`,
				'; 巡检路线：',
				pr.`name`,
				'; 开始时间：',
				ppt.begin_time,
				'; 结束时间：',
				ppt.end_time
			) pushMsg,
			ppt.user_id AS reciver,
			ppt.user_id AS reciverName,
			ppt.org_code AS orgCode
		FROM
			p_plan_task ppt
		LEFT JOIN p_plan pp ON pp.id = ppt.plan_id
		LEFT JOIN p_route pr ON ppt.route_id = pr.id
		WHERE
			ppt.finish_status = 1
		AND ppt.id = #{planTaskId}
    </select>
    <!-- 以结束有漏检 -->
    <select id="getPlanTaskEndMsgBos" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.TaskMsgBo">
    	SELECT
			ppt.id taskId,
			pptd.is_finish,
			<![CDATA[
			CONCAT(
				'您好，巡检工作已结束！<br />以下地点尚未巡检：',
				GROUP_CONCAT(p.`name`),
				'<br />计划名称：',
				pp.`name`,
				' <br />巡检路线：',
				pr.`name`,
				' <br />开始时间：',
				ppt.begin_time,
				' <br />结束时间：',
				ppt.end_time
			) saveMsg,
			]]>
			CONCAT(
				'您好，巡检工作已结束！以下地点尚未巡检：',
				GROUP_CONCAT(p.`name`),
				'；计划名称：',
				pp.`name`,
				'; 巡检路线：',
				pr.`name`,
				'; 开始时间：',
				ppt.begin_time,
				'; 结束时间：',
				ppt.end_time
			) pushMsg,
			ppt.user_id AS reciver,
			ppt.user_id AS reciverName,
			ppt.org_code AS orgCode
		FROM
			p_plan_task_detail pptd
		LEFT JOIN p_plan_task ppt ON ppt.id = pptd.task_no
		LEFT JOIN p_point p ON p.id = pptd.point_id
		LEFT JOIN p_plan pp ON pp.id = ppt.plan_id
		LEFT JOIN p_route pr ON ppt.route_id = pr.id
		WHERE
			pptd.is_finish = 2
			AND ppt.finish_status = 3
			AND ppt.id = #{planTaskId}
		GROUP BY
			ppt.id,
			pptd.is_finish
    </select>
    <select id="getChargerId" resultType="java.util.HashMap">
        SELECT
		    p.charge_person_id pointChargerId
		FROM
		    p_point p,
		    (
		        SELECT
		            point_id
		        FROM
		            `p_check`
		        WHERE
		            id = #{checkId}
		    ) t
		WHERE
		    p.id = t.point_id
    </select>

     <select id="getChargerIds" resultType="java.util.HashMap">
        SELECT
       r.boss routeChargerId
		FROM
		   p_route_point prp,
		   p_route r,
		    (
		        SELECT
		            point_id
		        FROM
		            `p_check`
		        WHERE
		            id = #{checkId}
		    ) t
		WHERE
		    prp.point_id = t.point_id
		AND r.id = prp.route_id
    </select>
</mapper>