<?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.manage.dao.butlerService.SysReportRepairDao">
    <select id="list" parameterType="com.api.model.butlerService.SearchReportRepair" resultType="com.api.vo.butlerService.VoReportRepair">
        select sdl.id,srr.code,sdl.status,concat_ws('-',cb.no,cbu.no,cbue.room_number) as room_name,ur.name as repairman_name,ur.tel as repair_tel,srr.repair_date as repair_date,a.*,srr.froms as froms_name
        from sys_report_repair as srr,cpm_building_unit_estate as cbue,cpm_building_unit as cbu,cpm_building as cb,user_resident as ur,sys_dispatch_list as sdl
        left join
        (
        select sdld.dispatch_list_id,su1.actual_name as distributor_name,su2.actual_name as operator_name,swotl.name as work_order_time_limit,su2.id as operator,su1.id as distributor
        from sys_dispatch_list_detail as sdld,sys_user as su1,sys_user as su2,sys_work_order_time_limit as swotl
        where su2.id = sdld.operator and su1.id = sdld.create_id and swotl.id = sdld.work_order_time_limit
        ) as a
        on sdl.id = a.dispatch_list_id
        <where>
            sdl.id = srr.dispatch_list_id
            and srr.building_unit_estate_id = cbue.id
            and cbue.building_unit_id = cbu.id
            and cbu.building_id = cb.id
            and ur.id = srr.repairman
            and sdl.sys_delete = 1
            and sdl.work_order_type = 1
            <if test="code != null and code != ''">
                and srr.code like concat('%',#{code},'%')
            </if>
            <if test="repairman != null and repairman != ''">
                and ur.name like concat('%',#{repairman},'%')
            </if>
            <if test="roomNumber != null and roomNumber != ''">
                and cbue.room_number like concat('%',#{roomNumber},'%')
            </if>
            <if test="unitNo != null">
                and cbu.no like concat('%',#{unitNo},'%')
            </if>
            <if test="estateNo != null">
                and cb.no like concat('%',#{estateNo},'%')
            </if>
            <if test="froms != null">
                and srr.froms = #{froms}
            </if>
            <if test="distributor != null and distributor != ''">
                and a.distributor_name like concat('%',#{distributor},'%')
            </if>
            <if test="operator != null and operator != ''">
                and a.operator_name like concat('%',#{operator},'%')
            </if>
            <if test="repairDateStart != null">
                and srr.repair_date <![CDATA[ > ]]> #{repairDateStart}
            </if>
            <if test="repairDateEnd != null">
                and srr.repair_date <![CDATA[ < ]]> #{repairDateEnd}
            </if>
            <if test="status != null">
                and sdl.status = #{status}
            </if>
        </where>
        order by srr.create_date desc
    </select>

    <select id="findRepairDetail" parameterType="java.lang.Integer" resultType="com.api.vo.butlerService.VoRepair">
        select srr.*,concat_ws('-',cb.no,cbu.no,cbue.room_number) as room_name,ur.name as repair_name,su.actual_name as dispatch_name
        from sys_report_repair as srr,cpm_building_unit_estate as cbue,cpm_building_unit as cbu,cpm_building as cb,user_resident as ur,sys_dispatch_list as sdl
        left JOIN sys_dispatch_list_detail as sdld on sdl.id = sdld.dispatch_list_id
        left JOIN sys_user as su on sdld.create_id = su.id
        where srr.building_unit_estate_id = cbue.id and cbue.building_unit_id = cbu.id and cbu.building_id = cb.id and ur.id = srr.repairman and sdl.id = srr.dispatch_list_id and sdl.id = #{id}
    </select>

    <insert id="insert" parameterType="com.api.model.butlerService.ReportRepair">
        insert into sys_report_repair(
            code,
            dispatch_list_id,
            type,
            repairman,
            building_unit_estate_id,
            tel,
            repair_date,
            froms,
            report_detail,
            create_id,
            create_date
        ) values (
            #{code},
            #{dispatchListId},
            #{type},
            #{repairman},
            #{buildingUnitEstateId},
            #{tel},
            #{repairDate},
            #{froms},
            #{reportDetail},
            #{createId},
            #{createDate}
        )
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>

    <select id="findById" parameterType="java.lang.Integer" resultType="com.api.vo.butlerService.VoFindByIdRepair">
        select srr.*,ur.name as repair_name,
        (
            case srr.create_id = -1 when true then 'app录单' else (select su.actual_name from sys_user as su where su.id = srr.create_id) end
        ) as create_name
        from sys_report_repair as srr,user_resident as ur
        where srr.repairman = ur.id and srr.dispatch_list_id = #{id}
    </select>

    <update id="update" parameterType="com.api.model.butlerService.ReportRepair">
        update sys_report_repair
        <set>
            <if test="type != null">
                type = #{type},
            </if>
            <if test="repairman != null">
                repairman = #{repairman},
            </if>
            <if test="tel != null and tel != ''">
                tel = #{tel},
            </if>
            <if test="froms != null">
                froms = #{froms},
            </if>
            <if test="repairDate != null">
                repair_date = #{repairDate},
            </if>
            <if test="reportDetail != null and reportDetail != ''">
                report_detail = #{reportDetail},
            </if>
            modify_id = #{modifyId},
            modify_date = #{modifyDate}
        </set>
        where id = #{id}
    </update>

    <select id="findEstateIdByRepairman" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select building_unit_estate_id from  cpm_resident_estate
        where resident_id = #{repairman} limit 1
    </select>

</mapper>