<?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.ss.channel.mapper.ChannelMapper">

    <resultMap type="ChannelVO" id="SmChannelResult" autoMapping="true">
        <result property="costRate" column="cost_rate" typeHandler="com.ruoyi.system.mapper.typehandler.NonNullDecimalTypeHandler"/>
        <result property="appIds" column="app_ids" typeHandler="com.ruoyi.system.mapper.typehandler.LongSplitListTypeHandler"/>
    </resultMap>

    <sql id="selectSmChannelVo">
        select
            sc.channel_id,
            sc.name,
            sc.code,
            sc.type,
            sc.enabled,
            sc.cost_rate,
            sc.picture,
            sc.merchant_id,
            sc.api_v3_key,
            sc.notify_url,
            sc.private_key_path,
            sc.merchant_serial_number,
            sc.refund_notify_url,
            sc.appid,
            sc.developer_id,
            sc.create_time,
            sc.shop_id,
            sc.http_url,
            sc.sign_key,
            sc.platform,
            sc.private_key,
            sc.app_cert_path,
            sc.alipay_public_cert_path,
            sc.root_cert_path,
            sc.aes_private_key,
            sc.app_ids
        from ss_channel sc
    </sql>

    <sql id="searchCondition">
        <if test="query.name != null  and query.name != ''"> and sc.name like concat('%', #{query.name}, '%')</if>
        <if test="query.enabled != null "> and sc.enabled = #{query.enabled}</if>
        <if test="query.type != null "> and sc.type = #{query.type}</if>
        <if test="query.channelIds != null and query.channelIds.size() > 0 ">
            and sc.channel_id in
            <foreach item="item" collection="query.channelIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </sql>

    <select id="selectSmChannelList" parameterType="ChannelQuery" resultMap="SmChannelResult">
        <include refid="selectSmChannelVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectChannelByChannelId" parameterType="Long" resultMap="SmChannelResult">
        <include refid="selectSmChannelVo"/>
        where sc.channel_id = #{channelId}
    </select>

    <insert id="insertSmChannel" parameterType="Channel">
        insert into ss_channel
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="channelId != null">channel_id,</if>
            <if test="name != null">`name`,</if>
            <if test="code != null">`code`,</if>
            <if test="type != null">type,</if>
            <if test="enabled != null">enabled,</if>
            <if test="costRate != null">cost_rate,</if>
            <if test="picture != null">picture,</if>
            <if test="merchantId != null">merchant_id,</if>
            <if test="apiV3Key != null">api_v3_key,</if>
            <if test="notifyUrl != null">notify_url,</if>
            <if test="privateKeyPath != null">private_key_path,</if>
            <if test="merchantSerialNumber != null">merchant_serial_number,</if>
            <if test="refundNotifyUrl != null">refund_notify_url,</if>
            <if test="appid != null">appid,</if>
            <if test="developerId != null">developer_id,</if>
            <if test="shopId != null">shop_id,</if>
            <if test="httpUrl != null">http_url,</if>
            <if test="signKey != null">sign_key,</if>
            <if test="platform != null">platform,</if>
            <if test="privateKey != null">private_key,</if>
            <if test="appCertPath != null">app_cert_path,</if>
            <if test="alipayPublicCertPath != null">alipay_public_cert_path,</if>
            <if test="rootCertPath != null">root_cert_path,</if>
            <if test="aesPrivateKey != null">aes_private_key,</if>
            <if test="createTime != null">create_time,</if>
            <if test="appIds != null">app_ids,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="channelId != null">#{channelId},</if>
            <if test="name != null">#{name},</if>
            <if test="code != null">#{code},</if>
            <if test="type != null">#{type},</if>
            <if test="enabled != null">#{enabled},</if>
            <if test="costRate != null">#{costRate},</if>
            <if test="picture != null">#{picture},</if>
            <if test="merchantId != null">#{merchantId},</if>
            <if test="apiV3Key != null">#{apiV3Key},</if>
            <if test="notifyUrl != null">#{notifyUrl},</if>
            <if test="privateKeyPath != null">#{privateKeyPath},</if>
            <if test="merchantSerialNumber != null">#{merchantSerialNumber},</if>
            <if test="refundNotifyUrl != null">#{refundNotifyUrl},</if>
            <if test="appid != null">#{appid},</if>
            <if test="developerId != null">#{developerId},</if>
            <if test="shopId != null">#{shopId},</if>
            <if test="httpUrl != null">#{httpUrl},</if>
            <if test="signKey != null">#{signKey},</if>
            <if test="platform != null">#{platform},</if>
            <if test="privateKey != null">#{privateKey},</if>
            <if test="appCertPath != null">#{appCertPath},</if>
            <if test="alipayPublicCertPath != null">#{alipayPublicCertPath},</if>
            <if test="rootCertPath != null">#{rootCertPath},</if>
            <if test="aesPrivateKey != null">#{aesPrivateKey},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="appIds != null">#{appIds,typeHandler=com.ruoyi.system.mapper.typehandler.LongSplitListTypeHandler},</if>
         </trim>
    </insert>

    <update id="updateSmChannel" parameterType="Channel">
        update ss_channel
        <trim prefix="SET" suffixOverrides=",">
            <if test="data.name != null">name = #{data.name},</if>
            <if test="data.code != null">code = #{data.code},</if>
            <if test="data.type != null">type = #{data.type},</if>
            <if test="data.enabled != null">enabled = #{data.enabled},</if>
            <if test="data.costRate != null">cost_rate = #{data.costRate},</if>
            <if test="data.picture != null">picture = #{data.picture},</if>
            <if test="data.merchantId != null">merchant_id = #{data.merchantId},</if>
            <if test="data.apiV3Key != null">api_v3_key = #{data.apiV3Key},</if>
            <if test="data.notifyUrl != null">notify_url = #{data.notifyUrl},</if>
            <if test="data.privateKeyPath != null">private_key_path = #{data.privateKeyPath},</if>
            <if test="data.merchantSerialNumber != null">merchant_serial_number = #{data.merchantSerialNumber},</if>
            <if test="data.refundNotifyUrl != null">refund_notify_url = #{data.refundNotifyUrl},</if>
            <if test="data.appid != null">appid = #{data.appid},</if>
            <if test="data.developerId != null">developer_id = #{data.developerId},</if>
            <if test="data.shopId != null">shop_id = #{data.shopId},</if>
            <if test="data.httpUrl != null">http_url = #{data.httpUrl},</if>
            <if test="data.signKey != null">sign_key = #{data.signKey},</if>
            <if test="data.platform != null">platform = #{data.platform},</if>
            <if test="data.privateKey != null">private_key = #{data.privateKey},</if>
            <if test="data.appCertPath != null">app_cert_path = #{data.appCertPath},</if>
            <if test="data.alipayPublicCertPath != null">alipay_public_cert_path = #{data.alipayPublicCertPath},</if>
            <if test="data.rootCertPath != null">root_cert_path = #{data.rootCertPath},</if>
            <if test="data.aesPrivateKey != null">aes_private_key = #{data.aesPrivateKey},</if>
            <if test="data.appIds != null">app_ids = #{data.appIds,typeHandler=com.ruoyi.system.mapper.typehandler.LongSplitListTypeHandler},</if>
        </trim>
        where channel_id = #{data.channelId}
    </update>

    <delete id="deleteSmChannelByChannelId" parameterType="Long">
        delete from ss_channel where channel_id = #{channelId}
    </delete>

    <delete id="deleteSmChannelByChannelIds" parameterType="String">
        delete from ss_channel where channel_id in
        <foreach item="channelId" collection="array" open="(" separator="," close=")">
            #{channelId}
        </foreach>
    </delete>

    <select id="selectChannelBySerialNumber" resultMap="SmChannelResult">
        <include refid="selectSmChannelVo"/>
        where wechatpay_serial = #{wechatpaySerial}
    </select>
</mapper>
