<?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.ErRecordCostMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.com.ty.lift.business.emergency.dto.CostResponse">
        <id column="id" property="id"/>
        <result column="er_record_id" property="erRecordId"/>
        <result column="cost_name" property="costName"/>
        <result column="cost_money" property="costMoney"/>
        <result column="cost_date" property="costDate"/>
        <result column="cost_desc" property="costDesc"/>
        <result column="pay_money" property="payMoney"/>
        <result column="pay_date" property="payDate"/>
        <result column="invoice_money" property="invoiceMoney"/>
        <result column="invoice_date" property="invoiceDate"/>
        <result column="invoice_enclosure" property="invoiceEnclosure"/>
        <result column="cost_status" property="costStatus"/>
        <result column="remark" property="remark"/>

        <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="registration_code" property="registrationCode" jdbcType="VARCHAR"/>
        <result column="device_position" property="devicePosition" jdbcType="VARCHAR"/>
        <result column="worker_name" property="workerName" jdbcType="VARCHAR"/>
        <result column="repair_diff" property="repairDiff" jdbcType="BIGINT"/>
        <result column="stop_date" property="stopDate" jdbcType="TIMESTAMP" />
        <result column="recovery_date" property="recoveryDate" jdbcType="TIMESTAMP" />
    </resultMap>

    <!--根据条件查询急修收费总条数-->
    <select id="countByCondition" resultType="java.lang.Long" parameterType="cn.com.ty.lift.business.emergency.dto.CostRequest" >
        SELECT
            count(*)
        FROM
            er_record_cost erc
        LEFT JOIN emergency_repair er ON erc.er_record_id = er.id
        <where>
            <if test="cond.mtCompanyId != null and cond.mtCompanyId > 0">
                AND er.mt_company_id = #{cond.mtCompanyId}
            </if>
            <if test="cond.status != null">
                AND erc.cost_status = #{cond.status}
            </if>
        </where>
    </select>

    <!--根据条件分页查询急修记录-->
    <select id="pageByCondition" resultMap="BaseResultMap" parameterType="cn.com.ty.lift.business.emergency.dto.CostRequest" >
        SELECT
            erc.*,
            li.registration_code,
            li.device_position,
            pr.project_name,
            pr.project_code,
            pr.address AS project_address,
            er.stop_date,
            er.recovery_date,
            ui.name AS worker_name,
            TimeStampDiff(SECOND ,er.stop_date,er.recovery_date) * 1000 AS repair_diff
        FROM
            er_record_cost erc
        LEFT JOIN emergency_repair er ON er.id = erc.er_record_id
        LEFT JOIN project pr ON er.project_id = pr.id AND pr.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
        <where>
            <if test="cond.mtCompanyId != null and cond.mtCompanyId > 0">
                AND er.mt_company_id = #{cond.mtCompanyId}
            </if>
            <if test="cond.workerId != null and cond.workerId > 0">
                AND er.worker_id1 = #{cond.workerId}
            </if>
            <if test="cond.repairBegin != null">
                AND er.caller_date &gt; #{cond.repairBegin}
            </if>
            <if test="cond.repairEnd != null">
                AND er.caller_date &lt; #{cond.repairEnd}
            </if>
            <if test="cond.liftId != null and cond.liftId > 0">
                AND er.lift_id = #{cond.liftId}
            </if>
            <if test="cond.status != null">
                AND erc.cost_status = #{cond.status}
            </if>
            <if test="cond.projectId != null and cond.projectId > 0">
                AND pr.id = #{cond.projectId}
            </if>
        </where>
    </select>
</mapper>
