<?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.aegis.resource.procurement.mapper.ResourceProcurementMapper">

    <resultMap type="Procurement" id="ProcurementResult">
        <id     property="id"                  column="id"                   />
        <result property="procurementNo"       column="procurement_no"        />
        <result property="title"               column="title"                 />
        <result property="urgency"             column="urgency"               />
        <result property="status"              column="status"                />
        <result property="awardedSupplierId"    column="awarded_supplier_id"   />
        <result property="awardedSupplierName"  column="awarded_supplier_name" />
        <result property="awardedAt"           column="awarded_at"            />
        <result property="approverId"          column="approver_id"            />
        <result property="approverName"        column="approver_name"         />
        <result property="approvedAt"          column="approved_at"            />
        <result property="approvalComment"     column="approval_comment"       />
        <result property="delFlag"             column="del_flag"              />
        <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"                 />
    </resultMap>

    <sql id="selectProcurementVo">
        select p.id, p.procurement_no, p.title, p.urgency, p.status, p.awarded_supplier_id, p.awarded_at,
               p.approver_id, p.approver_name, p.approved_at, p.approval_comment, p.del_flag, p.create_by,
               p.create_time, p.update_by, p.update_time, p.remark,
               s.supplier_name as awarded_supplier_name
        from resource_procurement p
        left join resource_supplier s on p.awarded_supplier_id = s.id and s.del_flag = '0'
    </sql>

    <select id="selectProcurementList" parameterType="Procurement" resultMap="ProcurementResult">
        <include refid="selectProcurementVo"/>
        <where>
            p.del_flag = '0'
            <if test="procurementNo != null and procurementNo != ''">
                AND p.procurement_no like concat('%', #{procurementNo}, '%')
            </if>
            <if test="title != null and title != ''">
                AND p.title like concat('%', #{title}, '%')
            </if>
            <if test="urgency != null and urgency != ''">
                AND p.urgency = #{urgency}
            </if>
            <if test="status != null and status != ''">
                AND p.status = #{status}
            </if>
            <if test="awardedSupplierId != null">
                AND p.awarded_supplier_id = #{awardedSupplierId}
            </if>
            <if test="params.beginTime != null and params.beginTime != ''">
                AND date_format(p.create_time,'%Y%m%d') &gt;= date_format(#{params.beginTime},'%Y%m%d')
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                AND date_format(p.create_time,'%Y%m%d') &lt;= date_format(#{params.endTime},'%Y%m%d')
            </if>
        </where>
        order by p.create_time desc
    </select>

    <select id="selectProcurementById" parameterType="Long" resultMap="ProcurementResult">
        <include refid="selectProcurementVo"/>
        where p.id = #{id} and p.del_flag = '0'
    </select>

    <select id="selectProcurementByNo" parameterType="String" resultMap="ProcurementResult">
        <include refid="selectProcurementVo"/>
        where p.procurement_no = #{procurementNo} and p.del_flag = '0'
    </select>

    <insert id="insertProcurement" parameterType="Procurement" useGeneratedKeys="true" keyProperty="id">
        insert into resource_procurement(
            <if test="procurementNo != null and procurementNo != ''">procurement_no,</if>
            <if test="title != null and title != ''">title,</if>
            <if test="urgency != null and urgency != ''">urgency,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="awardedSupplierId != null">awarded_supplier_id,</if>
            <if test="awardedAt != null">awarded_at,</if>
            <if test="approverId != null">approver_id,</if>
            <if test="approverName != null">approver_name,</if>
            <if test="approvedAt != null">approved_at,</if>
            <if test="approvalComment != null">approval_comment,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            create_time
        )values(
            <if test="procurementNo != null and procurementNo != ''">#{procurementNo},</if>
            <if test="title != null and title != ''">#{title},</if>
            <if test="urgency != null and urgency != ''">#{urgency},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="awardedSupplierId != null">#{awardedSupplierId},</if>
            <if test="awardedAt != null">#{awardedAt},</if>
            <if test="approverId != null">#{approverId},</if>
            <if test="approverName != null">#{approverName},</if>
            <if test="approvedAt != null">#{approvedAt},</if>
            <if test="approvalComment != null">#{approvalComment},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            sysdate()
        )
    </insert>

    <update id="updateProcurement" parameterType="Procurement">
        update resource_procurement
        <set>
            <if test="title != null and title != ''">title = #{title},</if>
            <if test="urgency != null and urgency != ''">urgency = #{urgency},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="awardedSupplierId != null">awarded_supplier_id = #{awardedSupplierId},</if>
            <if test="awardedAt != null">awarded_at = #{awardedAt},</if>
            <if test="approverId != null">approver_id = #{approverId},</if>
            <if test="approverName != null">approver_name = #{approverName},</if>
            <if test="approvedAt != null">approved_at = #{approvedAt},</if>
            <if test="approvalComment != null">approval_comment = #{approvalComment},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate()
        </set>
        where id = #{id}
    </update>

    <update id="deleteProcurementById" parameterType="Long">
        update resource_procurement set del_flag = '2' where id = #{id}
    </update>

    <update id="deleteProcurementByIds" parameterType="Long">
        update resource_procurement set del_flag = '2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="checkProcurementNoUnique" resultMap="ProcurementResult">
        <include refid="selectProcurementVo"/>
        where p.procurement_no = #{procurementNo} and p.del_flag = '0'
        <if test="id != null">
            AND p.id != #{id}
        </if>
        limit 1
    </select>

    <select id="selectDeletedProcurementByNo" parameterType="String" resultMap="ProcurementResult">
        <include refid="selectProcurementVo"/>
        where p.procurement_no = #{procurementNo} and p.del_flag = '2'
        limit 1
    </select>

    <update id="restoreProcurement" parameterType="Procurement">
        update resource_procurement
        <set>
            <if test="procurementNo != null and procurementNo != ''">procurement_no = #{procurementNo},</if>
            <if test="title != null and title != ''">title = #{title},</if>
            <if test="urgency != null and urgency != ''">urgency = #{urgency},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="awardedSupplierId != null">awarded_supplier_id = #{awardedSupplierId},</if>
            <if test="awardedAt != null">awarded_at = #{awardedAt},</if>
            <if test="approverId != null">approver_id = #{approverId},</if>
            <if test="approverName != null">approver_name = #{approverName},</if>
            <if test="approvedAt != null">approved_at = #{approvedAt},</if>
            <if test="approvalComment != null">approval_comment = #{approvalComment},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            del_flag = '0',
            update_time = sysdate()
        </set>
        where id = #{id}
    </update>

</mapper>