<?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.requisition.mapper.ResourceRequisitionMapper">

    <resultMap type="Requisition" id="RequisitionResult">
        <id     property="id"                  column="id"                   />
        <result property="teamId"              column="team_id"              />
        <result property="teamName"            column="team_name"            />
        <result property="requisitionNo"       column="requisition_no"        />
        <result property="reason"              column="reason"                />
        <result property="status"              column="status"                />
        <result property="startAt"             column="start_at"              />
        <result property="endAt"               column="end_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="selectRequisitionVo">
        select r.id, r.team_id, r.requisition_no, r.reason, r.status, r.start_at, r.end_at,
               r.approver_id, r.approver_name, r.approved_at, r.approval_comment, r.del_flag,
               r.create_by, r.create_time, r.update_by, r.update_time, r.remark,
               t.team_name
        from resource_requisition r
        left join resource_team t on r.team_id = t.id and t.del_flag = '0'
    </sql>

    <select id="selectRequisitionList" parameterType="Requisition" resultMap="RequisitionResult">
        <include refid="selectRequisitionVo"/>
        <where>
            r.del_flag = '0'
            <if test="requisitionNo != null and requisitionNo != ''">
                AND r.requisition_no like concat('%', #{requisitionNo}, '%')
            </if>
            <if test="teamId != null">
                AND r.team_id = #{teamId}
            </if>
            <if test="status != null and status != ''">
                AND r.status = #{status}
            </if>
            <if test="reason != null and reason != ''">
                AND r.reason like concat('%', #{reason}, '%')
            </if>
            <if test="params.beginTime != null and params.beginTime != ''">
                AND date_format(r.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(r.create_time,'%Y%m%d') &lt;= date_format(#{params.endTime},'%Y%m%d')
            </if>
        </where>
        order by r.create_time desc
    </select>

    <select id="selectRequisitionById" parameterType="Long" resultMap="RequisitionResult">
        <include refid="selectRequisitionVo"/>
        where r.id = #{id} and r.del_flag = '0'
    </select>

    <select id="selectRequisitionListByTeamId" parameterType="Long" resultMap="RequisitionResult">
        <include refid="selectRequisitionVo"/>
        where r.team_id = #{teamId} and r.del_flag = '0'
        order by r.create_time desc
    </select>

    <insert id="insertRequisition" parameterType="Requisition" useGeneratedKeys="true" keyProperty="id">
        insert into resource_requisition(
            <if test="teamId != null">team_id,</if>
            <if test="requisitionNo != null and requisitionNo != ''">requisition_no,</if>
            <if test="reason != null">reason,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="startAt != null">start_at,</if>
            <if test="endAt != null">end_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="teamId != null">#{teamId},</if>
            <if test="requisitionNo != null and requisitionNo != ''">#{requisitionNo},</if>
            <if test="reason != null">#{reason},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="startAt != null">#{startAt},</if>
            <if test="endAt != null">#{endAt},</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="updateRequisition" parameterType="Requisition">
        update resource_requisition
        <set>
            <if test="reason != null">reason = #{reason},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="startAt != null">start_at = #{startAt},</if>
            <if test="endAt != null">end_at = #{endAt},</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="deleteRequisitionById" parameterType="Long">
        update resource_requisition set del_flag = '2' where id = #{id}
    </update>

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

    <select id="checkRequisitionNoUnique" resultMap="RequisitionResult">
        <include refid="selectRequisitionVo"/>
        where r.requisition_no = #{requisitionNo} and r.del_flag = '0'
        <if test="id != null">
            AND r.id != #{id}
        </if>
        limit 1
    </select>

    <select id="selectDeletedRequisitionByNo" parameterType="String" resultMap="RequisitionResult">
        <include refid="selectRequisitionVo"/>
        where r.requisition_no = #{requisitionNo} and r.del_flag = '2'
        limit 1
    </select>

    <update id="restoreRequisition" parameterType="Requisition">
        update resource_requisition
        <set>
            <if test="teamId != null">team_id = #{teamId},</if>
            <if test="requisitionNo != null and requisitionNo != ''">requisition_no = #{requisitionNo},</if>
            <if test="reason != null">reason = #{reason},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="startAt != null">start_at = #{startAt},</if>
            <if test="endAt != null">end_at = #{endAt},</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>