<?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.njzscloud.dispose.finance.mapper.MoneyAccountMapper">
    <resultMap id="BaseResultMap" type="com.njzscloud.dispose.finance.pojo.result.MoneyAccountResult">
        <id column="id" property="id"/>
        <result column="sn" property="sn"/>
        <result column="account_name" property="accountName"/>
        <result column="account_type" property="accountType"/>
        <result column="user_id" property="userId"/>
        <result column="customer_id" property="customerId"/>
        <result column="org_id" property="orgId"/>
        <result column="revenue" property="revenue"/>
        <result column="recharge" property="recharge"/>
        <result column="creator_id" property="creatorId"/>
        <result column="modifier_id" property="modifierId"/>
        <result column="create_time" property="createTime"/>
        <result column="modify_time" property="modifyTime"/>
        <result column="deleted" property="deleted"/>
    </resultMap>

    <sql id="SearchMoneyAccountResultSql">
        SELECT fma.id,
               fma.sn,
               fma.account_type,
               fma.user_id,
               CASE fma.account_type
                   WHEN 'GeRen' THEN cc.customer_name
                   WHEN 'QiYe' THEN co.org_name
                   ELSE NULL
               END as account_name,
               fma.customer_id,
               fma.org_id,
               fma.revenue,
               fma.recharge,
               fma.creator_id,
               fma.modifier_id,
               fma.create_time,
               fma.modify_time,
               fma.deleted
        FROM fin_money_account fma
        LEFT JOIN cst_customer cc ON cc.id = fma.customer_id
        LEFT JOIN cst_org co ON co.id = fma.org_id
    </sql>

    <select id="paging" resultMap="BaseResultMap">
        <include refid="SearchMoneyAccountResultSql"/>
        <if test="ew.customSqlSegment != null and ew.customSqlSegment != ''">
            ${ew.customSqlSegment}
        </if>
    </select>

    <select id="detail" resultMap="BaseResultMap">
        <include refid="SearchMoneyAccountResultSql"/>
        WHERE fma.id = #{id}
    </select>
</mapper>
