<?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.PurchaseCheckDetailsMapper">
    
    <resultMap type="com.bonus.sgzb.material.domain.PurchaseCheckDetails" id="PurchaseCheckDetailsResult">
        <result property="id"    column="id"    />
        <result property="taskId"    column="task_id"    />
        <result property="typeId"    column="type_id"    />
        <result property="purchasePrice"    column="purchase_price"    />
        <result property="purchaseNum"    column="purchase_num"    />
        <result property="checkNum"    column="check_num"    />
        <result property="checkResult"    column="check_result"    />
        <result property="supplierId"    column="supplier_id"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="productionTime"    column="production_time"    />
        <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="checkUrlName"    column="check_url_name"    />
        <result property="checkUrl"    column="check_url"    />
        <result property="fileName"    column="file_name"    />
        <result property="fileUrl"    column="file_url"    />
        <result property="companyId"    column="company_id"    />
    </resultMap>

    <sql id="selectPurchaseCheckDetailsVo">
        select id, task_id, type_id, purchase_price, purchase_num, check_num,bind_num, check_result, supplier_id, status, create_by, production_time, create_time, update_by, update_time, remark, check_url_name, check_url, file_name, file_url, company_id from purchase_check_details
    </sql>

    <select id="selectPurchaseCheckDetailsList" parameterType="com.bonus.sgzb.material.domain.PurchaseCheckDetails" resultMap="PurchaseCheckDetailsResult">
        select pcd.id,pcd.task_id, pcd.type_id, pcd.purchase_price, pcd.purchase_num, pcd.check_num, pcd.check_result,
        pcd.supplier_id, pcd.status, msi.supplier, pcd.create_by, pcd.production_time, pcd.create_time, pcd.check_url_name,
        pcd.check_url, pcd.file_name, pcd.file_url,pcd.update_by, pcd.update_time, pcd.remark, pcd.company_id,
        mt1.type_name machineTypeName,mt.type_name specificationType, mt.manage_type manageType, tk.code ,mt.type_name typeName,
        mt.unit_name unitName
        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
        left join ma_supplier_info msi on pcd.supplier_id = msi.supplier_id
        left join tm_task tk on pcd.task_id = tk.task_id
        where 1=1
        <if test="taskId != null ">and pcd.task_id = #{taskId}</if>
        <if test="keyWord != null and keyWord != ''">
          and (mt.type_name like concat('%',#{keyWord},'%')
            or mt1.type_name like concat('%',#{keyWord},'%') or msi.supplier like concat('%',#{keyWord},'%'))
        </if>
        <if test="typeId != null ">and pcd.type_id = #{typeId}</if>
        <if test="purchasePrice != null  and purchasePrice != ''">and pcd.purchase_price = #{purchasePrice}</if>
        <if test="purchaseNum != null ">and pcd.purchase_num = #{purchaseNum}</if>
        <if test="supplierId != null ">and pcd.supplier_id = #{supplierId}</if>
        <if test="productionTime != null ">and pcd.production_time = #{productionTime}</if>
        <if test="companyId != null ">and pcd.company_id = #{companyId}</if>
    </select>
    
    <select id="selectPurchaseCheckDetailsByTaskId" parameterType="Long" resultMap="PurchaseCheckDetailsResult">
        <include refid="selectPurchaseCheckDetailsVo"/>
        where task_id = #{taskId}
    </select>
        
    <insert id="insertPurchaseCheckDetails" parameterType="com.bonus.sgzb.material.domain.PurchaseCheckDetails" useGeneratedKeys="true" keyProperty="taskId">
        insert into purchase_check_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="purchasePrice != null">purchase_price,</if>
            <if test="purchaseNum != null">purchase_num,</if>
            <if test="checkNum != null">check_num,</if>
            <if test="checkResult != null">check_result,</if>
            <if test="supplierId != null">supplier_id,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="productionTime != null">production_time,</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="checkUrlName != null">check_url_name,</if>
            <if test="checkUrl != null">check_url,</if>
            <if test="fileName != null">file_name,</if>
            <if test="fileUrl != null">file_url,</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="purchasePrice != null">#{purchasePrice},</if>
            <if test="purchaseNum != null">#{purchaseNum},</if>
            <if test="checkNum != null">#{checkNum},</if>
            <if test="checkResult != null">#{checkResult},</if>
            <if test="supplierId != null">#{supplierId},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="productionTime != null">#{productionTime},</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="checkUrlName != null">#{checkUrlName},</if>
            <if test="checkUrl != null">#{checkUrl},</if>
            <if test="fileName != null">#{fileName},</if>
            <if test="fileUrl != null">#{fileUrl},</if>
            <if test="companyId != null">#{companyId},</if>
        </trim>
    </insert>

    <update id="updatePurchaseCheckDetails" parameterType="com.bonus.sgzb.material.domain.PurchaseCheckDetails">
        update purchase_check_details
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="purchasePrice != null">purchase_price = #{purchasePrice},</if>
            <if test="purchaseNum != null">purchase_num = #{purchaseNum},</if>
            <if test="checkNum != null">check_num = #{checkNum},</if>
            <if test="bindNum != null">bind_num = #{bindNum},</if>
            <if test="checkResult != null">check_result = #{checkResult},</if>
            <if test="supplierId != null">supplier_id = #{supplierId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="productionTime != null">production_time = #{productionTime},</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="checkUrlName != null">check_url_name = #{checkUrlName},</if>
            <if test="checkUrl != null">check_url = #{checkUrl},</if>
            <if test="fileName != null">file_name = #{fileName},</if>
            <if test="fileUrl != null">file_url = #{fileUrl},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePurchaseCheckDetailsByTaskId" parameterType="Long">
        delete from purchase_check_details where id = #{id}
    </delete>

    <delete id="deletePurchaseCheckDetailsByTaskIds" parameterType="String">
        delete from purchase_check_details where task_id in 
        <foreach item="taskId" collection="array" open="(" separator="," close=")">
            #{taskId}
        </foreach>
    </delete>

    <insert id="insertPurchaseCheckDetailsList">
        <foreach item="checkDetails" collection="purchaseCheckDetailsList" open="" separator=";" close="">
            insert into purchase_check_details
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="checkDetails.taskId != null">task_id,</if>
                <if test="checkDetails.typeId != null">type_id,</if>
                <if test="checkDetails.purchasePrice != null">purchase_price,</if>
                <if test="checkDetails.purchaseNum != null">purchase_num,</if>
                <if test="checkDetails.checkNum != null">check_num,</if>
                <if test="checkDetails.checkResult != null">check_result,</if>
                <if test="checkDetails.supplierId != null">supplier_id,</if>
                <if test="checkDetails.status != null">status,</if>
                <if test="checkDetails.createBy != null">create_by,</if>
                <if test="checkDetails.productionTime != null">production_time,</if>
                <if test="checkDetails.createTime != null">create_time,</if>
                <if test="checkDetails.updateBy != null">update_by,</if>
                <if test="checkDetails.updateTime != null">update_time,</if>
                <if test="checkDetails.remark != null">remark,</if>
                <if test="checkDetails.checkUrlName != null">check_url_name,</if>
                <if test="checkDetails.checkUrl != null">check_url,</if>
                <if test="checkDetails.fileName != null">file_name,</if>
                <if test="checkDetails.fileUrl != null">file_url,</if>
                <if test="checkDetails.companyId != null">company_id,</if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="checkDetails.taskId != null">#{checkDetails.taskId},</if>
                <if test="checkDetails.typeId != null">#{checkDetails.typeId},</if>
                <if test="checkDetails.purchasePrice != null">#{checkDetails.purchasePrice},</if>
                <if test="checkDetails.purchaseNum != null">#{checkDetails.purchaseNum},</if>
                <if test="checkDetails.checkNum != null">#{checkDetails.checkNum},</if>
                <if test="checkDetails.checkResult != null">#{checkDetails.checkResult},</if>
                <if test="checkDetails.supplierId != null">#{checkDetails.supplierId},</if>
                <if test="checkDetails.status != null">#{checkDetails.status},</if>
                <if test="checkDetails.createBy != null">#{checkDetails.createBy},</if>
                <if test="checkDetails.productionTime != null">#{checkDetails.productionTime},</if>
                <if test="checkDetails.createTime != null">#{checkDetails.createTime},</if>
                <if test="checkDetails.updateBy != null">#{checkDetails.updateBy},</if>
                <if test="checkDetails.updateTime != null">#{checkDetails.updateTime},</if>
                <if test="checkDetails.remark != null">#{checkDetails.remark},</if>
                <if test="checkDetails.checkUrlName != null">#{checkDetails.checkUrlName},</if>
                <if test="checkDetails.checkUrl != null">#{checkDetails.checkUrl},</if>
                <if test="checkDetails.fileName != null">#{checkDetails.fileName},</if>
                <if test="checkDetails.fileUrl != null">#{checkDetails.fileUrl},</if>
                <if test="checkDetails.companyId != null">#{checkDetails.companyId},</if>
            </trim>
        </foreach>
    </insert>

    <update id="updateCheckDetailsByTaskId">
        update purchase_check_details set status = 0, update_time = NOW() where task_id = #{taskId}
    </update>

    <select id="selectPurchaseCheckDetailsStatus" resultType="int">
        select count(*) from purchase_check_details where task_id = #{taskId} and status != 3
    </select>
    <select id="selectInputNumByTypeId" resultType="java.lang.Integer">
        select IFNULL(input_num,0) from purchase_check_details where task_id = #{taskId} and type_id = #{typeId}
    </select>
    <select id="selectBindNumByTypeId" resultType="java.lang.Integer">
        select IFNULL(bind_num, 0) from purchase_check_details where task_id = #{taskId} and type_id = #{typeId}
    </select>
    <select id="isOperateAll" resultType="java.lang.Integer">
        SELECT
            count(*)
        FROM
            purchase_check_details
        WHERE
            task_id =  #{taskId} and status != 1 and status!=3
    </select>
    <select id="getAcceptanceForm" resultType="com.bonus.sgzb.material.domain.PurchaseCheckDetails">
        select pcd.id,pcd.task_id, pcd.type_id, pcd.purchase_price, pcd.purchase_num, pcd.check_num, pcd.check_result,
        pcd.supplier_id, pcd.status, msi.supplier, pcd.create_by, pcd.production_time, pcd.create_time,
        pcd.check_url_name,
        pcd.check_url, pcd.file_name, pcd.file_url,pcd.update_by, pcd.update_time, pcd.remark, pcd.company_id,
        mt1.type_name machineTypeName,mt.type_name specificationType, mt.manage_type manageType, tk.code ,mt.type_name
        typeName,
        mt.unit_name unitName
        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
        left join ma_supplier_info msi on pcd.supplier_id = msi.supplier_id
        left join tm_task tk on pcd.task_id = tk.task_id
        where 1=1
        and pcd.check_result='通过'
--         and pcd.status !=3
--         AND (
--         (pcd.bind_num IS NULL OR pcd.bind_num = 0) AND pcd.STATUS != 5
--         OR (pcd.bind_num IS NOT NULL AND pcd.bind_num != 0)
--         )
        <if test="taskId != null ">and pcd.task_id = #{taskId}</if>
        <if test="keyWord != null and keyWord != ''">and (mt.type_name like concat('%',#{keyWord},'%')
            or mt1.type_name like concat('%',#{keyWord}) or msi.supplier like concat('%',#{keyWord}))
        </if>
        <if test="typeId != null ">and pcd.type_id = #{typeId}</if>
        <if test="purchasePrice != null  and purchasePrice != ''">and pcd.purchase_price = #{purchasePrice}</if>
        <if test="purchaseNum != null ">and pcd.purchase_num = #{purchaseNum}</if>
        <if test="supplierId != null ">and pcd.supplier_id = #{supplierId}</if>
        <if test="productionTime != null ">and pcd.production_time = #{productionTime}</if>
        <if test="companyId != null ">and pcd.company_id = #{companyId}</if>
    </select>

    <delete id="deleteCheckDetailsByTaskId">
        delete from purchase_check_details where task_id = #{taskId}
    </delete>

    <update id="updateByTaskIdTypeId">
        update purchase_check_details set status = #{status},input_num=#{inputNum},input_status='1',input_time=NOW(), update_time = NOW() where task_id = #{taskId} and type_id = #{typeId}
    </update>
    <update id="updateCheckDetails">
        update purchase_check_details set status = '0' where task_id = #{taskId}
    </update>
    <update id="updateBindByTaskIdTypeId">
        update purchase_check_details set bind_num = #{bindNum}, update_time = NOW() where task_id = #{taskId} and type_id = #{typeId}
    </update>
    <update id="updatePurchaseCheckDetailsByTaskId">
        update purchase_check_details set bind_num = bind_num - 1 , status='5' where task_id = #{taskId} and type_id = #{typeId}
    </update>
    <update id="updateStatusByTaskIdTypeId">
        update purchase_check_details set status = #{status} where task_id = #{taskId} and type_id = #{typeId}
    </update>
    <update id="updateByTaskId">
        update purchase_check_details set bind_num = bind_num - 1 , status='7' where task_id = #{taskId} and type_id = #{typeId}
    </update>
    <update id="uploadPic">
        update purchase_check_details
        <trim prefix="SET" suffixOverrides=",">
            <if test="checkUrlName != null">check_url_name = #{checkUrlName},</if>
            <if test="checkUrl != null">check_url = #{checkUrl},</if>
        </trim>
        where id = #{id}
    </update>
</mapper>