<?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.PurchaseCheckInfoMapper">
    
    <resultMap type="com.bonus.sgzb.material.domain.PurchaseCheckInfo" id="PurchaseCheckInfoResult">
        <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 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.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
        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
        <where>  
            <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>
        </where>
    </select>
    
    <select id="selectPurchaseCheckInfoByTaskId" parameterType="Long" resultMap="PurchaseCheckInfoResult">
        <include refid="selectPurchaseCheckInfoVo"/>
        where 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 and purchaseTime != ''">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 and purchaseTime != ''">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>

    <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>

    <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}
                    ) 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>
</mapper>