<?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.SysDispatchListDao">
    <select id="list" parameterType="com.api.model.butlerService.SearchDispatchList" resultType="com.api.vo.butlerService.VoDispatchList">
        select a.*,sdld.create_date as dispatch_date,sdld.work_order_time_limit,sdld.remake as remake,su1.actual_name as operator_name,su2.actual_name as distributor_name from
        (
        select sdl.id,sdl.code,sdl.work_order_type,swot.name as work_order_type_name,sdl.status,ur.name as create_name,ur.tel, srr.building_unit_estate_id,srr.report_detail,sdl.begin_date,sdl.revisit_date,sdl.revisit_detail
        ,concat_ws('-',cb.no,cbu.no,cbue.room_number) as room_name,cbue.room_number,cbu.no as unit_no,cb.no as estate_no
        from sys_work_order_type as swot,cpm_building_unit_estate as cbue,cpm_building_unit as cbu,cpm_building as cb,sys_dispatch_list as sdl,sys_report_repair as srr left join user_resident as ur
        on ur.id = srr.repairman
        where sdl.work_order_type = swot.id and srr.dispatch_list_id = sdl.id and srr.building_unit_estate_id = cbue.id and cbue.building_unit_id = cbu.id and cbu.building_id =cb.id and sdl.sys_delete =1
        ) as a
        LEFT JOIN sys_dispatch_list_detail as sdld
        on sdld.dispatch_list_id = a.id left JOIN sys_user as su1 on sdld.operator = su1.id LEFT JOIN sys_user as su2 on sdld.create_id = su2.id
        <where>
            <if test="code != null and code != ''">
                and a.code like concat('%',#{code},'%')
            </if>
            <if test="type != null">
                and a.work_order_type = #{type}
            </if>
            <if test="workOrderTimeId != null">
                and sdld.work_order_time_limit = #{workOrderTimeId}
            </if>
            <if test="distributorName != null and distributorName != ''">
                and su2.actual_name like concat('%',#{distributorName},'%')
            </if>
            <if test="operatorName != null and operatorName != ''">
                and su1.actual_name like concat('%',#{operatorName},'%')
            </if>
            <if test="beginDateStart != null">
                and a.begin_date <![CDATA[ > ]]> #{beginDateStart}
            </if>
            <if test="beginDateEnd != null">
                and a.begin_date <![CDATA[ < ]]> #{beginDateEnd}
            </if>
            <if test="dispatchTimeStart != null">
                and sdld.dispatch_date <![CDATA[ > ]]> #{dispatchTimeStart}
            </if>
            <if test="dispatchTimeEnd != null">
                and sdld.dispatch_date <![CDATA[ < ]]> #{dispatchTimeEnd}
            </if>
            <if test="roomNumber != null and roomNumber != ''">
                and a.room_number like concat('%',#{roomNumber},'%')
            </if>
            <if test="unitNo != null">
                and a.unit_no like concat('%',#{unitNo},'%')
            </if>
            <if test="estateNo != null">
                and a.estate_no like concat('%',#{estateNo},'%')
            </if>
            <if test="status != null">
                and a.status = #{status}
            </if>
            <if test="createName != null and createName != ''">
                and a.create_name like concat('%',#{createName},'%')
            </if>
        </where>
    </select>

    <update id="falseDelete" parameterType="java.lang.Integer">
        update sys_dispatch_list set sys_delete = 0
        where id = #{id}
    </update>

    <update id="cancel" parameterType="com.api.model.butlerService.CancelWorkOrder">
        update sys_dispatch_list
        <set>
            status = 7,
            <if test="content != null and content != ''">
                cancel_detail = #{content},
            </if>
        </set>
        where id = #{id}
    </update>

    <update id="revisit" parameterType="com.api.model.butlerService.RevisitWorkOrder">
        update sys_dispatch_list
        <set>
            revisit_date = #{revisitDate},
            revisit_detail = #{content},
        </set>
        where id =#{id}
    </update>

    <select id="findStatusById" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select status from sys_dispatch_list where id = #{id}
    </select>

    <delete id="rollback" parameterType="java.lang.Integer">
        delete from sys_dispatch_list_detail where dispatch_list_id = #{id}
    </delete>

    <update id="updateStatus" parameterType="com.api.model.butlerService.UpdateDispatchStatus">
        update sys_dispatch_list set status = #{status} where id =#{id}
    </update>

    <insert id="dispatch" parameterType="com.api.model.butlerService.SysDispatchListDetail">
        insert into sys_dispatch_list_detail(
            code,
            dispatch_list_id,
            work_order_type,
            work_order_type_detail,
            work_order_time_limit,
            type,
            dispatch_date,
            operator,
            remake,
            create_id,
            create_date
        ) values (
            #{code},
            #{dispatchListId},
            #{workOrderType},
            #{workOrderTypeDetail},
            #{workOrderTimeLimit},
            #{type},
            #{dispatchDate},
            #{operator},
            #{remake},
            #{createId},
            #{createDate}
        )
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>

    <select id="findDispatchDetail" parameterType="java.lang.Integer" resultType="com.api.vo.butlerService.VoDispatch">
        select sdld.*,swotl.name as work_order_time_limit_name,su.actual_name as operator_name,swot.name as work_order_type_name,swotd.name as work_order_type_detail_name
        from sys_dispatch_list_detail as sdld,sys_work_order_time_limit as swotl,sys_user as su,sys_work_order_type as swot,sys_work_order_type_detail as swotd
        where sdld.work_order_type = swot.id and sdld.work_order_type_detail = swotd.id and sdld.work_order_time_limit = swotl.id and sdld.operator = su.id and dispatch_list_id = #{id}
    </select>

    <select id="findEvaluationById" parameterType="java.lang.Integer" resultType="com.api.vo.butlerService.VoEvaluation">
        select id,evaluation_level,evaluation_content,evaluation_date
        from sys_dispatch_list where id = #{id}
    </select>

    <select id="findRevisitById" parameterType="java.lang.Integer" resultType="com.api.vo.butlerService.VoRevisit">
        select id,revisit_date,revisit_detail
        from sys_dispatch_list where id = #{id}
    </select>
    <insert id="insert" parameterType="com.api.model.butlerService.DispatchList">
        insert into sys_dispatch_list(
            code,
            work_order_type,
            status,
            create_id,
            create_date,
            user_delete,
            sys_delete
        ) values (
            #{code},
            #{workOrderType},
            #{status},
            #{createId},
            #{createDate},
            #{userDelete},
            #{sysDelete}
        )
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>

    <select id="findCodeAndIdBySDPI" parameterType="java.lang.Integer" resultType="com.api.vo.butlerService.VoFindCodeAndIdSDPI">
        select id,code from sys_dispatch_list where sys_daily_payment_id = #{id}
    </select>
    <select id="findSysUserLikeActualName" parameterType="java.lang.String" resultType="com.api.vo.system.VoDispatchSysUser">
        select id,actual_name,tel from sys_user where actual_name like concat('%',#{actualName},'%')
    </select>
</mapper>