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

    <resultMap type="cn.ezhizao.project.business.domain.BizDayworkItem" id="BizDayworkItemResult">
        <id column="id" property="id"/>
        <result column="lot_id" property="lotId"/>
        <result column="daywork_id" property="dayworkId"/>
        <association property="lotCode" javaType="java.lang.String" column="lot_id" select="getLotCode"/>
        <association property="rejectNum" javaType="java.lang.Integer" column="id" select="getRejectNum"/>
    </resultMap>

    <select id="getLotCode" parameterType="Long" resultType="java.lang.String">
        SELECT lot_code
        FROM biz_lot
        WHERE deleted = 0
          AND id = #{lotId}
    </select>
    <select id="getList" parameterType="cn.ezhizao.project.business.domain.BizDayworkItem" resultMap="BizDayworkItemResult">
        SELECT * FROM biz_daywork_item
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
            <if test="dayworkId != null">AND daywork_id = #{dayworkId}</if>
            <if test="lotId != null ">AND lot_id = #{lotId}</if>
            <if test="processStepNumber != null ">AND process_step_number = #{processStepNumber}</if>
            <if test="outsourceDetailId != null ">AND outsource_detail_id = #{outsourceDetailId}</if>
        </trim>
        ORDER BY create_time DESC,process_step_number desc
    </select>

    <select id="getListForLotTechnological" parameterType="cn.ezhizao.project.business.domain.BizDayworkItem" resultMap="BizDayworkItemResult">
        SELECT * FROM biz_daywork_item
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
            <if test="dayworkId != null">AND daywork_id = #{dayworkId}</if>
            <if test="lotId != null ">AND lot_id = #{lotId}</if>
            <if test="processStepNumber != null ">AND process_step_number = #{processStepNumber}</if>
            <if test="outsourceDetailId != null ">AND outsource_detail_id = #{outsourceDetailId}</if>
        </trim>
        ORDER BY create_time ASC
    </select>


    <select id="getOutsourcesItemList" parameterType="BizDayworkItem" resultMap="BizDayworkItemResult">
        SELECT * FROM biz_daywork_item
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
            <if test="dayworkId != null">AND daywork_id = #{dayworkId}</if>
            <if test="lotId != null ">AND lot_id = #{lotId}</if>
            <if test="outsourceDetailId != 0 ">AND outsource_detail_id != #{outsourceDetailId}</if>
        </trim>
        ORDER BY create_time DESC
    </select>
    <select id="getLastFinished" parameterType="BizDayworkItem" resultMap="BizDayworkItemResult">
        SELECT * FROM biz_daywork_item
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0 and outsource_detail_id = 0
            <if test="dayworkId != null">AND daywork_id = #{dayworkId}</if>
        </trim>
        ORDER BY process_step_number DESC
    </select>

    <select id="getSpecialList" parameterType="BizDayworkItem" resultMap="BizDayworkItemResult">
        SELECT * FROM biz_daywork_item d
        LEFT JOIN biz_daywork dw ON d.daywork_id = dw.id
        LEFT JOIN biz_production_plan_detail p ON d.production_plan_detail_id = p.id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            d.deleted = 0 and d.outsource_detail_id = 0  and d.is_special=1
            <if test="specialStartTime != null "> AND
                dw.start_time
                >= #{specialStartTime}</if>
            <if test="specialEndTime != null "> AND
                dw.start_time &lt;= #{specialEndTime}</if>
            <if test="lotCode != null and  lotCode != ''">AND   dw.lot_code LIKE CONCAT('%', #{lotCode}, '%') </if>
            <if test="productionPlanNo != null and  productionPlanNo != ''">AND p.production_plan_no  LIKE CONCAT('%', #{productionPlanNo}, '%') </if>
            <if test="productDescription != null and  productDescription != ''">AND p.product_description  LIKE CONCAT('%', #{productDescription}, '%') </if>
        </trim>
        ORDER BY d.create_time ASC
    </select>

    <select id="selectListOfspecial" parameterType="list" resultMap="BizDayworkItemResult">
        SELECT * FROM biz_daywork_item
        <trim prefix="WHERE" suffixOverrides="AND">
            deleted = 0
            <if test="list != null and list.size() > 0">
                AND daywork_id IN
                <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
                    #{item.id}
                </foreach>
                AND technological_process_detail_id IN
                <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
                    #{item.specialProcessId}
                </foreach>
            </if>
            AND (status = 3 OR status = 2)
        </trim>
    </select>






    <select id="selectLatestItemByDayworkIds" resultType="cn.ezhizao.project.business.domain.BizDayworkItem">
        SELECT *
        FROM biz_daywork_item d
        WHERE d.daywork_id IN
        <foreach item="dayworkId" index="index" collection="ids" open="(" close=")" separator=",">
            #{dayworkId}
        </foreach>
        AND d.status = 7
        AND d.start_time = (
        SELECT MAX(di.start_time)
        FROM biz_daywork_item di
        WHERE di.daywork_id = d.daywork_id
        AND di.status = 7
        )
    </select>
    <select id="selectItemByOutsourcedDetailIds" resultType="BizDayworkItem">
        SELECT *
        FROM biz_daywork_item d
        WHERE d.outsource_detail_id IN
        <foreach item="outsourceDetailId" index="index" collection="ids" open="(" close=")" separator=",">
            #{outsourceDetailId}
        </foreach>
    </select>
    <select id="getDeptInfo" resultType="cn.ezhizao.project.system.domain.SysDept">
        select distinct t1.dept_id as value,t1.dept_name as label,t2.dept_code as code,t2.is_outsourcing as isOutsourcing from biz_daywork_item t1 left join sys_dept t2 on t1.dept_id = t2.dept_id
        where t1.daywork_id = #{dayworkId} and t1.deleted = 0 and t2.del_flag = 0
    </select>

    <select id="getItemScreen" parameterType="cn.ezhizao.project.business.domain.BizDayworkItem" resultMap="BizDayworkItemResult">
        SELECT bdi.*
        FROM biz_daywork_item bdi
                 JOIN (
            SELECT MAX(id) AS id
            FROM biz_daywork_item
            WHERE daywork_id = #{dayWorkId}
            GROUP BY technological_process_detail_id
        ) max_ids ON bdi.id = max_ids.id
        WHERE bdi.daywork_id = #{dayWorkId}
        and bdi.deleted = 0
    </select>

    <select id="getRejectNum"  resultType="java.lang.Integer">
        SELECT SUM(reject_num)
        FROM biz_daywork_item_reject
        WHERE daywork_item_id = #{id}
        AND deleted = 0
    </select>

    <select id="selectDayworkItemList" parameterType="cn.ezhizao.project.business.domain.BizDayworkItem" resultMap="BizDayworkItemResult">
        SELECT * FROM biz_daywork_item
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
            <if test="dayworkId != null and dayworkId != ''">AND daywork_id = #{dayworkId}</if>

        </trim>
        ORDER BY create_time ASC
    </select>
    <select id="selectBydayworkId" parameterType="list" resultMap="BizDayworkItemResult">
        SELECT * FROM biz_daywork_item
        <trim prefix="WHERE" suffixOverrides="AND">
            deleted = 0
            <if test="list != null and list.size() > 0">
                AND daywork_id IN
                <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </trim>
        ORDER BY create_time DESC
    </select>
    <update id="removeSpecial"  parameterType="list">
        UPDATE biz_daywork_item SET deleted=1
        WHERE daywork_id IN
        <foreach item="item" index="index" collection="firstDayworkIds" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>
    <update id="updateTechnologicalProcessDetailId" >
        UPDATE biz_daywork_item d SET technological_process_detail_id =(SELECT id
            FROM biz_lot_technological_process_detail l
             WHERE d.technological_process_id = l.lot_technological_process_id
             AND d.process_step_number = l.process_step_number) AND
        daywork_id =#{ dayworkId } AND deleted = 0;
    </update>




    <select id="getEquipmentInfo" resultType="cn.ezhizao.project.business.domain.BizProductionResourceGroupDetail">
        select equipment_detail_id as value,equipment_detail_code as label from biz_daywork_item where deleted = 0 and id = #{dayworkItemId}
    </select>
  <update id="updateDayworkItem" >
      UPDATE biz_daywork_item set status = #{status},prod_num = #{prodNum},qualified_num = #{qualifiedNum}

      <if test="workingHours != 0">,working_hours = #{workingHours}</if>
      <if test="workingHours == 0 and endTime == null ">,working_hours = 0 </if>
      <if test="endTime == null">,end_time = '0000-00-00 00:00:00.00000'</if>
      <if test="endTime != null">,end_time = #{endTime}</if>
      <if test="startTime == null">,start_time = '0000-00-00 00:00:00.00000'</if>
      <if test="startTime != null">,start_time = #{startTime}</if>
      <if test="equipmentDetailId != null">,equipment_detail_id = #{equipmentDetailId}</if>
      <if test="nickName != null">,nick_name = #{nickName}</if>
      <if test="userId != null">,user_id = #{userId}</if>
      <if test="userName != null">,user_name = #{userName}</if>
      <if test="rejectNum != null">,reject_num = #{rejectNum}</if>
      <if test="equipmentDetailId == null ">,equipment_detail_id = 0 </if>
      <if test="equipmentDetailCode != null">,equipment_detail_code = #{equipmentDetailCode}</if>
      <if test="equipmentDetailCode == null ">,equipment_detail_code = '' </if>
      <if test="deptId != null">,dept_id = #{deptId}</if>
      <if test="deptName != null">,dept_name = #{deptName}</if>
      <if test="turnoverType != null">, turnover_type = #{turnoverType}</if>
      <if test="turnoverId != null">, turnover_id = #{turnoverId}</if>
      <if test="turnoverArea != null">, turnover_area = #{turnoverArea}</if>
      <if test="place != null">, place = #{place}</if>
      <if test="placeId != null">, place_id = #{placeId}</if>
      <if test="updateTime != null">, update_time = #{updateTime}</if>
      <if test="updaterId != null">, updater_id = #{updaterId}</if>
      WHERE id = #{id}
  </update>
    <select id="getListByDayworkIds" resultMap="BizDayworkItemResult">
        select * from biz_daywork_item t1 where t1.deleted = 0 and t1.daywork_id in
        <foreach collection="dayworkIds" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>
    <select id="getListInfoByDayworkIds" resultType="cn.ezhizao.project.business.domain.BizDayworkItem">
        select t1.*,t2.process_alias as processAlias from biz_daywork_item t1 left join biz_process t2 on t1.process_id = t2.id where t1.deleted = 0 and t1.daywork_id in
        <foreach collection="dayworkIds" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>
    <select id="getListByDayworkItemIds" resultType="cn.ezhizao.project.business.domain.BizDayworkItem">
        select * from biz_daywork_item t1 where t1.deleted = 0 and t1.id in
        <foreach collection="dayworkItemIds" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>
<select id="getListByLotIds" resultType="cn.ezhizao.project.business.domain.BizDayworkItem">
    select t1.* from biz_daywork_item t1 where t1.deleted = 0 and t1.is_sort = 1 and t1.status = 3 and t1.lot_id in
    <foreach collection="lotIds" item="item" separator="," open="(" close=")">
        #{item}
    </foreach>
</select>
    <select id="getListByDayworkIdsForOutsourced" resultMap="BizDayworkItemResult">
        select * from biz_daywork_item t1 where t1.deleted = 0 and t1.is_wx = 1 and  t1.daywork_id in
        <foreach collection="dayworkIds" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="getLatestDayworkItemId" resultType="java.lang.Long">
        SELECT
        t1.id
        FROM biz_daywork_item t1
        LEFT JOIN sys_user t2 ON t1.user_id = t2.user_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 AND t1.status &lt; 4
            <if test="dayworkId != null and dayworkId != 0">AND t1.daywork_id = #{dayworkId}</if>
            <if test="isAmend != null and isAmend != 0">AND t1.form_daywork_item_id !=0 </if>
            <if test="processId != null  and processId != 0">AND t1.process_id = #{processId}</if>
            ORDER BY t1.process_step_number desc ,t1.create_time desc limit 1
        </trim>
    </select>
    <select id="getLastItem" resultType="cn.ezhizao.project.business.domain.BizDayworkItem">
        SELECT
        t1.*, t2.form_code outsource_form_no
        FROM
        biz_daywork_item t1 left join biz_outsourced_order_detail t2 on t2.id = t1.outsource_detail_id
        WHERE
        t1.daywork_id = #{dayworkId} and t1.deleted = 0 order by t1.process_step_number desc ,t1.create_time desc limit 1
    </select>
    <select id="getListByDayworkIdAndProcessStepNumber" resultType="cn.ezhizao.project.business.domain.BizDayworkItem">
        select * from biz_daywork_item where daywork_id = #{dayworkId} and process_step_number = #{processStepNumber}
    </select>

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