<?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.empty.mapper.CuMemberEmptyDetailMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.member.empty.CuMemberEmptyDetail">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_MEMBER_EMPTY" property="pkMemberEmpty"/>
        <result column="MEMBER_CODE" property="memberCode"/>
        <result column="MEMBER_NAME" property="memberName"/>
        <result column="PHONE" property="phone"/>
        <result column="PARENT_CODE" property="parentCode"/>
        <result column="PLACE_PARENT_CODE" property="placeParentCode"/>
        <result column="PLACE_DEPT" property="placeDept"/>
        <result column="PK_REGISTER_GRADE" property="pkRegisterGrade"/>
        <result column="REGISTER_ACHIEVE" property="registerAchieve"/>
        <result column="ORDER_SOURCE" property="orderSource"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_SETTLE_COUNTRY" property="pkSettleCountry"/>
        <result column="PK_AWARDS" property="pkAwards"/>
        <result column="BACK_AMOUNT" property="backAmount"/>
        <result column="POINT_STATUS" property="pointStatus"/>
        <result column="SETTLEMENT_FLAG" property="settlementFlag"/>
        <result column="SETTLEMENT_ACHIEVE" property="settlementAchieve"/>
        <result column="TRANSFER_ACHIEVE" property="transferAchieve"/>
        <result column="REC_NAME" property="recName"/>
        <result column="REC_PHONE" property="recPhone"/>
        <result column="REC_PROVINCE" property="recProvince"/>
        <result column="REC_CITY" property="recCity"/>
        <result column="REC_COUNTY" property="recCounty"/>
        <result column="REC_ADDRESS" property="recAddress"/>
        <result column="CONTENT_ABSTRACT" property="contentAbstract"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PRODUCT_FLAG" property="productFlag"/>
        <result column="ORDER_TYPE" property="orderType"/>
        <result column="AUTO_FLAG" property="autoFlag"/>
    </resultMap>

    <!-- 明细以及产品map -->
    <resultMap id="detailAndProductMap" type="com.hzs.common.domain.member.empty.ext.CuMemberEmptyDetailExt" extends="BaseResultMap">
        <result column="country_name" property="countryName"/>
        <result column="settle_country_name" property="settleCountryName"/>
        <result column="pk_grade_key" property="pkGradeKey"/>
        <result column="pk_award_key" property="pkAwardKey"/>
        <result column="rec_province_val" property="recProvinceVal"/>
        <result column="rec_city_val" property="recCityVal"/>
        <result column="rec_county_val" property="recCountyVal"/>
        <result column="remark" property="remark"/>
        <collection property="productDTOList" ofType="com.hzs.common.domain.member.empty.ext.CuMemberEmptyProductDTO">
            <result column="pk_product" property="pkProduct"/>
            <result column="product_code" property="productCode"/>
            <result column="product_name" property="productName"/>
            <result column="specs_name" property="specsName"/>
            <result column="specs_name_id" property="specsNameId"/>
            <result column="quantity" property="quantity"/>
        </collection>
    </resultMap>

    <!-- 查询指定数量SEQ -->
    <select id="querySeqList" resultType="long">
        SELECT CU_MEMBER_EMPTY_DETAIL_SEQ.nextval
        FROM (select level from dual connect by #{param} >= level)
    </select>

    <!-- 批量插入数据 -->
    <update id="insertBatch">
        insert into CU_MEMBER_EMPTY_DETAIL (PK_ID,
        PK_MEMBER_EMPTY, MEMBER_CODE, MEMBER_NAME, PHONE,
        PARENT_CODE,
        PK_COUNTRY, PK_SETTLE_COUNTRY, POINT_STATUS, CONTENT_ABSTRACT,
        PK_CREATOR)
        select tmp.*
        from (
        <foreach collection="detailList" item="item" separator=" union ">
            select #{item.pkId} PK_ID,
            #{item.pkMemberEmpty} PK_MEMBER_EMPTY,
            #{item.memberCode} MEMBER_CODE,
            #{item.memberName} MEMBER_NAME,
            #{item.phone} PHONE,
            #{item.parentCode} PARENT_CODE,
            #{item.pkCountry} PK_COUNTRY,
            #{item.pkSettleCountry} PK_SETTLE_COUNTRY,
            #{item.pointStatus} POINT_STATUS,
            #{item.contentAbstract} CONTENT_ABSTRACT,
            #{userId} PK_CREATOR
            from dual
        </foreach>
        ) tmp
    </update>

    <!-- 批量更新数据 -->
    <update id="updateBatch">
        merge into CU_MEMBER_EMPTY_DETAIL cmed
        using (
        <foreach collection="detailList" item="item" separator=" union ">
            select
            #{item.pkId} pk_id,
            #{item.pkMemberEmpty} pk_member_empty,
            #{item.memberCode} member_code,
            #{item.memberName} member_name,
            #{item.phone} phone,
            #{item.parentCode} parent_code,
            #{item.pkRegisterGrade} pk_register_grade,
            #{item.registerAchieve} register_achieve,
            #{item.orderSource} order_source,
            #{item.pkCountry} pk_country,
            #{item.pkSettleCountry} pk_settle_country,
            #{item.pkAwards} pk_awards,
            #{item.backAmount} back_amount,
            #{item.pointStatus} point_status,
            #{item.settlementFlag} settlement_flag,
            #{item.settlementAchieve, jdbcType=NUMERIC} settlement_achieve,
            #{item.transferAchieve, jdbcType=NUMERIC} transfer_achieve,
            #{item.recName, jdbcType=VARCHAR} rec_name,
            #{item.recPhone, jdbcType=VARCHAR} rec_phone,
            #{item.recProvince, jdbcType=NUMERIC} rec_province,
            #{item.recCity, jdbcType=NUMERIC} rec_city,
            #{item.recCounty, jdbcType=NUMERIC} rec_county,
            #{item.recAddress, jdbcType=VARCHAR} rec_address,
            #{item.contentAbstract, jdbcType=VARCHAR} content_abstract,
            #{item.productFlag} product_flag,
            #{item.orderType} order_type
            from dual
        </foreach>
        ) tmp
        on (cmed.del_flag = 0 and tmp.pk_id = cmed.pk_id)
        WHEN MATCHED THEN
        update set
        cmed.member_code = tmp.member_code,
        cmed.member_name = tmp.member_name,
        cmed.phone = tmp.phone,
        cmed.parent_code = tmp.parent_code,
        cmed.pk_register_grade = tmp.pk_register_grade,
        cmed.register_achieve = tmp.register_achieve,
        cmed.order_source = tmp.order_source,
        cmed.pk_country = tmp.pk_country,
        cmed.pk_settle_country = tmp.pk_settle_country,
        cmed.pk_awards = tmp.pk_awards,
        cmed.back_amount = tmp.back_amount,
        cmed.point_status = tmp.point_status,
        cmed.settlement_flag = tmp.settlement_flag,
        cmed.settlement_achieve = nvl(tmp.settlement_achieve, 0),
        cmed.transfer_achieve = nvl(tmp.transfer_achieve, 0),
        cmed.rec_name = tmp.rec_name,
        cmed.rec_phone = tmp.rec_phone,
        cmed.rec_province = tmp.rec_province,
        cmed.rec_city = tmp.rec_city,
        cmed.rec_county = tmp.rec_county,
        cmed.rec_address = tmp.rec_address,
        cmed.content_abstract = tmp.content_abstract,
        cmed.product_flag = tmp.product_flag,
        cmed.order_type = tmp.order_type,
        cmed.pk_modified = #{userId},
        cmed.modified_time = sysdate
    </update>

    <!-- 批量更新异常点位状态 -->
    <update id="updateAbnormalBatch">
        merge into CU_MEMBER_EMPTY_DETAIL cmed
        using (
        <foreach collection="paramList" item="item" separator=" union all ">
            select
            #{item.pkId} pk_id,
            #{item.pointStatus} point_status,
            #{item.contentAbstract} content_abstract
            from dual
        </foreach>
        ) tmp
        on (tmp.pk_id = cmed.pk_id)
        WHEN MATCHED THEN
        update set
        cmed.point_status = tmp.point_status,
        cmed.content_abstract = tmp.content_abstract,
        cmed.PK_MODIFIED = #{userId},
        cmed.MODIFIED_TIME = sysdate
    </update>

    <!-- 安置架构顶点 -->
    <select id="queryPlaceParentTop" resultType="com.hzs.common.domain.member.empty.ext.CuMemberEmptyDetailExt">
        select cmed.*, bg.color grade_color
        from CU_MEMBER_EMPTY_DETAIL cmed
                 left join CU_MEMBER_EMPTY_DETAIL cmedp
                           on cmedp.member_code = cmed.place_parent_code
                               and cmedp.del_flag = 0
                               and cmedp.pk_member_empty = #{pkMemberEmpty}
                 left join BD_GRADE bg
                           on bg.pk_id = cmed.pk_register_grade
        where cmed.del_flag = 0
          and cmed.pk_member_empty = #{pkMemberEmpty}
          and cmedp.member_code is null
    </select>

    <!-- 推荐架构顶点 -->
    <select id="queryParentTop" resultType="com.hzs.common.domain.member.empty.ext.CuMemberEmptyDetailExt">
        select cmed.*, bg.color grade_color
        from CU_MEMBER_EMPTY_DETAIL cmed
                 left join CU_MEMBER_EMPTY_DETAIL cmedp
                           on cmedp.member_code = cmed.parent_code
                               and cmedp.del_flag = 0
                               and cmedp.pk_member_empty = #{pkMemberEmpty}
                               and cmedp.pk_member_empty = #{pkMemberEmpty}
                 left join BD_GRADE bg
                           on bg.pk_id = cmed.pk_register_grade
        where cmed.del_flag = 0
          and cmed.pk_member_empty = #{pkMemberEmpty}
          and cmedp.member_code is null
        order by cmed.member_name
    </select>

    <!-- 安置架构顶点以及产品信息 -->
    <select id="queryPlaceParentTopAndProduct" resultMap="detailAndProductMap">
        select cmed.*,
               cmep.pk_product,
               bp.product_code,
               bp.product_name,
               cmep.specs_name,
               cmep.specs_name_id,
               cmep.quantity
        from CU_MEMBER_EMPTY_DETAIL cmed
                 left join CU_MEMBER_EMPTY_DETAIL cmedp
                           on cmedp.member_code = cmed.parent_code
                               and cmedp.del_flag = 0
                               and cmedp.pk_member_empty = #{pkMemberEmpty}
                 left join cu_member_empty_product cmep
                           on cmep.pk_member_empty_detail = cmed.pk_id
                               and cmep.del_flag = 0
                 left join bd_product bp
                           on bp.pk_id = cmep.pk_product
                               and bp.del_flag = 0
        where cmed.del_flag = 0
          and cmed.pk_member_empty = #{pkMemberEmpty}
          and cmedp.member_code is null
    </select>

    <!-- 安置架构树 -->
    <select id="queryPlaceParentTree" resultType="com.hzs.common.domain.member.empty.ext.CuMemberEmptyDetailExt">
        select cmed.*, bg.color grade_color
        from (
                 select cmed.*
                 from CU_MEMBER_EMPTY_DETAIL cmed
                     start
                 with cmed.pk_id = #{pkId}
                 connect by prior cmed.member_code = cmed.place_parent_code
                        and cmed.del_flag = 0
                        and cmed.pk_member_empty = #{pkMemberEmpty}
                 order by level, cmed.place_dept) cmed
                 left join bd_grade bg
                           on bg.pk_id = cmed.pk_register_grade
    </select>

    <!-- 推荐架构树 -->
    <select id="queryParentTree" resultType="com.hzs.common.domain.member.empty.ext.CuMemberEmptyDetailExt">
        select cmed.*, bg.color grade_color
        from (
                 select level, cmed.*
                 from CU_MEMBER_EMPTY_DETAIL cmed
                     start
                 with cmed.pk_id = #{pkId}
                 connect by prior cmed.member_code = cmed.parent_code
                        and cmed.del_flag = 0
                        and cmed.pk_member_empty = #{pkMemberEmpty}
                 order by level) cmed
                 left join bd_grade bg
                           on bg.pk_id = cmed.pk_register_grade
    </select>

    <!-- 安置架构树以及产品信息 -->
    <select id="queryPlaceParentTreeAndProduct" resultMap="detailAndProductMap">
        select cmed.*,
               cmep.pk_product,
               bp.product_code,
               bp.product_name,
               cmep.specs_name,
               cmep.specs_name_id,
               cmep.quantity
        from (
                 select cmed.*
                 from CU_MEMBER_EMPTY_DETAIL cmed
                     start
                 with cmed.pk_id = #{pkId}
                 connect by prior cmed.member_code = cmed.parent_code
                        and cmed.del_flag = 0
                        and cmed.pk_member_empty = #{pkMemberEmpty}
                 order by level, cmed.place_dept) cmed
                 left join cu_member_empty_product cmep
                           on cmep.pk_member_empty_detail = cmed.pk_id
                               and cmep.del_flag = 0
                 left join bd_product bp
                           on bp.pk_id = cmep.pk_product
                               and bp.del_flag = 0
    </select>

    <!-- 查询空单明细数据 -->
    <select id="queryEmptyDetailList" resultMap="detailAndProductMap">
        select cmed.*,
               cme.remark,
               bc.short_name      country_name,
               bcs.short_name     settle_country_name,
               bgr.pk_transaction pk_grade_key,
               ba.pk_transaction  pk_award_key,
               bap.name           rec_province_val,
               bac.name           rec_city_val,
               baco.name          rec_county_val,
               cmep.pk_product,
               bp.product_code,
               bp.product_name,
               cmep.specs_name,
               cmep.specs_name_id,
               cmep.quantity
        from cu_member_empty_detail cmed
                 left join cu_member_empty cme
                           on cme.pk_id = cmed.pk_member_empty
                 left join cu_member_empty_product cmep
                           on cmep.pk_member_empty_detail = cmed.pk_id
                               and cmep.del_flag = 0
                 left join bd_product bp
                           on bp.pk_id = cmep.pk_product
                               and bp.del_flag = 0
                 left join bd_grade bgr
                           on bgr.pk_id = cmed.pk_register_grade
                 left join bd_awards ba
                           on ba.pk_id = cmed.pk_awards
                 left join bd_country bc
                           on bc.pk_id = cmed.pk_country
                 left join bd_country bcs
                           on bcs.pk_id = cmed.pk_settle_country
                 left join bd_area bap
                           on bap.pk_id = cmed.rec_province
                 left join bd_area bac
                           on bac.pk_id = cmed.rec_city
                 left join bd_area baco
                           on baco.pk_id = cmed.rec_county
        where cmed.del_flag = 0
          and cmed.pk_member_empty = #{pkMemberEmpty}
        order by cmed.pk_id
    </select>

    <!-- 查询空单明细以及产品列表 -->
    <select id="queryDetailAndProductList" resultMap="detailAndProductMap">
        select cmed.*,
               cmep.pk_product,
               bp.product_code,
               bp.product_name,
               cmep.specs_name,
               cmep.specs_name_id,
               cmep.quantity
        from cu_member_empty_detail cmed
                 left join cu_member_empty_product cmep
                           on cmep.pk_member_empty_detail = cmed.pk_id
                               and cmep.del_flag = 0
                 left join bd_product bp
                           on bp.pk_id = cmep.pk_product
                               and bp.del_flag = 0
        where cmed.del_flag = 0
          and cmed.pk_member_empty = #{pkMemberEmpty}
        order by cmed.pk_id
    </select>

    <!-- 查询空单明细数据 -->
    <select id="queryEmptyDetail" resultMap="detailAndProductMap">
        select cmed.*,
        bc.short_name country_name,
        bcs.short_name settle_country_name
        from cu_member_empty_detail cmed
        left join bd_country bc
        on bc.pk_id = cmed.pk_country
        left join bd_country bcs
        on bcs.pk_id = cmed.pk_settle_country
        where cmed.del_flag = 0
        and cmed.pk_id in
        <foreach collection="detailIdList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        order by cmed.pk_id
    </select>

</mapper>
