<?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="supplierId"    column="supplier_id"    />
        <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="companyId"    column="company_id"    />
    </resultMap>

    <sql id="selectPurchaseCheckDetailsVo">
        select id,task_id, type_id, purchase_price, purchase_num, supplier_id, create_by, production_time, create_time, update_by, update_time, remark, 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.supplier_id, msi.supplier, pcd.create_by, pcd.production_time, pcd.create_time,
        pcd.update_by, pcd.update_time, pcd.remark, pcd.company_id, mt1.type_name typeName,mt.type_name specificationType
        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
        <where>
            <if test="taskId != null "> and pcd.task_id = #{taskId}</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>
        </where>
    </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="typeId != null">type_id,</if>
            <if test="purchasePrice != null">purchase_price,</if>
            <if test="purchaseNum != null">purchase_num,</if>
            <if test="supplierId != null">supplier_id,</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="companyId != null">company_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="typeId != null">#{typeId},</if>
            <if test="purchasePrice != null">#{purchasePrice},</if>
            <if test="purchaseNum != null">#{purchaseNum},</if>
            <if test="supplierId != null">#{supplierId},</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="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="supplierId != null">supplier_id = #{supplierId},</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="companyId != null">company_id = #{companyId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePurchaseCheckDetailsByTaskId" parameterType="Long">
        delete from purchase_check_details where task_id = #{taskId}
    </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.supplierId != null">supplier_id,</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.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.supplierId != null">#{checkDetails.supplierId},</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.companyId != null">#{checkDetails.companyId},</if>
            </trim>
        </foreach>
    </insert>
</mapper>