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

    <resultMap type="cn.ezhizao.project.business.domain.BizTaksStockLot" id="BizTaksStockLotResult">
        <id column="id" property="id"/>
        <association property="carrierName" column="daywork_id" select="getCarrierName"/>
    </resultMap>


    <select id="getList" parameterType="BizTaksStockLot" resultMap="BizTaksStockLotResult">
        SELECT t1.*,t2.dept_name, t2.dept_code,t3.production_plan_no,(case when t2.dept_code = '170000' and (select status from biz_daywork_item t where t.daywork_id = t1.daywork_id and t.deleted = 0 order by t.create_time limit 1) &lt; '4' then (select t2.form_code from biz_daywork_item t left join biz_outsourced_order_detail t2 on t2.id = t.outsource_detail_id where t.daywork_id = t1.daywork_id and t.deleted = 0 order by t.create_time limit 1) else '' end) as outsource_form_no,
        (select t4.status from biz_daywork_item t4 where t4.daywork_id = t1.daywork_id and t4.deleted = 0 order by t4.create_time desc limit 1) as statusLabel
        FROM biz_taks_stock_lot t1
        left join sys_dept t2 on t1.dept_id = t2.dept_id
        left join biz_production_plan_detail t3 on t1.production_plan_detail_id = t3.id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 and t1.take_stock_period_id = #{takeStockPeriodId} and t1.is_waste = 0 and t1.status &lt;2
            <if test="productDescription != null  and productDescription != ''"> AND t1.product_description like concat('%', #{productDescription}, '%')</if>
            <if test="lotCode != null  and lotCode != ''"> AND t1.lot_code like concat('%', #{lotCode},'%')</if>
            <if test="isTaksStock != null "> AND t1.is_taks_stock = #{isTaksStock}</if>
            <if test="deptId != null and deptId != 0"> AND t1.dept_id = #{deptId}</if>
            <if test="isProductStatus != null "> AND t1.is_product_status = #{isProductStatus}</if>
            <if test="lotIds != null and lotIds.size > 0">
                AND t1.lot_id in
                <foreach collection="lotIds" item="lotId" open="(" close=")" separator=",">
                    #{lotId}
                </foreach>
            </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>
                </foreach>
            </if>
        </trim>
    </select>
    <select id="getProductionPlanDetailList" resultType="cn.ezhizao.project.business.domain.BizTaksStockLot">
        select distinct CONCAT(t2.production_plan_no, ' ', t2.product_description) AS label,t2.id as value from biz_taks_stock_lot t1 left join biz_production_plan_detail t2 on t1.production_plan_detail_id = t2.id
        where t1.take_stock_period_id = #{takeStockPeriodId} and t1.dept_id = #{deptId} and t1.deleted = 0
        and t1.is_taks_stock = 0 and t1.daywork_id = 0
    </select>
    <select id="getInfo" resultMap="BizTaksStockLotResult">
        select t1.*,t2.product_code from biz_taks_stock_lot t1 left join biz_production_plan_detail t2 on t1.production_plan_detail_id = t2.id
        where t1.deleted = 0 and t1.take_stock_period_id = #{id}
    </select>

    <select id="getNotTaksLot" resultType="cn.ezhizao.project.business.domain.BizTaksStockLot">
        SELECT t1.*,t2.dept_name
        FROM biz_taks_stock_lot t1
        left join sys_dept t2 on t1.dept_id = t2.dept_id
        where t1.dept_id = #{deptId} and t1.deleted = 0 and t1.take_stock_period_id = #{takeStockPeriodId} and t1.daywork_id = 0 and t1.is_taks_stock = 0 and t1.production_plan_detail_id = #{productionPlanDetailId}
        order by t1.lot_code
    </select>
    <select id="getCarrierName" resultType="String">
        select GROUP_CONCAT(t1.carrier_code SEPARATOR ',') as carrierName from biz_daywork_carrier t1 where t1.daywork_id = #{dayworkId} and t1.deleted = 0
        and t1.is_changed = 0 and t1.process_inspection_id = 0
    </select>

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