<?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.PorkSideProduceRecordMapper">
    
    <resultMap type="com.ruoyi.web.v1.model.PorkSideProduceRecord" id="PorkSideProduceRecordResult">
        <result property="id"    column="id"    />
        <result property="productName"    column="product_name"    />
        <result property="produceTime"    column="produce_time"    />
        <result property="purchaserId"    column="purchaser_id"    />
        <result property="hookName"    column="hook_name"    />
        <result property="hookWeight"    column="hook_weight"    />
        <result property="scaleWeight"    column="scale_weight"    />
        <result property="dryLossWeight"    column="dry_loss_weight"    />
        <result property="finalWeight"    column="final_weight"    />
        <result property="remark"    column="remark"    />
        <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 type="com.ruoyi.web.v1.DTO.PorkSideProduceRecordDTO" id="PorkSideProduceRecordDTOResult">
        <result property="id"    column="id"    />
        <result property="productName"    column="product_name"    />
        <result property="produceTime"    column="produce_time"    />
        <result property="purchaserId"    column="purchaser_id"    />
        <result property="hookName"    column="hook_name"    />
        <result property="hookWeight"    column="hook_weight"    />
        <result property="scaleWeight"    column="scale_weight"    />
        <result property="dryLossWeight"    column="dry_loss_weight"    />
        <result property="finalWeight"    column="final_weight"    />
        <result property="remark"    column="remark"    />
        <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"    />
        <result property="purchaserName"    column="purchaser_name"    />
    </resultMap>

    <resultMap type="com.ruoyi.web.v1.DTO.TimeAndWeightDTO" id="TimeAndWeightDTOResult">
        <result property="timeSpot"    column="time_spot"    />
        <result property="totalWeight"    column="total_weight"    />
    </resultMap>

    <resultMap type="com.ruoyi.web.v1.DTO.PurchaserProduceTotalDTO" id="PurchaserProduceTotalDTOResult">
        <result property="purchaserId"    column="purchaser_id"    />
        <result property="purchaserName"    column="purchaser_name"    />
        <result property="totalAmount"    column="total_amount"    />
        <result property="dateTag"    column="date_tag"    />
        <result property="totalSideWeight"    column="total_side_weight"    />
        <result property="totalHeadWeight"    column="total_head_weight"    />
        <result property="totalRedWeight"    column="total_red_weight"    />
        <result property="totalWhiteWeight"    column="total_white_weight"    />
        <result property="salePlace"    column="sale_place"    />
    </resultMap>
    
    <sql id="selectPorkSideProduceRecordVo">
        select id, product_name, produce_time, purchaser_id, hook_name, hook_weight, scale_weight, dry_loss_weight, final_weight, remark, create_time,create_by, update_time, update_by, del_flag from pork_side_produce_record
    </sql>

    <select id="selectPorkSideProduceRecordList" parameterType="com.ruoyi.web.v1.model.request.ReqGetPorkSideProduceRecord" resultMap="PorkSideProduceRecordDTOResult">
        select a.*,b.purchaser_name
        from pork_side_produce_record as a
        left join purchaser as b on b.id = a.purchaser_id and b.del_flag = '0'
        <where>
            a.del_flag = '0'
            <if test="keyword != null  and keyword != ''"> and (b.purchaser_name like concat('%', #{keyword}, '%'))</if>
            <if test="produceTime != null "> and DATE(a.produce_time) = DATE(#{produceTime})</if>
        </where>
        order by a.produce_time desc
    </select>
    
    <select id="selectPorkSideProduceRecordById" parameterType="Long" resultMap="PorkSideProduceRecordDTOResult">
        select a.*,b.purchaser_name
            from pork_side_produce_record as a
            left join purchaser as b on b.id = a.purchaser_id and b.del_flag = '0'
        where a.id = #{id} and a.del_flag = '0'
    </select>

    <insert id="insertPorkSideProduceRecord" parameterType="com.ruoyi.web.v1.model.PorkSideProduceRecord" useGeneratedKeys="true" keyProperty="id">
        insert into pork_side_produce_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="productName != null and productName != ''">product_name,</if>
            <if test="produceTime != null">produce_time,</if>
            <if test="purchaserId != null">purchaser_id,</if>
            <if test="hookName != null">hook_name,</if>
            <if test="hookWeight != null">hook_weight,</if>
            <if test="scaleWeight != null">scale_weight,</if>
            <if test="dryLossWeight != null">dry_loss_weight,</if>
            <if test="finalWeight != null">final_weight,</if>
            <if test="remark != null">remark,</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="productName != null and productName != ''">#{productName},</if>
            <if test="produceTime != null">#{produceTime},</if>
            <if test="purchaserId != null">#{purchaserId},</if>
            <if test="hookName != null">#{hookName},</if>
            <if test="hookWeight != null">#{hookWeight},</if>
            <if test="scaleWeight != null">#{scaleWeight},</if>
            <if test="dryLossWeight != null">#{dryLossWeight},</if>
            <if test="finalWeight != null">#{finalWeight},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            '0'
         </trim>
    </insert>

    <update id="updatePorkSideProduceRecord" parameterType="com.ruoyi.web.v1.model.PorkSideProduceRecord">
        update pork_side_produce_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="productName != null and productName != ''">product_name = #{productName},</if>
            <if test="produceTime != null">produce_time = #{produceTime},</if>
            <if test="purchaserId != null">purchaser_id = #{purchaserId},</if>
            <if test="hookName != null">hook_name = #{hookName},</if>
            <if test="hookWeight != null">hook_weight = #{hookWeight},</if>
            <if test="scaleWeight != null">scale_weight = #{scaleWeight},</if>
            <if test="dryLossWeight != null">dry_loss_weight = #{dryLossWeight},</if>
            <if test="finalWeight != null">final_weight = #{finalWeight},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

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

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

    <!--查询每个肉商每天的称重统计-->
    <select id="selectPurchaserProduceTotal" parameterType="com.ruoyi.web.v1.model.request.ReqGetPorkSideProduceRecord" resultMap="PurchaserProduceTotalDTOResult">
        SELECT
        combined.purchaser_id,
        b.purchaser_name,
        combined.date_tag,
        COALESCE(db.total_amount, 0) as total_amount,
        SUM(combined.side_weight) AS total_side_weight,
        SUM(combined.head_weight) AS total_head_weight,
        SUM(combined.red_weight) AS total_red_weight,
        SUM(combined.white_weight) AS total_white_weight,
        b.sale_place
        FROM (
        -- 白条统计（处理跨天日期）
        SELECT
        purchaser_id,
        CASE
        WHEN HOUR(produce_time) <![CDATA[ < ]]> 5 THEN DATE(DATE_SUB(produce_time, INTERVAL 1 DAY))
        ELSE DATE(produce_time)
        END AS date_tag,
        SUM(CASE WHEN HOUR(produce_time) <![CDATA[ >= ]]> 23 OR HOUR(produce_time) <![CDATA[ < ]]> 5 THEN final_weight ELSE 0 END) AS side_weight,
        0 AS head_weight,
        0 AS red_weight,
        0 AS white_weight
        FROM pork_side_produce_record
        <where>
            del_flag = '0'
            <if test="produceTime != null "> and DATE(produce_time) = DATE(#{produceTime})</if>
        </where>
        GROUP BY purchaser_id,
        CASE
        WHEN HOUR(produce_time) <![CDATA[ < ]]> 5 THEN DATE(DATE_SUB(produce_time, INTERVAL 1 DAY))
        ELSE DATE(produce_time)
        END

        UNION ALL

        -- 猪头统计（处理跨天日期）
        SELECT
        b.purchaser_id,
        CASE
        WHEN HOUR(a.produce_time) <![CDATA[ < ]]> 5 THEN DATE(DATE_SUB(a.produce_time, INTERVAL 1 DAY))
        ELSE DATE(a.produce_time)
        END AS date_tag,
        0 AS side_weight,
        SUM(CASE WHEN HOUR(a.produce_time) <![CDATA[ >= ]]> 23 OR HOUR(a.produce_time) <![CDATA[ < ]]> 5 THEN a.final_weight ELSE 0 END) AS head_weight,
        0 AS red_weight,
        0 AS white_weight
        FROM pork_other_produce a
        INNER JOIN distribute_batch b ON a.distribute_batch_id = b.id AND b.del_flag = '0'
        <where>
            a.product_name = '猪头' AND a.del_flag = '0'
            <if test="produceTime != null "> and DATE(a.produce_time) = DATE(#{produceTime})</if>
        </where>
        GROUP BY b.purchaser_id,
        CASE
        WHEN HOUR(a.produce_time) <![CDATA[ < ]]> 5 THEN DATE(DATE_SUB(a.produce_time, INTERVAL 1 DAY))
        ELSE DATE(a.produce_time)
        END

        UNION ALL

        -- 红脏统计（处理跨天日期）
        SELECT
        b.purchaser_id,
        CASE
        WHEN HOUR(a.produce_time) <![CDATA[ < ]]> 5 THEN DATE(DATE_SUB(a.produce_time, INTERVAL 1 DAY))
        ELSE DATE(a.produce_time)
        END AS date_tag,
        0 AS side_weight,
        0 AS head_weight,
        SUM(CASE WHEN HOUR(a.produce_time) <![CDATA[ >= ]]> 23 OR HOUR(a.produce_time) <![CDATA[ < ]]> 5 THEN a.final_weight ELSE 0 END) AS red_weight,
        0 AS white_weight
        FROM pork_other_produce a
        INNER JOIN distribute_batch b ON a.distribute_batch_id = b.id AND b.del_flag = '0'
        <where>
            a.product_name = '红脏' AND a.del_flag = '0'
            <if test="produceTime != null "> and DATE(a.produce_time) = DATE(#{produceTime})</if>
        </where>
        GROUP BY b.purchaser_id,
        CASE
        WHEN HOUR(a.produce_time) <![CDATA[ < ]]> 5 THEN DATE(DATE_SUB(a.produce_time, INTERVAL 1 DAY))
        ELSE DATE(a.produce_time)
        END

        UNION ALL

        -- 白脏统计（处理跨天日期）
        SELECT
        b.purchaser_id,
        CASE
        WHEN HOUR(a.produce_time) <![CDATA[ < ]]> 5 THEN DATE(DATE_SUB(a.produce_time, INTERVAL 1 DAY))
        ELSE DATE(a.produce_time)
        END AS date_tag,
        0 AS side_weight,
        0 AS head_weight,
        0 AS red_weight,
        SUM(CASE WHEN HOUR(a.produce_time) <![CDATA[ >= ]]> 23 OR HOUR(a.produce_time) <![CDATA[ < ]]> 5 THEN a.final_weight ELSE 0 END) AS white_weight
        FROM pork_other_produce a
        INNER JOIN distribute_batch b ON a.distribute_batch_id = b.id AND b.del_flag = '0'
        <where>
            a.product_name = '白脏' AND a.del_flag = '0'
            <if test="produceTime != null "> and DATE(a.produce_time) = DATE(#{produceTime})</if>
        </where>
        GROUP BY b.purchaser_id,
        CASE
        WHEN HOUR(a.produce_time) <![CDATA[ < ]]> 5 THEN DATE(DATE_SUB(a.produce_time, INTERVAL 1 DAY))
        ELSE DATE(a.produce_time)
        END
        ) combined
        INNER JOIN purchaser b ON b.id = combined.purchaser_id AND b.del_flag = '0'
        LEFT JOIN (
        -- 预计算每日数量（处理跨天日期）
        SELECT
        purchaser_id,
        CASE
        WHEN HOUR(create_time) <![CDATA[ < ]]> 5 THEN DATE(DATE_SUB(create_time, INTERVAL 1 DAY))
        ELSE DATE(create_time)
        END AS date_tag,
        SUM(amount) AS total_amount
        FROM distribute_batch
        WHERE del_flag = '0'
        GROUP BY purchaser_id,
        CASE
        WHEN HOUR(create_time) <![CDATA[ < ]]> 5 THEN DATE(DATE_SUB(create_time, INTERVAL 1 DAY))
        ELSE DATE(create_time)
        END
        ) db ON db.purchaser_id = combined.purchaser_id AND db.date_tag = combined.date_tag
        <where>
            1 = 1
            <if test="keyword != null  and keyword != ''"> and (b.purchaser_name like concat('%', #{keyword}, '%'))</if>
            <if test="produceTime != null "> and combined.date_tag = DATE(#{produceTime})</if>
        </where>
        GROUP BY combined.purchaser_id, b.purchaser_name, combined.date_tag, b.sale_place, db.total_amount
        ORDER BY combined.date_tag DESC
    </select>
</mapper>