<?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.DistributeBatchMapper">
    
    <resultMap type="com.ruoyi.web.v1.model.DistributeBatch" id="DistributeBatchResult">
        <result property="id"    column="id"    />
        <result property="entranceBatchId"    column="entrance_batch_id"    />
        <result property="slaughterCode"    column="slaughter_code"    />
        <result property="supplierId"    column="supplier_id"    />
        <result property="purchaserId"    column="purchaser_id"    />
        <result property="amount"    column="amount"    />
        <result property="weight"    column="weight"    />
        <result property="pigpenName"    column="pigpen_name"    />
        <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"    />
        <association property="purchaser"  javaType="com.ruoyi.web.v1.model.Purchaser"  resultMap="purchaserResult" />
        <association property="supplier"   javaType="com.ruoyi.web.v1.model.Supplier"  resultMap="supplierResult" />
    </resultMap>
    <resultMap id="supplierResult" type="com.ruoyi.web.v1.model.Supplier">
        <result property="id"    column="id"    />
        <result property="supplierName"    column="supplier_name"    />
        <result property="phone"    column="phone"    />
        <result property="supplySpecies"    column="supply_species"    />
        <result property="businessPlace"    column="business_place"    />
        <result property="originPlace"    column="origin_place"    />
        <result property="region"    column="region"    />
        <result property="evaluate"    column="evaluate"    />
        <result property="remark"    column="remark"    />
        <result property="inBlacklist"    column="in_blacklist"    />
    </resultMap>
    <resultMap id="purchaserResult" type="com.ruoyi.web.v1.model.Purchaser" >
        <result property="id"    column="id"    />
        <result property="purchaserName"    column="purchaser_name"    />
        <result property="idNumber"    column="id_number"    />
        <result property="phone"    column="phone"    />
        <result property="businessPlace"    column="business_place"    />
        <result property="salePlace"    column="sale_place"    />
        <result property="region"    column="region"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <resultMap id="ValidDistributeListResult" type="com.ruoyi.web.v1.DTO.ValidDistributeListDTO">
        <result property="id"    column="id"    />
        <result property="entranceBatchId"    column="entrance_batch_id"    />
        <result property="purchaserId"    column="purchaser_id"    />
        <result property="supplierId"    column="supplier_id"    />
        <result property="purchaserName"    column="purchaser_name"    />
        <result property="supplierName"    column="supplier_name"    />
        <result property="slaughterCode"    column="slaughter_code"    />
        <result property="animalCertNo"    column="animal_cert_no"    />
    </resultMap>

    <resultMap id="ValidSlaughterCodeResult" type="com.ruoyi.web.v1.DTO.ValidSlaughterCodeDTO">
        <result property="purchaserNo"    column="purchaser_no"    />
        <result property="supplierNo"    column="supplier_no"    />
        <result property="purchaserName"    column="purchaser_name"    />
        <result property="supplierName"    column="supplier_name"    />
        <result property="slaughterCode"    column="slaughter_code"    />
    </resultMap>

    <resultMap type="com.ruoyi.web.v1.DTO.TimeAndNumberDTO" id="TimeAndNumberDTOResult">
        <result property="timeSpot"    column="time_spot"    />
        <result property="totalAmount"    column="total_amount"    />
    </resultMap>

    <sql id="selectDistributeBatchVo">
        select id, entrance_batch_id, slaughter_code, supplier_id, purchaser_id, amount, weight, pigpen_name, create_time, create_by, update_time, update_by, del_flag from distribute_batch
    </sql>

    <select id="selectDistributeBatchList" parameterType="com.ruoyi.web.v1.model.DistributeBatch" resultMap="DistributeBatchResult">
        select a.*,b.purchaser_name,c.supplier_name from distribute_batch as a
        left join purchaser as b on b.id = a.purchaser_id
        left join supplier as c on c.id = a.supplier_id
        <where>
            a.del_flag = '0'
            <if test="entranceBatchId != null  and entranceBatchId != 0">
            and a.entrance_batch_id = #{entranceBatchId}
            </if>
            <if test="slaughterCode != null  and slaughterCode != ''"> and a.slaughter_code like concat('%', #{slaughterCode}, '%')</if>
            <if test="supplierId != null "> and a.supplier_id = #{supplierId}</if>
            <if test="purchaserId != null "> and a.purchaser_id = #{purchaserId}</if>
        </where>
        order by a.create_time desc
    </select>
    
    <select id="selectDistributeBatchById" parameterType="Long" resultMap="DistributeBatchResult">
        <include refid="selectDistributeBatchVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <!--判断当前批次下下肉商是否已有分销-->
    <select id="checkPurchaserUnique" parameterType="com.ruoyi.web.v1.model.DistributeBatch"  resultMap="DistributeBatchResult">
        select a.id, a.purchaser_id,b.purchaser_name from distribute_batch as a
        left join purchaser as b on b.id = a.purchaser_id
        where a.purchaser_id = #{purchaserId} and a.entrance_batch_id = #{entranceBatchId} and a.del_flag = '0'
        limit 1
    </select>

    <!--获取指定入场批次下的全部分销总量   如果是修改则排除当前批次-->
    <select id="getTotalByEntranceBatch" parameterType="com.ruoyi.web.v1.model.DistributeBatch"  resultType="Long">
        select IFNULL(sum(amount),0) from distribute_batch
        <where>
            entrance_batch_id = #{entranceBatchId} and del_flag = '0'
            <if test="id != null "> and id != #{id}</if>
        </where>
        group by entrance_batch_id
    </select>

    <!--获取血码关联的未完成（24小时内）屠宰的分销批次-->
    <select id="selectValidDistribute" parameterType="String"  resultMap="ValidDistributeListResult">
        select a.id,a.entrance_batch_id,a.purchaser_id,c.purchaser_name,a.supplier_id,d.supplier_name,a.slaughter_code,e.animal_cert_no
        from distribute_batch as a
        left join purchaser as c on c.id = a.purchaser_id and c.del_flag = '0'
        left join supplier as d on d.id = a.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.slaughter_code = #{slaughterCode}
          and a.create_time >= DATE_SUB(NOW(), INTERVAL 24 HOUR)
          and a.del_flag = '0'
    </select>

    <insert id="insertDistributeBatch" parameterType="com.ruoyi.web.v1.model.DistributeBatch" useGeneratedKeys="true" keyProperty="id">
        insert into distribute_batch
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="entranceBatchId != null">entrance_batch_id,</if>
            <if test="slaughterCode != null">slaughter_code,</if>
            <if test="supplierId != null">supplier_id,</if>
            <if test="purchaserId != null">purchaser_id,</if>
            <if test="amount != null">amount,</if>
            <if test="weight != null">weight,</if>
            <if test="pigpenName != null">pigpen_name,</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="entranceBatchId != null">#{entranceBatchId},</if>
            <if test="slaughterCode != null">#{slaughterCode},</if>
            <if test="supplierId != null">#{supplierId},</if>
            <if test="purchaserId != null">#{purchaserId},</if>
            <if test="amount != null">#{amount},</if>
            <if test="weight != null">#{weight},</if>
            <if test="pigpenName != null">#{pigpenName},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            '0'
         </trim>
    </insert>

    <insert id="insertDistributeBatchByBatch" parameterType="list" >
        INSERT INTO distribute_batch (entrance_batch_id, slaughter_code, supplier_id, purchaser_id, amount, weight, pigpen_name, create_time, create_by,del_flag)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.entranceBatchId}, #{item.slaughterCode}, #{item.supplierId}, #{item.purchaserId}, #{item.amount}, #{item.weight}, #{item.pigpenName}, #{item.createTime}, #{item.createBy},'0')
        </foreach>
    </insert>

    <update id="updateDistributeBatch" parameterType="com.ruoyi.web.v1.model.DistributeBatch">
        update distribute_batch
        <trim prefix="SET" suffixOverrides=",">
            <if test="entranceBatchId != null">entrance_batch_id = #{entranceBatchId},</if>
            <if test="slaughterCode != null">slaughter_code = #{slaughterCode},</if>
            <if test="supplierId != null">supplier_id = #{supplierId},</if>
            <if test="purchaserId != null">purchaser_id = #{purchaserId},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="weight != null">weight = #{weight},</if>
            <if test="pigpenName != null">pigpen_name = #{pigpenName},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

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

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

    <!--根据时间范围获取分销次数-->
    <select id="selectCountByTime" parameterType="com.ruoyi.web.v1.model.request.TimeRangeParam" resultType="Long">
        select count(1) from distribute_batch
        <where>
            del_flag = '0'
            <if test="startTime != null "> and create_time <![CDATA[ >= ]]> #{startTime}</if>
            <if test="endTime != null "> and create_time <![CDATA[ <= ]]> #{endTime}</if>
        </where>
    </select>

    <!--根据时间范围获取累计屠宰-->
    <select id="selectAmountByTime" parameterType="com.ruoyi.web.v1.model.request.TimeRangeParam" resultType="Long">
        select IFNULL(sum(amount),0) from distribute_batch
        <where>
            del_flag = '0'
            <if test="startTime != null "> and create_time <![CDATA[ >= ]]> #{startTime}</if>
            <if test="endTime != null "> and create_time <![CDATA[ <= ]]> #{endTime}</if>
        </where>
    </select>

    <!--根据供应商ID统计分销批次数量-->
    <select id="countBatchBySupplier" parameterType="Long" resultType="Integer">
        select count(1) from distribute_batch
        where supplier_id = #{supplierId} and del_flag = '0'
    </select>

    <!--根据肉商ID统计分销批次数量-->
    <select id="countBatchByPurchaser" parameterType="Long" resultType="Integer">
        select count(1) from distribute_batch
        where purchaser_id = #{purchaserId} and del_flag = '0'
    </select>

    <!--根据血码统计分销批次数量-->
    <select id="countBatchBySlaughterCode" parameterType="String" resultType="Integer">
        select count(1) from distribute_batch
        where slaughter_code = #{slaughterCode} and del_flag = '0'
    </select>

    <!--根据入场批次统计分销批次数量-->
    <select id="countBatchByEntrance" parameterType="Long" resultType="Integer">
        select count(1) from distribute_batch
        where entrance_batch_id = #{entranceBatchId} and del_flag = '0'
    </select>

    <!--根据时间范围获取今日入场次数-->
    <select id="selectTodayCount" resultType="Long">
        select count(1) from distribute_batch
        <where>
            del_flag = '0'
            and create_time <![CDATA[ >= ]]> CURDATE()
            and create_time <![CDATA[ < ]]> DATE_ADD(CURDATE(), INTERVAL 1 DAY)
        </where>
    </select>

    <!--根据时间范围获取昨日入场次数-->
    <select id="selectYesterdayCount"  resultType="Long">
        select count(1) from distribute_batch
        <where>
            del_flag = '0'
            and create_time <![CDATA[ >= ]]> DATE_SUB(CURDATE(), INTERVAL 1 DAY)
            and create_time <![CDATA[ < ]]> CURDATE()
        </where>
    </select>

    <!--根据获取近七天的每日入场订单-->
    <select id="selectWeekCountByTime" resultMap="TimeAndNumberDTOResult">
        SELECT
            dates.date AS time_spot,
            IFNULL(count(eb.id), 0) AS total_amount
        FROM (
                 SELECT CURDATE() AS date UNION ALL
                 SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY) UNION ALL
                 SELECT DATE_SUB(CURDATE(), INTERVAL 2 DAY) UNION ALL
                 SELECT DATE_SUB(CURDATE(), INTERVAL 3 DAY) UNION ALL
                 SELECT DATE_SUB(CURDATE(), INTERVAL 4 DAY) UNION ALL
                 SELECT DATE_SUB(CURDATE(), INTERVAL 5 DAY) UNION ALL
                 SELECT DATE_SUB(CURDATE(), INTERVAL 6 DAY)
             ) AS dates
                 LEFT JOIN distribute_batch eb ON
            DATE(eb.create_time) = dates.date and eb.del_flag = '0'
        GROUP BY
            dates.date
        ORDER BY
            dates.date;
    </select>

    <!--获取存在未完成（24小时内）的分销批次的血码-->
    <select id="selectValidCode" parameterType="Integer"  resultMap="ValidSlaughterCodeResult">
        select a.slaughter_code,c.purchaser_no,c.purchaser_name,d.supplier_no,d.supplier_name
        from distribute_batch as a
        left join purchaser as c on c.id = a.purchaser_id and c.del_flag = '0'
        left join supplier as d on d.id = a.supplier_id and d.del_flag = '0'
        where
            a.del_flag = '0'
            and a.create_time >= DATE_SUB(NOW(), INTERVAL 24 HOUR)
        group by a.slaughter_code,c.purchaser_no,c.purchaser_name,d.supplier_no,d.supplier_name
    </select>
</mapper>