<?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.api.app.dao.butler.AppReportRepairDao">
    <select id="list" parameterType="java.lang.Integer" resultType="com.api.vo.app.AppReportRepairVo">
        select srr.*,sdl.status
        from sys_report_repair as srr,sys_dispatch_list as sdl
        where srr.dispatch_list_id = sdl.id and sdl.sys_delete = 1 and sdl.user_delete = 1 and srr.repairman = #{id}
        order by srr.create_date DESC
    </select>
    <select id="findRepairByIds" parameterType="com.api.model.app.UserIdAndRepairId" resultType="com.api.vo.app.AppReportRepairVo">
        select srr.*,sdl.status,CONCAT(cb.no,'栋',cbu.no,'单元',cbue.room_number) as room_name
        from cpm_building_unit_estate as cbue,cpm_building_unit as cbu,cpm_building as cb,sys_report_repair as srr,sys_dispatch_list as sdl
        where srr.building_unit_estate_id = cbue.id
          and cbue.building_unit_id = cbu.id
          and cbu.building_id = cb.id
          and srr.dispatch_list_id = sdl.id and sdl.sys_delete = 1 and sdl.user_delete = 1 and srr.repairman = #{id} and srr.id = #{repairId}
    </select>

    <select id="findDispatchListByIds" parameterType="com.api.model.app.UserIdAndRepairId" resultType="com.api.vo.app.AppDispatchListVo">
        select sdld.code,srr.repair_date as order_date,sdld.type,su1.actual_name as operator_name,su2.actual_name as distributor_name
        from sys_report_repair as srr,sys_dispatch_list as sdl,sys_dispatch_list_detail as sdld,sys_user as su1,sys_user as su2
        where srr.dispatch_list_id = sdl.id and sdld.dispatch_list_id = sdl.id and sdld.operator = su1.id and sdld.create_id = su2.id and sdl.user_delete =1 and sdl.sys_delete =1
        and srr.repairman = #{id} and srr.id = #{repairId}
    </select>

    <select id="findProcessRecordByIds" parameterType="com.api.model.app.UserIdAndRepairId" resultType="com.api.vo.app.AppProcessRecordVo">
        select spr.* from sys_process_record as spr,sys_dispatch_list as sdl,sys_report_repair as srr
        where spr.dispatch_list_id = sdl.id and srr.dispatch_list_id = sdl.id and sdl.user_delete = 1 and sdl.sys_delete = 1 and srr.id = #{repairId}
    </select>

    <select id="findHandleCompleteByIds" parameterType="com.api.model.app.UserIdAndRepairId" resultType="com.api.vo.app.AppMaintenanceResultVo">
        select shcd.* from sys_handle_complete_detail as shcd,sys_dispatch_list as sdl,sys_report_repair as srr
        where shcd.dispatch_list_id = sdl.id and srr.dispatch_list_id = sdl.id and sdl.user_delete = 1 and sdl.sys_delete = 1 and srr.id = #{repairId}
    </select>

    <update id="falseDelete" parameterType="com.api.model.app.UserIdAndRepairId">
        update sys_dispatch_list set user_delete = 0
        where id = (select dispatch_list_id from sys_report_repair where id = #{repairId} and repairman = #{id})
    </update>

    <update id="cancel" parameterType="com.api.model.app.UserIdAndRepairId">
        update sys_dispatch_list set status = 8
        where id = (select dispatch_list_id from sys_report_repair where id = #{repairId} and repairman = #{id})
    </update>

    <select id="findDispatchListIdByIds" parameterType="com.api.model.app.UserIdAndRepairId" resultType="java.lang.Integer">
        select dispatch_list_id from sys_report_repair where repairman = #{id} and id = #{repairId}
    </select>

    <update id="completeOrder" parameterType="com.api.model.app.UserIdAndRepairId">
        update sys_dispatch_list set status = 5
        where id = (select dispatch_list_id from sys_report_repair where id = #{repairId} and repairman = #{id})
    </update>

    <update id="evaluate" parameterType="com.api.model.app.AppRepairEvaluate" >
        update sys_dispatch_list
        <set>
            evaluation_level = #{evaluationLevel},
            evaluation_content = #{evaluationContent},
            evaluation_date = #{evaluationDate}
        </set>
        where id = (select dispatch_list_id from sys_report_repair where id = #{repairId} and repairman = #{id})
    </update>
    <select id="findRepairmanByRepairId" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select repairman from sys_report_repair where id = #{repairId}
    </select>

    <select id="findPayPriceById" parameterType="java.lang.Integer" resultType="java.math.BigDecimal">
        select (maintenance_labor_cast+maintenance_material_cost) as payPrice
        from sys_report_repair where id = #{reportRepairId}
    </select>

    <insert id="insertOrder" parameterType="com.api.model.app.AppRepairOrder">
        insert into sys_report_repair_order(
            report_repair_id,
            code,
            name,
            tel,
            pay_type,
            pay_price,
            create_id,
            create_date,
            status
        )values (
            #{reportRepairId},
            #{code},
            #{name},
            #{tel},
            #{payType},
            #{payPrice},
            #{createId},
            #{createDate},
            #{status}
        )
    </insert>

    <select id="findRepairOrderByCode" parameterType="java.lang.String" resultType="com.api.model.app.AppRepairOrder">
        select * from sys_report_repair_order where code = #{outTradeNo}
    </select>
    <select id="findUnPaymentOrder" resultType="com.api.model.app.AppRepairOrder">
        select * from sys_report_repair_order where status = 0 and NOW() <![CDATA[ >= ]]> DATE_ADD(create_date,INTERVAL 30 MINUTE)
    </select>

    <update id="updateDPOrderStatusByCode" parameterType="com.api.model.app.AppRepairOrder">
        update sys_report_repair_order set status = #{status} where code = #{code}
    </update>

</mapper>