<?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.web.modules.pay.mapper.BizShopWechatPayMapper">

    <resultMap id="BizShopWechatPayResult" type="com.ruoyi.web.modules.pay.domain.BizShopWechatPay">
        <id property="id" column="id"/>
        <result property="shopId" column="shop_id"/>
        <result property="merchantId" column="merchant_id"/>
        <result property="applyStatus" column="apply_status"/>
        <result property="payMode" column="pay_mode"/>
        <result property="mchId" column="mch_id"/>
        <result property="subMchId" column="sub_mch_id"/>
        <result property="companyName" column="company_name"/>
        <result property="legalPerson" column="legal_person"/>
        <result property="licenseNo" column="license_no"/>
        <result property="contactName" column="contact_name"/>
        <result property="contactMobile" column="contact_mobile"/>
        <result property="bankName" column="bank_name"/>
        <result property="bankAccountName" column="bank_account_name"/>
        <result property="bankAccountNo" column="bank_account_no"/>
        <result property="auditRemark" column="audit_remark"/>
        <result property="enabled" column="enabled"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectVo">
        select id, shop_id, merchant_id, apply_status, pay_mode, mch_id, sub_mch_id,
               company_name, legal_person, license_no, contact_name, contact_mobile,
               bank_name, bank_account_name, bank_account_no, audit_remark, enabled,
               create_by, create_time, update_by, update_time
        from biz_shop_wechat_pay
    </sql>

    <select id="selectByShopId" resultMap="BizShopWechatPayResult">
        <include refid="selectVo"/>
        where shop_id = #{shopId}
    </select>

    <select id="selectById" resultMap="BizShopWechatPayResult">
        <include refid="selectVo"/>
        where id = #{id}
    </select>

    <select id="selectPlatformList" resultType="com.ruoyi.web.modules.pay.vo.ShopWechatPayPlatformVO">
        select p.id, p.shop_id as shopId, s.shop_name as shopName, p.merchant_id as merchantId,
               ifnull(nullif(m.merchant_name, ''), ifnull(m.company_name, m.person_name)) as merchantName,
               p.apply_status as applyStatus, p.pay_mode as payMode, p.mch_id as mchId, p.sub_mch_id as subMchId,
               p.company_name as companyName, p.legal_person as legalPerson, p.license_no as licenseNo,
               p.contact_name as contactName, p.contact_mobile as contactMobile,
               p.bank_name as bankName, p.bank_account_name as bankAccountName, p.bank_account_no as bankAccountNo,
               p.audit_remark as auditRemark, p.enabled, p.create_time as createTime, p.update_time as updateTime
        from biz_shop_wechat_pay p
        inner join biz_shop s on p.shop_id = s.shop_id and s.del_flag = '0'
        inner join biz_merchant m on p.merchant_id = m.merchant_id and m.del_flag = '0'
        <where>
            <if test="applyStatus != null and applyStatus != ''">
                and p.apply_status = #{applyStatus}
            </if>
            <if test="shopName != null and shopName != ''">
                and s.shop_name like concat('%', #{shopName}, '%')
            </if>
            <if test="companyName != null and companyName != ''">
                and p.company_name like concat('%', #{companyName}, '%')
            </if>
        </where>
        order by p.update_time desc
    </select>

    <select id="selectPlatformById" resultType="com.ruoyi.web.modules.pay.vo.ShopWechatPayPlatformVO">
        select p.id, p.shop_id as shopId, s.shop_name as shopName, p.merchant_id as merchantId,
               ifnull(nullif(m.merchant_name, ''), ifnull(m.company_name, m.person_name)) as merchantName,
               p.apply_status as applyStatus, p.pay_mode as payMode, p.mch_id as mchId, p.sub_mch_id as subMchId,
               p.company_name as companyName, p.legal_person as legalPerson, p.license_no as licenseNo,
               p.contact_name as contactName, p.contact_mobile as contactMobile,
               p.bank_name as bankName, p.bank_account_name as bankAccountName, p.bank_account_no as bankAccountNo,
               p.audit_remark as auditRemark, p.enabled, p.create_time as createTime, p.update_time as updateTime
        from biz_shop_wechat_pay p
        inner join biz_shop s on p.shop_id = s.shop_id and s.del_flag = '0'
        inner join biz_merchant m on p.merchant_id = m.merchant_id and m.del_flag = '0'
        where p.id = #{id}
    </select>

    <select id="countByApplyStatus" resultType="int">
        select count(1)
        from biz_shop_wechat_pay
        where apply_status = #{applyStatus}
    </select>

    <insert id="insert" parameterType="com.ruoyi.web.modules.pay.domain.BizShopWechatPay" useGeneratedKeys="true" keyProperty="id">
        insert into biz_shop_wechat_pay (
            shop_id, merchant_id, apply_status, pay_mode, mch_id, sub_mch_id,
            company_name, legal_person, license_no, contact_name, contact_mobile,
            bank_name, bank_account_name, bank_account_no, audit_remark, enabled,
            create_by, create_time, update_by, update_time
        ) values (
            #{shopId}, #{merchantId}, #{applyStatus}, #{payMode}, #{mchId}, #{subMchId},
            #{companyName}, #{legalPerson}, #{licenseNo}, #{contactName}, #{contactMobile},
            #{bankName}, #{bankAccountName}, #{bankAccountNo}, #{auditRemark}, #{enabled},
            #{createBy}, #{createTime}, #{updateBy}, #{updateTime}
        )
    </insert>

    <update id="updateByShopId" parameterType="com.ruoyi.web.modules.pay.domain.BizShopWechatPay">
        update biz_shop_wechat_pay
        set apply_status = #{applyStatus},
            pay_mode = #{payMode},
            mch_id = #{mchId},
            sub_mch_id = #{subMchId},
            company_name = #{companyName},
            legal_person = #{legalPerson},
            license_no = #{licenseNo},
            contact_name = #{contactName},
            contact_mobile = #{contactMobile},
            bank_name = #{bankName},
            bank_account_name = #{bankAccountName},
            bank_account_no = #{bankAccountNo},
            audit_remark = #{auditRemark},
            enabled = #{enabled},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where shop_id = #{shopId}
    </update>

    <update id="updateAuditStatus" parameterType="com.ruoyi.web.modules.pay.domain.BizShopWechatPay">
        update biz_shop_wechat_pay
        set apply_status = #{applyStatus},
            audit_remark = #{auditRemark},
            enabled = #{enabled},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where shop_id = #{shopId}
          and apply_status = '1'
    </update>
</mapper>
