<?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="cn.ezhizao.project.websocket.mapper.BizProcessInspecionMapper">
    <resultMap id="ExportResult" type="cn.ezhizao.project.websocket.domain.BizProcessInspectionExport">
    </resultMap>

    <select id="getExportList" parameterType="BizProcessInspecion" resultMap="ExportResult">
        select bs.code as mnemonic_code, bs.name as supplier_name, bod.product_num, brr.audit_num, bod.lot_code
        , (case when bod.product_num &lt; brr.audit_num then 0 else bod.product_num - brr.audit_num end) as material_loss
        , brr.process_alias, brr.new_carrier_count as carrier_num
        , (case when bpi.audit_status = 1 then '已审核' else '未审核' end) as audit_status, bpi.start_time, bpi.end_time, bpi.reject_num, bpi.lot_code, bpi.product_description, bod.form_code as outsourcedOrder_detail_form_code, bod.form_date as outsource_date, brr.form_code as return_receipt_detail_form_code, brr.form_date as return_receipt_date, (case when bpi.audit_status = 1 then bpi.update_time else null end) as audit_date, (select boa.update_time from biz_outsource_balance_account boa where boa.status = 1 and boa.process_inspection_id = bpi.id order by update_time desc limit 1) as balance_date from biz_process_inspecion bpi
        left join biz_outsourced_order_detail bod on bod.id = bpi.outsource_order_detail_id left join biz_return_receipt_detail brr on brr.id = bpi.return_receipt_detail_id
        left join biz_supplier bs on bs.id = bod.supplier_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            recheck_date is null and is_outsourced_inspection = 1 and bpi.deleted = 0
            <if test="lotCode != null  and lotCode != ''">AND bpi.lot_code like concat ('%',#{lotCode},'%')</if>
            <if test="userId != null  ">AND bpi.user_id = #{userId}</if>
            <if test="status != null ">AND bpi.status = #{status}</if>
            <if test="startTime != null"> AND DATE(bpi.start_time) &gt;= #{startTime}</if>
            <if test="endTime != null"> AND DATE(bpi.start_time) &lt;= #{endTime}</if>
            <!--            <if test="startTime != null"> AND t.date_format(t.start_time, '%Y-%m-%d') = date_format(#{startTime}, '%Y-%m-%d')</if>-->
            <if test="supplierName != null">AND exists (select supplier_name from biz_outsourced_order_detail o where o.id = bpi.outsource_order_detail_id and o.supplier_name like concat('%', #{supplierName}, '%'))</if>
            <if test="returnReceiptDetailFormCode != null ">AND rrd.form_code like concat('%', #{returnReceiptDetailFormCode}, '%') </if>
            <if test="outsourcedOrderDetailFormCode != null ">AND bod.form_code like concat('%', #{outsourcedOrderDetailFormCode}, '%') </if>
            <if test="auditStatus != null ">AND bpi.audit_status = #{auditStatus}</if>
            <if test="keyword != null  and keyword !='' ">AND (bpi.lot_code LIKE CONCAT('%',#{keyword},'%') OR bpi.carrier_code
                LIKE CONCAT('%',#{keyword},'%') OR
                bpi.product_description LIKE CONCAT('%',#{keyword},'%'))</if>
        </trim>
        <if test="start != null and pageSize != null">
            limit #{start}, #{pageSize}
        </if>
    </select>
    <select id="getCountForExport" resultType="java.lang.Long" parameterType="BizProcessInspecion">
        select count(1) from biz_process_inspecion bpi
        left join biz_outsourced_order_detail bod on bod.id = bpi.outsource_order_detail_id left join biz_return_receipt_detail brr on brr.id = bpi.return_receipt_detail_id
        left join biz_supplier bs on bs.id = bod.supplier_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            recheck_date is null and is_outsourced_inspection = 1 and bpi.deleted = 0
            <if test="lotCode != null  and lotCode != ''">AND bpi.lot_code like concat ('%',#{lotCode},'%')</if>
            <if test="userId != null  ">AND bpi.user_id = #{userId}</if>
            <if test="status != null ">AND bpi.status = #{status}</if>
            <if test="startTime != null"> AND DATE(bpi.start_time) &gt;= #{startTime}</if>
            <if test="endTime != null"> AND DATE(bpi.start_time) &lt;= #{endTime}</if>
            <!--            <if test="startTime != null"> AND t.date_format(t.start_time, '%Y-%m-%d') = date_format(#{startTime}, '%Y-%m-%d')</if>-->
            <if test="supplierName != null">AND exists (select supplier_name from biz_outsourced_order_detail o where o.id = bpi.outsource_order_detail_id and o.supplier_name like concat('%', #{supplierName}, '%'))</if>
            <if test="returnReceiptDetailFormCode != null ">AND rrd.form_code like concat('%', #{returnReceiptDetailFormCode}, '%') </if>
            <if test="outsourcedOrderDetailFormCode != null ">AND bod.form_code like concat('%', #{outsourcedOrderDetailFormCode}, '%') </if>
            <if test="auditStatus != null ">AND bpi.audit_status = #{auditStatus}</if>
            <if test="keyword != null  and keyword !='' ">AND (bpi.lot_code LIKE CONCAT('%',#{keyword},'%') OR bpi.carrier_code
                LIKE CONCAT('%',#{keyword},'%') OR
                bpi.product_description LIKE CONCAT('%',#{keyword},'%'))</if>
        </trim>
    </select>
</mapper>
