<?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="com.bonus.sgzb.material.mapper.PurchaseCheckServiceCenterMapper">
    
    <resultMap type="com.bonus.sgzb.material.domain.PurchaseCheckInfo" id="PurchaseCheckInfoResult">
        <result property="id"    column="id"    />
        <result property="taskId"    column="task_id"    />
        <result property="purchaseTime"    column="purchase_time"    />
        <result property="arrivalTime"    column="arrival_time"    />
        <result property="purchaser"    column="purchaser"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="companyId"    column="company_id"    />
    </resultMap>

    <sql id="selectPurchaseCheckInfoVo">
        select id,task_id, purchase_time, arrival_time, purchaser, create_by, create_time, update_by, update_time, remark, company_id from purchase_check_info
    </sql>

    <select id="selectPurchaseCheckInfoList" parameterType="com.bonus.sgzb.material.domain.PurchaseCheckInfo" resultMap="PurchaseCheckInfoResult">
        select pci.id,pci.task_id, pci.purchase_time, pci.arrival_time, pci.purchaser, pci.create_by, pci.create_time,
        pci.update_by,
        pci.update_time, pci.remark, pci.company_id ,dict.name purchasingStatus,tk.code,su.user_name
        purchaserName,tk.task_status taskStatus
        from purchase_check_info pci
        left join tm_task tk on pci.task_id = tk.task_id
        left join (select id,name from sys_dic where p_id = 23) dict on tk.task_status = dict.id
        left join sys_user su on pci.purchaser = su.user_id
        LEFT JOIN purchase_check_details pcd on pcd.task_id=pci.task_id
        where task_type = 23
        <if test="keyWord != null  and keyWord != ''">
            and (tk.code like concat('%',#{keyWord},'%') or
            su.user_name like concat('%',#{keyWord},'%')
            )
        </if>
        <if test="purchaseTime != null  and purchaseTime != ''">and pci.purchase_time = #{purchaseTime}</if>
        <if test="arrivalTime != null  and arrivalTime != ''">and pci.arrival_time = #{arrivalTime}</if>
        <if test="purchaser != null ">and pci.purchaser = #{purchaser}</if>
        <if test="companyId != null ">and pci.company_id = #{companyId}</if>
        <if test="typeId != null">AND #{typeId} IN (SELECT mt2.type_id
            FROM purchase_check_details pcd
            LEFT JOIN ma_type mt on mt.type_id=pcd.type_id
            LEFT JOIN ma_type mt2 on mt2.type_id=mt.parent_id
            WHERE pcd.task_id = pci.task_id
            )
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND ((pci.purchase_time BETWEEN CONCAT(#{startTime}, ' 00:00:00') AND CONCAT(#{endTime}, ' 23:59:59'))
        or (pci.arrival_time BETWEEN CONCAT(#{startTime}, ' 00:00:00') AND CONCAT(#{endTime}, ' 23:59:59')))
        </if>
        GROUP BY pcd.task_id
        order by pci.create_time desc
    </select>
    
    <select id="selectPurchaseCheckInfoByTaskId" parameterType="Long" resultMap="PurchaseCheckInfoResult">
        select pci.id,pci.task_id, pci.purchase_time, pci.arrival_time, pci.purchaser, pci.create_by, pci.create_time,
               pci.update_by, pci.update_time, pci.remark, pci.company_id, tk.code
        from purchase_check_info pci
            left join tm_task tk on pci.task_id = tk.task_id
        where pci.task_id = #{taskId}
    </select>
        
    <insert id="insertPurchaseCheckInfo" parameterType="com.bonus.sgzb.material.domain.PurchaseCheckInfo" useGeneratedKeys="true" keyProperty="taskId">
        insert into purchase_check_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="purchaseTime != null">purchase_time,</if>
            <if test="arrivalTime != null">arrival_time,</if>
            <if test="purchaser != null">purchaser,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null">#{taskId},</if>
            <if test="purchaseTime != null and purchaseTime != ''">#{purchaseTime},</if>
            <if test="arrivalTime != null">#{arrivalTime},</if>
            <if test="purchaser != null">#{purchaser},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
         </trim>
    </insert>

    <update id="updatePurchaseCheckInfo" parameterType="com.bonus.sgzb.material.domain.PurchaseCheckInfo">
        update purchase_check_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="purchaseTime != null">purchase_time = #{purchaseTime},</if>
            <if test="arrivalTime != null">arrival_time = #{arrivalTime},</if>
            <if test="purchaser != null">purchaser = #{purchaser},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
        </trim>
        where task_id = #{taskId}
    </update>
    <update id="updateMaMachine">
        update ma_machine set ma_status = #{maStatus} where ma_id = #{maId}
    </update>
    <update id="updateTypeByTypeId">
        update ma_type set num = #{num} where type_id = #{typeId}
    </update>
    <update id="updateMacodeByType">
        update purchase_macode_info
        set status = #{status}
        where task_id = #{taskId}
          and type_id = #{typeId}
          and ma_code = #{maCode}
    </update>
    <update id="updatetePurchaseMaCodeInfoByMaCodeAndTaskIdAndTypeId">
        update
            purchase_macode_info
        set status='2'
        where ma_code = #{maCode}
          and task_id = #{taskId}
          and type_id = #{typeId}
    </update>

    <delete id="deletePurchaseCheckInfoByTaskId" parameterType="Long">
        delete from purchase_check_info where task_id = #{taskId}
    </delete>

    <delete id="deletePurchaseCheckInfoByTaskIds" parameterType="String">
        delete from purchase_check_info where task_id in 
        <foreach item="taskId" collection="array" open="(" separator="," close=")">
            #{taskId}
        </foreach>
    </delete>
    <delete id="deleteMaMachineInfoByMaId">
        delete from ma_machine where ma_id = #{maId}
    </delete>
    <delete id="deleteMaMachineLabelByMaId">
        delete from ma_machine_label where ma_id = #{maId}
    </delete>
    <delete id="deleteMaLabelBindByMaId">
        delete from ma_label_bind where ma_id = #{maId}
    </delete>

    <select id="selectTypeNameByTaskId" resultType="java.lang.String">
        select GROUP_CONCAT(type_name) typeName from
        (select distinct pcd.task_id, mt1.type_name
        from purchase_check_details pcd
        left join ma_type mt on pcd.type_id = mt.type_id
        left join ma_type mt1 on mt.parent_id = mt1.type_id
        where pcd.task_id = #{taskId}
        <if test="type == 2">and pcd.`status`!=3</if>
        ) t
        GROUP BY task_id
    </select>

    <insert id="insertBmNoticeInfo" parameterType="com.bonus.sgzb.material.domain.BmNoticeInfo" useGeneratedKeys="true" keyProperty="id">
        insert into bm_notice_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="content != null and content != ''">content,</if>
            <if test="noticeUser != null">notice_user,</if>
            <if test="phone != null">phone,</if>
            <if test="modelName != null">model_name,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null">#{taskId},</if>
            <if test="content != null and content != ''">#{content},</if>
            <if test="noticeUser != null">#{noticeUser},</if>
            <if test="phone != null">#{phone},</if>
            <if test="modelName != null">#{modelName},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
        </trim>
    </insert>
    <insert id="insertInputApplyDetails">
        insert into input_apply_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="maId != null">ma_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="inputNum != null">input_num,</if>
            <if test="inputType != null">input_type,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null">#{taskId},</if>
            <if test="maId != null">#{maId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="inputNum != null">#{inputNum},</if>
            <if test="inputType != null">#{inputType},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
        </trim>
    </insert>
    <insert id="insertMaInputRecord">
        insert into ma_input_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="maId != null">ma_id,</if>
            <if test="inputType != null">input_type,</if>
            <if test="inputNum != null">input_num,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null">#{taskId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="maId != null">#{maId},</if>
            <if test="inputType != null">#{inputType},</if>
            <if test="inputNum != null">#{inputNum},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
        </trim>
    </insert>

    <select id="selectPutInListList" resultMap="PurchaseCheckInfoResult">
        select pci.id,pci.task_id, pci.purchase_time, pci.arrival_time, pci.purchaser, pci.create_time, pci.update_by,
        pci.update_time, pci.remark, pci.company_id ,dict.name purchasingStatus,tk.code,tk.task_status taskStatus,su.user_name purchaserName,
        tk.create_by,
        CASE tk.task_status
        WHEN 28 THEN
        tk.update_time
        ELSE
        ''
        END       as inputTime,
        CASE tk.task_status
        WHEN 28 THEN
        us.user_name
        ELSE
        ''
        END       as inputUser
        from purchase_check_info pci
        left join tm_task tk on pci.task_id = tk.task_id
        left join (select id,name from sys_dic where p_id = 50) dict on tk.task_status = dict.id
        left join sys_user su on pci.purchaser = su.user_id
        LEFT JOIN sys_user us on us.user_id = tk.update_by
        LEFT JOIN purchase_check_details pcd on pcd.task_id=pci.task_id
        where task_type = 23 and tk.task_status in (26,28,105,106,107)
        <if test="keyWord != null  and keyWord != ''">
            and (tk.code like concat('%',#{keyWord},'%') or
            su.user_name like concat('%',#{keyWord},'%')
            )
        </if>
        <if test="purchaseTime != null  and purchaseTime != ''"> and pci.purchase_time = #{purchaseTime}</if>
        <if test="arrivalTime != null  and arrivalTime != ''"> and pci.arrival_time = #{arrivalTime}</if>
        <if test="purchaser != null "> and pci.purchaser = #{purchaser}</if>
        <if test="companyId != null "> and pci.company_id = #{companyId}</if>
        <if test="typeId != null">AND #{typeId} IN (SELECT mt2.type_id
            FROM purchase_check_details pcd
            LEFT JOIN ma_type mt on mt.type_id=pcd.type_id
            LEFT JOIN ma_type mt2 on mt2.type_id=mt.parent_id
            WHERE pcd.task_id = pci.task_id
            )
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND (pci.purchase_time BETWEEN #{startTime} AND #{endTime}
            or pci.arrival_time BETWEEN #{startTime} AND #{endTime})
        </if>
        GROUP BY pcd.task_id
        order by pci.id desc
    </select>
    <select id="exportList" resultType="com.bonus.sgzb.material.domain.PurchaseCheckInfo">
        SELECT
            tk.CODE,
            pci.purchase_time as purchaseTime,
            pci.arrival_time as arrivalTime,
            su.user_name purchaserName,
            mt1.type_name as purchasingTypeName,
            mt.type_name AS purchasingTypeCode,
            mt.manage_type as manageType,
            msi.supplier,
            pcd.purchase_num as purchaseNum,
            pcd.check_num as checkNum,
            dict.NAME AS purchasingStatus,
            tk.task_status as taskStatus
        FROM
            purchase_check_details pcd
                LEFT JOIN purchase_check_info pci ON pci.task_id = pcd.task_id
                LEFT JOIN tm_task tk ON pci.task_id = tk.task_id
                LEFT JOIN ( SELECT id, NAME FROM sys_dic WHERE p_id = 23 ) dict ON tk.task_status = dict.id
                LEFT JOIN sys_user su ON pci.purchaser = su.user_id
                LEFT JOIN ma_type mt ON pcd.type_id = mt.type_id
                LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
                LEFT JOIN ma_supplier_info msi ON msi.supplier_id = pcd.supplier_id
        WHERE
            task_type = 23
        <if test="keyWord != null  and keyWord != ''"> and tk.code like concat('%',#{keyWord},'%')</if>
        <if test="purchaseTime != null  and purchaseTime != ''"> and pci.purchase_time = #{purchaseTime}</if>
        <if test="arrivalTime != null  and arrivalTime != ''"> and pci.arrival_time = #{arrivalTime}</if>
        <if test="purchaser != null "> and pci.purchaser = #{purchaser}</if>
        <if test="companyId != null "> and pci.company_id = #{companyId}</if>
        ORDER BY
            pcd.task_id DESC
    </select>
    <select id="putInExportList" resultType="com.bonus.sgzb.material.domain.PurchaseInput">
        SELECT pcd.task_id as taskId,
               tk.`code`,
               pci.purchase_time as purchaseTime,
               pci.arrival_time as arrivalTime,
               pci.purchaser,
               su.user_name        purchaserName,
               mt1.type_name as purchasingTypeName,
               mt.type_name     as purchasingTypeCode,
               pcd.input_time   as inputTime,
               su.user_name     as inputUser,
               pcd.input_status as inputStatus
        FROM purchase_check_details pcd
                 LEFT JOIN purchase_check_info pci on pci.task_id = pcd.task_id
                 left join tm_task tk on pcd.task_id = tk.task_id
                 left join sys_user su on pci.purchaser = su.user_id
                 left join ma_type mt on pcd.type_id = mt.type_id
                 left join ma_type mt1 on mt.parent_id = mt1.type_id
        where task_type = 23
          and tk.task_status in (26, 28)
        <if test="keyWord != null  and keyWord != ''"> and tk.code like concat('%',#{keyWord},'%')</if>
        <if test="purchaseTime != null  and purchaseTime != ''"> and pci.purchase_time = #{purchaseTime}</if>
        <if test="arrivalTime != null  and arrivalTime != ''"> and pci.arrival_time = #{arrivalTime}</if>
        <if test="purchaser != null "> and pci.purchaser = #{purchaser}</if>
        <if test="companyId != null "> and pci.company_id = #{companyId}</if>
        ORDER BY pcd.task_id desc
    </select>

    <select id="selectPutinDetails" resultType="com.bonus.sgzb.material.domain.PurchaseMacodeInfo">
        select pcd.production_time                           productionTime,
        mt.type_name                                  specificationType,
        mt1.type_name                                 typeName,
        pmi.ma_code                                   maCode,
        mm.assets_code                                assetsCode,
        pmi.fix_code                                  fixCode,
        pcd.type_id                                   typeId,
        pcd.task_id                                   taskId,
        mt.code                                       specsCode,
        mt1.code                                      typeCode,
        CASE
        WHEN pmi.ma_code is null THEN
        CASE
        WHEN pcd.status = 4 THEN 1
        WHEN pcd.status = 5 THEN 2
        WHEN pcd.status = 6 THEN 3
        WHEN pcd.status = 7 THEN 4
        ELSE 0
        END
        ELSE pmi.status
        END AS                                    status,
        mm.qr_code                                    qrCode,
        mm.ma_id                                      maId,
        if(pmi.ma_code is not null, 1, pcd.check_num) checkNum
        from purchase_check_details pcd
        left join purchase_macode_info pmi on pmi.task_id = pcd.task_id and pmi.type_id = pcd.type_id
        left join ma_machine mm on pmi.ma_code = mm.ma_code
        left join ma_type mt on pcd.type_id = mt.type_id
        left join ma_type mt1 on mt.parent_id = mt1.type_id
        where pcd.task_id = #{taskId}
        and pcd.`status`!=3
        <if test="dictName != null and dictName != ''">
            and (mt.type_name like concat('%',#{dictName},'%') or
            mt1.type_name like concat('%',#{dictName},'%'))
        </if>
        order by status
    </select>
    <select id="selectTypeByTypeId" resultType="com.bonus.sgzb.base.api.domain.MaType">
        select type_id typeId, type_name, parent_id, status, num, unit_id, manage_type, lease_price, buy_price, pay_price,
               level, rated_load, test_load, holding_time, warn_num, del_flag, create_by,
               create_time, remark, company_id
        from ma_type where type_id = #{typeId}
    </select>
    <select id="selectMacodeInfoStatusByTaskId" resultType="java.lang.Integer">
        select count(*) from purchase_check_details where task_id = #{taskId} and status ='1'
    </select>
    <select id="selectExamineType" resultType="java.lang.Integer">
        SELECT
            COUNT(*)
        FROM
            bm_flow_relation bfr
                LEFT JOIN bm_flow bf on bfr.flow_id=bf.id
        WHERE
            bf.task_type='23'
          AND bfr.company_id = #{companyId}
          and bfr.`status`='1'
          and bf.`status`='1'
    </select>
    <select id="isOperateAll" resultType="java.lang.Integer">
        SELECT
            count(*)
        FROM
            purchase_check_details
        WHERE
            task_id =  #{taskId} and status =1
    </select>
    <select id="selectPurchaseCheckDetailsStatus" resultType="java.lang.Integer">
        SELECT
            count(*)
        FROM
            purchase_check_details
        WHERE
            task_id = #{taskId} and status != 7 and `status` !=3
    </select>
</mapper>