<?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.material.purchase.mapper.PurchaseCheckInfoMapper">
    <resultMap type="com.bonus.material.purchase.domain.PurchaseCheckInfo" id="PurchaseCheckInfoResult">
        <result property="id"    column="id"    />
        <result property="taskId"    column="task_id"    />
        <result property="code"    column="code"    />
        <result property="purchaseTime"    column="purchase_time"    />
        <result property="arrivalTime"    column="arrival_time"    />
        <result property="purchaser"    column="purchaser"    />
        <result property="supplierId"    column="supplier_id"    />
        <result property="supplier"    column="supplier"    />
        <result property="taxRate"    column="tax_rate"    />
        <result property="createBy"    column="create_by"    />
        <result property="purchaseName"    column="purchase_name"    />
        <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"    />
        <result property="taskStatus"    column="task_status"    />
        <result property="checkUser"    column="checkUser"    />
        <result property="productionTime"    column="production_time"    />
    </resultMap>

    <resultMap type="com.bonus.material.purchase.domain.vo.PurchaseCheckFormVo" id="PurchaseCheckFormResult">
        <result property="taskId"    column="task_id"    />
        <result property="code"    column="code"    />
        <result property="arrivalDate"    column="arrival_time"    />
    </resultMap>

    <sql id="selectPurchaseCheckInfoBaseSQL">
        select
            pci.id, pci.task_id, pci.purchase_time, pci.arrival_time, pci.purchaser, pci.supplier_id, pci.tax_rate, pci.create_by,
            pci.create_time, pci.update_by, pci.update_time, pci.remark, pci.company_id, pcd.production_time
        from
            purchase_check_info pci
            left join purchase_check_details pcd ON pci.task_id = pcd.task_id
    </sql>

    <sql id="selectPurchaseCheckInfoJoinSQL">
        select
            pci.id, pci.task_id, pci.purchase_time, pci.arrival_time, pci.purchaser, pci.supplier_id, pci.tax_rate, pci.create_by,
            pci.create_time, pci.update_by, pci.update_time, pci.remark, pci.company_id,
            t.task_status, t.task_type, t.code, su.user_name as create_user_name, msi.supplier
        from
            purchase_check_info pci
        left join tm_task t on t.task_id = pci.task_id
        left join sys_user su ON pci.create_by = su.user_id
        left join ma_supplier_info msi on pci.supplier_id = msi.supplier_id
    </sql>

    <select id="selectPurchaseCheckInfoList" parameterType="com.bonus.material.purchase.domain.PurchaseCheckInfo" resultMap="PurchaseCheckInfoResult">
        <include refid="selectPurchaseCheckInfoBaseSQL"/>
        <where>
            <if test="taskId != null "> and task_id = #{taskId}</if>
            <if test="purchaseTime != null "> and purchase_time = #{purchaseTime}</if>
            <if test="arrivalTime != null "> and arrival_time = #{arrivalTime}</if>
            <if test="purchaser != null "> and purchaser = #{purchaser}</if>
            <if test="supplierId != null "> and supplier_id = #{supplierId}</if>
            <if test="taxRate != null "> and tax_rate = #{taxRate}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
        </where>
    </select>

    <select id="selectPurchaseCheckInfoById" parameterType="Long" resultMap="PurchaseCheckInfoResult">
        <include refid="selectPurchaseCheckInfoBaseSQL"/>
        where pci.id = #{id}
        LIMIT 1
    </select>

    <insert id="insertPurchaseCheckInfo" parameterType="com.bonus.material.purchase.domain.PurchaseCheckInfo" useGeneratedKeys="true" keyProperty="id">
        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="supplierId != null">supplier_id,</if>
            <if test="taxRate != null">tax_rate,</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">#{purchaseTime},</if>
            <if test="arrivalTime != null">#{arrivalTime},</if>
            <if test="purchaser != null">#{purchaser},</if>
            <if test="supplierId != null">#{supplierId},</if>
            <if test="taxRate != null">#{taxRate},</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.material.purchase.domain.PurchaseCheckInfo">
        update purchase_check_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskId != null">task_id = #{taskId},</if>
            <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="supplierId != null">supplier_id = #{supplierId},</if>
            <if test="taxRate != null">tax_rate = #{taxRate},</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 id = #{id}
    </update>

    <delete id="deletePurchaseCheckInfoById" parameterType="Long">
        delete from purchase_check_info where id = #{id}
    </delete>

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

    <select id="selectPurchaseCheckInfoJoinList" resultMap="PurchaseCheckInfoResult">
        <include refid="selectPurchaseCheckInfoJoinSQL"/>
        <where>
            <if test="code != null and code != ''"> and t.code = #{code}</if>
            <if test="taskId != null "> and pci.task_id = #{taskId}</if>
            <if test="purchaseTime != null "> and pci.purchase_time = #{purchaseTime}</if>
            <if test="startTime != null "> and date_format(pci.arrival_time,'%y%m%d') &gt;= date_format(#{startTime},'%y%m%d')</if>
            <if test="endTime != null "> and date_format(pci.arrival_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')</if>
            <if test="purchaser != null "> and pci.purchaser = #{purchaser}</if>
            <if test="supplierId != null "> and pci.supplier_id = #{supplierId}</if>
            <if test="taxRate != null "> and pci.tax_rate = #{taxRate}</if>
            <if test="taskStatus != null "> and t.task_status = #{taskStatus}</if>

        </where>
        order by pci.create_time desc
    </select>

    <select id="selectPurchaseCheckFormVoByTaskId" resultMap="PurchaseCheckFormResult">
        <include refid="selectPurchaseCheckInfoJoinSQL"/>
        <where>
            <if test="taskId != null "> and pci.task_id = #{taskId}</if>
        </where>
    </select>

    <select id="selectPurchaseCheckUserById" resultMap="PurchaseCheckInfoResult">

        select
            id, task_id as taskId, auditor as userId, su.nick_name AS checkUser,org_id as orgId
        from
            purchase_audit_record pur
                LEFT JOIN sys_user su on pur.auditor = su.user_id
        where
            task_id =#{taskId}
        GROUP BY auditor
    </select>
</mapper>
