<?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.entry.mapper.BizMerchantEntryApplyMapper">

    <resultMap id="BizMerchantEntryApplyResult" type="com.ruoyi.web.modules.entry.domain.BizMerchantEntryApply">
        <id property="applyId" column="apply_id"/>
        <result property="applyNo" column="apply_no"/>
        <result property="applyStatus" column="apply_status"/>
        <result property="merchantType" column="merchant_type"/>
        <result property="memberId" column="member_id"/>
        <result property="memberCode" column="member_code"/>
        <result property="subjectLabel" column="subject_label"/>
        <result property="contactName" column="contact_name"/>
        <result property="contactPhone" column="contact_phone"/>
        <result property="idCardNo" column="id_card_no"/>
        <result property="creditCode" column="credit_code"/>
        <result property="merchantName" column="merchant_name"/>
        <result property="shopName" column="shop_name"/>
        <result property="formJson" column="form_json"/>
        <result property="rejectReason" column="reject_reason"/>
        <result property="merchantId" column="merchant_id"/>
        <result property="shopId" column="shop_id"/>
        <result property="applyTime" column="apply_time"/>
        <result property="auditBy" column="audit_by"/>
        <result property="auditTime" column="audit_time"/>
        <result property="publicityStartTime" column="publicity_start_time"/>
        <result property="publicityEndTime" column="publicity_end_time"/>
        <result property="delFlag" column="del_flag"/>
        <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="EntryApplyListVOResult" type="com.ruoyi.web.modules.entry.vo.EntryApplyListVO">
        <id property="applyId" column="apply_id"/>
        <result property="applyNo" column="apply_no"/>
        <result property="applyStatus" column="apply_status"/>
        <result property="merchantType" column="merchant_type"/>
        <result property="memberCode" column="member_code"/>
        <result property="subjectLabel" column="subject_label"/>
        <result property="contactName" column="contact_name"/>
        <result property="contactPhone" column="contact_phone"/>
        <result property="applyTime" column="apply_time"/>
        <result property="auditTime" column="audit_time"/>
    </resultMap>

    <sql id="selectVo">
        select apply_id, apply_no, apply_status, merchant_type, member_id, member_code,
               subject_label, contact_name, contact_phone, id_card_no, credit_code,
               merchant_name, shop_name, form_json, reject_reason, merchant_id, shop_id,
               apply_time, audit_by, audit_time, publicity_start_time, publicity_end_time, del_flag,
               create_by, create_time, update_by, update_time, remark
        from biz_merchant_entry_apply
    </sql>

    <select id="selectById" resultMap="BizMerchantEntryApplyResult">
        <include refid="selectVo"/>
        where apply_id = #{applyId} and del_flag = '0'
    </select>

    <select id="selectList" resultMap="EntryApplyListVOResult">
        select apply_id, apply_no, apply_status, merchant_type, member_code,
               subject_label, contact_name, contact_phone, apply_time, audit_time
        from biz_merchant_entry_apply
        where del_flag = '0'
        <if test="applyStatus != null and applyStatus != ''">
            and apply_status = #{applyStatus}
        </if>
        <if test="keyword != null and keyword != ''">
            and member_code like concat('%', #{keyword}, '%')
        </if>
        order by
        case when apply_status = '0' then 0 else 1 end,
        case when apply_status = '0' then apply_time end asc,
        case when apply_status != '0' then apply_time end desc
    </select>

    <select id="selectMyByMemberId" resultMap="BizMerchantEntryApplyResult">
        <include refid="selectVo"/>
        where member_id = #{memberId} and del_flag = '0'
        order by apply_time desc
        limit #{limit}
    </select>

    <select id="countPendingByMemberId" resultType="int">
        select count(1) from biz_merchant_entry_apply
        where member_id = #{memberId}
          and apply_status = '0'
          and del_flag = '0'
    </select>

    <select id="countBlockingByMemberId" resultType="int">
        select count(1) from biz_merchant_entry_apply
        where member_id = #{memberId}
          and apply_status in ('0', '3')
          and del_flag = '0'
    </select>

    <select id="selectPublicityExpiredList" resultMap="BizMerchantEntryApplyResult">
        <include refid="selectVo"/>
        where apply_status = '3'
          and del_flag = '0'
          and publicity_end_time is not null
          and publicity_end_time &lt;= sysdate()
    </select>

    <select id="countPlatformPending" resultType="int">
        select count(1) from biz_merchant_entry_apply
        where apply_status = '0' and del_flag = '0'
    </select>

    <sql id="activeApplyStatus">
        apply_status in ('0', '1', '3')
    </sql>

    <select id="countByCreditCodeActive" resultType="int">
        select count(1) from biz_merchant_entry_apply
        where credit_code = #{creditCode}
          and del_flag = '0'
          and <include refid="activeApplyStatus"/>
        <if test="excludeApplyId != null">
            and apply_id != #{excludeApplyId}
        </if>
    </select>

    <select id="countByIdCardNoActive" resultType="int">
        select count(1) from biz_merchant_entry_apply
        where id_card_no = #{idCardNo}
          and del_flag = '0'
          and <include refid="activeApplyStatus"/>
        <if test="excludeApplyId != null">
            and apply_id != #{excludeApplyId}
        </if>
    </select>

    <select id="countByMerchantNameActive" resultType="int">
        select count(1) from biz_merchant_entry_apply
        where merchant_name = #{merchantName}
          and del_flag = '0'
          and <include refid="activeApplyStatus"/>
        <if test="excludeApplyId != null">
            and apply_id != #{excludeApplyId}
        </if>
    </select>

    <select id="countByShopNameActive" resultType="int">
        select count(1) from biz_merchant_entry_apply
        where shop_name = #{shopName}
          and del_flag = '0'
          and <include refid="activeApplyStatus"/>
        <if test="excludeApplyId != null">
            and apply_id != #{excludeApplyId}
        </if>
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="applyId">
        insert into biz_merchant_entry_apply (
            apply_no, apply_status, merchant_type, member_id, member_code,
            subject_label, contact_name, contact_phone, id_card_no, credit_code,
            merchant_name, shop_name, form_json, apply_time, del_flag,
            create_by, create_time
        ) values (
            #{applyNo}, #{applyStatus}, #{merchantType}, #{memberId}, #{memberCode},
            #{subjectLabel}, #{contactName}, #{contactPhone}, #{idCardNo}, #{creditCode},
            #{merchantName}, #{shopName}, #{formJson}, #{applyTime}, #{delFlag},
            #{createBy}, sysdate()
        )
    </insert>

    <update id="updateAuditResult">
        update biz_merchant_entry_apply
        <set>
            apply_status = #{applyStatus},
            reject_reason = #{rejectReason},
            merchant_id = #{merchantId},
            shop_id = #{shopId},
            audit_by = #{auditBy},
            audit_time = #{auditTime},
            publicity_start_time = #{publicityStartTime},
            publicity_end_time = #{publicityEndTime},
            update_by = #{updateBy},
            update_time = sysdate()
        </set>
        where apply_id = #{applyId} and del_flag = '0'
    </update>

</mapper>
