<?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="cn.com.ty.lift.business.maintenance.dao.mapper.MaintenancePlanMapper" >
	<resultMap id="BaseResultMap" type="cn.com.ty.lift.business.maintenance.dao.entity.MaintenancePlan" >
		<id column="id" property="id" jdbcType="BIGINT" />
		<result column="lift_id" property="liftId" jdbcType="BIGINT" />
		<result column="mt_company_id" property="mtCompanyId" jdbcType="BIGINT" />
		<result column="project_id" property="projectId" jdbcType="BIGINT" />
		<result column="worker_id" property="workerId" jdbcType="BIGINT" />
		<result column="plan_date" property="planDate" jdbcType="TIMESTAMP" />
		<result column="work_date" property="workDate" jdbcType="TIMESTAMP" />
		<result column="count" property="count" jdbcType="INTEGER" />
		<result column="type" property="type" jdbcType="TINYINT" />
		<result column="status" property="status" jdbcType="TINYINT" />
		<result column="creator_id" property="creatorId" jdbcType="BIGINT" />
		<result column="create_date" property="createDate" jdbcType="TIMESTAMP" />
		<result column="update_id" property="updateId" jdbcType="BIGINT" />
		<result column="update_date" property="updateDate" jdbcType="TIMESTAMP" />
        <result column="demand" property="demand" jdbcType="VARCHAR" />
	</resultMap>

	<resultMap id="maintenanceRecordResultMap" type="cn.com.ty.lift.business.maintenance.dao.entity.model.response.MtPlanResponse"
			   extends="BaseResultMap">
		<result column="lift_type" property="liftType" jdbcType="INTEGER" />
		<result column="lift_category" property="liftCategory" jdbcType="INTEGER" />
		<result column="project_name" property="projectName" jdbcType="VARCHAR" />
		<result column="use_company_code" property="useCompanyCode" jdbcType="VARCHAR" />
		<result column="registration_code" property="registrationCode" jdbcType="VARCHAR" />
		<result column="device_position" property="devicePosition" jdbcType="VARCHAR" />
		<result column="worker_name" property="workerName" jdbcType="VARCHAR" />
		<result column="mt_record_id" property="mtRecordId" jdbcType="BIGINT" />
		<result column="record_status" property="recordStatus" jdbcType="VARCHAR" />
	</resultMap>

	<sql id="Base_Column_List" >
		id, lift_id, mt_company_id, project_id, worker_id, plan_date, work_date, count, type, 
		status
	</sql>

	<!-- 查询当天所有维保工的维保任务 -->
	<select id="queryMaintenanceTask" resultType="cn.com.ty.lift.business.maintenance.dao.entity.model.MaintenanceTask">
		SELECT count(1) AS num,
			   mp.worker_id,
			   ua.device_model,
			   ua.device_flag
		FROM maintenance_plan mp
				 LEFT JOIN user_account ua ON mp.worker_id = ua.user_id
		WHERE plan_date = current_date() AND mp.status = 0
		GROUP BY mp.worker_id, ua.device_model, ua.device_flag
	</select>

	<select id="findByCondition" resultType="cn.com.ty.lift.business.maintenance.dao.entity.model.response.MaintenancePlanResponse"
			parameterType="cn.com.ty.lift.business.maintenance.dao.entity.model.request.MaintenancePlanRequest">
		SELECT
		mp.id                       AS id,
		if(isnull(mp.id), '0', '1') AS status,
		mp.plan_date                AS planDate,
		l.id                        AS liftId,
		l.registration_code         AS registrationCode,
		l.device_position           AS devicePosition,
		plr.id                      AS relevanceId,
		plr.mt_company_id           AS mtCompanyId,
		plr.worker_id               AS workerId,
		plr.plan_interval           AS planInterval,
		p.id                        AS projectId,
		p.project_name              AS projectName,
		p.project_code              AS projectCode,
		pclr.lift_company_status    AS liftStatus,
		r.area_name                 AS regionName,
		ui.name                     AS workerName
		FROM
		project_lift_relevance plr
		LEFT JOIN platform_company_lift_relevance pclr ON plr.mt_company_id = pclr.mt_company_id AND plr.lift_id = pclr.lift_id
		LEFT JOIN lift l ON plr.lift_id = l.id
		LEFT JOIN project p ON plr.mt_company_id = p.mt_company_id AND plr.project_id = p.id
		LEFT JOIN maintenance_plan mp ON plr.mt_company_id = mp.mt_company_id AND plr.project_id = mp.project_id
		AND mp.status = 0 AND mp.plan_date = (SELECT MIN(plan_date) FROM maintenance_plan WHERE status = 0
		AND mt_company_id = #{request.mtCompanyId,jdbcType=VARCHAR} AND lift_id = plr.lift_id) AND plr.lift_id = mp.lift_id
		LEFT JOIN user_info ui ON plr.worker_id = ui.user_id
		LEFT JOIN region r ON p.region_id = r.id
		WHERE plr.mt_company_id = #{request.mtCompanyId,jdbcType=VARCHAR}
		AND pclr.lift_company_status != '1'
		<if test="request.projectId!=null and request.projectId > 0">
			AND p.id = #{request.projectId,jdbcType=BIGINT}
		</if>
		<if test="request.workerId!=null and request.workerId > 0">
			AND mp.worker_id = #{request.workerId,jdbcType=BIGINT}
		</if>
		<if test="request.regionId!=null and request.regionId > 0">
			AND p.region_id = #{request.regionId,jdbcType=BIGINT}
		</if>
		<if test="request.planInterval!=null and request.planInterval > 0">
			AND plr.plan_interval = #{request.planInterval,jdbcType=INTEGER}
		</if>
		<if test="request.beginTime!=null">
			AND <![CDATA[mp.plan_date > #{request.beginTime}]]>
		</if>
		<if test="request.endTime!=null">
			AND <![CDATA[mp.plan_date < #{request.endTime,jdbcType=VARCHAR}]]>
		</if>
		<if test="request.registrationCode!=null and request.registrationCode!=''">
			AND l.registration_code LIKE #{request.registrationCode,jdbcType=VARCHAR}
		</if>
	</select>

	<select id="queryPlanListByIds" parameterType="java.util.Map" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
			  FROM maintenance_plan
		WHERE mt_company_id = #{paramMap.mtCompanyId,jdbcType=VARCHAR}
		  AND plan_date BETWEEN #{paramMap.beginTime} AND #{paramMap.endTime}
		  AND lift_id IN
		<foreach collection="paramMap.liftIdList" item="id" index="index" open="(" close=")" separator=",">
			#{id}
		</foreach>
	</select>

	<!--app端 保养任务列表 (0:待保养、-1:超期) -->
	<select id="findPlanByWorkerId" resultType="cn.com.ty.lift.business.maintenance.dao.entity.model.response.MaintenanceAppResponse"
			parameterType="cn.com.ty.lift.business.maintenance.dao.entity.model.request.MaintenanceRecordRequest">
		SELECT p.project_name      AS projectName,
		       p.id                AS projectId,
			   mp.id               AS mtPlanId,
			   l.id                AS liftId,
			   l.coordinate        AS coordinate,
			   l.registration_code AS registrationCode,
			   l.use_company_code  AS useCompanyCode,
			   l.lift_type         AS liftType,
			   l.category          AS category,
		       l.lift_code         AS liftCode,
		       l.device_position   AS devicePosition,
			   ui.name             AS workerName,
		       mp.worker_id        AS workerId,
			   mp.plan_date        AS planDate,
			   mp.type             AS maintenanceType,
		       mp.status           AS status
		FROM maintenance_plan mp
				 LEFT JOIN project p ON mp.project_id = p.id
		         LEFT JOIN project_user pu ON p.id = pu.project_id
				 LEFT JOIN lift l ON mp.lift_id = l.id
				 LEFT JOIN user_info ui ON mp.worker_id = ui.user_id
		WHERE mp.mt_company_id = #{request.mtCompanyId,jdbcType=BIGINT}
		<if test="request.userId!=null">
			AND pu.user_id = #{request.userId,jdbcType=BIGINT}
		</if>
		<if test="request.status!=null and request.status!=''">
			AND mp.status = #{request.status,jdbcType=VARCHAR}
		</if>
		<if test="request.currentTime!=null">
			AND <![CDATA[ mp.plan_date = #{request.currentTime}]]>
		</if>
		<if test="request.beginTime!=null">
			AND <![CDATA[ mp.plan_date >= #{request.beginTime}]]>
		</if>
		<if test="request.endTime!=null">
			AND <![CDATA[ mp.plan_date <= #{request.endTime}]]>
		</if>
	</select>

	<!--app端 保养任务统计 (0:待保养、-1:超期) -->
	<select id="countPlanByWorkerId" resultType="cn.com.ty.lift.business.maintenance.dao.entity.model.MaintenanceCount"
			parameterType="cn.com.ty.lift.business.maintenance.dao.entity.model.request.MaintenanceRecordRequest">
		SELECT t.plan_date,
			MAX(CASE t.status WHEN '0' THEN t.total ELSE 0 END) waitingMaintenance,
			MAX(CASE t.status WHEN '-1' THEN t.total ELSE 0 END) overdue
		FROM (
			SELECT mp.status,
				   mp.plan_date,
				  count(1) total
		FROM maintenance_plan mp
				LEFT JOIN project p ON mp.project_id = p.id
				LEFT JOIN project_user pu ON p.id = pu.project_id
				LEFT JOIN lift l ON mp.lift_id = l.id
				LEFT JOIN user_info ui ON mp.worker_id = ui.user_id
		WHERE mp.mt_company_id = #{request.mtCompanyId,jdbcType=BIGINT}
		<if test="request.userId!=null">
			AND pu.user_id = #{request.userId,jdbcType=BIGINT}
		</if>
		<if test="request.beginTime!=null">
			AND <![CDATA[ mp.plan_date >= #{request.beginTime}]]>
		</if>
		<if test="request.endTime!=null">
			AND <![CDATA[ mp.plan_date <= #{request.endTime}]]>
		</if>
		GROUP BY plan_date,
		status
		) t
		GROUP BY t.plan_date
	</select>

	<!--日常保养-计划超期：根据条件分页查询-->
    <select id="pagePlanByCondition" resultMap="maintenanceRecordResultMap" parameterType="cn.com.ty.lift.business.maintenance.dao.entity.model.request.MtPlanRequest">
        SELECT
            mp.*,
            li.use_company_code,
            li.device_position,
            li.registration_code,
            li.lift_type,
			li.category AS lift_category,
            pr.project_name,
            ui.`name` AS worker_name,
			mr.id AS mt_record_id,
            ifnull(mr.status, -3) AS record_status
        FROM
            maintenance_plan mp
		LEFT JOIN lift li ON li.id = mp.lift_id
		LEFT JOIN project pr ON pr.id = mp.project_id AND pr.mt_company_id = mp.mt_company_id
		LEFT JOIN user_info ui ON ui.user_id = mp.worker_id
		LEFT JOIN maintenance_record mr ON mr.mt_plan_id = mp.id
        WHERE mp.plan_date &lt; now() AND mp.work_date IS NULL
        <if test="cond.mtCompanyId != null and cond.mtCompanyId > 0">
            AND mp.mt_company_id = #{cond.mtCompanyId}
        </if>
        <if test="cond.regionId != null and cond.regionId > 0">
            AND pr.region_id = #{cond.regionId}
        </if>
        <if test="cond.projectId != null and cond.projectId > 0">
            AND pr.id = #{cond.projectId}
        </if>
        <if test="cond.workerId != null and cond.workerId > 0">
            AND mp.worker_id = #{cond.workerId}
        </if>
        <if test="cond.planBegin != null">
            AND mp.plan_date &gt; #{cond.planBegin}
        </if>
        <if test="cond.planEnd != null">
            AND mp.plan_date &lt; #{cond.planEnd}
        </if>
        <if test="cond.workDate != null">
            AND mp.work_date = #{cond.workDate}
        </if>
    </select>

	<!--日常保养-计划超期：根据条件总计条数-->
    <select id="countPlanByCondition" resultType="java.lang.Long" parameterType="cn.com.ty.lift.business.maintenance.dao.entity.model.request.MtPlanRequest">
        SELECT
            count(*)
        FROM
          maintenance_plan mp
        WHERE mp.plan_date &lt; now() AND mp.work_date IS NULL
        <if test="cond.mtCompanyId != null and cond.mtCompanyId > 0">
              AND mp.mt_company_id = #{cond.mtCompanyId}
        </if>
    </select>
</mapper>