<?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.jeeplus.psimanage.loss.mapper.PsiLossBasicMapper">

    <update id="updateStatusById">
        UPDATE psi_management_loss_basics
        SET status = #{status}
        WHERE id = #{id}
    </update>

    <select id="findChildIds" resultType="java.lang.String">
        SELECT id
        FROM sys_office
        WHERE parent_id = #{department}
    </select>

    <select id="findList" resultType="com.jeeplus.psimanage.loss.service.dto.PsiLossDto">
        SELECT DISTINCT
            a.id,
            a.create_by_id AS createBy,
            a.create_time,
            a.status,
            a.proc_ins_id,
            a.process_definition_id,
            a.collect_no AS lossNo,
            a.handled_by,
            a.handled_by_office,
            a.collect_date AS lossDate,
            b.goods_name,
            b.id AS detail_id,
            so.name AS handledByOfficeName,
            su.name AS handledByName,
            art.ID_ AS task_id
        FROM psi_management_loss_basics a
        LEFT JOIN sys_user su ON a.handled_by = su.id
        LEFT JOIN sys_office so ON su.office_id = so.id
        LEFT JOIN psi_management_loss_detailed b ON a.id = b.basic_id AND b.del_flag = '0'
        LEFT JOIN act_ru_task art ON a.proc_ins_id = art.PROC_INST_ID_
        ${ew.customSqlSegment}
        ORDER BY a.update_time DESC
    </select>

    <select id="getLossByTradeName" resultType="com.jeeplus.psimanage.loss.domain.PsiLossDetailed">
        SELECT
            a.goods_name,
            b.id,
            so.name AS handledByOfficeName,
            su.name AS handledByName,
            a.loss_number,
            a.produce_date,
            b.collect_no as lossNo,
            b.collect_date as createTime
        FROM psi_management_loss_detailed a
            left join psi_management_loss_basics b on a.basic_id = b.id
                 LEFT JOIN sys_user su ON b.handled_by = su.id
                 LEFT JOIN sys_office so ON su.office_id = so.id
            ${ew.customSqlSegment}
        ORDER BY a.update_time DESC
    </select>

    <select id="getUserNameByUserId" resultType="java.lang.String">
        SELECT name
        FROM sys_user
        WHERE id = #{handledBy}
    </select>

    <select id="getOfficeNameByOfficeId" resultType="java.lang.String">
        SELECT name
        FROM sys_office
        WHERE id = #{handledByOffice}
    </select>
</mapper>
