<?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.ruoyi.web.v1.mapper.SlaughterBatchMapper">
    
    <resultMap type="com.ruoyi.web.v1.model.SlaughterBatch" id="SlaughterBatchResult">
        <result property="id"    column="id"    />
        <result property="slaughterTime"    column="slaughter_time"    />
        <result property="distributeBatchId"    column="distribute_batch_id"    />
        <result property="entranceBatchId"    column="entrance_batch_id"    />
        <result property="salePlace"    column="sale_place"    />
        <result property="meatCert"    column="meat_cert"    />
        <result property="animalCert"    column="animal_cert"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <resultMap id="slaughterBatchDTOResultMap" type="com.ruoyi.web.v1.DTO.SlaughterBatchDTO">
        <!-- 主表 -->
        <id property="id" column="id"/>
        <result property="slaughterTime" column="slaughter_time"/>
        <result property="salePlace" column="sale_place"/>
        <result property="meatCert" column="meat_cert"/>
        <result property="animalCert" column="animal_cert"/>

        <!-- 分销批次 -->
        <result property="slaughterCode" column="slaughter_code"/>
        <result property="amount" column="amount"/>
        <result property="beforeWeight" column="before_weight"/>

        <!--肉商 -->
        <result property="purchaserName" column="purchaser_name"/>

        <!-- 供应商 -->
        <result property="supplierName" column="supplier_name"/>

        <!-- 入场批次 -->
        <result property="variety" column="variety"/>
        <result property="animalCertNo" column="animal_cert_no"/>

        <!-- 重量统计 -->
        <result property="sideWeight" column="side_weight"/>
        <result property="headWeight" column="head_weight"/>
        <result property="redWeight" column="red_weight"/>
        <result property="whiteWeight" column="white_weight"/>
    </resultMap>

    <resultMap id="SlaughterReportResultMap" type="com.ruoyi.web.v1.DTO.SlaughterReportDTO">
        <!-- 主表 -->
        <id property="id" column="id"/>
        <result property="slaughterTime" column="slaughter_time"/>
        <result property="salePlace" column="sale_place"/>
        <result property="meatCert" column="meat_cert"/>
        <result property="animalCert" column="animal_cert"/>

        <!-- 分销批次 -->
        <result property="slaughterCode" column="slaughter_code"/>
        <result property="amount" column="amount"/>
        <result property="beforeWeight" column="before_weight"/>

        <!--肉商 -->
        <result property="purchaserName" column="purchaser_name"/>

        <!-- 供应商 -->
        <result property="supplierName" column="supplier_name"/>

        <!-- 入场批次 -->
        <result property="variety" column="variety"/>
        <result property="animalCertNo" column="animal_cert_no"/>

        <!-- 重量统计 -->
        <result property="sideWeight" column="side_weight"/>
        <result property="headWeight" column="head_weight"/>
        <result property="redWeight" column="red_weight"/>
        <result property="whiteWeight" column="white_weight"/>
    </resultMap>

    <resultMap type="com.ruoyi.web.v1.DTO.PorkProduceDTO" id="PorkProduceResult">
        <result property="productName"    column="product_name"    />
        <result property="produceTime"    column="produce_time"    />
        <result property="hookNo"    column="hook_no"    />
        <result property="slaughterCode"    column="slaughter_code"    />
        <result property="finalWeight"    column="final_weight"    />
    </resultMap>

    <!--生猪屠宰情况日汇总表  2025-10-09-->
    <resultMap type="com.ruoyi.web.v1.model.response.GetDailySlaughterReportResp" id="GetDailySlaughterReportResp">
        <result property="createTime"    column="date"    />
        <result property="amount"    column="daily_total_amount"    />
        <result property="checkAmount"    column="daily_check_amount"    />
        <result property="qualifiedAmount"    column="daily_qualified_amount"    />
        <result property="tuzaiAmount"    column="daily_tuzai_amount"    />
        <result property="dongAmount"    column="daily_dong_amount"    />
        <result property="weight"    column="daily_weight"    />
        <result property="roadDealAmount"    column="daily_road_deal_amount"    />
        <result property="illnessDeadAmount"    column="daily_illness_dead_amount"    />
        <result property="illnessDiseaseAmount"    column="daily_illness_disease_amount"    />
        <result property="snapKillAmount"    column="daily_snap_kill_amount"    />
        <result property="dealAmount"    column="daily_deal_amount"    />
        <result property="animalType"    column="animal_type"    />
    </resultMap>

    <!--生猪入场查验情况表  2025-10-15-->
    <resultMap type="com.ruoyi.web.v1.model.response.GetDailyEntranceReportResp" id="GetDailyEntranceReportResp">
        <result property="createTime"    column="entrance_date"    />
        <result property="cargoOwner"    column="supplier_name"    />
        <result property="originPlace"    column="origin_place"    />
        <result property="animalCertNo"    column="animal_cert_no"    />
        <result property="amount"    column="daily_total_amount"    />
        <result property="roadDealAmount"    column="daily_road_deal_amount"    />
        <result property="illnessDeadAmount"    column="daily_illness_dead_amount"    />
        <result property="illnessDiseaseAmount"    column="daily_illness_disease_amount"    />
        <result property="snapKillAmount"    column="daily_snap_kill_amount"    />
        <result property="animalType"    column="animal_type"    />
    </resultMap>

    <!--违禁药物检测及屠宰检验情况表  2025-10-10-->
    <resultMap type="com.ruoyi.web.v1.model.response.GetDailyDrugCheckAndSlaughterReportResp" id="GetDailyDrugCheckAndSlaughterReportResp">

        <result property="entranceBatchId"    column="entrance_batch_id"    />
        <result property="createTime"    column="entrance_date"    />
        <result property="cargoOwner"    column="supplier_name"    />
        <result property="amount"    column="daily_total_amount"    />
        <result property="dealAmount"    column="daily_deal_amount"    />
        <result property="unqualifiedDongAmount"    column="daily_unqualified_dong_amount"    />
        <result property="weight"    column="daily_unqualified_weight"    />
        <result property="roadDealAmount"    column="daily_road_deal_amount"    />
        <result property="illnessDeadAmount"    column="daily_illness_dead_amount"    />
        <result property="illnessDiseaseAmount"    column="daily_illness_disease_amount"    />
        <result property="snapKillAmount"    column="daily_snap_kill_amount"    />
    </resultMap>

    <!--生猪屠宰环节无害化处理表  2025-10-11-->
    <resultMap type="com.ruoyi.web.v1.model.response.GetDailyHarmlessReportResp" id="GetDailyHarmlessReportResp">
        <result property="entranceBatchId"    column="entrance_batch_id"    />
        <result property="createTime"    column="entrance_date"    />
        <result property="cargoOwner"    column="supplier_name"    />
        <result property="dealAmount"    column="daily_deal_amount"    />
        <result property="roadDealAmount"    column="daily_road_deal_amount"    />
        <result property="illnessDeadAmount"    column="daily_illness_dead_amount"    />
        <result property="illnessDiseaseAmount"    column="daily_illness_disease_amount"    />
        <result property="snapKillAmount"    column="daily_snap_kill_amount"    />
        <result property="produceName"    column="produce_name"    />
        <result property="unqualifiedNum"    column="unqualified_num"    />
        <result property="unit"    column="unit"    />
        <result property="animalType"    column="animal_type"    />
    </resultMap>

    <!--产品-->
    <resultMap type="com.ruoyi.web.v1.model.response.ProduceCheckDTO" id="ProduceCheckDTO">
        <result property="produceName"    column="produce_name"    />
        <result property="unqualifiedNum"    column="unqualified_num"    />
        <result property="unit"    column="unit"    />
    </resultMap>

    <!--生猪产品出场情况表  2025-10-12-->
    <resultMap type="com.ruoyi.web.v1.model.response.GetDailyOutputReportResp" id="GetDailyOutputReportResp">
        <result property="createTime"    column="create_time"    />
        <result property="cargoOwner"    column="purchaser_name"    />
        <result property="animalCert"    column="animal_cert"    />
        <result property="meatCert"    column="meat_cert"    />
        <result property="produceName"    column="produce_name"    />
        <result property="animalType"    column="animal_type"    />
        <result property="businessPlace"    column="business_place"    />
        <result property="unit"    column="unit"    />
        <result property="amount_1"    column="amount_1"    />
        <result property="weight_1"    column="weight_1"    />
        <result property="amount_2"    column="amount_2"    />
        <result property="weight_2"    column="weight_2"    />
        <result property="amount_3"    column="amount_3"    />
        <result property="weight_3"    column="weight_3"    />
    </resultMap>

    <sql id="selectSlaughterBatchVo">
        select id, slaughter_time, distribute_batch_id, entrance_batch_id, sale_place, meat_cert, animal_cert, create_time, create_by, update_time, update_by, del_flag from slaughter_batch
    </sql>

    <select id="selectSlaughterBatchList" parameterType="com.ruoyi.web.v1.model.request.ReqSlaughterBatch" resultMap="slaughterBatchDTOResultMap">
        select a.id,a.slaughter_time,a.sale_place,a.meat_cert,a.animal_cert,
        b.slaughter_code,b.amount,b.weight as before_weight,
        c.purchaser_name,
        d.supplier_name,
        e.variety,e.animal_cert_no,
        (select sum(final_weight) from pork_side_produce where distribute_batch_id = a.distribute_batch_id and del_flag = '0') as side_weight,
        (select sum(final_weight) from pork_other_produce where distribute_batch_id = a.distribute_batch_id and product_name='猪头' and del_flag = '0') as head_weight,
        (select sum(final_weight) from pork_other_produce where distribute_batch_id = a.distribute_batch_id and product_name='红脏' and del_flag = '0') as red_weight,
        (select sum(final_weight) from pork_other_produce where distribute_batch_id = a.distribute_batch_id and product_name='白脏' and del_flag = '0') as white_weight
        from slaughter_batch as a
        left join distribute_batch as b on b.id = a.distribute_batch_id and b.del_flag = '0'
        left join purchaser as c on c.id = b.purchaser_id and c.del_flag = '0'
        left join supplier as d on d.id = b.supplier_id and d.del_flag = '0'
        left join entrance_batch as e on e.id = a.entrance_batch_id and e.del_flag = '0'
        <where>
            a.del_flag = '0'
            <if test="slaughterTime != null "> and DATE(a.slaughter_time) = DATE(#{slaughterTime})</if>
            <if test="animalCertNo != null  and animalCertNo != ''"> and e.animal_cert_no like concat('%', #{animalCertNo}, '%')</if>
            <if test="slaughterCode != null  and slaughterCode != ''"> and b.slaughter_code like concat('%', #{slaughterCode}, '%')</if>
        </where>
        order by a.slaughter_time desc
    </select>
    
    <select id="selectSlaughterBatchById" parameterType="Long" resultMap="slaughterBatchDTOResultMap">
        select a.id,a.slaughter_time,a.sale_place,a.meat_cert,animal_cert,
               b.slaughter_code,b.amount,b.weight as before_weight,
               c.purchaser_name,
               d.supplier_name,
               e.variety,e.animal_cert_no,
               (select sum(final_weight) from pork_side_produce where distribute_batch_id = a.distribute_batch_id and del_flag = '0') as side_weight,
               (select sum(final_weight) from pork_other_produce where distribute_batch_id = a.distribute_batch_id and product_name='猪头' and del_flag = '0') as head_weight,
               (select sum(final_weight) from pork_other_produce where distribute_batch_id = a.distribute_batch_id and product_name='红脏' and del_flag = '0') as red_weight,
               (select sum(final_weight) from pork_other_produce where distribute_batch_id = a.distribute_batch_id and product_name='白脏' and del_flag = '0') as white_weight
            from slaughter_batch as a
            left join distribute_batch as b on b.id = a.distribute_batch_id and b.del_flag = '0'
            left join purchaser as c on c.id = b.purchaser_id and c.del_flag = '0'
            left join supplier as d on d.id = b.supplier_id and d.del_flag = '0'
            left join entrance_batch as e on e.id = a.entrance_batch_id and e.del_flag = '0'
        where a.id = #{id} and a.del_flag = '0'
    </select>


    <insert id="insertSlaughterBatch" parameterType="com.ruoyi.web.v1.model.SlaughterBatch" useGeneratedKeys="true" keyProperty="id">
        insert into slaughter_batch
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="slaughterTime != null">slaughter_time,</if>
            <if test="distributeBatchId != null">distribute_batch_id,</if>
            <if test="entranceBatchId != null">entrance_batch_id,</if>
            <if test="salePlace != null">sale_place,</if>
            <if test="meatCert != null">meat_cert,</if>
            <if test="animalCert != null">animal_cert,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            del_flag
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="slaughterTime != null">#{slaughterTime},</if>
            <if test="distributeBatchId != null">#{distributeBatchId},</if>
            <if test="entranceBatchId != null">#{entranceBatchId},</if>
            <if test="salePlace != null">#{salePlace},</if>
            <if test="meatCert != null">#{meatCert},</if>
            <if test="animalCert != null">#{animalCert},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            '0'
         </trim>
    </insert>

    <update id="updateSlaughterBatch" parameterType="com.ruoyi.web.v1.model.SlaughterBatch">
        update slaughter_batch
        <trim prefix="SET" suffixOverrides=",">
            <if test="slaughterTime != null">slaughter_time = #{slaughterTime},</if>
            <if test="distributeBatchId != null">distribute_batch_id = #{distributeBatchId},</if>
            <if test="entranceBatchId != null">entrance_batch_id = #{entranceBatchId},</if>
            <if test="salePlace != null">sale_place = #{salePlace},</if>
            <if test="meatCert != null">meat_cert = #{meatCert},</if>
            <if test="animalCert != null">animal_cert = #{animalCert},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSlaughterBatchById" parameterType="Long">
        update slaughter_batch set del_flag = '2'  where id = #{id}
    </delete>

    <!--根据分销批次删除屠宰批次-->
    <delete id="deleteSlaughterBatchByDistributeId" parameterType="Long">
        update slaughter_batch set del_flag = '2'  where distribute_batch_id = #{id}
    </delete>

    <delete id="deleteSlaughterBatchByIds" parameterType="String">
        update slaughter_batch set del_flag = '2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!--根据分销批次批量删除屠宰批次-->
    <delete id="deleteSlaughterBatchByDistributeIds" parameterType="String">
        update slaughter_batch set del_flag = '2' where distribute_batch_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!--根据入场批次和分销批次ID获取各部位称重记录-->
    <select id="selectPorkProduceByBatch" parameterType="com.ruoyi.web.v1.model.request.ReqEntranceAndDistribute" resultMap="PorkProduceResult">
            (
                select product_name,produce_time,hook_no,slaughter_code,final_weight
                    from pork_side_produce
                    <where>
                        del_flag = '0'
                        <if test="entranceBatchId != null "> and entrance_batch_id = #{entranceBatchId}</if>
                        <if test="distributeBatchId != null "> and distribute_batch_id = #{distributeBatchId}</if>
                    </where>
            )
            union all
            (
            select product_name,produce_time,'-' as hook_no,slaughter_code,final_weight
            from pork_other_produce
            <where>
                del_flag = '0'
                <if test="entranceBatchId != null "> and entrance_batch_id = #{entranceBatchId}</if>
                <if test="distributeBatchId != null "> and distribute_batch_id = #{distributeBatchId}</if>
            </where>
            )
            order by produce_time desc
    </select>

    <!--生产情况报表-->
    <select id="selectSlaughterReport" parameterType="com.ruoyi.web.v1.model.request.ReqSlaughterReport" resultMap="SlaughterReportResultMap">
        select a.id,a.slaughter_time,a.sale_place,a.meat_cert,a.animal_cert,
        b.slaughter_code,b.amount,b.weight as before_weight,
        c.purchaser_name,
        d.supplier_name,
        (select sum(final_weight) from pork_side_produce where distribute_batch_id = a.distribute_batch_id and del_flag = '0') as side_weight,
        (select sum(final_weight) from pork_other_produce where distribute_batch_id = a.distribute_batch_id and product_name='猪头' and del_flag = '0') as head_weight,
        (select sum(final_weight) from pork_other_produce where distribute_batch_id = a.distribute_batch_id and product_name='红脏' and del_flag = '0') as red_weight,
        (select sum(final_weight) from pork_other_produce where distribute_batch_id = a.distribute_batch_id and product_name='白脏' and del_flag = '0') as white_weight
        from slaughter_batch as a
        left join distribute_batch as b on b.id = a.distribute_batch_id and b.del_flag = '0'
        left join purchaser as c on c.id = b.purchaser_id and c.del_flag = '0'
        left join supplier as d on d.id = b.supplier_id and d.del_flag = '0'
        left join entrance_batch as e on e.id = a.entrance_batch_id and e.del_flag = '0'
        <where>
            a.del_flag = '0'
            <if test="slaughterTime != null "> and DATE(a.slaughter_time) = DATE(#{slaughterTime})</if>
            <if test="purchaserName != null  and purchaserName != ''"> and c.purchaser_name like concat('%', #{purchaserName}, '%')</if>
            <if test="supplierName != null  and supplierName != ''"> and d.supplier_name like concat('%', #{supplierName}, '%')</if>
        </where>
        order by a.slaughter_time desc
    </select>

    <select id="getAllList" resultType="com.ruoyi.web.v1.DTO.ProductionVo">
        select
            b.productName produceName,
            a.afterWeight weight,
            b.unit unit,
            a.amount amount
        FROM
        (
            select produce_name produceName,SUM(after_weight) afterWeight,SUM(amount) amount
            FROM js_distribution
            <where>
                <if test="purchaserId != null "> and purchaser_id = #{purchaserId}</if>
                <if test="entranceBatchId != null "> and entrance_batch_id = #{entranceBatchId}</if>
            </where>
            GROUP BY produce_name
        )
        as a inner join
        (
            select product_name productName,unit unit
            FROM product_category
            WHERE  del_flag='0' and on_sale_flag='1'
        ) AS b
        on a.produceName=b.productName

    </select>

    <!--生猪屠宰情况日汇总表  2025-10-09-->
    <select id="selectDailySlaughterReport" parameterType="com.ruoyi.web.v1.model.request.GetDailySlaughterReportReq" resultMap="GetDailySlaughterReportResp">
        SELECT
            `date`,
        b.animal_type,
            COALESCE(SUM(b.entrance_amount_total),0) AS daily_total_amount,

            COALESCE(SUM(c.check_amount_total), 0) AS daily_check_amount,
            COALESCE(SUM(c.qualified_amount_total), 0) AS daily_qualified_amount,
            CEIL(COALESCE(SUM(q.pork_side_amount_total), 0)/2) AS daily_dong_amount,
            CONVERT(COALESCE(SUM(q.after_weight_total), 0),decimal(10,2)) AS daily_weight,
            COALESCE(SUM(p.illness_dead_amount_total), 0) AS daily_illness_dead_amount,
            COALESCE(SUM(p.illness_disease_amount_total), 0) AS daily_illness_disease_amount,
            COALESCE(SUM(p.road_deal_amount_total), 0) AS daily_road_deal_amount,
            COALESCE(SUM(p.snap_kill_amount_total), 0) AS daily_snap_kill_amount,
            COALESCE(SUM(m.deal_amount_total), 0) AS daily_deal_amount
        FROM
            (
                -- 获取所有入场日期
                SELECT DISTINCT DATE(entrance_time) AS `date`
                FROM entrance_batch
                WHERE del_flag = '0'
            UNION
                -- 获取所有药物检测日期
                SELECT DISTINCT DATE(check_time) AS `date`
                FROM js_drug_check
            UNION
                -- 获取所有产品登记日期
                SELECT DISTINCT DATE(distribution_time) AS `date`
                FROM js_distribution
            UNION
                -- 获取无害化登记日期
                SELECT DISTINCT DATE(create_time) AS `date`
                FROM harmless_treatment
            UNION
                -- 获取无害化登记日期
                SELECT DISTINCT DATE(rest_inspection_time) AS `date`
                FROM js_rest_inspection

            ) AS all_dates
            LEFT JOIN (
                SELECT DATE(entrance_time) AS entrance_date, SUM(amount) AS entrance_amount_total ,animal_type
                FROM entrance_batch
                WHERE del_flag = '0'
                <if test="animalType != null">
                    and    animal_type = #{animalType}
                </if>

                GROUP BY DATE(entrance_time)
            ) b ON b.entrance_date = all_dates.`date`
            LEFT JOIN (
                SELECT DATE(check_time) AS check_date, SUM(check_amount) AS check_amount_total,SUM(CASE WHEN result = '合格' THEN COALESCE(check_amount, 0) ELSE 0 END) AS qualified_amount_total
                FROM js_drug_check
                GROUP BY DATE(check_time)
            ) c ON c.check_date = all_dates.`date`
            LEFT JOIN (
                SELECT DATE(distribution_time) AS distribution_date, SUM(amount) AS pork_side_amount_total, SUM(after_weight) AS after_weight_total
                FROM js_distribution
                WHERE produce_name='白条'
                GROUP BY DATE(distribution_time)
            ) q ON q.distribution_date = all_dates.`date`
            LEFT JOIN (
                SELECT DATE(create_time) AS harmless_create_date, SUM(illness_dead_amount) AS illness_dead_amount_total, SUM(illness_disease_amount) AS illness_disease_amount_total, SUM(road_deal_amount) AS road_deal_amount_total, SUM(snap_kill_amount) AS snap_kill_amount_total
                FROM harmless_treatment
                GROUP BY DATE(create_time)
            ) p ON p.harmless_create_date = all_dates.`date`
            LEFT JOIN (
                SELECT DATE(rest_inspection_time) AS rest_inspection_date,SUM(deal_amount) AS deal_amount_total
                FROM js_rest_inspection
                GROUP BY DATE(rest_inspection_time)
            ) m ON m.rest_inspection_date = all_dates.`date`
        <where>
            <if test="startTime != null">
                and `date` <![CDATA[>=]]> DATE(#{startTime})
            </if>
            <if test="endTime != null">
                and `date` <![CDATA[<=]]> DATE(#{endTime})
            </if>
        </where>
        GROUP BY `date`
        ORDER BY `date` DESC
    </select>

    <!--生猪入场查验情况表  2025-10-15-->
    <select id="selectDailyEntranceReport" parameterType="com.ruoyi.web.v1.model.request.GetDailyEntranceReportReq" resultMap="GetDailyEntranceReportResp">
        SELECT
            DATE(b.entrance_time) AS entrance_date,
            s.supplier_name,
            b.origin_place,
            b.animal_cert_no,
            b.animal_type,
            COALESCE(SUM(b.amount),0) AS daily_total_amount,
            COALESCE(SUM(p.illness_dead_amount_total), 0) AS daily_illness_dead_amount,
            COALESCE(SUM(p.illness_disease_amount_total), 0) AS daily_illness_disease_amount,
            COALESCE(SUM(p.road_deal_amount_total), 0) AS daily_road_deal_amount,
            COALESCE(SUM(p.snap_kill_amount_total), 0) AS daily_snap_kill_amount
        FROM
        entrance_batch b
        LEFT JOIN supplier AS s ON s.id = b.supplier_id and s.del_flag = '0'
        LEFT JOIN (
            SELECT entrance_batch_id, SUM(illness_dead_amount) AS illness_dead_amount_total, SUM(illness_disease_amount) AS illness_disease_amount_total, SUM(road_deal_amount) AS road_deal_amount_total, SUM(snap_kill_amount) AS snap_kill_amount_total
            FROM harmless_treatment
            GROUP BY entrance_batch_id
        ) p ON b.id = p.entrance_batch_id
        <where>
            b.del_flag = '0'
            <if test="startTime != null">
                and DATE(b.entrance_time) <![CDATA[>=]]> DATE(#{startTime})
            </if>
            <if test="endTime != null">
                and DATE(b.entrance_time) <![CDATA[<=]]> DATE(#{endTime})
            </if>
            <if test="supplierName != null and supplierName != ''">
                and s.supplier_name like concat('%', #{supplierName}, '%')
            </if>
            <if test="animalCertNo != null and animalCertNo != ''">
                and b.animal_cert_no like concat('%', #{animalCertNo}, '%')
            </if>
            <if test="animalType != null and animalType != ''">
                and  b.animal_type like concat('%', #{animalType}, '%')
            </if>
        </where>
        GROUP BY DATE(b.entrance_time),s.supplier_name,b.origin_place,b.animal_cert_no
        ORDER BY entrance_date desc
    </select>

    <!--违禁药物检测及屠宰检验情况表  2025-10-10-->
    <select id="selectDailyDrugCheckAndSlaughterReport" parameterType="com.ruoyi.web.v1.model.request.GetDailyDrugCheckAndSlaughterReportReq" resultMap="GetDailyDrugCheckAndSlaughterReportResp">
        SELECT
            b.id AS entrance_batch_id,
            DATE(b.entrance_time) AS entrance_date,
            s.supplier_name,
            COALESCE(SUM(b.amount),0) AS daily_total_amount,
            CEIL(COALESCE(SUM(q.unqualified_pork_side_amount_total), 0)/2) AS daily_unqualified_dong_amount,
            CONVERT(COALESCE(SUM(q.unqualified_weight_total), 0),decimal(10,2)) AS daily_unqualified_weight,
            COALESCE(SUM(p.illness_dead_amount_total), 0) AS daily_illness_dead_amount,
            COALESCE(SUM(p.illness_disease_amount_total), 0) AS daily_illness_disease_amount,
            COALESCE(SUM(p.road_deal_amount_total), 0) AS daily_road_deal_amount,
            COALESCE(SUM(p.snap_kill_amount_total), 0) AS daily_snap_kill_amount,
            COALESCE(SUM(m.deal_amount_total), 0) AS daily_deal_amount
        FROM
            entrance_batch b
            LEFT JOIN supplier AS s ON s.id = b.supplier_id and s.del_flag = '0'
            LEFT JOIN (
                SELECT entrance_batch_id, SUM(CASE WHEN produce_name = '白条' AND types = 1 THEN COALESCE(unqualified_num, 0) ELSE 0 END) AS unqualified_pork_side_amount_total,SUM(CASE WHEN types = 0 THEN COALESCE(unqualified_num, 0) ELSE 0 END) AS unqualified_weight_total
                FROM js_produce_check
                WHERE is_deleted=0
                GROUP BY entrance_batch_id
            ) q ON b.id = q.entrance_batch_id
            LEFT JOIN (
                SELECT entrance_batch_id, SUM(illness_dead_amount) AS illness_dead_amount_total, SUM(illness_disease_amount) AS illness_disease_amount_total, SUM(road_deal_amount) AS road_deal_amount_total, SUM(snap_kill_amount) AS snap_kill_amount_total
                FROM harmless_treatment
                GROUP BY entrance_batch_id
            ) p ON b.id = p.entrance_batch_id
            LEFT JOIN (
                SELECT jdc.entrance_batch_id,COALESCE(SUM(jri.deal_amount), 0) AS deal_amount_total
                FROM js_rest_inspection AS jri
                INNER JOIN js_divide_circle AS jdc ON jri.divide_circle_id = jdc.id
                GROUP BY jdc.entrance_batch_id
            ) m ON b.id = m.entrance_batch_id
        <where>
            b.del_flag = '0'
            <if test="startTime != null">
                and DATE(b.entrance_time) <![CDATA[>=]]> DATE(#{startTime})
            </if>
            <if test="endTime != null">
                and DATE(b.entrance_time) <![CDATA[<=]]> DATE(#{endTime})
            </if>
            <if test="supplierName != null and supplierName != ''">
                and s.supplier_name like concat('%', #{supplierName}, '%')
            </if>
        </where>
        GROUP BY DATE(b.entrance_time),s.supplier_name,b.id
        ORDER BY entrance_date desc
    </select>

    <!--查询违禁药物 2025-10-10-->
    <select id="getDrug" resultType="com.ruoyi.web.v1.model.response.DrugCheckDTO">
        select
            IFNULL(sum(check_amount),'0') checkAmount,
            GROUP_CONCAT(result SEPARATOR ' | ') AS result ,
            GROUP_CONCAT(examine_items SEPARATOR ',') examineItems
        from js_drug_check
        <where>
            entrance_batch_id=#{entranceBatchId}
            <if test="checkType!=null and checkType !=''">
                and check_type=#{checkType}
            </if>
        </where>
        group by  entrance_batch_id
        limit 1
    </select>

    <!--生猪屠宰环节无害化处理表  2025-10-11-->
    <select id="selectDailyHarmlessReport" parameterType="com.ruoyi.web.v1.model.request.GetDailyHarmlessReportReq" resultMap="GetDailyHarmlessReportResp">
        (
            SELECT
                b.id AS entrance_batch_id,
                b.animal_type AS animal_type,
                DATE(b.entrance_time) AS entrance_date,
                s.supplier_name,
                COALESCE(SUM(p.illness_dead_amount_total), 0) AS daily_illness_dead_amount,
                COALESCE(SUM(p.illness_disease_amount_total), 0) AS daily_illness_disease_amount,
                COALESCE(SUM(p.road_deal_amount_total), 0) AS daily_road_deal_amount,
                COALESCE(SUM(p.snap_kill_amount_total), 0) AS daily_snap_kill_amount,
                COALESCE(SUM(m.deal_amount_total), 0) AS daily_deal_amount,
                NULL AS produce_name,
                NULL AS unit,
                NULL AS unqualified_num
            FROM
                entrance_batch b
            LEFT JOIN supplier AS s ON s.id = b.supplier_id AND s.del_flag = '0'
            LEFT JOIN (
                SELECT
                    entrance_batch_id,
                    SUM(illness_dead_amount) AS illness_dead_amount_total,
                    SUM(illness_disease_amount) AS illness_disease_amount_total,
                    SUM(road_deal_amount) AS road_deal_amount_total,
                    SUM(snap_kill_amount) AS snap_kill_amount_total
                FROM harmless_treatment
                GROUP BY entrance_batch_id
            ) p ON b.id = p.entrance_batch_id
            LEFT JOIN (
                SELECT
                    jdc.entrance_batch_id,
                    COALESCE(SUM(jri.deal_amount), 0) AS deal_amount_total
                FROM js_rest_inspection AS jri
                INNER JOIN js_divide_circle AS jdc ON jri.divide_circle_id = jdc.id
                GROUP BY jdc.entrance_batch_id
            ) m ON b.id = m.entrance_batch_id
            <where>
                b.del_flag = '0'
                <if test="startTime != null">
                    and DATE(b.entrance_time) <![CDATA[>=]]> DATE(#{startTime})
                </if>
                <if test="endTime != null">
                    and DATE(b.entrance_time) <![CDATA[<=]]> DATE(#{endTime})
                </if>
                <if test="supplierName != null and supplierName != ''">
                    and s.supplier_name like concat('%', #{supplierName}, '%')
                </if>
                <if test="animalType != null">
                    and    b.animal_type =  #{animalType}
                </if>
            </where>
            GROUP BY DATE(b.entrance_time), s.supplier_name, b.id
        )
        UNION ALL
        (
            SELECT
                NULL AS entrance_batch_id,
                DATE(check_time) AS entrance_date,
                NULL AS supplier_name,
                NULL AS animal_type,
                NULL AS daily_illness_dead_amount,
                NULL AS daily_illness_disease_amount,
                NULL AS daily_road_deal_amount,
                NULL AS daily_snap_kill_amount,
                NULL AS daily_deal_amount,
                "不可食用（修割三腺）" AS produce_name,
                unit,
                CONVERT(COALESCE(SUM(unqualified_num), 0),decimal(10,2)) AS unqualified_num
            FROM js_produce_check
            <where>
                produce_name = '不可食用（修割三腺）' and unqualified_num > 0 and is_deleted=0
                <if test="startTime != null">
                    and DATE(check_time) <![CDATA[>=]]> DATE(#{startTime})
                </if>
                <if test="endTime != null">
                    and DATE(check_time) <![CDATA[<=]]> DATE(#{endTime})
                </if>
                <if test="supplierName != null and supplierName != ''">
                    and supplier_name like concat('%', #{supplierName}, '%')
                </if>
            </where>
            GROUP BY DATE(check_time), unit
        )
        ORDER BY entrance_date DESC,entrance_batch_id DESC
    </select>

    <!--查询生产检疫检验 2025-10-11-->
    <select id="getProduceCheck" resultMap="ProduceCheckDTO">
        select produce_name,CONVERT(COALESCE(SUM(unqualified_num), 0),decimal(10,2)) AS unqualified_num,unit
        from js_produce_check
        <where>
            entrance_batch_id=#{entranceBatchId} and unqualified_num > 0 and is_deleted=0
        </where>
        group by  entrance_batch_id,produce_name,unit
    </select>

    <!--生猪产品出场情况表  2025-10-12-->
    <select id="selectDailyOutputReport" parameterType="com.ruoyi.web.v1.model.request.GetDailyOutputReportReq" resultMap="GetDailyOutputReportResp">
        SELECT
            DATE(jd.distribution_time) as create_time,
            p.business_place ,
            jp.animal_cert,
            jp.meat_cert,
            jd.produce_name,
            jd.unit,
            jd.purchaser_name,
            jd.animal_type,
            SUM(CASE WHEN p.sale_place = '1' THEN COALESCE(jd.amount, 0) ELSE 0 END) AS amount_1,
            convert(SUM(CASE WHEN p.sale_place = '1' THEN COALESCE(jd.after_weight, 0) ELSE 0 END),decimal(10,2)) AS weight_1,
            SUM(CASE WHEN p.sale_place = '2' THEN COALESCE(jd.amount, 0) ELSE 0 END) AS amount_2,
            convert(SUM(CASE WHEN p.sale_place = '2' THEN COALESCE(jd.after_weight, 0) ELSE 0 END),decimal(10,2)) AS weight_2,
            SUM(CASE WHEN p.sale_place = '3' THEN COALESCE(jd.amount, 0) ELSE 0 END) AS amount_3,
            convert(SUM(CASE WHEN p.sale_place = '3' THEN COALESCE(jd.after_weight, 0) ELSE 0 END),decimal(10,2)) AS weight_3
        FROM js_distribution AS jd
        LEFT JOIN
            (
                SELECT entrance_batch_id,purchaser_id,supplier_id, animal_cert,meat_cert
                FROM js_produce_code
                GROUP BY entrance_batch_id,purchaser_id,supplier_id, animal_cert,meat_cert
            )jp ON jd.entrance_batch_id=jp.entrance_batch_id AND jd.purchaser_id=jp.purchaser_id AND jd.supplier_id=jp.supplier_id
        LEFT JOIN purchaser as p on p.id=jd.purchaser_id
        <where>
            <if test="startTime != null">
                and DATE(jd.distribution_time) <![CDATA[>=]]> DATE(#{startTime})
            </if>
            <if test="endTime != null">
                and DATE(jd.distribution_time) <![CDATA[<=]]> DATE(#{endTime})
            </if>
            <if test="purchaserName != null and purchaserName != ''">
                and jd.purchaser_name like concat('%', #{purchaserName}, '%')
            </if>
            <if test="animalType != null">
                and    jd.animal_type =  #{animalType}
            </if>
        </where>
        GROUP BY
            DATE(jd.distribution_time),
            jd.produce_name,
            jd.unit,
            jd.purchaser_id,
            jd.purchaser_name,
            jd.entrance_batch_id,
            jp.animal_cert,
            jp.meat_cert
        ORDER BY DATE(jd.distribution_time) DESC
    </select>
    <select id="selectStockReport" resultType="com.ruoyi.web.v1.model.response.JsStockInfo">

         SELECT SUM(in_num)  in_num ,SUM(out_num)  out_num ,`number` ,DATE_FORMAT(create_time,'%Y-%m-%d') create_time   ,material_name ,linghuoren,shenpi FROM `js_stock_info`
        <where>
            <if test="startTime != null">
                create_time &gt;=  #{startTime}
            </if>
            <if test="endTime != null">
               and  create_time &lt;=  #{endTime}
            </if>
        </where>

          GROUP BY `material_name` , DATE_FORMAT(create_time,'%Y-%m-%d') , linghuoren
    </select>
    <select id="getZhaoHuiReport" resultType="com.ruoyi.web.v1.model.response.GetZhaoHuiResp">

         SELECT *  from  js_produce_recall
        <where>
            <if test="startTime != null">
                recall_time &gt;=  #{startTime}
            </if>
            <if test="endTime != null">
                and  recall_time &lt;=  #{endTime}
            </if>
            <if test="endTime != null">
                and  recall_time &lt;=  #{endTime}
            </if>
            <if test="animalType != null">
                and    animal_type =  #{animalType}
            </if>
        </where>
    </select>
    <select id="getZaiQianReport" resultType="com.ruoyi.web.v1.model.response.GetZaiQianResp">
  select distinct   a.animal_type ,a.animal_cert_no , a.amount ,b.divide_time ,c.snap_kill_amount ,c.road_deal_amount + c.illness_dead_amount+ c.illness_disease_amount   siWang,
     a.amount - c.road_deal_amount - c.illness_dead_amount- c.illness_disease_amount  zunZai ,
     d.staff_name ,c.deal_way inspection_detail
      from entrance_batch  a
      left join  `js_divide_circle` b on a.animal_cert_no =b.animal_cert_no
      left join `harmless_treatment`  c on a.id  = c. entrance_batch_id
      left join `entrance_inspection`  d on a.id = d.entrance_batch_id
        <where>
            divide_time is not null
            <if test="startTime != null">
                and b.divide_time &gt;=  #{startTime}
            </if>
            <if test="endTime != null">
                and  b.divide_time &lt;=  #{endTime}
            </if>
            <if test="animalType != null">
                and    a.animal_type =  #{animalType}
            </if>
        </where>
        order by   b.divide_time desc
    </select>
    <select id="getYangPinReport" resultType="com.ruoyi.web.v1.model.response.GetYangPinResp">
        SELECT * FROM `js_env_check`
        <where>
                1 = 1
            <if test="startTime != null">
              and  check_time &gt;=  #{startTime}
            </if>
            <if test="endTime != null">
                and  check_time &lt;=  #{endTime}
            </if>
            order by   check_time desc
        </where>
    </select>
    <select id="getProect" resultType="com.ruoyi.web.v1.model.response.GetEnvCheck">
        select project_name,check_content,check_method  from `js_env_check_project`
    </select>
    <select id="getDaiZaiReport" resultType="com.ruoyi.web.v1.model.response.JingYangPinResp">

 SELECT DISTINCT b.pigpen_name ,a.amount , a.animal_cert_no ,  b.divide_time , e.plan_start_time ,
 round(TIMESTAMPDIFF(SECOND, b.divide_time, e.plan_start_time) / 3600,0) AS hour_diff ,
 c.road_deal_amount + c.illness_dead_amount   siWang, c.illness_disease_amount ,
    a.amount - c.road_deal_amount - c.illness_dead_amount- c.illness_disease_amount  zunZai ,
c.snap_kill_amount ,

     d.staff_name ,
      CASE
        WHEN c.deal_way IS NULL OR TRIM(c.deal_way) = '' THEN '无'
        ELSE c.deal_way
    END AS inspection_detail, ifnull(c.deal_reason ,"无") deal_reason

      FROM entrance_batch  a
      LEFT JOIN  `js_divide_circle` b ON a.animal_cert_no =b.animal_cert_no
      LEFT JOIN `harmless_treatment`  c ON a.id  = c. entrance_batch_id
      LEFT JOIN `entrance_inspection`  d ON a.id = d.entrance_batch_id

            LEFT JOIN `js_produce_plan`  e ON a.animal_cert_no = e.animal_cert_no
        <where>
            b.divide_time is not null
            and a.del_flag  = 0
            <if test="startTime != null">
                and  b.divide_time &gt;=  #{startTime}
            </if>
            <if test="endTime != null">
                and  b.divide_time &lt;=  #{endTime}
            </if>

        </where>
        ORDER BY   b.divide_time DESC

    </select>
    <select id="getRuChangReport" resultType="com.ruoyi.web.v1.model.response.RunChagnResp">
   SELECT
    a.cargo_owner,
    a.cargo_owner_phone,
    a.transport_vehicle,
    b.dict_label,
    a.animal_cert_no,
     a.entrance_time,
     a.animal_type,
    -- 预处理空值后计算
    COALESCE(c.road_deal_amount, 0) +
    COALESCE(c.illness_dead_amount, 0) +
    COALESCE(c.illness_disease_amount, 0) AS buhege,
    a.amount - (
        COALESCE(c.road_deal_amount, 0) +
        COALESCE(c.illness_dead_amount, 0) +
        COALESCE(c.illness_disease_amount, 0)
    ) AS hege,
    d.pigpen_name,
    d.amount ,
    CASE
        WHEN COALESCE(c.road_deal_amount, 0) +
             COALESCE(c.illness_dead_amount, 0) +
             COALESCE(c.illness_disease_amount, 0) = 0 THEN '合格'
        ELSE '不合格'
    END AS jieGuo
FROM `entrance_batch` a
LEFT JOIN `sys_dict_data` b
    ON a.origin_place = b.dict_value
    AND b.dict_type = "app_origin_type"  -- 修正条件位置
LEFT JOIN `harmless_treatment` c
    ON a.id = c.entrance_batch_id
LEFT JOIN `js_divide_circle` d
    ON a.animal_cert_no = d.animal_cert_no
        <where>
           1=1
            <if test="startTime != null">
                and    a.entrance_time &gt;=  #{startTime}
            </if>
            <if test="endTime != null">
                and    a.entrance_time &lt;=  #{endTime}
            </if>
            <if test="animalType != null">
                and    a.animal_type =  #{animalType}
            </if>
        </where>
  ORDER BY   a.id DESC
    </select>
    <select id="getChuChangReport" resultType="com.ruoyi.web.v1.model.response.ChuChagnResp">
    SELECT  produ_num ,  supplier_name ,produce_name, unit,amount ,animal_cert_no ,meat_cert,distribution_time tuZaiDate,
    distribution_time
    FROM `js_distribution`
        <where>
            1=1
            <if test="startTime != null">
                and    distribution_time &gt;=  #{startTime}
            </if>
            <if test="endTime != null">
                and    entrance_time &lt;=  #{endTime}
            </if>
        </where>
        ORDER BY   id DESC
    </select>
    <select id="getZhuanYunReport" resultType="com.ruoyi.web.v1.model.response.GetZhuanYunReportResp">
           SELECT instock_date ruKuTime ,instock_num ruKuNum  ,outstock_num   chuLinum
           ,guanliyuan,jiaojieren,stock_num  kuChunNum  , instock_date chuLiTime FROM `js_store_transport_info`
        <where>
            1=1
            <if test="startTime != null">
                and    instock_date &gt;=  #{startTime}
            </if>
            <if test="endTime != null">
                and    instock_date &lt;=  #{endTime}
            </if>
        </where>
        ORDER BY   id DESC
    </select>
    <select id="getFuJianReport" resultType="com.ruoyi.web.v1.model.response.FuJianResp">
         SELECT produce_name ,keep_time , keep_time check_time ,  examine_items  , recheck_time ,  recheck_item FROM `js_sample`
        <where>
            1=1
            <if test="startTime != null">
                and    keep_time &gt;=  #{startTime}
            </if>
            <if test="endTime != null">
                and    keep_time &lt;=  #{endTime}
            </if>
        </where>
        ORDER BY   id DESC
    </select>
    <select id="getZaiHouReport" resultType="com.ruoyi.web.v1.model.response.ZaiHouResp">
       SELECT  DISTINCT a.animal_cert_no , a.cargo_owner , a.animal_type ,
 a.amount - c.road_deal_amount - c.illness_dead_amount- c.illness_disease_amount  tuZai ,
  a.amount - c.road_deal_amount - c.illness_dead_amount- c.illness_disease_amount -  d.deal_amount   heGe ,
   c.road_deal_amount + c.illness_dead_amount+ c.illness_disease_amount+  d.deal_amount  illnessDiseaseAmount ,
        e.unqualified_num  illnessDiseaseProduAmount,
        f.series_no ,

 CASE
        WHEN c.deal_way IS NULL OR TRIM(c.deal_way) = '' THEN '无'
        ELSE c.deal_way
    END AS inspection_detail,
     IFNULL(c.deal_reason ,"无")  deal_reason

  FROM `entrance_batch` a
  LEFT JOIN `harmless_treatment`  c ON a.id  = c. entrance_batch_id
  LEFT JOIN `js_rest_inspection` d ON  d.series_no  LIKE  CONCAT(a.animal_cert_no , '%')
        LEFT JOIN `js_produce_check` e ON  e.animal_cert_no  =  a.animal_cert_no
        LEFT JOIN `js_divide_circle` f ON  f.animal_cert_no  =  a.animal_cert_no
        <where>
             a.amount - c.road_deal_amount - c.illness_dead_amount- c.illness_disease_amount -  d.deal_amount  IS NOT NULL
            <if test="startTime != null">
                and    a.entrance_time &gt;=  #{startTime}
            </if>
            <if test="endTime != null">
                and     a.entrance_time &lt;=  #{endTime}
            </if>
            <if test="animalType != null">
                and    a.animal_type =  #{animalType}
            </if>
        </where>
        ORDER BY    a.entrance_time DESC
    </select>
    <select id="selectXunChaReport" resultType="com.ruoyi.web.v1.model.response.XunChaResp">
    select * from inspection_record

        <where>
         1 =1
            <if test="startTime != null">
                and    inspect_time &gt;=  #{startTime}
            </if>
            <if test="endTime != null">
                and    inspect_time &lt;=  #{endTime}
            </if>
        </where>
        ORDER BY    inspect_time DESC
    </select>
    <select id="selectXunChaDelReport" resultType="com.ruoyi.web.v1.model.response.XunChaDel">
        select * from js_patrol

        <where>
            1 =1
            <if test="startTime != null">
                and    patrol_time &gt;=  #{startTime}
            </if>
            <if test="endTime != null">
                and    patrol_time &lt;=  #{endTime}
            </if>
        </where>
        ORDER BY    patrol_time DESC
    </select>
    <select id="gysReport" resultType="com.ruoyi.web.v1.model.response.GysResp">
        select * from supplier_eva

        <where>
            1 =1
            <if test="startTime != null">
                and    eva_date &gt;=  #{startTime}
            </if>
            <if test="endTime != null">
                and    eva_date &lt;=  #{endTime}
            </if>
            <if test="endTime != null">
                and    supplier_name  =  #{supplierName}
            </if>
        </where>
        ORDER BY    eva_date DESC

    </select>
    <select id="tuzaiShengChanReport" resultType="com.ruoyi.web.v1.model.response.TuZaiShengChanResp">

        select  DISTINCT b.animal_type ,a.animal_cert_no , c.default_weight , a.series_no, b.amount , b.amount * c.default_weight   zaiQianZhonliang , b.variety ,
         d.after_weight  zaiHouZhongLiang ,d.supplier_name,d.produ_num ,e.plan_start_time
 from  `js_divide_circle`  a
 left join  `entrance_batch` b on a.animal_cert_no =b.animal_cert_no
 left join  `pig_category`  c on b.variety =  c.category_name
  LEFT JOIN  `js_distribution`  d ON d.series_no_group =  a.series_no
   LEFT JOIN `js_produce_plan`  e ON e.animal_cert_no =  a.animal_cert_no

        <where>
             after_weight is not null
            <if test="startTime != null">
                and    e.plan_start_time &gt;=  #{startTime}
            </if>
            <if test="endTime != null">
                and    e.plan_start_time &lt;=  #{endTime}
            </if>
            <if test="animalType != null">
                and    b.animal_type = #{animalType}
            </if>

        </where>

  order by  e.plan_start_time desc

    </select>

</mapper>