<?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.chinaweal.aiccs.aiccs.market.mapper.BizOwnerAssignMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.chinaweal.aiccs.aiccs.market.entity.BizOwnerAssign">
        <id column="assign_id" property="assignId"/>
        <result column="pripid" property="pripid"/>
        <result column="slice_id" property="sliceId"/>
        <result column="assign_date" property="assignDate"/>
        <result column="assign_operator" property="assignOperator"/>
        <result column="rem_date" property="remDate"/>
        <result column="rem_operator" property="remOperator"/>
        <result column="updater" property="updater"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>


    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        assign_id, pripid, slice_id, assign_date, assign_operator, rem_date, rem_operator, updater, update_time
    </sql>

    <select id="countAssign" resultType="java.util.Map">
        <if test="org == null and psid == null">
            (select
            null as slice_id, a.org_number, a.slice_name, 0 as aiClaimNum,
            (SELECT count(*) FROM biz_owner_assign o LEFT JOIN area_slice s on o.slice_id = s.slice_id
            WHERE s.p_slice_id is null and o.assign_operator != 'AreaSliceClaimAI' and o.assign_type = '1') as assignNum, 0 as claimNum, 0 as remNum
            from biz_owner_assign limit 1)
            union all
        </if>
        (select
        a.slice_id, a.org_number, a.slice_name,
        count(case when b.assign_operator = 'AreaSliceClaimAI' then 1 else null end) as aiClaimNum,
        (SELECT count(*) FROM biz_owner_assign o LEFT JOIN area_slice s on o.slice_id = s.slice_id
        WHERE s.p_slice_id = a.slice_id and o.assign_operator != 'AreaSliceClaimAI' and o.assign_type = '1') as assignNum,
        count(case when b.assign_operator != 'AreaSliceClaimAI' and assign_type = '2' then 1 else null end) as claimNum,
        count(case when b.assign_operator != 'AreaSliceClaimAI' and rem_type is not null then 1 else null end) as remNum
        from biz_owner_assign b LEFT JOIN area_slice a on b.slice_id = a.slice_id
        <where>
            <if test="org == null and psid == null">
                and a.p_slice_id is null
            </if>
            <if test="org != null and psid != null">
                and (a.org_number = #{org} or a.p_slice_id = #{psid})
            </if>
            <if test="param.startDate != null and param.startDate != ''">
                <if test="param.startDate != param.endDate">
                    and b.assign_date > #{param.startDate} and b.assign_date <![CDATA[ < ]]> #{param.endDate}
                </if>
                <if test="param.startDate == param.endDate">
                    and DATE_FORMAT(assign_date,'%Y-%m-%d') = #{param.startDate}
                </if>
            </if>
        </where>
        GROUP BY a.org_number, a.slice_id, a.slice_name
        order by a.org_number asc)
    </select>

    <select id="ownerAssignPage" resultType="com.chinaweal.aiccs.aiccs.market.entity.dto.EBaseInfoAssignDto">
        select e.pripid,e.uniscid, e.entname, e.regno, e.regorg, e.lerep, e.assign_id as "removeAssignId",
        (case when e.DOM = null then e.address else e.DOM end) as address,
        (case when REGEXP_LIKE(e.ENTTYPE, '^[1-8][0-9]{3}', 'i') = 1 then '企业' when e.ENTTYPE in ('9910', '9999') then '个体户' else '农合' end) as enttype,
        e.sareacode_temp as "slice_id", e.sareaname_temp as "slice_name"
        from aiccs.e_baseinfo e where e.regstate = '1'
        <if test="params != null">
            <if test="params.entName != null and params.entName != ''">
                and e.entname like concat('%', #{params.entName} ,'%')
            </if>
            <if test="params.uniscid != null and params.uniscid != ''">
                and (e.uniscid = #{params.uniscid} or e.regno = #{params.uniscid})
            </if>
            <if test="params.name != null and params.name != ''">
                and e.lerep like concat('%', #{params.name} ,'%')
            </if>
            <if test="params.enttype != null and params.enttype != ''">
                and e.enttype = #{params.enttype}
            </if>
            <if test="params.regOrg != null and params.regOrg != ''">
                and e.regorg = #{params.regOrg}
            </if>
            <if test="params.entTypeCn != null and params.entTypeCn == '企业'">
                and e.enttype not in
                ('8000','8100','8200','8300','9000','9100','9200','9910','99100','99101','99102','99103','9999')
            </if>
            <if test="params.entTypeCn != null and params.entTypeCn == '个体户'">
                and e.enttype in ('9910','99100','99101','99102','99103','9999')
            </if>
            <if test="params.entTypeCn != null and params.entTypeCn == '农合'">
                and e.enttype in ('9100','9200')
            </if>
            <if test="params.sliceId != null and params.sliceId != ''">
                and e.sareacode_temp = #{params.sliceId}
            </if>
            <if test="params.address != null and params.address != ''">
                and e.dom like concat('%', #{params.address} ,'%')
            </if>
            <if test="params.orgNumber != null">
                <choose>
                    <when test="params.orgNumber == '440101'">
                        and e.sareacode_temp is null
                    </when>
                    <otherwise>
                        and e.regulatory_temp = #{params.orgNumber}
                    </otherwise>
                </choose>
            </if>
            <if test="params.isDraftRevoke != null and params.isDraftRevoke == 1">
                and exists (select 1 from draft_revoke_entity d where d.pripid = e.pripid and d.status = '1')
            </if>
            <if test="params.beginPripid != null and params.beginPripid != ''">
                and e.pripid &gt; #{params.beginPripid}
            </if>
        </if>
    </select>
    <select id="ownerAssign" resultType="com.chinaweal.aiccs.aiccs.market.entity.dto.EBaseInfoAssignDto">
        select e.pripid,e.uniscid, e.entname, e.regno, e.lerep, e.estdate, e.assign_id as "removeAssignId",
        (select orgUnitName  from cxaicorg.t_orgunits to2 where to2.orgNumber = e.regorg ) as regorg,
        (case when e.DOM = null then e.address else e.DOM end) as address,
        (case when REGEXP_LIKE(e.ENTTYPE, '^[1-8][0-9]{3}', 'i') = 1 then '企业' when e.ENTTYPE in ('9910', '9999') then '个体户' else '农合' end) as enttype,
        e.sareacode_temp as "slice_id", e.sareaname_temp as "slice_name", e.regulatory_name_temp as "regulatory"
        from aiccs.e_baseinfo e where e.regstate = '1'
        <if test="params != null">
            <if test="params.address != null and params.address != ''">
                and e.dom like concat('%', #{params.address} ,'%')
            </if>
            <if test="params.entName != null and params.entName != ''">
                and e.entname like concat('%', #{params.entName} ,'%')
            </if>
            <if test="params.uniscid != null and params.uniscid != ''">
                and (e.uniscid = #{params.uniscid} or e.regno = #{params.uniscid})
            </if>
            <if test="params.name != null and params.name != ''">
                and e.lerep like concat('%', #{params.name} ,'%')
            </if>
            <if test="params.enttype != null and params.enttype != ''">
                and e.enttype = #{params.enttype}
            </if>
            <if test="params.regOrg != null and params.regOrg != ''">
                and e.regorg = #{params.regOrg}
            </if>
            <if test="params.entTypeCn != null and params.entTypeCn == '企业'">
                and e.enttype not in
                ('8000','8100','8200','8300','9000','9100','9200','9910','99100','99101','99102','99103','9999')
            </if>
            <if test="params.entTypeCn != null and params.entTypeCn == '个体户'">
                and e.enttype in ('9910','99100','99101','99102','99103','9999')
            </if>
            <if test="params.entTypeCn != null and params.entTypeCn == '农合'">
                and e.enttype in ('9100','9200')
            </if>
            <if test="params.sliceId != null and params.sliceId != ''">
                and e.sareacode_temp = #{params.sliceId}
            </if>
            <if test="params.orgNumber != null">
                <choose>
                    <when test="params.orgNumber == '440101'">
                        and e.sareacode_temp is null
                    </when>
                    <otherwise>
                        and e.regulatory_temp = #{params.orgNumber}
                    </otherwise>
                </choose>
            </if>
            <if test="params.isDraftRevoke != null and params.isDraftRevoke == 1">
                and exists (select 1 from draft_revoke_entity d where d.pripid = e.pripid and d.status = '1')
            </if>
            <if test="params.beginPripid != null and params.beginPripid != ''">
                and e.pripid &gt; #{params.beginPripid}
            </if>
        </if>
    </select>
    <select id="selectBizRecordForUpdate" resultType="com.chinaweal.aiccs.aiccs.market.entity.BizOwnerAssign">
        select
        <include refid="Base_Column_List"/>
        from aiccs.biz_owner_assign
        where pripid = #{pripid} for update
    </select>
    <select id="listBySliceInfo" resultType="com.chinaweal.aiccs.aiccs.market.entity.dto.SliceAssignLogDto">
        select o.*, s.org_number orgNumber, s.slice_name sliceName, s.slice_addr_key sliceAddrKey,
        s.slice_addr sliceAddr, s.supervisor_id supervisorId, s.remark, s.state, s.is_deleted isDeleted,
        s.slice_type sliceType
        from biz_owner_assign o left join area_slice s on o.slice_id = s.slice_id
        <where>
            <if test="pripid != null and pripid != ''">
                and o.pripid = #{pripid}
            </if>
            <if test="sliceId != null and sliceId != ''">
                and o.slice_id = #{sliceId}
            </if>
        </where>
    </select>

    <select id="selectLatestAssignLog" resultMap="BaseResultMap">
        select o.*
        from biz_owner_assign o left join area_slice s on o.slice_id = s.slice_id
        where o.rem_date is null and o.pripid = #{pripid}
        order by s.slice_type desc
        limit 1
    </select>
    <select id="selectWaitAssignPage"
            resultType="com.chinaweal.aiccs.aiccs.market.entity.dto.EBaseInfoAssignDto">
        select e.pripid,e.uniscid, e.entname, e.regno, e.enttype, e.regorg, e.lerep, (case when e.DOM = null then
        e.address else e.DOM end) as address,
        biz.assign_date, biz.assign_id, biz.slice_id, sl.slice_name, slice_type
        from aiccs.e_baseinfo e left join aiccs.biz_owner_assign biz on biz.pripid = e.pripid and biz.rem_date is
        null
        left join aiccs.area_slice sl on sl.slice_id = biz.slice_id
        <where>
            <!--取最大的slice_type记录-->
            and not exists(select 1 from biz_owner_assign boa2 left join area_slice as2
            on boa2.slice_id = as2.slice_id where boa2.pripid = e.pripid and boa2.rem_date is null
            and as2.slice_type > sl.slice_type)
            and not exists(
            select 1 from biz_owner_assign boa left join area_slice bas on boa.slice_id = bas.slice_id and
            boa.pripid = e.pripid
            where bas.slice_type = '2' and rem_date is null
            )
            <if test="beginId != null and beginId != ''">
                and e.pripid &gt; #{beginId}
            </if>
        </where>
        order by e.pripid asc
        limit ${limitSize}
    </select>
    <select id="selectNoAssignRecordInfo" resultType="com.chinaweal.aiccs.aiccs.abnormal.entity.EBaseinfo">
        select *
        from e_baseinfo e
        where REGSTATE ='1'
        and sareacode_temp is null
        <if test="beginId != null and beginId != ''">
            and e.pripid &gt; #{beginId}
        </if>
        order by e.pripid asc
        limit ${limitSize}
    </select>
</mapper>
