<?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.butlerApp.dao.jurisdiction.ButlerRepairEngineeringDao">

    <select id="list1" parameterType="com.api.model.butlerApp.ButlerRepairEngineeringSearch" resultType="com.api.vo.butlerApp.ButlerRepairEngineeringVo">
        select * from sys_report_repair_engineering
        <where>
            <if test="repairEngineeringStatus == 1">
                and status in (1,2)
            </if>
            <if test="repairEngineeringStatus != null and repairEngineeringStatus != 1">
                and status = #{repairEngineeringStatus}
            </if>
        </where>
        order by create_date desc
    </select>
    <select id="list2" parameterType="com.api.model.butlerApp.ButlerRepairEngineeringSearch" resultType="com.api.vo.butlerApp.ButlerRepairEngineeringVo">
        select * from sys_report_repair_engineering
        <where>
            #{organizationIdPath} like concat('%',organization_id,'%')
            <if test="repairEngineeringStatus == 1">
                and status in (1,2)
            </if>
            <if test="repairEngineeringStatus != null and repairEngineeringStatus != 1">
                and status = #{repairEngineeringStatus}
            </if>
        </where>
        order by create_date desc
    </select>
    <select id="list3" parameterType="com.api.model.butlerApp.ButlerRepairEngineeringSearch" resultType="com.api.vo.butlerApp.ButlerRepairEngineeringVo">
        select * from sys_report_repair_engineering
        <where>
            maintenance_staff = #{id}
            <if test="repairEngineeringStatus != null">
                and status = #{repairEngineeringStatus}
            </if>
        </where>
        order by create_date desc
    </select>

    <insert id="insert" parameterType="com.api.model.butlerApp.ButlerRepairEngineering">
        insert into sys_report_repair_engineering(
            repair_area,
            code,
            type,
            report_detail,
            status,
            create_id,
            create_date
        )values (
            #{repairArea},
            #{code},
            #{type},
            #{reportDetail},
            #{status},
            #{createId},
            #{createDate}
        )
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>
    <insert id="insertProcessRecord" parameterType="com.api.model.butlerApp.ButlerReportRepairEngineeringProcessRecord">
        insert into sys_report_repair_engineering_process_record(
            repair_engineering_id,
            operation_date,
            operation_type,
            operator,
            operator_type,
            operator_content
        )values (
            #{repairEngineeringId},
            #{operationDate},
            #{operationType},
            #{operator},
            #{operatorType},
            #{operatorContent}
        )
    </insert>

    <select id="findById" parameterType="java.lang.Integer" resultType="com.api.vo.butlerApp.ButlerRepairEngineeringFBIVo">
        select srre.*,su1.actual_name as create_name,su1.tel as create_tel,so.name as organization_name,
        su2.actual_name as organization_leading_name,su2.tel as organization_leading_tel,
        su3.actual_name as maintenance_staff_name,su3.tel as maintenance_staff_tel,
        so2.name as maintenance_staff_organization_name
        from sys_user as su1,sys_report_repair_engineering as srre
        LEFT JOIN sys_organization as so on srre.organization_id = so.id
        LEFT JOIN sys_user as su2 on so.leading_id = su2.id
        LEFT JOIN sys_user as su3 on srre.maintenance_staff = su3.id
        LEFT JOIN sys_organization as so2 on su3.organization_id = so2.id
        where srre.create_id = su1.id and srre.id = #{repairEngineeringId}
    </select>

    <select id="findProcessRecordById" parameterType="java.lang.Integer"
            resultType="com.api.model.butlerApp.ButlerReportRepairEngineeringProcessRecord">
        select * from sys_report_repair_engineering_process_record
        where repair_engineering_id = #{repairEngineeringId}
        order by operation_date desc
    </select>
    <select id="findRepairOrganization" resultType="com.api.model.businessManagement.SysOrganization">
        select * from sys_organization where category_id = 5
    </select>
    <select id="findSysUserByOrganizationId" parameterType="java.lang.Integer" resultType="com.api.model.businessManagement.SysUser">
        select id,actual_name,tel from sys_user
        where organization_id_path like concat('%:',#{repairOrganizationId},':%')
        and is_delete = 1
    </select>

    <update id="maintenanceCompanySendSingle" parameterType="com.api.model.butlerApp.ButlerRepairEngineering">
        update sys_report_repair_engineering
        set status = #{status},
        maintenance_company_send_single = #{maintenanceCompanySendSingle},
        maintenance_company_send_single_date = #{maintenanceCompanySendSingleDate},
        organization_id = #{organizationId}
        where id = #{id}
    </update>
    <update id="maintenancePersonnelSendSingle" parameterType="com.api.model.butlerApp.ButlerRepairEngineering">
        update sys_report_repair_engineering
        set status = #{status},
        maintenance_personnel_send_single = #{maintenancePersonnelSendSingle},
        maintenance_personnel_send_single_date = #{maintenancePersonnelSendSingleDate},
        maintenance_staff = #{maintenanceStaff}
        where id = #{id}
    </update>
    <update id="maintenanceStaffPickSingle" parameterType="com.api.model.butlerApp.ButlerRepairEngineering">
        update sys_report_repair_engineering
        set status = #{status},
        maintenance_staff_pick_single_date = #{maintenanceStaffPickSingleDate}
        where id = #{id} and maintenance_staff = #{maintenanceStaff}
    </update>

    <insert id="submitReport" parameterType="com.api.model.butlerApp.ButlerRepairEngineeringReport">
        insert into sys_report_repair_engineering_report(
        repair_engineering_id,
        <if test="content != null and content != ''">
            content,
        </if>
        create_id,
        create_date
        )values (
        #{repairEngineeringId},
        <if test="content != null and content != ''">
            #{content},
        </if>
        #{createId},
        #{createDate}
        )
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>
    <insert id="completeMaintenance" parameterType="com.api.model.butlerApp.ButlerRepairEngineeringMaintenanceResults">
        insert into sys_report_repair_engineering_maintenance_results(
            repair_engineering_id,
            <if test="content != null and content != ''">
                content,
            </if>
            <if test="billMaterials != null and billMaterials != ''">
                bill_materials,
            </if>
            create_id,
            create_date
        )values (
            #{repairEngineeringId},
            <if test="content != null and content != ''">
                #{content},
            </if>
            <if test="billMaterials != null and billMaterials != ''">
                #{billMaterials},
            </if>
            #{createId},
            #{createDate}
        )
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>

    <update id="updateStatusById" parameterType="com.api.model.butlerApp.ButlerRepairEngineering">
        update sys_report_repair_engineering
        set status = #{status}
        where id = #{id}
    </update>

    <select id="findReportByRepairEngineeringId" parameterType="java.lang.Integer"
            resultType="com.api.vo.butlerApp.ButlerRepairEngineeringReportVo">
        select srrer.*,su.actual_name as create_name
        from sys_report_repair_engineering_report as srrer,sys_user as su
        where srrer.create_id = su.id and srrer.repair_engineering_id = #{repairEngineeringId}
        order by srrer.create_date desc
    </select>
    <select id="findNewResultByRepairEngineeringId" parameterType="java.lang.Integer"
            resultType="com.api.vo.butlerApp.ButlerRepairEngineeringResultsVo">
        select srremr.*,su.actual_name as create_name
        from sys_report_repair_engineering_maintenance_results as srremr,sys_user as su
        where srremr.create_id = su.id and srremr.repair_engineering_id = #{repairEngineeringId}
        order by srremr.create_date desc limit 1
    </select>

    <update id="submitAcceptance" parameterType="com.api.model.butlerApp.ButlerRepairEngineeringMaintenanceResults">
        update sys_report_repair_engineering_maintenance_results
        <set>
            results = #{results},
            <if test="advice != null and advice != ''">
                advice = #{advice},
            </if>
            acceptance_people = #{acceptancePeople},
            acceptance_date = #{acceptanceDate}
        </set>
        where id = #{id} and repair_engineering_id = #{repairEngineeringId}
    </update>

    <select id="findAcceptanceRecordByRepairEngineeringId" parameterType="java.lang.Integer"
            resultType="com.api.vo.butlerApp.ButlerRepairEngineeringAcceptanceRecordVo">
        select srremr.*,su.actual_name as create_name,su1.actual_name as acceptance_people_name
        from sys_user as su,sys_report_repair_engineering_maintenance_results as srremr
		LEFT JOIN sys_user as su1 on srremr.acceptance_people = su1.id
        where srremr.create_id = su.id and srremr.acceptance_people is not null
        and srremr.repair_engineering_id = #{repairEngineeringId}
        order by srremr.create_date DESC
    </select>
    <select id="findNewAcceptanceRecordByRepairEngineeringId" parameterType="java.lang.Integer"
            resultType="com.api.vo.butlerApp.ButlerRepairEngineeringAcceptanceRecordVo">
        select srremr.*,su.actual_name as create_name,su1.actual_name as acceptance_people_name
        from sys_user as su,sys_report_repair_engineering_maintenance_results as srremr
		LEFT JOIN sys_user as su1 on srremr.acceptance_people = su1.id
        where srremr.create_id = su.id and srremr.acceptance_people is not null
        and srremr.repair_engineering_id = #{repairEngineeringId}
        order by srremr.create_date DESC limit 1
    </select>

    <update id="startRectification" parameterType="com.api.model.butlerApp.ButlerRepairEngineering">
        update sys_report_repair_engineering
        set status = #{status}
        where id = #{id} and maintenance_staff = #{maintenanceStaff}
    </update>



</mapper>