<?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.business.mapper.BizProcessInspecionMapper">

    <resultMap type="cn.ezhizao.project.business.domain.BizProcessInspecion" id="BizProcessInspecionResult">
        <id column="id" property="id"/>
        <result column="product_id" property="productId"/>
        <association property="productDescription"
                     javaType="java.lang.String"
                     column="product_id" select="getProcessDescription"/>
        <association property="drawingNumber"  javaType="java.lang.String"
                     column="product_id" select="getDrawingNumber"/>
        <association property="technologyVersion"  javaType="java.lang.String"
                     column="lot_id" select="getTechnologyVersion"/>
        <collection property="images" ofType="cn.ezhizao.project.business.domain.BizProcessInspectionPicture" select="getImages" column="id" />
    </resultMap>

    <resultMap type="cn.ezhizao.project.business.domain.BizProcessInspectionPicture" id="BizProcessInspectionPictureResult">
        <id column="id" property="id"/>
    </resultMap>

    <resultMap id="ExportResult" type="cn.ezhizao.project.websocket.domain.BizProcessInspectionExport">
    </resultMap>


    <select id="getList" parameterType="BizProcessInspecion" resultMap="BizProcessInspecionResult">
        SELECT * FROM biz_process_inspecion
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
            <if test="lotCode != null  and lotCode != ''">AND lot_code = #{lotCode}</if>
            <if test="userId != null  ">AND user_id = #{userId}</if>
            <if test="status != null ">AND status = #{status}</if>
            <if test="keyword != null  and keyword != '' ">AND (lot_code LIKE CONCAT('%',#{keyword},'%') OR carrier_code
                LIKE CONCAT('%',#{keyword},'%') OR
                product_description LIKE CONCAT('%',#{keyword},'%'))
            </if>
        </trim>
    </select>
    <select id="getVerifyList" parameterType="BizProcessInspecion" resultMap="BizProcessInspecionResult">
        SELECT distinct t.* FROM biz_process_inspecion t
        left join biz_return_receipt_detail rrd on rrd.id = t.return_receipt_detail_id
        left join biz_outsourced_order oo on oo.id = t.outsource_order_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t.deleted = 0 and t.recheck_date is null and t.status = 1 and t.is_outsourced_inspection = 1 and rrd.deleted = 0 and oo.deleted = 0
            and not exists (select 1 from biz_outsourced_order oo left join biz_outsourced_order_detail od on od.master_id = oo.id left join biz_process_inspecion pi on pi.outsource_order_detail_id = od.id  where oo.id = t.outsource_order_id and oo.settlement_type = 2 and pi.recheck_date is null and (pi.id is null or pi.status != 1) and pi.deleted = 0 and od.deleted = 0)
            <if test="lotCode != null  and lotCode != ''">AND t.lot_code like concat ('%',#{lotCode},'%')</if>
            <if test="userId != null  ">AND t.user_id = #{userId}</if>
            <if test="status != null ">AND t.status = #{status}</if>
            <if test="startTime != null"> AND DATE(t.start_time) &gt;= #{startTime}</if>
            <if test="endTime != null"> AND DATE(t.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 = t.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 oo.form_code like concat('%', #{outsourcedOrderDetailFormCode}, '%') </if>
            <if test="auditStatus != null ">AND t.audit_status = #{auditStatus}</if>
            <if test="keyword != null  and keyword !='' ">AND (t.lot_code LIKE CONCAT('%',#{keyword},'%') OR t.carrier_code
                LIKE CONCAT('%',#{keyword},'%') OR
                t.product_description LIKE CONCAT('%',#{keyword},'%'))</if>
        </trim>
        order by rrd.form_code,rrd.lot_code
    </select>
    <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>
    <select id="getInfo" parameterType="BizProcessInspecion" resultMap="BizProcessInspecionResult">
        SELECT distinct t1.*,t4.process_alias,t3.dept_name,t5.user_name as technicianCode,t5.nick_name as technicianName ,
        (SELECT user_name FROM sys_user WHERE t1.user_id=user_id) as userName
        FROM biz_process_inspecion t1
        left join biz_daywork_item t2 on t1.daywork_item_id = t2.id
        left join sys_dept t3 on t3.dept_id = t1.dept_id
        left join biz_process t4 on t2.process_id = t4.id
        left join sys_user t5 on t5.user_id = t1.reviewer_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0  and t3.del_flag = 0 and t4.deleted = 0
            <if test="lotCode != null  and lotCode != ''">AND t1.lot_code like concat('%',#{lotCode},'%')</if>
            <if test="status != null ">AND t1.status = #{status}</if>
            <if test="deptId != null and deptId != 0  and type == null">AND t1.dept_id = #{deptId}</if>
            <if test="type != null">AND t1.type = #{type}</if>
<!--            如果不选择检测类别，不显示外协检查和出厂检-->
            <if test="type == null">AND t1.type != 'outsourcedInspector' and t1.type != 'factoryInspection'</if>
            <if test="productDescription != null  and productDescription != ''">AND t1.product_description like concat('%',#{productDescription},'%')</if>
            <if test="processAlias != null  and processAlias != ''">AND t4.process_alias like concat('%',#{processAlias},'%')</if>
            <if test="startTime != null ">AND t1.create_time &gt;= #{startTime}</if>
            <if test="endTime != null ">AND t1.create_time &lt;= #{endTime}</if>
        </trim>
        ORDER BY
        FIELD(t1.status, 0, 2, 1),
        t1.create_time asc
    </select>
    <select id="getProcessInspectionById" resultMap="BizProcessInspecionResult">
        SELECT t1.*,t3.process_alias FROM biz_process_inspecion t1
        left join biz_daywork_item t2 on t1.daywork_item_id = t2.id
        left join biz_process t3 on t2.process_id = t3.id
        where t1.deleted = 0 and t3.deleted = 0 and t1.id = #{id}
    </select>
    <select id="getDrawingNumber" resultType="java.lang.String">
        SELECT drawing_number FROM biz_product
        WHERE id=#{product_id}
    </select>
    <select id="getTechnologyVersion" resultType="java.lang.String">
        select technology_version from biz_lot where id=#{lot_id}
    </select>
    <select id="getProcessDescription" parameterType="java.lang.Long" resultType="java.lang.String" >
        SELECT description FROM biz_product
        WHERE id=#{product_id}

    </select>
    <select id="getImages" resultMap="BizProcessInspectionPictureResult">
        select * from biz_process_inspection_picture
        where deleted = 0 and process_inspection_id = #{id}
    </select>
    <select id="getAllList" resultType="cn.ezhizao.project.business.domain.BizProcessInspecion">
        select * from biz_process_inspecion where type = 'outsourcedInspector' order by outsourced_inspection_no desc
    </select>

    <delete id="physicalDelete">
        DELETE FROM biz_process_inspecion
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            <if test="id != null">
                id = #{id} AND
            </if>
       <!-- 删除条件为其他外键可以在这里加 -->
        </trim>
    </delete>
</mapper>
