<?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.payment.mapper.FinancialDetailMapper">

    <resultMap id="FinancialDetailResult" type="com.ghy.payment.domain.FinancialDetail">
        <id property="id"                        column="id"/>
        <result property="deptId"                column="dept_id"/>
        <result property="code"                  column="code"/>
        <result property="financialMasterId"     column="financial_master_id"/>
        <result property="financialMasterCode"   column="financial_master_code"/>
        <result property="orderDetailId"         column="order_detail_id"/>
        <result property="orderDetailCode"       column="order_detail_code"/>
        <result property="totalMoney"            column="total_money"/>
        <result property="discountMoney"         column="discount_money"/>
        <result property="payMoney"              column="pay_money"/>
        <result property="financialDetailType"   column="financial_detail_type"/>
        <result property="payeeId"               column="payee_id"/>
        <result property="reverseId"             column="reverse_id"/>
        <result property="payType"               column="pay_type"/>
        <result property="payStatus"             column="pay_status"/>
        <result property="payTime"               column="pay_time"/>
        <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>
    
    <resultMap id="countResult" type="com.ghy.payment.response.FinancialCountResponse">
        <result property="createTime"                column="create_time"/>
        <result property="payCount"                column="pay_count"/>
        <result property="incomeCount"                column="income_count"/>
    </resultMap>

    <sql id="selectFinancialDetail">
        SELECT id, dept_id, code, financial_master_id, financial_master_code, order_detail_id, order_detail_code,
               total_money, discount_money, pay_money, financial_detail_type,payee_id, reverse_id, pay_type,
               pay_status, pay_time, create_by, create_time, update_by, update_time, remark
        FROM financial_detail
    </sql>

    <select id="selectFinancialDetailList" parameterType="com.ghy.payment.domain.FinancialDetail"
            resultMap="FinancialDetailResult">
        <include refid="selectFinancialDetail"/>
        <where>
            <if test="financialMasterId != null and financialMasterId != 0">
                AND financial_master_id = #{financialMasterId}
            </if>
            <if test="financialMasterCode != null and financialMasterCode != ''">
                AND financial_master_code LIKE concat('%', #{financialMasterCode}, '%')
            </if>
            <if test="orderDetailId != null and orderDetailId != 0">
                AND order_detail_id = #{orderDetailId}
            </if>
            <if test="orderDetailCode != null and orderDetailCode != ''">
                AND order_detail_code LIKE concat('%', #{orderDetailCode}, '%')
            </if>
            <if test="payType != null">
                AND pay_type = #{payType}
            </if>
            <if test="payStatus != null">
                AND pay_status = #{payStatus}
            </if>
            <if test="financialDetailType != null">
                AND financial_detail_type = #{financialDetailType}
            </if>
            <if test="beginTime != null and beginTime != ''"><!-- 开始时间检索 -->
                and date_format(create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')
            </if>
            <if test="endTime != null and endTime != ''"><!-- 结束时间检索 -->
                and date_format(create_time,'%y%m%d') &lt; date_format(#{endTime},'%y%m%d')
            </if>
        </where>
    </select>

    <select id="selectById" parameterType="long" resultMap="FinancialDetailResult">
        <include refid="selectFinancialDetail"/> WHERE id = #{financialDetailId}
    </select>

    <select id="selectByFinancialMasterId" resultMap="FinancialDetailResult">
        <include refid="selectFinancialDetail"/> WHERE financial_master_id = #{financialMasterId}
    </select>

    <delete id="deleteFinancialDetailByIds" parameterType="Long">
        DELETE FROM financial_detail WHERE id IN
        <foreach collection="array" item="financialDetailId" open="(" separator="," close=")">
            #{financialDetailId}
        </foreach>
    </delete>

    <update id="updateFinancialDetail" parameterType="com.ghy.payment.domain.FinancialDetail">
        UPDATE financial_detail
        <set>
            <if test="code != null and code != ''">code = #{code},</if>
            <if test="totalMoney != null">total_money = #{totalMoney},</if>
            <if test="discountMoney != null">discount_money = #{discountMoney},</if>
            <if test="payType != null">pay_type = #{payType},</if>
            <if test="payStatus != null">pay_status = #{payStatus},</if>
            <if test="payTime != null">pay_time = #{payTime},</if>
            <if test="payMoney != null">pay_money = #{payMoney},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = SYSDATE()
        </set>
        WHERE id = #{id}
    </update>

    <update id="payReverseSucceeded">
        UPDATE financial_detail SET
        pay_status = 3
        WHERE reverse_id = #{reverseId}
    </update>

    <update id="updateByFinancialMasterId">
        UPDATE financial_detail
        <set>
            <if test="payType != null">pay_type = #{payType},</if>
            <if test="payStatus != null">pay_status = #{payStatus},</if>
            <if test="payTime != null">pay_time = #{payTime},</if>
            update_time = SYSDATE()
        </set>
        WHERE financial_master_id = #{financialMasterId}
    </update>

    <select id="selectByOrderDetailId" resultMap="FinancialDetailResult">
        <include refid="selectFinancialDetail" />
        WHERE
            order_detail_id = #{orderDetailId}
    </select>

    <select id="count" resultMap="countResult">
        select DATE_FORMAT(create_time,'%Y-%m') as create_time,
               sum(pay_money) as income_count from financial_detail
        <where>
            <if test="createTime != null">
                and DATE_FORMAT(create_time,'%Y-%m') = #{createTime}
            </if>
            <if test="flag == 'true'">
                and pay_money &gt;= 0
            </if>
            <if test="flag == 'false'">
                and pay_money &lt;= 0
            </if>
        </where>
        group by DATE_FORMAT(create_time,'%Y-%m') order by create_time desc
    </select>
    
    <insert id="insertFinancialDetail" parameterType="com.ghy.payment.domain.FinancialDetail" useGeneratedKeys="true"
            keyProperty="id">
        INSERT INTO financial_detail(
        <if test="deptId != null and deptId != 0">dept_id,</if>
        <if test="code != null and code != ''">code,</if>
        <if test="financialMasterId != null and financialMasterId != 0">financial_master_id,</if>
        <if test="financialMasterCode != null and financialMasterCode != ''">financial_master_code,</if>
        <if test="orderDetailId != null and orderDetailId != 0">order_detail_id,</if>
        <if test="orderDetailCode != null and orderDetailCode != ''">order_detail_code,</if>
        <if test="totalMoney != null">total_money,</if>
        <if test="discountMoney != null">discount_money,</if>
        <if test="payMoney != null">pay_money,</if>
        <if test="financialDetailType != null">financial_detail_type,</if>
        <if test="payeeId != null and payeeId != 0">payee_id,</if>
        <if test="reverseId != null and reverseId != 0">reverse_id,</if>
        <if test="payType != null">pay_type,</if>
        <if test="payStatus != null">pay_status,</if>
        <if test="payTime != null">pay_time,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        create_time
        )VALUES(
        <if test="deptId != null and deptId != 0">#{deptId},</if>
        <if test="code != null and code != ''">#{code},</if>
        <if test="financialMasterId != null and financialMasterId != 0">#{financialMasterId},</if>
        <if test="financialMasterCode != null and financialMasterCode != ''">#{financialMasterCode},</if>
        <if test="orderDetailId != null and orderDetailId != 0">#{orderDetailId},</if>
        <if test="orderDetailCode != null and orderDetailCode != ''">#{orderDetailCode},</if>
        <if test="totalMoney != null">#{totalMoney},</if>
        <if test="discountMoney != null">#{discountMoney},</if>
        <if test="payMoney != null">#{payMoney},</if>
        <if test="financialDetailType != null">#{financialDetailType},</if>
        <if test="payeeId != null and payeeId != 0">#{payeeId},</if>
        <if test="reverseId != null and reverseId != 0">#{reverseId},</if>
        <if test="payType != null">#{payType},</if>
        <if test="payStatus != null">#{payStatus},</if>
        <if test="payTime != null">#{payTime},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        SYSDATE()
        )
    </insert>

    <select id="checkFinancialDetailCodeUnique" parameterType="String" resultMap="FinancialDetailResult">
        <include refid="selectFinancialDetail"/>
        WHERE `code` = #{financialDetailCode} LIMIT 1
    </select>

    <select id="getByOrderIdList" resultMap="FinancialDetailResult">
        <include refid="selectFinancialDetail" />
        <where>
            and order_detail_id in
            <foreach item="item" index="orderIdList" collection="orderIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </where>
    </select>

</mapper>