<?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.ruoyi.invest.mapper.CreditCardBillMapper">

    <resultMap type="CreditCardBillVo" id="CreditCardBillResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="billDate"    column="bill_date"    />
        <result property="creditCardId"    column="credit_card_id"    />
        <result property="billDatePeriod"    column="bill_date_period"    />
        <result property="billAmount"    column="bill_amount"    />
        <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="delFlag"    column="del_flag"    />
        <result property="billState"    column="bill_state"    />
        <result property="bankName"    column="bank_name"    />
        <result property="bankCode"    column="bank_code"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectCreditCardBillVo">
        select
        a.id,
        a.name,
        a.bill_date,
        a.credit_card_id,
        a.bill_date_period,
        a.bill_amount,
        a.create_by,
        a.create_time,
        a.update_by,
        a.update_time,
        a.del_flag,
        a.bill_state,
        a.remark,
        bc."name" as bank_name,
        bc.code as bank_code
        from
        credit_card_bill a
        left join bank_card_lend bc on
        bc.id = a.credit_card_id
    </sql>

    <select id="selectCreditCardBillList" parameterType="CreditCardBillDto" resultMap="CreditCardBillResult">
        <include refid="selectCreditCardBillVo"/>
        <where>
            a.del_flag='0'
            <if test="name != null  and name != ''"> and a.name like '%'|| #{name}||'%'</if>
            <if test="creditCardId != null "> and a.credit_card_id = #{creditCardId}</if>
            <if test="billState != null and billState != ''"> and a.bill_state = #{billState}</if>
            <if test="startMonth!=null and startMonth !=''">
                and to_char(a.bill_date, 'yyyy-MM')>=#{startMonth}
            </if>
            <if test="endMonth!=null and endMonth !=''">
                and #{endMonth}>=to_char(a.bill_date, 'yyyy-MM')
            </if>
            <if test="billDate!=null and billDate !=''">
                and #{billDate}=to_char(a.bill_date, 'yyyy-MM-dd')
            </if>
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by a.bill_date desc
    </select>

    <select id="selectCreditCardBillById" parameterType="Long" resultMap="CreditCardBillResult">
        <include refid="selectCreditCardBillVo"/>
        where a.id = #{id}
    </select>

    <insert id="insertCreditCardBill" parameterType="CreditCardBill">
        insert into credit_card_bill
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="name != null">name,</if>
            <if test="billDate != null">bill_date,</if>
            <if test="creditCardId != null">credit_card_id,</if>
            <if test="billDatePeriod != null">bill_date_period,</if>
            <if test="billAmount != null">bill_amount,</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="delFlag != null">del_flag,</if>
            <if test="billState != null">bill_state,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="name != null">#{name},</if>
            <if test="billDate != null">#{billDate},</if>
            <if test="creditCardId != null">#{creditCardId},</if>
            <if test="billDatePeriod != null">#{billDatePeriod},</if>
            <if test="billAmount != null">#{billAmount},</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="delFlag != null">#{delFlag},</if>
            <if test="billState != null">#{billState},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateCreditCardBill" parameterType="CreditCardBill">
        update credit_card_bill
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="billDate != null">bill_date = #{billDate},</if>
            <if test="creditCardId != null">credit_card_id = #{creditCardId},</if>
            <if test="billDatePeriod != null">bill_date_period = #{billDatePeriod},</if>
            <if test="billAmount != null">bill_amount = #{billAmount},</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="delFlag != null">del_flag = #{delFlag},</if>
            <if test="billState != null">bill_state = #{billState},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCreditCardBillById" parameterType="Long">
        delete from credit_card_bill where id = #{id}
    </delete>

    <delete id="deleteCreditCardBillByIds" parameterType="String">
        delete from credit_card_bill where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <update id="removeCreditCardBillById" parameterType="Long">
         update credit_card_bill set del_flag='1' where id = #{id}
    </update>

    <update id="removeCreditCardBillByIds" parameterType="String">
        update credit_card_bill set del_flag='1' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>
