<?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.ghy.customer.mapper.CustomerBankMapper">

    <resultMap id="CustomerBankResult" type="com.ghy.customer.domain.CustomerBank">
        <result property="customerBankId"       column="customer_bank_id"/>
        <result property="settleAccountId"       column="settle_account_id"/>
        <result property="customerId"           column="customer_id"/>
        <result property="name"                 column="name"/>
        <result property="certId"               column="cert_id"/>
        <result property="bankName"             column="bank_name"/>
        <result property="bankNum"              column="bank_num"/>
        <result property="phone"                column="phone"/>
        <result property="deptId"               column="dept_id"/>
        <result property="adapayMemberId"       column="adapay_member_id"/>
        <result property="settleAccount"        column="settle_account"/>
        <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="selectCustomerBank">
        SELECT customer_bank_id, settle_account_id, customer_id, name, cert_id, bank_name, bank_num, phone, dept_id,
               adapay_member_id, settle_account, create_by, create_time, update_by, update_time, remark
        FROM customer_bank
    </sql>

    <update id="updateCustomerBank" parameterType="com.ghy.customer.domain.CustomerBank">
        UPDATE customer_bank
        <set>
            <if test="customerId != null and customerId != 0">customer_id = #{customerId},</if>
            <if test="name != null and name != ''">`name` = #{name},</if>
            <if test="bankName != null and bankName != ''">bank_name = #{bankName},</if>
            <if test="bankNum != null and bankNum != ''">bank_num = #{bankNum},</if>
            <if test="phone != null and phone != ''">phone = #{phone},</if>
            <if test="deptId != null and deptId != 0">dept_id = #{deptId},</if>
            <if test="settleAccount != null">settle_account = #{settleAccount},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate()
        </set>
        WHERE customer_bank_id = #{customerBankId}
    </update>

    <insert id="insertCustomerBank" parameterType="com.ghy.customer.domain.CustomerBank" useGeneratedKeys="true"
            keyProperty="customerBankId">
        INSERT INTO customer_bank( settle_account_id,
        <if test="customerId != null and customerId != 0">customer_id,</if>
        <if test="name != null and name != ''">name,</if>
        <if test="certId != null and certId != ''">cert_id,</if>
        <if test="bankName != null and bankName != ''">bank_name,</if>
        <if test="bankNum != null and bankNum != ''">bank_num,</if>
        <if test="phone != null and phone != ''">phone,</if>
        <if test="deptId != null and deptId != 0">dept_id,</if>
        <if test="adapayMemberId != null and adapayMemberId != ''">adapay_member_id,</if>
        <if test="settleAccount != null">settle_account,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        <if test="remark != null and remark != ''">remark,</if>
        create_time
        )VALUES( #{settleAccountId},
        <if test="customerId != null and customerId != 0">#{customerId},</if>
        <if test="name != null and name != ''">#{name},</if>
        <if test="certId != null and certId != ''">#{certId},</if>
        <if test="bankName != null and bankName != ''">#{bankName},</if>
        <if test="bankNum != null and bankNum != ''">#{bankNum},</if>
        <if test="phone != null and phone != ''">#{phone},</if>
        <if test="deptId != null and deptId != 0">#{deptId},</if>
        <if test="adapayMemberId != null and adapayMemberId != ''">#{adapayMemberId},</if>
        <if test="settleAccount != null">#{settleAccount},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        <if test="remark != null and remark != ''">#{remark},</if>
        sysdate()
        )
    </insert>

    <select id="getCustomerBankList" resultMap="CustomerBankResult">
        <include refid="selectCustomerBank"/>
        <where>
            <if test="customerId != null and customerId != ''">
                AND customer_id = #{customerId}
            </if>
            <if test="deptId != null and deptId != ''">
                AND dept_id = #{deptId}
            </if>
            <if test="adapayMemberId != null and adapayMemberId != ''">
                AND adapay_member_id = #{adapayMemberId}
            </if>
            <if test="settleAccount != null and settleAccount != ''">
                AND settle_account = #{settleAccount}
            </if>
        </where>
    </select>

    <delete id="deleteByIds">
        DELETE FROM customer_bank WHERE customer_bank_id IN
        <foreach collection="array" item="customerBankId" open="(" separator="," close=")">
            #{customerBankId}
        </foreach>
    </delete>

    <delete id="deleteByCustomerBankId" parameterType="Long">
        DELETE
        FROM customer_bank
        WHERE customer_bank_id = #{customerBankId}
    </delete>

    <select id="selectByCustomerIdAndDeptId" resultMap="CustomerBankResult">
        <include refid="selectCustomerBank"/>
        WHERE customer_id = #{customerId} AND dept_id = #{deptId}
    </select>

    <select id="selectByCustomerId" resultMap="CustomerBankResult">
        <include refid="selectCustomerBank"/>
        WHERE customer_id = #{customerId}
    </select>

    <select id="selectByCustomerBankId" resultMap="CustomerBankResult">
        <include refid="selectCustomerBank"/> WHERE customer_bank_id = #{customerBankId}
    </select>

    <select id="getByMemberId" resultMap="CustomerBankResult">
        <include refid="selectCustomerBank"/>
        WHERE adapay_member_id = #{adapayMemberId}
    </select>

    <update id="updateByMemberId" parameterType="com.ghy.customer.domain.CustomerBank">
        UPDATE customer_bank
        <set>
            <if test="phone != null and phone != ''">phone = #{phone},</if>
            <if test="bankNum != null and bankNum != ''">bank_num  = #{bankNum},</if>
            <if test="settleAccountId != null and settleAccountId != ''">settle_account_id  = #{settleAccountId},</if>
            update_time = NOW()
        </set>
        WHERE adapay_member_id = #{adapayMemberId}
    </update>
</mapper>
