<?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.BizLotMapper">

    <resultMap type="cn.ezhizao.project.business.domain.BizLot" id="BizLotResult">
        <id column="id" property="id"/>
        <result column="from_id" property="fromId"/>
        <result column="product_id" property="productId"/>
        <result column="technological_process_id" property="technologicalProcessId"/>
        <association property="fromCode" column="from_id" select="getFromCode"/>
        <association property="product" column="product_id" select="getProduct"/>
    </resultMap>
    <resultMap type="cn.ezhizao.project.business.domain.BizDaywork" id="BizDayworkResult">
        <id column="id" property="id"/>
        <result column="lot_id" property="lotId"/>
        <result column="production_plan_detail_id" property="productionPlanDetailId"/>
        <result column="production_plan_detail_sub_detail_id" property="productionPlanDetailSubDetailId"/>
        <result column="technological_process_id" property="technologicalProcessId"/>
        <result column="lot_code" property="lotCode"/>
        <result column="is_waste" property="isWaste"/>
        <result column="description" property="productDescription"/>
        <result column="furnace_number" property="furnaceNumber"/>
        <result column="production_quantity" property="productionQuantity"/>
        <result column="carrier_name" property="carrierName"/>
        <result column="dept_id" property="deptId"/>
        <association property="lotWaste" column="lot_id" select="getLotWaste"/>
        <association property="processAlias" column="lot_id" select="getProcessAlias"/>
        <association property="productDescription" column="product_id" javaType="java.lang.String"
                     select="getProductDescription"/>
        <association property="productShaftCategoryName" column="technological_process_id"
                     select="getProductShaftCategoryName"/>
        <association property="product" column="product_id" select="getProduct"/>
    </resultMap>
    <resultMap id="BizTechnologyProcessResult" type="cn.ezhizao.project.business.domain.BizTechnologicalProcessDetail">
        <id column="id" property="id"/>
    </resultMap>
    <select id="getLotInfo" resultMap="BizLotResult">
        select t1.lot_code,t1.technology_version,t2.product_description,
        t1.technological_process_id,t3.product_shaft_category_name from biz_lot t1 left join biz_production_plan_detail
        t2 on t1.production_plan_detail_id = t2.id
        left join biz_technological_process t3 on t1.technological_process_id = t3.id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 and t2.deleted = 0
            <if test="productDescription != null  and productDescription != ''">AND t2.product_description like
                concat('%', #{productDescription}, '%')
            </if>
            <if test="lotCode != null  and lotCode != ''">AND t1.lot_code like concat('%', #{lotCode},'%')</if>
        </trim>
        order by t1.create_time desc
    </select>

    <select id="getProductDescription" resultType="java.lang.String">
        select description
        from biz_product
        where deleted = 0
        and id = #{productId}
    </select>
    <select id="getAllLot" resultType="cn.ezhizao.project.business.domain.BizLot">
        select t1.*,
        EXISTS (
        SELECT 1
        FROM biz_daywork t2
        WHERE t2.lot_id = t1.id AND t2.deleted = 0
        ) AS hasDaywork from biz_lot t1 where t1.deleted = 0 and t1.production_plan_detail_id = #{id}
        order by t1.lot_code
    </select>
    <select id="getProduct" resultType="cn.ezhizao.project.business.domain.BizProduct">
        select * from biz_product t1 where t1.id = #{product_id}
    </select>
    <select id="getNotStartList" resultType="cn.ezhizao.project.business.domain.BizLot">
        SELECT * FROM biz_lot where
        deleted = 0
        AND id NOT IN(select lot_id from biz_daywork t1 ) and production_plan_detail_id = #{id}
    </select>
    <select id="getProductShaftCategoryName" resultType="String">
        select product_shaft_category_name from biz_technological_process t1 where t1.id = #{technological_process_id}
    </select>

    <select id="specialGetLotList" parameterType="BizLot" resultMap="BizLotResult">
        SELECT * FROM biz_lot
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
            AND id NOT IN(select lot_id from biz_daywork where biz_daywork.deleted = 0)
            <if test="id != null and id != ''"> AND id = #{id}</if>
            <if test="lotCode != null  and lotCode != ''"> AND lot_code = #{lotCode}</if>
            <if test="productionPlanDetailId != null and productionPlanDetailId!=''"> AND production_plan_detail_id = #{productionPlanDetailId}</if>
            <if test="productCode != null  and productCode != ''"> AND product_code = #{productCode}</if>
            <if test="specification != null  and specification != ''"> AND specification = #{specification}</if>
            <if test="drawingNumber != null  and drawingNumber != ''"> AND drawing_number = #{drawingNumber}</if>
            <if test="flags != null and flags.size() > 0">
                AND
                <foreach collection="flags" item="flag" separator=" OR " open="(" close=")">
                    <if test="flag != null and flag == 2"> t4.is_amend= 1</if>
                    <if test="flag != null and flag == 1"> t4.is_waste_recycling= 1</if>
                    <if test="flag != null and flag == 4"> t4.is_superaddition= 1</if>
                    <!--                    <if test="flag != null and flag == 0"> t4.is_waste= 1</if>-->
                    <if test="flag != null and flag == 3"> t4.is_waste_recycling= 0 and t4.is_amend = 0 and t4.is_superaddition = 0 </if>
                    <!--                    t1.is_waste_recycling = #{flag} OR t1.is_waste = #{flag} OR t1.is_amend = #{flag}-->
                </foreach>
            </if>
        </trim>
        order by lot_code asc
    </select>

    <select id="getList" parameterType="BizLot" resultMap="BizLotResult">
        SELECT t1.*, t4.total_lot_number,t4.company_alias as companyAlias, t3.furnace_number  FROM biz_lot t1
        LEFT JOIN biz_production_plan_detail t4 ON t1.production_plan_detail_id = t4.id
        left join biz_daywork t2 on t2.deleted = 0 and t2.lot_id = t1.id
        left join biz_furnace_no_info t3 on t3.daywork_id = t2.id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0
            <if test="id != null">AND t1.id = #{id}</if>
            <if test="lotCode != null  and lotCode != ''">AND t1.lot_code = #{lotCode}</if>
            <if test="isWaste != null ">AND t1.is_waste = #{isWaste}</if>
            <if test="isFallback != null ">AND t1.is_fallback = #{isFallback}</if>
            <if test="isWasteRecycling != null ">AND t1.is_waste_recycling = #{isWasteRecycling}</if>
            <if test="isLast != null ">AND t1.is_last = #{isLast}</if>
            <if test="technologyVersion != null  and technologyVersion != ''">AND t1.technology_version =
                #{technologyVersion}
            </if>
            <if test="flags != null and flags.size() > 0">
                AND
                <foreach collection="flags" item="flag" separator=" OR " open="(" close=")">
                    <if test="flag != null and flag == 2"> t1.is_amend= 1</if>
                    <if test="flag != null and flag == 1"> t1.is_waste_recycling= 1</if>
                    <if test="flag != null and flag == 0"> t1.is_waste= 1</if>
                    <if test="flag != null and flag == 4"> t1.is_superaddition= 1</if>
<!--                    <if test="flag != null and flag == 5"> t1.from_id != 0</if>-->
                    <if test="flag != null and flag == 3"> t1.is_waste= 0 and  t1.is_waste_recycling= 0 and t1.is_amend = 0 and t1.is_superaddition = 0 </if>
                    <!--                    t1.is_waste_recycling = #{flag} OR t1.is_waste = #{flag} OR t1.is_amend = #{flag}-->
                </foreach>
            </if>
            <if test="productCode != null  and productCode != ''">AND t1.product_code = #{productCode}</if>
            <if test="specification != null  and specification != ''">AND t1.specification = #{specification}</if>
            <if test="drawingNumber != null  and drawingNumber != ''">AND t1.drawing_number = #{drawingNumber}</if>
            <if test="productDescription != null  and productDescription != ''">AND t4.product_description like concat('%',#{productDescription},'%')
            </if>
            <if test="furnaceNumber != null  and furnaceNumber != ''">AND t3.furnace_number = #{furnaceNumber}
            </if>
            <if test="status == 0">
                AND not exists (select 1 from biz_daywork where t1.id = biz_daywork.lot_id and biz_daywork.deleted = 0)
            </if>
            <if test="status == 1">
                AND exists (select 1 from biz_daywork where t1.id = biz_daywork.lot_id and biz_daywork.status &lt; 2 and biz_daywork.deleted = 0)
            </if>
            <if test="status == 2">
                AND exists (select 1 from biz_daywork where t1.id = biz_daywork.lot_id and biz_daywork.status = 2 and biz_daywork.deleted = 0)
            </if>
            <if test="reportStatus != null">
                AND (select status from biz_daywork_item where lot_id = t1.id and technological_process_detail_id = (select technological_process_detail_id from biz_daywork_item where lot_id = t1.id order by create_time desc limit 1) order by status desc limit 1) = #{reportStatus}
            </if>
            <if test="queryStartTime != null">
                and date_format(t1.create_time,'%y%m%d') &gt;= date_format(#{queryStartTime},'%y%m%d')
            </if>
            <if test="queryEndTime != null">
                and date_format(t1.create_time,'%y%m%d') &lt;= date_format(#{queryEndTime},'%y%m%d')
            </if>
            <if test="productionQuantity != null ">AND t1.production_quantity = #{productionQuantity}</if>
            <if test="isAmend != null ">AND t1.is_amend = #{isAmend}</if>
            <if test="productionPlanNo != null  and productionPlanNo != ''">
                AND (t4.production_plan_no LIKE CONCAT('%', #{productionPlanNo}, '%')
                OR t1.lot_code LIKE CONCAT('%', #{productionPlanNo}, '%'))
            </if>
        </trim>
    </select>
    <select id="selectNeedTakeStock" resultType="cn.ezhizao.project.business.domain.BizLot">
        select t1.*,t2.product_description as description,t2.requisition_department_id as dept_id,t2.drawing_number as drawingNumberName  from biz_lot t1
        left join biz_production_plan_detail t2 on t1.production_plan_detail_id = t2.id
        where t1.id not in (select lot_id from biz_daywork where deleted = 0) and t1.deleted = 0 and t1.deleted = 0
    </select>
    <select id="getLotWaste" resultType="cn.ezhizao.project.business.domain.BizLotWaste">
        select * from biz_lot_waste t1 where t1.lot_id = #{lotId} and t1.deleted = 0
    </select>
    <select id="getProcessAlias" resultType="String">
        select t2.process_alias,t1.process_id from biz_daywork_item t1 left join biz_process t2 on t1.process_id = t2.id
        where t1.lot_id = #{lot_id}
        AND t1.deleted=0
        order by t1.create_time desc limit 1
    </select>
    <select id="getLastLot" resultType="cn.ezhizao.project.business.domain.BizLot">
        select lot_code from biz_lot t1 where t1.deleted = 0 and t1.from_id = 0 and t1.production_plan_detail_id =
        #{productionPlanDetailId} order by t1.lot_code desc limit 1
    </select>
    <select id="getLotList" resultMap="BizDayworkResult">
        select t1.id as
        lot_id,t1.lot_code,t1.product_id,t1.technology_version,t1.technological_process_id,t1.production_quantity,(select
        lot_code from biz_lot
        where from_id = id) as
        fromName,t1.is_waste_recycling,t1.is_fallback,t1.is_superaddition,t1.is_waste,t1.is_amend,t1.production_plan_detail_id,t2.company_alias,t2.production_plan_no,t2.product_description
        as description,t2.line_number,t3.id,t1.create_time,t4.dept_name,t4.dept_id,(select lot_code from biz_lot where t1.from_id =biz_lot.id ) as fromCode  from biz_lot t1
        left join biz_production_plan_detail t2 on t1.production_plan_detail_id = t2.id
        left join biz_daywork t3 on t1.id = t3.lot_id
        left join sys_dept t4 on t3.dept_id = t4.dept_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 and t2.deleted = 0 and t3.deleted = 0
            <if test="deptId != null  and deptId != 0">AND t3.dept_id = #{deptId}</if>
            <if test="productDescription != null  and productDescription != ''">AND t2.product_description like
                concat('%', #{productDescription}, '%')
            </if>
            <if test="lotCode != null  and lotCode != ''">AND t1.lot_code like concat('%', #{lotCode},'%')</if>
            <if test="productionPlanNo != null  and productionPlanNo != ''">AND t2.production_plan_no like concat('%',
                #{productionPlanNo},'%')
            </if>
            <if test="lineNumber != null ">AND t2.line_number = #{lineNumber}</if>
            <if test="isAmend != null">AND t1.is_amend = #{isAmend}</if>
            <if test="isWasteRecycling != null">AND t1.is_waste_recycling = #{isWasteRecycling}</if>
            <if test="isWaste != null">AND t1.is_waste = #{isWaste}</if>
            <if test="flags != null and flags.size() > 0">
                AND
                <foreach collection="flags" item="flag" separator=" OR " open="(" close=")">
                    <if test="flag != null and flag == 2"> t1.is_amend= 1</if>
                    <if test="flag != null and flag == 1"> t1.is_waste_recycling= 1</if>
                    <if test="flag != null and flag == 0"> t1.is_waste= 1</if>
                    <if test="flag != null and flag == 4"> t1.is_superaddition= 1</if>
                    <if test="flag != null and flag == 3"> t1.is_waste= 0 and  t1.is_waste_recycling= 0 and t1.is_amend = 0 and t1.is_superaddition = 0 </if>
                    <!--                    t1.is_waste_recycling = #{flag} OR t1.is_waste = #{flag} OR t1.is_amend = #{flag}-->
                </foreach>
            </if>
            <if test="status != null and status == '0'">
                AND not exists (select 1 from biz_daywork where t1.id = biz_daywork.lot_id)
            </if>
            <if test="status != null and status == '1'">
                AND exists (select 1 from biz_daywork where t1.id = biz_daywork.lot_id and t1.status &lt; 2)
            </if>
            <if test="status != null and status == '2'">
                AND exists (select 1 from biz_daywork where t1.id = biz_daywork.lot_id and t1.status = 2)
            </if>
            <if test="reportStatus != null">
                (select status from biz_daywork_item where daywork_id = t1.id and technological_process_detail_id = (select technological_process_detail_id from biz_daywork_item where daywork_id = t1.id order by create_time desc limit 1) order by status desc limit 1) = #{reportStatus}
            </if>
            <if test="queryStartTime != null">
                and date_format(t1.create_time,'%y%m%d') &gt;= date_format(#{queryStartTime},'%y%m%d')
            </if>
            <if test="queryEndTime != null">
                and date_format(t1.create_time,'%y%m%d') &lt;= date_format(#{queryEndTime},'%y%m%d')
            </if>
            <if test="reportStatus != null ">AND (select status from biz_daywork_item where lot_id = t1.id and technological_process_detail_id = (select technological_process_detail_id from biz_daywork_item where lot_id = t1.id order by create_time desc limit 1) order by status desc limit 1) = #{reportStatus}</if>
            <!-- <if test="flag != null and flag == 2">AND t1.is_amend= 1</if>-->
            <!-- <if test="flag != null and flag == 1">AND t1.is_waste_recycling= 1</if>-->
            <!-- <if test="flag != null and flag == 0">AND t1.is_waste= 1</if>-->
            <if test="startTime != null ">AND t1.create_time &gt; #{startTime}</if>
            <if test="endTime != null ">AND t1.create_time &lt; #{endTime}</if>
            <if test="processId != null">AND (select process_id from biz_daywork_item where lot_id = t1.id order by
                create_time desc limit 1) = #{processId}
            </if>
            <if test="processAlias!= null">AND
            (select process_alias from biz_process where id = (select process_id from biz_daywork_item where lot_id = t1.id order by create_time desc limit 1))  like concat('%', #{processAlias},'%')
            </if>
            <!--   查询biz_daywork_item表中lot_id=t1.id的数据按照create_time倒序排列，取第一个process_id，然后根据processId查询biz_process表中process_alias是否等于#{processAlias} -->
<!--            (select processId from biz_daywork_item where lot_id = t1.id order by-->
<!--                create_time desc limit 1) = #{processId}-->
<!--            </if>-->



        </trim>
        order by t1.lot_code asc
    </select>

    <select id="getFromCode" resultType="java.lang.String">
        select lot_code from biz_lot where id = #{lot_id}
    </select>

    <select id="getTechnologyProcessList" resultMap="BizTechnologyProcessResult">
        select * from biz_technological_process_detail t1 where t1.deleted = 0 and t1.technological_process_id =
        #{technologicalProcessId}
    </select>
    <delete id="physicalDelete">
        DELETE FROM biz_lot
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            <if test="id != null">
                id = #{id} AND
            </if>
            <!-- 删除条件为其他外键可以在这里加 -->
        </trim>
    </delete>
</mapper>
