<?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.app.mapper.SlaughterBatchMapper">
    
    <resultMap type="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.app.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.app.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.app.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>
    <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.app.domain.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="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="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.app.domain.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.app.domain.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.app.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 purchaser_name=#{purchaserName}
        GROUP BY produce_name)
        as a right JOIN
        (select product_name productName,unit unit FROM product_category WHERE del_flag='0') AS b
        on a.produceName=b.productName

    </select>
</mapper>