<?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.fs.qw.mapper.CustomerTransferApprovalMapper">

    <resultMap type="CustomerTransferApproval" id="CustomerTransferApprovalResult">
        <result property="id"    column="id"    />
        <result property="corpId"    column="corp_id"    />
        <result property="transferType"    column="transfer_type"    />
        <result property="customerIds"    column="customer_ids"    />
        <result property="originalUserId"    column="original_user_id"    />
        <result property="targetUserId"    column="target_user_id"    />
        <result property="initiatorUserId"    column="initiator_user_id"    />
        <result property="content"    column="content"    />
        <result property="approvalStatus"    column="approval_status"    />
        <result property="approverUserId"    column="approver_user_id"    />
        <result property="approvalRemark"    column="approval_remark"    />
        <result property="processedAt"    column="processed_at"    />
        <result property="createdAt"    column="created_at"    />
        <result property="updatedAt"    column="updated_at"    />
        <result property="transferBefore"    column="transfer_before"    />
    </resultMap>

    <sql id="selectCustomerTransferApprovalVo">
        select id, corp_id, transfer_type, customer_ids, original_user_id, target_user_id, initiator_user_id, content, approval_status, approver_user_id, approval_remark, processed_at, created_at, updated_at,transfer_before from customer_transfer_approval
    </sql>

    <select id="selectCustomerTransferApprovalList" parameterType="CustomerTransferApproval" resultMap="CustomerTransferApprovalResult">
        <include refid="selectCustomerTransferApprovalVo"/>
        <where>
            <if test="corpId != null  and corpId != ''"> and corp_id = #{corpId}</if>
            <if test="transferType != null "> and transfer_type = #{transferType}</if>
            <if test="customerIds != null  and customerIds != ''"> and customer_ids = #{customerIds}</if>
            <if test="originalUserId != null "> and original_user_id = #{originalUserId}</if>
            <if test="targetUserId != null "> and target_user_id = #{targetUserId}</if>
            <if test="initiatorUserId != null "> and initiator_user_id = #{initiatorUserId}</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="approvalStatus != null "> and approval_status = #{approvalStatus}</if>
            <if test="approverUserId != null "> and approver_user_id = #{approverUserId}</if>
            <if test="approvalRemark != null  and approvalRemark != ''"> and approval_remark = #{approvalRemark}</if>
            <if test="processedAt != null "> and processed_at = #{processedAt}</if>
            <if test="createdAt != null "> and created_at = #{createdAt}</if>
            <if test="updatedAt != null "> and updated_at = #{updatedAt}</if>
        </where>
        order by id desc
    </select>

    <select id="selectCustomerTransferApprovalById" parameterType="Long" resultMap="CustomerTransferApprovalResult">
        <include refid="selectCustomerTransferApprovalVo"/>
        where id = #{id}
    </select>

    <insert id="insertCustomerTransferApproval" parameterType="CustomerTransferApproval" useGeneratedKeys="true" keyProperty="id">
        insert into customer_transfer_approval
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="corpId != null and corpId != ''">corp_id,</if>
            <if test="transferType != null">transfer_type,</if>
            <if test="customerIds != null and customerIds != ''">customer_ids,</if>
            <if test="originalUserId != null">original_user_id,</if>
            <if test="targetUserId != null">target_user_id,</if>
            <if test="initiatorUserId != null">initiator_user_id,</if>
            <if test="content != null">content,</if>
            <if test="approvalStatus != null">approval_status,</if>
            <if test="approverUserId != null">approver_user_id,</if>
            <if test="approvalRemark != null">approval_remark,</if>
            <if test="processedAt != null">processed_at,</if>
            <if test="createdAt != null">created_at,</if>
            <if test="updatedAt != null">updated_at,</if>
            <if test="transferBefore != null">transfer_before,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="corpId != null and corpId != ''">#{corpId},</if>
            <if test="transferType != null">#{transferType},</if>
            <if test="customerIds != null and customerIds != ''">#{customerIds},</if>
            <if test="originalUserId != null">#{originalUserId},</if>
            <if test="targetUserId != null">#{targetUserId},</if>
            <if test="initiatorUserId != null">#{initiatorUserId},</if>
            <if test="content != null">#{content},</if>
            <if test="approvalStatus != null">#{approvalStatus},</if>
            <if test="approverUserId != null">#{approverUserId},</if>
            <if test="approvalRemark != null">#{approvalRemark},</if>
            <if test="processedAt != null">#{processedAt},</if>
            <if test="createdAt != null">#{createdAt},</if>
            <if test="updatedAt != null">#{updatedAt},</if>
            <if test="transferBefore != null">#{transferBefore},</if>
        </trim>
    </insert>

    <update id="updateCustomerTransferApproval" parameterType="CustomerTransferApproval">
        update customer_transfer_approval
        <trim prefix="SET" suffixOverrides=",">
            <if test="corpId != null and corpId != ''">corp_id = #{corpId},</if>
            <if test="transferType != null">transfer_type = #{transferType},</if>
            <if test="customerIds != null and customerIds != ''">customer_ids = #{customerIds},</if>
            <if test="originalUserId != null">original_user_id = #{originalUserId},</if>
            <if test="targetUserId != null">target_user_id = #{targetUserId},</if>
            <if test="initiatorUserId != null">initiator_user_id = #{initiatorUserId},</if>
            <if test="content != null">content = #{content},</if>
            <if test="approvalStatus != null">approval_status = #{approvalStatus},</if>
            <if test="approverUserId != null">approver_user_id = #{approverUserId},</if>
            <if test="approvalRemark != null">approval_remark = #{approvalRemark},</if>
            <if test="processedAt != null">processed_at = #{processedAt},</if>
            <if test="createdAt != null">created_at = #{createdAt},</if>
            <if test="updatedAt != null">updated_at = #{updatedAt},</if>
            <if test="transferBefore != null">transfer_before = #{transferBefore},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCustomerTransferApprovalById" parameterType="Long">
        delete from customer_transfer_approval where id = #{id}
    </delete>

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

</mapper>
