<?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.fjhx.mes.mapper.report.ReportLossesDetailsMapper">
    <select id="getPage" resultType="com.fjhx.mes.entity.report.vo.ReportLossesDetailsVo">
        SELECT rld.id,
               rld.type,
               rld.prod_order_id,
               rld.material_id,
               rld.quantity,
               rld.resp_user_set,
               rld.remark,
               rld.create_user,
               rld.create_time,
               rld.update_user,
               rld.update_time,
               po.`code`                  AS prodOrderCode,
               GROUP_CONCAT(su.nick_name) AS respUserName,
               rld.repo_time,
               rld.process_id
        FROM report_losses_details rld
                 LEFT JOIN production_order po ON rld.prod_order_id = po.id
                 LEFT JOIN sys_user su ON FIND_IN_SET(su.user_id, rld.resp_user_set)
            ${ew.customSqlSegment}
    </select>
    <select id="lossesStatistics" resultType="com.alibaba.fastjson2.JSONObject">
        SELECT IFNULL(SUM(IF(rld.type = 1, rld.quantity, 0)), 0) AS replenishSumQuantity,
               IFNULL(SUM(IF(rld.type = 2, rld.quantity, 0)), 0) AS lossSumQuantity
        FROM report_losses_details rld
            ${ew.customSqlSegment}
    </select>

</mapper>
