<?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.emergency.mapper.EmergencyRepairMapper" >
	<resultMap id="BaseResultMap" type="cn.com.ty.lift.business.emergency.dto.RepairResponse" >
		<id column="id" property="id" jdbcType="BIGINT" />
		<result column="mt_company_id" property="mtCompanyId" jdbcType="BIGINT" />
		<result column="lift_id" property="liftId" jdbcType="BIGINT" />
		<result column="is_trapped" property="isTrapped" jdbcType="TINYINT" />
		<result column="is_critical" property="isCritical" jdbcType="TINYINT" />
		<result column="source" property="source" jdbcType="TINYINT" />
		<result column="caller_id" property="callerId" jdbcType="BIGINT" />
		<result column="caller_name" property="callerName" jdbcType="VARCHAR" />
		<result column="caller_tel" property="callerTel" jdbcType="CHAR" />
		<result column="caller_date" property="callerDate" jdbcType="TIMESTAMP" />
		<result column="stop_date" property="stopDate" jdbcType="TIMESTAMP" />
		<result column="recovery_date" property="recoveryDate" jdbcType="TIMESTAMP" />
		<result column="assign_time" property="assignTime" jdbcType="TIMESTAMP" />
		<result column="taking_time" property="takingTime" jdbcType="TIMESTAMP" />
		<result column="arrive_time" property="arriveTime" jdbcType="TIMESTAMP" />
		<result column="charge" property="charge" jdbcType="VARCHAR" />
		<result column="worker_id" property="workerId" jdbcType="BIGINT" />
		<result column="worker_tel" property="workerTel" jdbcType="CHAR" />
		<result column="position" property="position" jdbcType="VARCHAR" />
		<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="repair_reason" property="repairReason" jdbcType="TINYINT" />
		<result column="parts_cost" property="partsCost" jdbcType="DECIMAL" />
		<result column="safety_confirm" property="safetyConfirm" jdbcType="VARCHAR" />
		<result column="read_time" property="readTime" jdbcType="TIMESTAMP" />
		<result column="worker_id1" property="workerId1" jdbcType="BIGINT" />
		<result column="wl_floor" property="wlFloor" jdbcType="INTEGER" />
		<result column="wl_liftdoor" property="wlLiftdoor" jdbcType="TINYINT" />
		<result column="wl_fault" property="wlFault" jdbcType="VARCHAR" />
		<result column="wl_fault_reason" property="wlFaultReason" jdbcType="VARCHAR" />
		<result column="wl_fault_handle" property="wlFaultHandle" jdbcType="VARCHAR" />
		<result column="wl_run_direction" property="wlRunDirection" jdbcType="TINYINT" />
		<result column="cost_total" property="costTotal" jdbcType="DECIMAL" />
        <result column="pay_total" property="payTotal" jdbcType="DECIMAL" />
        <result column="invoice_total" property="invoiceTotal" jdbcType="DECIMAL" />
		<result column="project_id" property="projectId" jdbcType="BIGINT" />
		<result column="caller_fault_description" property="callerFaultDescription" jdbcType="LONGVARCHAR" />
		<result column="sparepart" property="sparepart" jdbcType="LONGVARCHAR" />
		<result column="worker_fault_description" property="workerFaultDescription" jdbcType="LONGVARCHAR" />
		<result column="field_description" property="fieldDescription" jdbcType="LONGVARCHAR" />
		<result column="repair_procedure" property="repairProcedure" jdbcType="LONGVARCHAR" />
		<result column="close_reason" property="closeReason" jdbcType="VARCHAR" />
		<result column="main_sign" property="mainSign" jdbcType="VARCHAR" />
		<result column="second_sign" property="secondSign" jdbcType="VARCHAR" />
		<result column="has_evaluate" property="hasEvaluate" jdbcType="INTEGER" />

		<result column="project_name" property="projectName" jdbcType="VARCHAR" />
        <result column="project_code" property="projectCode" jdbcType="VARCHAR" />
		<result column="project_address" property="projectAddress" jdbcType="VARCHAR" />
        <result column="area_name" property="areaName" jdbcType="VARCHAR" />
		<result column="registration_code" property="registrationCode" jdbcType="VARCHAR" />
		<result column="use_company_code" property="useCompanyCode" jdbcType="VARCHAR" />
		<result column="lift_category" property="liftCategory" jdbcType="INTEGER" />
        <result column="lift_type" property="liftType" jdbcType="INTEGER" />
		<result column="device_position" property="devicePosition" jdbcType="VARCHAR" />
        <result column="coordinate" property="coordinate" jdbcType="VARCHAR" />
		<result column="create_name" property="createName" jdbcType="VARCHAR" />
		<result column="worker_name" property="workerName" jdbcType="VARCHAR" />
        <result column="worker_mobile" property="workerMobile" jdbcType="VARCHAR" />
		<result column="lift_company_status" property="liftCompanyStatus" jdbcType="VARCHAR" />
		<result column="repair_diff" property="repairDiff" jdbcType="BIGINT" />
		<result column="wait_diff" property="waitDiff" jdbcType="BIGINT" />
        <result column="advice" property="advice" jdbcType="VARCHAR" />
        <result column="img_url" property="imgUrl" jdbcType="VARCHAR" />
        <result column="service_level" property="serviceLevel" jdbcType="INTEGER" />
        <result column="star_level" property="starLevel" jdbcType="INTEGER" />
	</resultMap>

	<!--分页查询急修中-->
    <select id="pageByRepairing" resultMap="BaseResultMap" parameterType="cn.com.ty.lift.business.emergency.dto.RepairRequest">
        SELECT
        	t.*,
        	ui.name AS worker_name
        FROM
			(SELECT
				er.*,
				ui.`name` AS create_name,
				li.registration_code,
                li.category AS lift_category,
                li.lift_type,
				li.use_company_code,
				li.device_position,
                li.coordinate,
				pr.project_name,
                pr.project_code,
                pr.address AS project_address,
				pclr.lift_company_status,
                TimeStampDiff(SECOND ,er.assign_time,ifnull(er.taking_time, now())) * 1000 AS wait_diff
			FROM
				emergency_repair er
				LEFT JOIN user_info ui ON ui.user_id = er.creator_id
				LEFT JOIN lift li ON li.id = er.lift_id
				LEFT JOIN project pr ON pr.id = er.project_id
				LEFT JOIN platform_company_lift_relevance pclr ON pclr.lift_id = er.lift_id AND pclr.mt_company_id = er.mt_company_id
			WHERE
			  er.`status` IN (0,1)
			<if test="cond.mtCompanyId != null and cond.mtCompanyId > 0">
				AND er.mt_company_id = #{cond.mtCompanyId}
			</if>
            ORDER BY wait_diff DESC
			) t
          LEFT JOIN user_info ui ON ui.user_id = t.worker_id1
    </select>

    <!--根据条件查询急修记录总条数-->
    <select id="countByCondition" resultType="java.lang.Long" parameterType="cn.com.ty.lift.business.emergency.dto.RepairRequest" >
        SELECT
            count(*)
        FROM
            emergency_repair er
        <where>
            <if test="cond.status != null">
                AND er.status = #{cond.status}
            </if>
            <if test="cond.mtCompanyId != null and cond.mtCompanyId > 0">
                AND er.mt_company_id = #{cond.mtCompanyId}
            </if>
            <if test="cond.repairBegin != null">
                AND date(er.caller_date) &gt;= #{cond.repairBegin}
            </if>
            <if test="cond.repairEnd != null">
                AND date(er.caller_date) &lt;= #{cond.repairEnd}
            </if>
        </where>
    </select>

	<!--根据条件分页查询急修记录-->
    <select id="pageByCondition" resultMap="BaseResultMap" parameterType="cn.com.ty.lift.business.emergency.dto.RepairRequest" >
        SELECT
            er.*,
            li.registration_code,
            li.device_position,
            li.category AS lift_category,
            li.lift_type,
            li.use_company_code,
            pr.project_name,
            pr.project_code,
            pr.address AS project_address,
            re.area_name,
            ui.name AS worker_name,
            ui.mobile AS worker_mobile,
            TimeStampDiff(SECOND ,er.stop_date,er.recovery_date) * 1000 AS repair_diff
        FROM
            emergency_repair er
            LEFT JOIN project pr ON er.project_id = pr.id AND pr.mt_company_id = er.mt_company_id
            LEFT JOIN region re ON pr.region_id = re.id
            LEFT JOIN lift li ON er.lift_id = li.id
            LEFT JOIN user_info ui ON ui.user_id = er.worker_id1
        <where>
            <if test="cond.mtCompanyId != null and cond.mtCompanyId > 0">
                AND er.mt_company_id = #{cond.mtCompanyId}
            </if>
            <if test="cond.projectId != null and cond.projectId > 0">
                AND pr.id = #{cond.projectId}
            </if>
            <if test="cond.regionId != null and cond.regionId > 0">
                AND pr.region_id = #{cond.regionId}
            </if>
            <if test="cond.workerId != null and cond.workerId > 0">
                AND er.worker_id1 = #{cond.workerId}
            </if>
            <if test="cond.repairBegin != null">
                AND date(er.caller_date) &gt;= #{cond.repairBegin}
            </if>
            <if test="cond.repairEnd != null">
                AND date(er.caller_date) &lt;= #{cond.repairEnd}
            </if>
            <if test="cond.status != null">
                AND er.status = #{cond.status}
            </if>
            <if test="cond.source != null">
                AND er.source = #{cond.source}
            </if>
            <if test="cond.info != null and cond.info != ''">
                AND li.registration_code LIKE concat('%',#{cond.info},'%')
            </if>
        </where>
    </select>

    <select id="listByIdList" resultMap="BaseResultMap" parameterType="cn.com.ty.lift.business.emergency.dto.RepairRequest" >
        SELECT
            er.*,
            li.registration_code,
            li.device_position,
            li.category AS lift_category,
            li.lift_type,
            li.use_company_code,
            pr.project_code,
            pr.address AS project_address,
            pr.project_name,
            re.area_name,
            ev.advice,
            ev.service_level,
            ev.star_level,
            ev.img_url,
            ui.name AS worker_name,
            TimeStampDiff(SECOND ,er.stop_date,er.recovery_date) * 1000 AS repair_diff
        FROM
            emergency_repair er
        LEFT JOIN project pr ON er.project_id = pr.id AND pr.mt_company_id = er.mt_company_id
        LEFT JOIN region re ON pr.region_id = re.id AND re.mt_company_id = er.mt_company_id
        LEFT JOIN lift li ON er.lift_id = li.id
        LEFT JOIN user_info ui ON ui.user_id = er.worker_id1
        LEFT JOIN evaluation ev ON ev.record_id = er.id AND ev.source = 2
        <where>
            <if test="cond.ids != null and cond.ids.size > 0">
                AND er.id IN
                <foreach collection="cond.ids" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>

	<!--根据id查找带扩展信息-->
	<select id="infoById" resultMap="BaseResultMap" parameterType="java.lang.Long" >
		SELECT
            er.*,
            li.registration_code,
			li.device_position,
            li.category AS lift_category,
            li.lift_type,
            li.use_company_code,
            pr.project_name,
            pr.project_code,
            pr.address AS project_address,
            re.area_name,
			ui.name AS worker_name
		FROM
			emergency_repair er
		LEFT JOIN project pr ON er.project_id = pr.id
		LEFT JOIN region re ON pr.region_id = re.id
		LEFT JOIN lift li ON er.lift_id = li.id
		LEFT JOIN user_info ui ON ui.user_id = er.worker_id1
		WHERE er.id = #{id}
	</select>

    <!--根据用户查询急修信息-->
    <select id="pageByUser" resultMap="BaseResultMap" parameterType="cn.com.ty.lift.business.emergency.dto.RepairRequest" >
        SELECT
            er.*,
            pr.project_name,
            pr.project_code,
            pr.address AS project_address,
            li.registration_code,
            li.category AS lift_category,
            li.lift_type,
            li.use_company_code,
            re.area_name
        FROM
	        project_user pu
        LEFT JOIN project pr ON pu.project_id = pr.id
        LEFT JOIN emergency_repair er ON er.project_id = pr.id AND er.mt_company_id = pu.mt_company_id
        LEFT JOIN lift li ON er.lift_id = li.id
        LEFT JOIN region re ON re.id = pr.region_id
        <where>
            <if test="cond.mtCompanyId != null and cond.mtCompanyId > 0">
                AND er.mt_company_id = #{cond.mtCompanyId}
            </if>
            <if test="cond.userId != null and cond.userId > 0">
                AND pu.user_id = #{cond.userId}
            </if>
            <if test="cond.status != null">
                AND er.status = #{cond.status}
            </if>
        </where>
	</select>

	<!--根据条件分页查询急修记录-->
	<select id="pageByLift" resultMap="BaseResultMap" parameterType="cn.com.ty.lift.business.emergency.dto.RepairRequest" >
		SELECT
            er.*,
            li.registration_code,
            li.use_company_code,
            ui.name AS worker_name
		FROM
			emergency_repair er
		LEFT JOIN lift li ON er.lift_id = li.id
		LEFT JOIN user_info ui ON ui.user_id = er.worker_id1
		<where>
			<if test="cond.mtCompanyId != null and cond.mtCompanyId > 0">
				AND er.mt_company_id = #{cond.mtCompanyId}
			</if>
			<if test="cond.projectId != null and cond.projectId > 0">
				AND er.project_id = #{cond.projectId}
			</if>
			<if test="cond.liftId != null and cond.liftId > 0">
				AND er.lift_id = #{cond.liftId}
			</if>
		</where>
	</select>

    <select id="countDoingByWorker" resultType="java.lang.Long" parameterType="cn.com.ty.lift.business.common.CommonRequest">
        SELECT
            count(*)
        FROM
            emergency_repair er
        WHERE er.status IN (0, 1)
        <if test="cond.userId != null and cond.userId > 0">
            AND er.worker_id1 = #{cond.userId}
        </if>
        <if test="cond.mtCompanyId != null and cond.mtCompanyId > 0">
            AND er.mt_company_id = #{cond.mtCompanyId}
        </if>

    </select>

    <select id="countDoingByUser" resultType="java.lang.Long" parameterType="cn.com.ty.lift.business.common.CommonRequest">
        SELECT
            count(*)
        FROM
            emergency_repair er
            LEFT JOIN project_user pu ON er.project_id = pu.project_id AND er.mt_company_id = pu.mt_company_id
        WHERE er.status IN (0, 1)
        <if test="cond.userId != null and cond.userId > 0">
            AND pu.user_id = #{cond.userId}
        </if>
        <if test="cond.mtCompanyId != null and cond.mtCompanyId > 0">
            AND er.mt_company_id = #{cond.mtCompanyId}
        </if>
    </select>
</mapper>