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

    <resultMap type="cn.ezhizao.project.business.domain.BizDayworkItemExamine" id="BizDayworkItemExamineResult">
        <id column="id" property="id"/>
        <id column="daywork_id" property="dayworkId"/>
        <association property="lastTechnologicalProcessDetailId" javaType="Long" column="daywork_id" select="getLastTechnologicalProcessDetailId"/>
    </resultMap>
    <resultMap type="cn.ezhizao.project.business.domain.BizDayworkItemExamine" id="BizDayworkItemExamineDetailResult">
        <id column="id" property="id"/>
        <association property="lastTechnologicalProcessDetailId" javaType="Long" column="daywork_id" select="getLastTechnologicalProcessDetailId"/>
        <collection property="dayworkItemList" ofType="cn.ezhizao.project.business.domain.BizDayworkItem" column="daywork_id" select="getDayworkItemList"/>
        <collection property="processInspectionList" ofType="cn.ezhizao.project.business.domain.BizProcessInspecion" column="daywork_id" select="getProcessInspection"/>
    </resultMap>
    <resultMap id="ProcessInspectionResult" type="cn.ezhizao.project.business.domain.BizProcessInspecion">
        <id column="id" property="id"/>
        <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>

    <select id="getList" parameterType="BizDayworkItemExamine" resultMap="BizDayworkItemExamineResult">
        SELECT DISTINCT t1.*, t2.production_plan_no,t5.dept_name,
        (SELECT t4.nick_name FROM sys_user t4 WHERE t4.user_id = t1.reviewer_id) AS reviewerName
        FROM biz_daywork_item_examine t1
        LEFT JOIN biz_production_plan_detail t2
        ON t1.production_plan_detail_id = t2.id
        left join sys_dept t5 on t1.dept_id = t5.dept_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 AND t2.deleted = 0
            <if test="keywords != null">
                AND (t1.lot_code LIKE CONCAT('%', #{keywords}, '%')
                OR t2.production_plan_no LIKE CONCAT('%', #{keywords}, '%'))
            </if>
            <if test="tenantId != null and tenantId != 0"> AND t1.tenant_id = #{tenantId} </if>
            <if test="productDescription != null">
                AND t1.product_description LIKE CONCAT('%', #{productDescription}, '%')
            </if>
            <if test="deptId != 0">
                AND t1.dept_id = #{deptId}
            </if>
            <if test="status != null ">
                AND t1.status = #{status}
            </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="reviewerName != null ">
                AND t1.reviewer_id IN (SELECT t3.user_id FROM sys_user t3 WHERE t3.nick_name LIKE CONCAT('%', #{reviewerName}, '%'))
            </if>
        </trim>
        order by t1.create_time desc
    </select>
    <select id="getLastTechnologicalProcessDetailId" resultType="long">
        select technological_process_detail_id from biz_daywork_item where daywork_id = #{dayworkId} and deleted = 0 order by process_step_number desc limit 1
    </select>
    <select id="getAllowAddedList" resultMap="BizDayworkItemExamineResult">
        select t1.daywork_id,t1.id as dayworkItemId,t1.dept_id,t1.production_plan_detail_id,t2.product_description,t1.lot_id,t4.lot_code,t2.product_id,
        t1.technological_process_id,t1.technological_process_detail_id,t1.process_id,t3.process_alias from biz_daywork_item t1 left join biz_production_plan_detail t2 on t1.production_plan_detail_id = t2.id
        left join biz_process t3 on t1.process_id = t3.id
        left join biz_lot t4 on t1.lot_id = t4.id
        where t1.deleted = 0 and t4.deleted = 0 and t1.id in
        <foreach collection="dayworkItemIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <select id="getDetailById" resultMap="BizDayworkItemExamineDetailResult">
        select t1.*,t2.drawing_number,t3.technology_version,(SELECT nick_name FROM sys_user  WHERE user_id = t1.reviewer_id) AS reviewerName from biz_daywork_item_examine t1
        left join biz_product t2 on t1.product_id = t2.id
        left join biz_lot t3 on t1.lot_id = t3.id
        where t1.id = #{id}
    </select>
    <select id="getListByDayworkIds" resultType="cn.ezhizao.project.business.domain.BizDayworkItemExamine">
        select * from biz_daywork_item_examine
        where deleted = 0 and  daywork_id in
        <foreach collection="dayworkIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <select id="getDayworkItemList" resultType="cn.ezhizao.project.business.domain.BizDayworkItem">
        select t1.*,t2.process_alias from biz_daywork_item t1 left join biz_process t2 on t1.process_id = t2.id
        where t1.daywork_id = #{dayworkId} and t1.deleted = 0 and t1.is_sort = 1
        and t1.status = 3 order by t1.create_time
    </select>
    <select id="getProcessInspection" resultMap="ProcessInspectionResult">
        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 t2.daywork_id = #{dayworkId} and t1.deleted = 0
        order by t2.process_step_number,t1.create_time
    </select>
    <delete id="physicalDelete">
        DELETE FROM biz_daywork_item_examine
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            <if test="id != null">
                id = #{id} AND
            </if>
       <!-- 删除条件为其他外键可以在这里加 -->
        </trim>
    </delete>
    <select id="getImages" resultMap="BizProcessInspectionPictureResult">
        select * from biz_process_inspection_picture
        where deleted = 0 and process_inspection_id = #{id}
    </select>
</mapper>
