<?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.v2.mapper.JsDistributionMapper">
    <resultMap id="BaseDistributionMap" type="com.ruoyi.web.v2.model.JsProduction">
        <result column="create_time" property="createTime" />
        <result column="animal_cert_no" property="animalCertNo" />
        <result column="entrance_batch_id" property="entranceBatchId" />
        <result column="supplier_id" property="supplierId" />
        <result column="purchaser_id" property="purchaserId" />
        <result column="supplier_name" property="supplierName" />
        <result column="purchaser_name" property="purchaserName" />
        <result column="variety" property="variety" />
        <result column="sale_place" property="salePlace" />
        <result column="animal_cert" property="animalCert" />
        <result column="meat_cert" property="meatCert" />
        <result column="animal_type" property="animalType" />
    </resultMap>
    <!--获取最近添加的记录,不加锁-->
    <select id="selectLastRecord" resultType="com.ruoyi.web.v2.model.JsDistribution">
        select *
        from js_distribution
        order by id desc
        limit 1
    </select>

    <select id="getAllList" resultType="com.ruoyi.web.v2.model.vo.ProductionVo">
select b.productName produceName,a.afterWeight weight,a.amount amount,b.unit unit FROM
(select produce_name produceName,convert(SUM(after_weight),decimal(10,2)) afterWeight,SUM(amount) amount FROM js_distribution where purchaser_name=#{purchaserName}
        <if test="startTime != null">
            AND distribution_time between #{startTime} and #{endTime}
        </if>
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>

    <select id="getAllList1" resultType="com.ruoyi.web.v2.model.vo.ProductionVo">
        select b.productName produceName,a.afterWeight weight,a.amount amount,b.unit unit FROM
        (select produce_name produceName,convert(SUM(after_weight),decimal(10,2)) afterWeight,SUM(amount) amount FROM js_distribution where purchaser_name=#{purchaserName}
        and supplier_name=#{supplierName}
        <if test="startTime != null">
            AND distribution_time between #{startTime} and #{endTime}
        </if>
        GROUP BY produce_name)
        as a right 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>

    <select id="getAllList2" parameterType="com.ruoyi.web.v2.model.JsProduction" resultType="com.ruoyi.web.v2.model.vo.ProductionVo">
        select
            b.productName produceName,
            a.afterWeight weight,
            a.amount amount,
            a.animal_type animalType ,
            b.unit unit
        FROM
        (
            select produce_name produceName,convert(SUM(after_weight),decimal(10,2)) afterWeight,SUM(amount) amount ,animal_type
            FROM js_distribution
            <where>
                <if test="createTime != null">
                    and DATE(distribution_time) = DATE(#{createTime})
                </if>
                <if test="supplierId != null">
                    and supplier_id = #{supplierId}
                </if>
                <if test="entranceBatchId != null">
                    and entrance_batch_id = #{entranceBatchId}
                </if>
                <if test="purchaserId != null">
                    and purchaser_id = #{purchaserId}
                </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>

    <select id="getDistributionByDay"  parameterType="com.ruoyi.web.v2.model.GetDistributionListReq" resultMap="BaseDistributionMap">
        SELECT
            DATE(jd.distribution_time) as create_time,
            jd.entrance_batch_id,
            jd.purchaser_id,
            jd.supplier_id,
            jd.supplier_name,
            jd.animal_cert_no,
            jd.purchaser_name,
            jp.animal_cert,
            jp.meat_cert,
            p.sale_place,
            eb.variety,
          eb.animal_type animalType
        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
        LEFT JOIN entrance_batch as eb on eb.id=jd.entrance_batch_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="animalCertNo != null and animalCertNo != ''">
                and jd.animal_cert_no like concat('%', #{animalCertNo}, '%')
            </if>
            <if test="animalType != null and animalType != ''">
                and eb.animal_type like concat('%', #{animalType}, '%')
            </if>
        </where>
        GROUP BY DATE(jd.distribution_time),jd.entrance_batch_id,jd.purchaser_id,jd.supplier_id,jd.supplier_name,jd.animal_cert_no,jd.purchaser_name,jp.animal_cert,jp.meat_cert,p.sale_place,eb.variety
        ORDER BY DATE(jd.distribution_time) DESC

    </select>
</mapper>
