<?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.EntranceBatchMapper">
    
    <resultMap type="com.ruoyi.web.v1.model.EntranceBatch" id="EntranceBatchResult">
        <result property="id"    column="id"    />
        <result property="supplierId"    column="supplier_id"    />
        <result property="originPlace"    column="origin_place"    />
        <result property="entranceTime"    column="entrance_time"    />
        <result property="variety"    column="variety"    />
        <result property="transportVehicle"    column="transport_vehicle"    />
        <result property="weight"    column="weight"    />
        <result property="pigpens"    column="pigpens"    />
        <result property="animalCertNo"    column="animal_cert_no"    />
        <result property="animalCertPics"    column="animal_cert_pics"    />
        <result property="cargoOwner"    column="cargo_owner"    />
        <result property="companyNo"    column="company_no"    />
        <result property="cargoOwnerPhone"    column="cargo_owner_phone"    />
        <result property="animalCategory"    column="animal_category"    />
        <result property="amount"    column="amount"    />
        <result property="units"    column="units"    />
        <result property="useto"    column="useto"    />
        <result property="fromPlace"    column="from_place"    />
        <result property="toPlace"    column="to_place"    />
        <result property="carrier"    column="carrier"    />
        <result property="carrierPhone"    column="carrier_phone"    />
        <result property="animalEartags"    column="animal_eartags"    />
        <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="animalType"    column="animal_type"    />
        <result property="yunzaifangshi"    column="yunzaifangshi"    />
        <result property="yunzaipaihao"    column="yunzaipaihao"    />
        <result property="xiaoduqingkuang"    column="xiaoduqingkuang"    />
        <association property="harmlessTreatment"  javaType="com.ruoyi.web.v1.model.HarmlessTreatment"  resultMap="HarmlessTreatmentResult" />
        <association property="supplier"  javaType="com.ruoyi.web.v1.model.Supplier"  resultMap="SupplierResult" />
    </resultMap>
    <resultMap type="com.ruoyi.web.v1.model.Supplier" id="SupplierResult">
        <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"    />
        <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="carNum"    column="car_num"    />
    </resultMap>
    <resultMap type="com.ruoyi.web.v1.model.HarmlessTreatment" id="HarmlessTreatmentResult">
        <result property="id"    column="id"    />
        <result property="entranceBatchId"    column="entrance_batch_id"    />
        <result property="illnessDeadAmount"    column="illness_dead_amount"    />
        <result property="illnessDiseaseAmount"    column="illness_disease_amount"    />
        <result property="roadDealAmount"    column="road_deal_amount"    />
        <result property="snapKillAmount"    column="snap_kill_amount"    />
        <result property="unqualifiedWeight"    column="unqualified_weight"    />
        <result property="illnessPigsDetail"    column="illness_pigs_detail"    />
        <result property="dealReason"    column="deal_reason"    />
        <result property="dealWay"    column="deal_way"    />
        <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.TimeAndNumberDTO" id="TimeAndNumberDTOResult">
            <result property="timeSpot"    column="time_spot"    />
            <result property="totalAmount"    column="total_amount"    />
    </resultMap>

    <resultMap id="productTraceResultMap" type="com.ruoyi.web.v1.DTO.ProductTraceDTO">
        <result property="entranceBatchId" column="entrance_batch_id"/>
        <result property="animalCertNo" column="animal_cert_no"/>
        <result property="entranceTime" column="entrance_time"/>
        <result property="cargoOwner" column="cargo_owner"/>
        <result property="companyNo" column="company_no"/>
        <result property="animalCategory" column="animal_category"/>
        <result property="variety" column="variety"/>
        <result property="amount" column="amount"/>
        <result property="units" column="units"/>
        <result property="fromPlace" column="from_place"/>
        <result property="toPlace" column="to_place"/>
        <result property="carrier" column="carrier"/>
        <result property="cargoOwnerPhone" column="cargo_owner_phone"/>
        <result property="carrierPhone" column="carrier_phone"/>
        <result property="spirit" column="spirit"/>
        <result property="appearance" column="appearance"/>
        <result property="breathe" column="breathe"/>
        <result property="metabolite" column="metabolite"/>
        <result property="otherRemark" column="other_remark"/>
        <result property="inspectionDetail" column="inspection_detail"/>
        <result property="illnessDeadAmount" column="illness_dead_amount"/>
        <result property="illnessDiseaseAmount" column="illness_disease_amount"/>
        <result property="snapKillAmount" column="snap_kill_amount"/>
        <result property="roadDealAmount" column="road_deal_amount"/>
        <result property="unqualifiedWeight" column="unqualified_weight"/>
        <result property="dealWay" column="deal_way"/>
        <result property="oneCode" column="one_code"/>
        <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"/>

        <!-- 处理 slaughterBatchDTO 集合 -->
        <collection property="slaughterBatchs" ofType="com.ruoyi.web.v1.DTO.SlaughterBatchDTO">
            <!-- 主表 -->
            <id property="id" column="distribute_batch_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="slaughter_amount"/>
            <result property="beforeWeight" column="before_weight"/>

            <!--肉商 -->
            <result property="purchaserName" column="purchaser_name"/>
            <result property="purchaserId" column="purchaser_id"/>
            <result property="salePlace" column="sale_place"/>
            <result property="meatCert" column="meat_cert"/>
            <result property="animalCert" column="animal_cert"/>

            <!-- 供应商 -->
            <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"/>
        </collection>
    </resultMap>

    <resultMap id="entranceReportResultMap" type="com.ruoyi.web.v1.DTO.EntranceReportDTO">
        <result property="entranceTime" column="entrance_time"/>
        <result property="animalCertNo" column="animal_cert_no"/>
        <result property="animalEartags" column="animal_eartags"/>
        <result property="amount" column="amount"/>
        <result property="weight" column="weight"/>
        <result property="pigpens" column="pigpens"/>
        <result property="supplierName" column="supplier_name"/>
        <result property="originPlace" column="origin_place"/>
        <result property="illnessDeadAmount" column="illness_dead_amount"/>
        <result property="illnessDiseaseAmount" column="illness_disease_amount"/>
        <result property="snapKillAmount" column="snap_kill_amount"/>
        <result property="unqualifiedWeight" column="unqualified_weight"/>
        <result property="illnessPigsDetail" column="illness_pigs_detail"/>
        <result property="dealWay" column="deal_way"/>
        <result property="inspectionDetail" column="inspection_detail"/>
    </resultMap>

    <resultMap type="com.ruoyi.web.v1.DTO.DailyEntranceAmountDTO" id="DailyEntranceAmountResult">
        <result property="illnessDeadAmountSum"    column="illness_dead_amount_sum"    />
        <result property="illnessDiseaseAmountSum"    column="illness_disease_amount_sum"    />
        <result property="entranceAmountSum"    column="entrance_amount_sum"    />
    </resultMap>

    <sql id="selectEntranceBatchVo">
        select id, supplier_id,origin_place,entrance_time, variety, transport_vehicle, weight, pigpens, animal_cert_no,animal_cert_pics, cargo_owner, company_no, cargo_owner_phone, animal_category, amount, units, useto, from_place, to_place, carrier, carrier_phone, animal_eartags, remark, create_time,create_by, update_time, update_by, del_flag from entrance_batch
    </sql>
    <select id="selectEntranceBatchList" parameterType="com.ruoyi.web.v1.model.request.ReqEntranceBatchList" resultMap="EntranceBatchResult">
        select a.id, a.entrance_time,a.animal_cert_no, a.cargo_owner,a.amount,a.supplier_id,a.variety,a.animal_type,
               b.supplier_name,
               c.illness_dead_amount,c.illness_disease_amount,c.snap_kill_amount,c.unqualified_weight,c.road_deal_amount
        from entrance_batch as a
        left join supplier as b on b.id = a.supplier_id and b.del_flag = '0'
        left join harmless_treatment as c on c.entrance_batch_id = a.id and c.del_flag = '0'
        <where>
            a.del_flag = '0'
            <if test="supplierId != null "> and a.supplier_id = #{supplierId}</if>
            <if test="animalType != null "> and a.animal_type = #{animalType}</if>
            <if test="startDate != null "> and DATE(a.entrance_time) <![CDATA[ >= ]]> DATE(#{startDate})</if>
            <if test="endDate != null "> and DATE(a.entrance_time) <![CDATA[ <= ]]> DATE(#{endDate})</if>
            <if test="animalCertNo != null  and animalCertNo != ''"> and a.animal_cert_no like concat('%', #{animalCertNo}, '%')</if>
        </where>
        order by a.entrance_time desc,a.id desc
    </select>

    <select id="selectEntranceBatchList1" parameterType="com.ruoyi.web.v1.model.EntranceBatch" resultMap="EntranceBatchResult">
        select a.id, a.entrance_time,a.animal_cert_no, a.cargo_owner,a.amount,a.supplier_id,a.variety,
        b.supplier_name,
        c.illness_dead_amount,c.illness_disease_amount,c.snap_kill_amount,c.unqualified_weight,c.road_deal_amount
        from entrance_batch as a
        left join supplier as b on b.id = a.supplier_id and b.del_flag = '0'
        left join harmless_treatment as c on c.entrance_batch_id = a.id and c.del_flag = '0'
        <where>
            a.del_flag = '0'
            <if test="supplierId != null "> and a.supplier_id = #{supplierId}</if>
            <if test="createTime != null "> and a.create_time >= #{createTime}</if>
            <if test="animalCertNo != null  and animalCertNo != ''"> and a.animal_cert_no like concat('%', #{animalCertNo}, '%')</if>
        </where>
        order by a.entrance_time desc,a.id desc
    </select>

    <select id="selectEntranceBatchDetailById" parameterType="Long" resultMap="EntranceBatchResult">
        select a.*,b.supplier_name,b.phone,b.origin_place,b.region,b.business_place,b.supply_species
        from entrance_batch as a
        left join supplier as b on b.id = a.supplier_id and b.del_flag = '0'
        where a.id = #{id} and a.del_flag = '0'
    </select>

    <select id="selectEntranceBatchById" parameterType="Long" resultMap="EntranceBatchResult">
        select a.id, a.entrance_time,a.animal_cert_no, a.cargo_owner,a.amount,a.supplier_id,a.variety,a.animal_eartags,
               c.illness_dead_amount,c.illness_disease_amount,c.snap_kill_amount,c.unqualified_weight
        from entrance_batch as a
        left join harmless_treatment as c on c.entrance_batch_id = a.id and c.del_flag = '0'
        where a.id = #{id} and a.del_flag = '0'
    </select>

    <!--检查检疫证号是否存在-->
    <select id="checkAnimalCertNoUnique" parameterType="String" resultMap="EntranceBatchResult">
        select id, animal_cert_no from entrance_batch where animal_cert_no = #{animalCertNo} and del_flag = '0' limit 1
    </select>

    <insert id="insertEntranceBatch" parameterType="com.ruoyi.web.v1.model.EntranceBatch" useGeneratedKeys="true" keyProperty="id">
        insert into entrance_batch
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="supplierId != null">supplier_id,</if>
            <if test="originPlace != null">origin_place,</if>
            <if test="entranceTime != null">entrance_time,</if>
            <if test="variety != null">variety,</if>
            <if test="transportVehicle != null">transport_vehicle,</if>
            <if test="weight != null">weight,</if>
            <if test="pigpens != null">pigpens,</if>
            <if test="animalCertNo != null">animal_cert_no,</if>
            <if test="animalCertPics != null">animal_cert_pics,</if>
            <if test="cargoOwner != null">cargo_owner,</if>
            <if test="companyNo != null">company_no,</if>
            <if test="cargoOwnerPhone != null">cargo_owner_phone,</if>
            <if test="animalCategory != null">animal_category,</if>
            <if test="amount != null">amount,</if>
            <if test="units != null">units,</if>
            <if test="useto != null">useto,</if>
            <if test="fromPlace != null">from_place,</if>
            <if test="toPlace != null">to_place,</if>
            <if test="carrier != null">carrier,</if>
            <if test="carrierPhone != null">carrier_phone,</if>
            <if test="animalEartags != null">animal_eartags,</if>
            <if test="remark != null">remark,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="yunzaifangshi != null">yunzaifangshi,</if>
            <if test="yunzaipaihao != null">yunzaipaihao,</if>
            <if test="xiaoduqingkuang != null">xiaoduqingkuang,</if>
            <if test="animalType != null">animal_type,</if>
            del_flag
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="supplierId != null">#{supplierId},</if>
            <if test="originPlace != null">#{originPlace},</if>
            <if test="entranceTime != null">#{entranceTime},</if>
            <if test="variety != null">#{variety},</if>
            <if test="transportVehicle != null">#{transportVehicle},</if>
            <if test="weight != null">#{weight},</if>
            <if test="pigpens != null">#{pigpens},</if>
            <if test="animalCertNo != null">#{animalCertNo},</if>
            <if test="animalCertPics != null">#{animalCertPics},</if>
            <if test="cargoOwner != null">#{cargoOwner},</if>
            <if test="companyNo != null">#{companyNo},</if>
            <if test="cargoOwnerPhone != null">#{cargoOwnerPhone},</if>
            <if test="animalCategory != null">#{animalCategory},</if>
            <if test="amount != null">#{amount},</if>
            <if test="units != null">#{units},</if>
            <if test="useto != null">#{useto},</if>
            <if test="fromPlace != null">#{fromPlace},</if>
            <if test="toPlace != null">#{toPlace},</if>
            <if test="carrier != null">#{carrier},</if>
            <if test="carrierPhone != null">#{carrierPhone},</if>
            <if test="animalEartags != null">#{animalEartags},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="yunzaifangshi != null">#{yunzaifangshi},</if>
            <if test="yunzaipaihao != null">#{yunzaipaihao},</if>
            <if test="xiaoduqingkuang != null">#{xiaoduqingkuang},</if>
            <if test="animalType != null">#{animalType},</if>
            '0'
         </trim>
    </insert>

    <update id="updateEntranceBatch" parameterType="com.ruoyi.web.v1.model.EntranceBatch">
        update entrance_batch
        <trim prefix="SET" suffixOverrides=",">
            <if test="supplierId != null">supplier_id = #{supplierId},</if>
            <if test="originPlace != null">origin_place = #{originPlace},</if>
            <if test="entranceTime != null">entrance_time = #{entranceTime},</if>
            <if test="variety != null">variety = #{variety},</if>
            <if test="transportVehicle != null">transport_vehicle = #{transportVehicle},</if>
            <if test="weight != null">weight = #{weight},</if>
            <if test="pigpens != null">pigpens = #{pigpens},</if>
            <if test="animalCertNo != null">animal_cert_no = #{animalCertNo},</if>
            <if test="animalCertPics != null">animal_cert_pics = #{animalCertPics},</if>
            <if test="cargoOwner != null">cargo_owner = #{cargoOwner},</if>
            <if test="companyNo != null">company_no = #{companyNo},</if>
            <if test="cargoOwnerPhone != null">cargo_owner_phone = #{cargoOwnerPhone},</if>
            <if test="animalCategory != null">animal_category = #{animalCategory},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="units != null">units = #{units},</if>
            <if test="useto != null">useto = #{useto},</if>
            <if test="fromPlace != null">from_place = #{fromPlace},</if>
            <if test="toPlace != null">to_place = #{toPlace},</if>
            <if test="carrier != null">carrier = #{carrier},</if>
            <if test="carrierPhone != null">carrier_phone = #{carrierPhone},</if>
            <if test="animalEartags != null">animal_eartags = #{animalEartags},</if>
            <if test="animalType != null">animal_type = #{animalType},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="yunzaifangshi != null">yunzaifangshi = #{yunzaifangshi},</if>
            <if test="yunzaipaihao != null">yunzaipaihao = #{yunzaipaihao},</if>
            <if test="xiaoduqingkuang != null">xiaoduqingkuang = #{xiaoduqingkuang},</if>
        </trim>
        where id = #{id}
    </update>

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

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

    <!--获取最近的一条记录-->
    <select id="selectLatelyEntrance" resultMap="EntranceBatchResult">
        select * from entrance_batch
        <where>
            del_flag = '0'
        </where>
        order by entrance_time desc
        limit 1
    </select>

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

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

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

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

    <!--根据时间范围获取今日累计计划屠宰-->
    <select id="selectTodayAmount"  resultMap="DailyEntranceAmountResult">
        select IFNULL(sum(b.amount),0) as entrance_amount_sum,IFNULL(sum(illness_dead_amount),0) as illness_dead_amount_sum,IFNULL(sum(illness_disease_amount),0) as illness_disease_amount_sum
        from harmless_treatment as a
        left join entrance_batch as b on b.id = a.entrance_batch_id
        <where>
            a.del_flag = '0' and b.del_flag = '0'
            and b.entrance_time <![CDATA[ >= ]]> CURDATE()
            and b.entrance_time <![CDATA[ < ]]> DATE_ADD(CURDATE(), INTERVAL 1 DAY)
        </where>
    </select>

    <!--根据时间范围获取昨日累计计划屠宰-->
    <select id="selectYesterdayAmount"  resultMap="DailyEntranceAmountResult">
        select IFNULL(sum(b.amount),0) as entrance_amount_sum,IFNULL(sum(illness_dead_amount),0) as illness_dead_amount_sum,IFNULL(sum(illness_disease_amount),0) as illness_disease_amount_sum
        from harmless_treatment as a
        left join entrance_batch as b on b.id = a.entrance_batch_id
        <where>
            a.del_flag = '0' and b.del_flag = '0'
            and b.entrance_time <![CDATA[ >= ]]> DATE_SUB(CURDATE(), INTERVAL 1 DAY)
            and b.entrance_time <![CDATA[ < ]]> CURDATE()
        </where>
    </select>

    <!--统计按年全部数据-->
    <select id="selectAllYearAmountByTime" parameterType="com.ruoyi.web.v1.model.request.TimeRangeParam" resultMap="TimeAndNumberDTOResult">
        SELECT
            YEAR(entrance_time) AS time_spot,
            IFNULL(SUM(amount),0) AS total_amount
        FROM
            entrance_batch
        where del_flag = '0'
        GROUP BY
            YEAR(entrance_time)
        ORDER BY
            time_spot;
    </select>

    <!--根据获取近一年每月累计计划屠宰-->
    <select id="selectYearAmountByTime" parameterType="com.ruoyi.web.v1.model.request.TimeRangeParam" resultMap="TimeAndNumberDTOResult">
        SELECT
            months.month_period as time_spot,
            IFNULL(SUM(eb.amount), 0) AS total_amount
        FROM (
                 SELECT
                     DATE_FORMAT(DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01'), INTERVAL seq MONTH), '%Y-%m') AS month_period,
                     DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01'), INTERVAL seq MONTH) AS month_start,
                     LAST_DAY(DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01'), INTERVAL seq MONTH)) AS month_end
                 FROM (
                          SELECT 0 AS seq UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL
                          SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL
                          SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL
                          SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11
                      ) AS sequences
             ) AS months
                 LEFT JOIN entrance_batch eb ON
            eb.entrance_time BETWEEN months.month_start AND months.month_end and eb.del_flag = '0'
        GROUP BY
            months.month_period, months.month_start, months.month_end
        ORDER BY
            months.month_start;
    </select>

    <!--根据获取近三十天的每日累计计划屠宰-->
    <select id="selectMonthAmountByTime" parameterType="com.ruoyi.web.v1.model.request.TimeRangeParam" resultMap="TimeAndNumberDTOResult">
        SELECT
            dates.date AS time_spot,
            IFNULL(SUM(eb.amount), 0) AS total_amount
        FROM (
                 SELECT DATE_SUB(CURDATE(), INTERVAL seq DAY) AS date
                 FROM (
                     SELECT 0 AS seq UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL
                     SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL
                     SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL
                     SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL
                     SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL
                     SELECT 15 UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL
                     SELECT 18 UNION ALL SELECT 19 UNION ALL SELECT 20 UNION ALL
                     SELECT 21 UNION ALL SELECT 22 UNION ALL SELECT 23 UNION ALL
                     SELECT 24 UNION ALL SELECT 25 UNION ALL SELECT 26 UNION ALL
                     SELECT 27 UNION ALL SELECT 28 UNION ALL SELECT 29
                     ) AS sequences
             ) AS dates
                 LEFT JOIN entrance_batch eb ON
            DATE(eb.entrance_time) = dates.date and eb.del_flag = '0'
        GROUP BY
            dates.date
        ORDER BY
            dates.date;
    </select>

    <!--根据获取近七天的每日累计计划屠宰-->
    <select id="selectWeekAmountByTime" parameterType="com.ruoyi.web.v1.model.request.TimeRangeParam" resultMap="TimeAndNumberDTOResult">
        SELECT
            dates.date AS time_spot,
            IFNULL(SUM(eb.amount), 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 entrance_batch eb ON
            DATE(eb.entrance_time) = dates.date and eb.del_flag = '0'
        GROUP BY
            dates.date
        ORDER BY
            dates.date;
    </select>

    <!--根据获取每小时累计计划屠宰-->
    <select id="selectTodayAmountByTime" parameterType="com.ruoyi.web.v1.model.request.TimeRangeParam" resultMap="TimeAndNumberDTOResult">
        SELECT
            hours.hour AS time_spot,
            IFNULL(SUM(eb.amount), 0) AS total_amount
        FROM (
            SELECT '00:00' AS hour UNION SELECT '01:00' UNION SELECT '02:00' UNION SELECT '03:00'
            UNION SELECT '04:00' UNION SELECT '05:00' UNION SELECT '06:00' UNION SELECT '07:00'
            UNION SELECT '08:00' UNION SELECT '09:00' UNION SELECT '10:00' UNION SELECT '11:00'
            UNION SELECT '12:00' UNION SELECT '13:00' UNION SELECT '14:00' UNION SELECT '15:00'
            UNION SELECT '16:00' UNION SELECT '17:00' UNION SELECT '18:00' UNION SELECT '19:00'
            UNION SELECT '20:00' UNION SELECT '21:00' UNION SELECT '22:00' UNION SELECT '23:00'
            ) AS hours
            LEFT JOIN entrance_batch eb ON
            DATE_FORMAT(eb.entrance_time, '%H:00') = hours.hour
            AND DATE(eb.entrance_time) = CURDATE() and eb.del_flag = '0'
        GROUP BY
            hours.hour
        ORDER BY
            hours.hour;
    </select>

    <!--根据获取指定时间范围内每日累计计划屠宰-->
    <select id="selectRangeAmountByTime" parameterType="com.ruoyi.web.v1.model.request.TimeRangeParam" resultMap="TimeAndNumberDTOResult">
        SELECT
            DATE_FORMAT(dates.date, '%Y-%m-%d') AS time_spot,
            IFNULL(SUM(eb.amount), 0) AS total_amount
        FROM (
                 SELECT DATE_ADD(#{startTime}, INTERVAL seq DAY) AS date
                 FROM (
                     SELECT a.n + b.n*10 + c.n*100 AS seq
                     FROM
                     (SELECT 0 AS n UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
                     UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) a,
                     (SELECT 0 AS n UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
                     UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) b,
                     (SELECT 0 AS n UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
                     UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) c
                     WHERE a.n + b.n*10 + c.n*100 <![CDATA[ <= ]]> DATEDIFF(#{endTime}, #{startTime})
                     ) AS sequences
             ) AS dates
                 LEFT JOIN entrance_batch eb ON
            DATE(eb.entrance_time) = dates.date
            AND eb.del_flag = '0'
        GROUP BY
            dates.date
        ORDER BY
            dates.date
    </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 entrance_batch eb ON
            DATE(eb.entrance_time) = dates.date and eb.del_flag = '0'
        GROUP BY
            dates.date
        ORDER BY
            dates.date;
    </select>

    <!--产品追溯-根据肉商名称，供应商名称，血码，检疫证号查询-->
    <select id="selectProductTrace" parameterType="com.ruoyi.web.v1.model.request.ProductTraceParam" resultMap="productTraceResultMap">
        select
            a.id as entrance_batch_id,
            a.animal_cert_no,a.entrance_time,a.cargo_owner,a.company_no,a.animal_category,a.variety,a.amount,a.units,a.from_place,a.to_place,a.carrier,a.cargo_owner_phone,a.carrier_phone,
            f.spirit,f.appearance,f.breathe,f.metabolite,f.other_remark,f.inspection_detail,
            IFNULL(g.illness_dead_amount,0) as illness_dead_amount,IFNULL(g.illness_disease_amount,0) as illness_disease_amount,IFNULL(g.road_deal_amount,0) as road_deal_amount,
            IFNULL(g.snap_kill_amount,0) as snap_kill_amount,IFNULL(g.unqualified_weight,0) as unqualified_weight,g.deal_way,
            distribute.purchaser_id,distribute.purchaser_name,distribute.sale_place,distribute.animal_cert,distribute.meat_cert

        from entrance_batch as a
        left join (
            select
                jd.entrance_batch_id,
                jd.purchaser_id,
                jd.purchaser_name,
                p.sale_place,
                jp.animal_cert,
                jp.meat_cert
            from js_distribution as jd
            left join purchaser as p on p.id=jd.purchaser_id
            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
            group by jd.entrance_batch_id,jd.purchaser_id,jd.purchaser_name,p.sale_place,jp.animal_cert,jp.meat_cert
        ) as distribute on distribute.entrance_batch_id = a.id
        <if test="animalCertNo != null "> and a.animal_cert_no = #{animalCertNo}</if>
        left join entrance_inspection as f on f.entrance_batch_id = a.id and f.del_flag = '0'
        left join harmless_treatment as g on g.entrance_batch_id = a.id and g.del_flag = '0'
        <where>
            a.del_flag = '0'
            <if test="animalCertNo != null "> and a.animal_cert_no = #{animalCertNo}</if>
            <if test="purchaserName != null "> and distribute.purchaser_name = #{purchaserName}</if>
        </where>
        order by a.entrance_time desc
    </select>

    <!--入场统计报表-->
    <select id="selectEntranceReport" parameterType="com.ruoyi.web.v1.model.request.ReqEntranceReport" resultMap="entranceReportResultMap">
        select
            a.entrance_time,a.origin_place,a.animal_cert_no,a.animal_eartags,a.amount,a.weight,
            b.supplier_name,
            c.illness_dead_amount,c.illness_disease_amount,c.snap_kill_amount,c.unqualified_weight,c.deal_way,c.illness_pigs_detail,
            d.inspection_detail
        from entrance_batch as a
        left join supplier as b on b.id = a.supplier_id and b.del_flag = '0'
        left join harmless_treatment as c on c.entrance_batch_id = a.id and c.del_flag = '0'
        left join entrance_inspection as d on d.entrance_batch_id = a.id and d.del_flag = '0'
        <where>
            a.del_flag = '0'
            <if test="animalCertNo != null "> and a.animal_cert_no like concat('%', #{animalCertNo}, '%')</if>
            <if test="supplierName != null "> and b.supplier_name like concat('%', #{supplierName}, '%')</if>
            <if test="entranceTime != null "> and DATE(a.entrance_time) = DATE(#{entranceTime})</if>
        </where>
        order by a.entrance_time desc
    </select>

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

    <select id="selectByAnimalNo" parameterType="string" resultType="com.ruoyi.web.v1.model.EntranceBatch">
        select * from entrance_batch where animal_cert_no=#{anNo}
    </select>


    <select id="getAll"  resultType="com.ruoyi.web.v1.model.EntranceBatch">
        select a.id id,a.origin_place originPlace,a.entrance_time entranceTime,a.variety variety,a.weight weight,a.animal_cert_no animalCertNo
         ,a.cargo_owner cargoOwner,a.amount amount,a.supplier_id supplierId from entrance_batch as a
        left join supplier as b on b.id = a.supplier_id and b.del_flag = '0'
        where a.del_flag = '0'
        <if test="startTime !=null">
            and a.entrance_time between #{startTime} and #{endTime}
        </if>
        <if test="animalCertNo !=null">
            and a.animal_cert_no  like concat('%', #{animalCertNo}, '%')
        </if>
        <if test="cargoOwner !=null">
          and b.supplier_name like concat('%', #{cargoOwner}, '%')
        </if>
        order by a.entrance_time desc
    </select>

    <select id="selectByTime" resultType="com.ruoyi.web.v1.DTO.JsReportDto">
        SELECT * FROM `js_report`
        order BY id DESC
        LIMIT 1
    </select>

    <!--获取所有可用批次，过滤掉所有状态是closed的批次（保留状态是opened的批次以及还未分圈的批次）-->
    <select id="getAllValidEntrance"  resultMap="EntranceBatchResult">
        SELECT
            eb.*,s.supplier_name ,eb.transport_vehicle car_num
        FROM entrance_batch eb
        LEFT JOIN js_divide_circle dc ON eb.id = dc.entrance_batch_id AND dc.status_flag = 'closed'
        LEFT JOIN supplier as s ON eb.supplier_id = s.id AND s.del_flag='0'
        WHERE dc.id IS NULL and eb.del_flag='0'
        ORDER BY eb.id DESC;
    </select>
    <select id="getOne" resultType="java.lang.Integer">
        SELECT SUM(amount) amount
FROM entrance_batch
WHERE del_flag = 0 AND  create_time  &gt; #{dateTime}
    </select>
    <select id="listAll" resultType="com.ruoyi.web.v1.model.EntranceBatch">
         SELECT *
FROM entrance_batch
    </select>

</mapper>