<?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.ym.mec.biz.dal.dao.SysPaymentConfigDao">
    <resultMap id="SysPaymentConfig" type="com.ym.mec.biz.dal.entity.SysPaymentConfig">
        <!--@Table sys_payment_config-->
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="organ_id_" property="organId"/>
        <result column="account_type_" property="accountType"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="pay_type_" property="payType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="yq_mer_no_" property="yqMerNo"/>
        <result column="hf_mer_no_" property="hfMerNo"/>
        <result column="type_" property="type"/>
        <result column="route_scale_" property="routeScale"/>
        <result column="type_route_scale_" property="typeRouteScale"/>
        <result column="organ_name" property="organName"/>
        <result column="per_scale_" property="perScale"/>
        <result column="com_scale_" property="comScale"/>
        <result column="course_fee_" property="courseFee"/>
        <result column="instrument_fee_" property="instrumentFee"/>
        <result column="accessories_fee_" property="accessoriesFee"/>
        <result column="other_fee_" property="otherFee"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id_, organ_id_,account_type_,pay_type_,yq_mer_no_,hf_mer_no_,type_,route_scale_,type_route_scale_,create_time_,
        update_time_
    </sql>
    <select id="get" parameterType="java.lang.Integer" resultMap="SysPaymentConfig">
        <!--@mbg.generated-->
        select
        <include refid="Base_Column_List"/>
        from sys_payment_config
        where id_ = #{id,jdbcType=INTEGER}
    </select>
    <delete id="delete" parameterType="java.lang.Integer">
        delete
        from sys_payment_config
        where id_ = #{id,jdbcType=INTEGER}
    </delete>
    <insert id="insert" keyColumn="id_" keyProperty="id" parameterType="com.ym.mec.biz.dal.entity.SysPaymentConfig"
            useGeneratedKeys="true">
        <!--@mbg.generated-->
        insert into sys_payment_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="organId != null">
                organ_id_,
            </if>
            <if test="accountType != null">
                account_type_,
            </if>
            <if test="payType != null">
                pay_type_,
            </if>
            <if test="yqMerNo != null">
                yq_mer_no_,
            </if>
            <if test="hfMerNo != null">
                hf_mer_no_,
            </if>
            <if test="routeScale != null">
                route_scale_,
            </if>
            <if test="typeRouteScale != null">
                type_route_scale_,
            </if>
            <if test="type != null">
                type_,
            </if>
            <if test="createTime != null">
                create_time_,
            </if>
            <if test="updateTime != null">
                update_time_,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="organId != null">
                #{organId},
            </if>
            <if test="accountType != null">
                #{accountType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="payType != null">
                #{payType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="yqMerNo != null">
                #{yqMerNo},
            </if>
            <if test="hfMerNo != null">
                #{hfMerNo},
            </if>
            <if test="routeScale != null">
                #{routeScale},
            </if>
            <if test="typeRouteScale != null">
                #{typeRouteScale},
            </if>
            <if test="type != null">
                #{type},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>
        </trim>
    </insert>
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.SysPaymentConfig">
        <!--@mbg.generated-->
        update sys_payment_config
        <set>
            <if test="organId != null">
                organ_id_ = #{organId},
            </if>
            <if test="accountType != null">
                account_type_ = #{accountType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="payType != null">
                pay_type_ = #{payType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="yqMerNo != null">
                yq_mer_no_ = #{yqMerNo},
            </if>
            <if test="hfMerNo != null">
                hf_mer_no_ = #{hfMerNo},
            </if>
            <if test="routeScale != null">
                route_scale_ = #{routeScale},
            </if>
            <if test="typeRouteScale != null">
                type_route_scale_ = #{typeRouteScale},
            </if>
            <if test="type != null">
                type_ = #{type},
            </if>
            update_time_ = now()
        </set>
        where id_ = #{id,jdbcType=INTEGER}
    </update>

    <!-- 全查询 -->
    <select id="findAll" resultMap="SysPaymentConfig">
        SELECT *
        FROM sys_payment_config
        ORDER BY id_
    </select>

    <sql id="queryCondition">
        <where>
            <if test="organId!=null">
                AND spc.organ_id_=#{organId}
            </if>
            <if test='hasTypeRoute != null and hasTypeRoute.toString()=="1".toString()'>
                AND spc.type_route_scale_ != ''
            </if>
            <if test='hasTypeRoute != null and hasTypeRoute.toString()=="0".toString()'>
                AND spc.type_route_scale_ = ''
            </if>
            <if test='hasRouteScale != null and hasRouteScale.toString()=="1".toString()'>
                AND (spc.hf_mer_no_ !='' || spc.route_scale_ != '')
            </if>
        </where>
    </sql>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="SysPaymentConfig" parameterType="map">
        SELECT spc.*,o.name_ organ_name FROM sys_payment_config spc LEFT JOIN organization o on spc.organ_id_ = o.id_
        <include refid="queryCondition"/>
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*) FROM sys_payment_config spc
        <include refid="queryCondition"/>
        <include refid="global.limit"/>
    </select>

    <!-- 按分部编号查配置 -->
    <select id="findPaymentConfigByOrganId" resultMap="SysPaymentConfig">
        SELECT *
        FROM sys_payment_config
        WHERE organ_id_ = #{organId}
    </select>
    <select id="findPaymentConfigByOrganIds" resultMap="SysPaymentConfig">
        SELECT *
        FROM sys_payment_config
        WHERE organ_id_ IN
        <foreach collection="organIds" item="organId" open="(" close=")" separator=",">
            #{organId}
        </foreach>
    </select>

    <!-- 根据支付类型获取支付配置 -->
    <select id="getPaymentConfigs" resultMap="SysPaymentConfig">
        SELECT spc.*, o.name_ organ_name
        FROM sys_payment_config spc
        LEFT JOIN organization o on spc.organ_id_ = o.id_
        <where>
            <if test="payType == null">
                pay_type_ IS NOT NULL
                AND (spc.yq_mer_no_ !='' OR spc.hf_mer_no_ !='')
            </if>
            <if test="payType != null">
                AND pay_type_ = #{payType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test='payType != null and payType.code.equals("YQPAY")'>
                AND spc.yq_mer_no_ != ''
            </if>
            <if test='payType != null and payType.code.equals("ADAPAY")'>
                AND spc.hf_mer_no_ !=''
            </if>
        </where>
    </select>

    <!-- 获取所有账户类型为外部的账户 -->
    <select id="getOutAccounts" resultMap="SysPaymentConfig">
        SELECT * FROM sys_payment_config WHERE account_type_ = 1
        <if test="payType != null">
            AND pay_type_ = #{payType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
        </if>
        <if test='payType != null and payType.code.equals("YQPAY")'>
            AND yq_mer_no_ != ''
        </if>
        <if test='payType != null and payType.code.equals("ADAPAY")'>
            AND hf_mer_no_ != ''
        </if>
    </select>
</mapper>