<?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.hzs.member.account.mapper.CuMemberBusinessLicenseMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.member.account.CuMemberBusinessLicense">
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="PK_ID" property="pkId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="LICENSE_NAME" property="licenseName"/>
        <result column="CREDIT_CODE" property="creditCode"/>
        <result column="LEGAL_NAME" property="legalName"/>
        <result column="LICENSE_PROVINCE" property="licenseProvince"/>
        <result column="LICENSE_CITY" property="licenseCity"/>
        <result column="LICENSE_COUNTY" property="licenseCounty"/>
        <result column="LICENSE_ADDRESS" property="licenseAddress"/>
        <result column="LICENSE_IMAGE" property="licenseImage"/>
        <result column="APPROVE_TIME" property="approveTime"/>
        <result column="APPROVE_STATUS" property="approveStatus"/>
        <result column="REJECT_MSG" property="rejectMsg"/>
        <result column="BUSINESS_NO" property="businessNo"/>
    </resultMap>

    <select id="selectBusinessLicenseById" resultType="com.hzs.common.domain.member.account.CuMemberBusinessLicenseExt">
        select a.*,
               p.name  province,
               c.name  city,
               pc.name county
        from cu_member_business_license a
                 left join bd_area p
                           on p.pk_id = a.license_province
                               and p.del_flag = 0
                 left join bd_area c
                           on c.pk_id = a.license_city
                               and c.del_flag = 0
                 left join bd_area pc
                           on pc.pk_id = a.license_county
                               and pc.del_flag = 0
        where a.del_flag = 0
          and a.pk_member = #{pkMember}
          and a.pk_country = #{pkCountry}
    </select>

    <select id="isExistBusinessLicense" resultType="integer">
        select count(1)
        from cu_member_business_license a
        where a.del_flag = 0
          and a.approve_status = 5
          and a.pk_member = #{pkMember}
          and a.pk_country = #{pkCountry}
    </select>

    <sql id="select_base">
        select a.pk_id pkId,
        a.license_image licenseImage,
        m.member_code memberCode,
        m.member_name memberName,
        m.phone,
        ba.PK_TRANSACTION award_transaction,
        bv.vertex_name,
        cmt.team_name,
        a.license_name licenseName,
        a.credit_code creditCode,
        a.legal_name legalName,
        p.name province,
        c.name city,
        pc.name county,
        a.license_address licenseAddress,
        a.approve_status approveStatus,
        a.creation_time creationTime,
        a.approve_time approveTime,
        a.reject_Msg rejectMsg,
        <choose>
            <when test="awardsList != null and awardsList.size > 0">
                (select wm_concat(cm.member_code || '-' || cm.member_name)
                from cu_member cm
                where cm.pk_awards in
                <foreach collection="awardsList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
                start with cm.pk_id = m.pk_id
                connect by cm.pk_id = prior cm.pk_parent)
            </when>
            <otherwise>''</otherwise>
        </choose>
        parentChief
        from cu_member m
        left join BD_AWARDS ba
        on ba.pk_id = m.pk_awards
        and ba.del_flag = 0
        left join BD_VERTEX bv
        on bv.pk_id = m.pk_vertex
        and bv.del_flag = 0
        left join CU_MEMBER_TEAM cmt
        on cmt.pk_id = m.pk_team_code
        and cmt.del_flag = 0,
        cu_member_business_license a
        left join bd_area p
        on p.pk_id = a.license_province
        and p.del_flag = 0
        left join bd_area c
        on c.pk_id = a.license_city
        and c.del_flag = 0
        left join bd_area pc
        on pc.pk_id = a.license_county
        and pc.del_flag = 0
    </sql>

    <sql id="list_where">
        <where>
            m.pk_id = a.pk_member
            and a.del_flag = 0
            and m.del_flag = 0
            and m.pay_status = 1
            and a.pk_country = #{pkCountry}
            <if test="memberName != null and memberName != ''">
                and m.member_name like #{memberName} || '%'
            </if>
            <if test="memberCode != null and memberCode != ''">
                and m.member_code like #{memberCode} || '%'
            </if>
            <if test="pkAward != null">
                and m.pk_awards = #{pkAward}
            </if>
            <if test="pkVertex != null">
                and m.pk_vertex = #{pkVertex}
            </if>
            <if test="pkTeam != null">
                and m.pk_team_code = #{pkTeam}
            </if>
            <if test="licenseName != null and licenseName != ''">
                and a.LICENSE_NAME like #{licenseName} || '%'
            </if>

            <if test="creditCode != null and creditCode != ''">
                and a.CREDIT_CODE like #{creditCode} || '%'
            </if>

            <if test="legalName != null and legalName != ''">
                and a.LEGAL_NAME like #{legalName} || '%'
            </if>

            <if test="licenseProvince != null">
                and a.license_province = #{licenseProvince}
            </if>
            <if test="licenseCity != null">
                and a.license_city = #{licenseCity}
            </if>
            <if test="licenseCounty != null">
                and a.license_county = #{licenseCounty}
            </if>
            <if test="licenseAddress != null and licenseAddress != ''">
                and a.license_address like #{licenseAddress} || '%'
            </if>

            <if test="approveStatus != null">
                and a.APPROVE_STATUS = #{approveStatus}
            </if>
            <if test="startDate != null and startDate != ''">
                and a.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
            </if>
            <if test="endDate != null and endDate != ''">
                and a.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
            </if>
            <if test="startApproveTime != null and startApproveTime != ''">
                and a.APPROVE_TIME >= to_date(#{startApproveTime}, 'yyyy-mm-dd')
            </if>
            <if test="endApproveTime != null and endApproveTime != ''">
                and a.APPROVE_TIME &lt;= to_date(#{endApproveTime} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
            </if>
        </where>
    </sql>

    <sql id="ids_where">
        <where>
            a.del_flag='0'
            and a.pk_id in
            <foreach collection="pkIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </where>
    </sql>

    <select id="selectLicenseList" resultType="com.hzs.common.domain.member.account.CuMemberBusinessLicenseExt">
        <include refid="select_base"/>
        <choose>
            <when test="pkIds != null and pkIds.size >0">
                <include refid="ids_where"></include>
            </when>
            <otherwise>
                <include refid="list_where"></include>
            </otherwise>
        </choose>
        order by nvl(a.MODIFIED_TIME,a.creation_time) Desc
    </select>

    <select id="selectLicenseDetail" resultType="com.hzs.common.domain.member.account.CuMemberBusinessLicenseExt">
        select a.pk_id           pkId,
               a.license_image   licenseImage,
               m.member_code     memberCode,
               m.member_name     memberName,
               a.license_name    licenseName,
               a.pk_member       pkMember,
               a.business_no     businessNo,
               a.legal_name      legalName,
               a.credit_code     creditCode,
               a.license_address licenseAddress,
               a.approve_time    approveTime,
               a.approve_status  approveStatus,
               a.creation_time   creationTime
        from cu_member m,
             cu_member_business_license a
        where m.pk_id = a.pk_member
          and a.del_flag = 0
          and m.del_flag = 0
          and a.pk_id = #{pkId}
    </select>

    <update id="updateLicenseByBusinessNo">
        update cu_member_business_license
        <set>
            <if test="approveStatus != null">APPROVE_STATUS = #{approveStatus},</if>
            <if test="approveTime != null">approve_time = #{approveTime},</if>
            <if test="pkModified != null">pk_modified = #{pkModified},</if>
            modified_time = sysdate
        </set>
        where BUSINESS_NO = #{businessNo}
    </update>

</mapper>
