<?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="org.springblade.customer.mapper.FctCsRecordsMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="org.springblade.customer.entity.FctCsRecords">
        <result column="THEDAY" property="theday" />
        <result column="WORKSHEETID" property="worksheetid" />
        <result column="ACCEPT_TIME" property="acceptTime" />
        <result column="WORKSHEET_STATUS" property="worksheetStatus" />
        <result column="DIAL_PHONENUM" property="dialPhonenum" />
        <result column="ACCEPT_PHONENUM" property="acceptPhonenum" />
        <result column="SERVICE_TYPE" property="serviceType" />
        <result column="SERVICE_TYPE2" property="serviceType2" />
        <result column="SPNAME" property="spname" />
        <result column="SPID" property="spid" />
        <result column="PROVINCE" property="province" />
        <result column="ROLE_SOURCE" property="roleSource" />
        <result column="ACCEPT_TYPE" property="acceptType" />
        <result column="COMPLAINT_TYPE" property="complaintType" />
        <result column="COMPLAINT_SUBTYPE" property="complaintSubtype" />
        <result column="REFUND_REASON" property="refundReason" />
        <result column="DESCRIBE" property="describe" />
        <result column="HANDLE_RESULT" property="handleResult" />
        <result column="IS_REFUNDED" property="isRefunded" />
        <result column="REFUND_PNONENUM" property="refundPnonenum" />
        <result column="REFUND_TIMES" property="refundTimes" />
        <result column="REFUND_MONEY" property="refundMoney" />
        <result column="IS_HIGH_REFUND" property="isHighRefund" />
        <result column="SERVICE_NO" property="serviceNo" />
        <result column="FINISH_TIME" property="finishTime" />
        <result column="BP_ACCEPT_TIME" property="bpAcceptTime" />
        <result column="BP_HANDLE_TIME" property="bpHandleTime" />
        <result column="BP_SPEND_TIME" property="bpSpendTime" />
        <result column="FEEDBACK_LIMIT" property="feedbackLimit" />
        <result column="FEEDBACK1" property="feedback1" />
        <result column="FEEDBACK2" property="feedback2" />
        <result column="FEEDBACK3" property="feedback3" />
        <result column="NOTCLOSE_ONETIMES" property="notcloseOnetimes" />
        <result column="NOTCLOSE_REASON" property="notcloseReason" />
        <result column="NO10010" property="no10010" />
        <result column="IS_UPGRADE" property="isUpgrade" />
        <result column="COMPLAINT" property="complaint" />
        <result column="WORKFLOW_ID" property="workflowId" />
        <result column="BP_HANDLERS" property="bpHandlers" />
        <result column="DEPARTMENT" property="department" />
        <result column="MCOMMENT" property="mcomment" />
        <result column="SPCODE" property="spcode" />
        <result column="SHORTNAME" property="shortname" />
        <result column="CATEGORYNAME" property="categoryname" />
        <result column="CPTYPE" property="cptype" />
        <result column="SERVICE_TYPE3" property="serviceType3" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        THEDAY, WORKSHEETID, ACCEPT_TIME, WORKSHEET_STATUS, DIAL_PHONENUM, ACCEPT_PHONENUM, SERVICE_TYPE, SERVICE_TYPE2, SPNAME, SPID, PROVINCE, ROLE_SOURCE, ACCEPT_TYPE, COMPLAINT_TYPE, COMPLAINT_SUBTYPE, REFUND_REASON, DESCRIBE, HANDLE_RESULT, IS_REFUNDED, REFUND_PNONENUM, REFUND_TIMES, REFUND_MONEY, IS_HIGH_REFUND, SERVICE_NO, FINISH_TIME, BP_ACCEPT_TIME, BP_HANDLE_TIME, BP_SPEND_TIME, FEEDBACK_LIMIT, FEEDBACK1, FEEDBACK2, FEEDBACK3, NOTCLOSE_ONETIMES, NOTCLOSE_REASON, NO10010, IS_UPGRADE, COMPLAINT, WORKFLOW_ID, BP_HANDLERS, DEPARTMENT, MCOMMENT, SPCODE, SHORTNAME, CATEGORYNAME, CPTYPE, SERVICE_TYPE3
    </sql>



    <insert id="insertBatch" parameterType="java.util.List">
        INSERT INTO FCT_CS_RECORDS
        (
        WORKSHEETID,
        ACCEPT_TIME,
        WORKSHEET_STATUS,
        DIAL_PHONENUM,
        ACCEPT_PHONENUM,
        SERVICE_TYPE,
        SERVICE_TYPE2,
        SPNAME,
        SPID,
        PROVINCE,
        ROLE_SOURCE,
        ACCEPT_TYPE,
        COMPLAINT_TYPE,
        COMPLAINT_SUBTYPE,
        REFUND_REASON,
        'DESCRIBE',
        HANDLE_RESULT,
        IS_REFUNDED,
        REFUND_PNONENUM,
        REFUND_TIMES,
        REFUND_MONEY,
        IS_HIGH_REFUND,
        SERVICE_NO,
        FINISH_TIME,
        BP_ACCEPT_TIME,
        BP_HANDLE_TIME,
        BP_SPEND_TIME,
        FEEDBACK_LIMIT,
        FEEDBACK1,
        FEEDBACK2,
        FEEDBACK3,
        NOTCLOSE_ONETIMES,
        NOTCLOSE_REASON,
        NO10010,
        IS_UPGRADE,
        COMPLAINT,
        WORKFLOW_ID,
        BP_HANDLERS,
        DEPARTMENT,
        MCOMMENT
        )
        SELECT a.*
        FROM(
        <foreach item="item" index="index" collection="list" separator="union all">
            SELECT
            #{item.worksheetid,jdbcType=VARCHAR} as WORKSHEETID,
            to_date(#{item.acceptTime,jdbcType=DATE},'yyyy-MM-dd HH:mm:ss') as ACCEPT_TIME,
            #{item.worksheetStatus,jdbcType=VARCHAR} as WORKSHEET_STATUS,
            #{item.dialPhonenum,jdbcType=VARCHAR} as DIAL_PHONENUM,
            #{item.acceptPhonenum,jdbcType=VARCHAR} as ACCEPT_PHONENUM,
            #{item.serviceType,jdbcType=VARCHAR} as SERVICE_TYPE,
            #{item.serviceType2,jdbcType=VARCHAR} as SERVICE_TYPE2,
            #{item.spname,jdbcType=VARCHAR} as SPNAME,
            #{item.province,jdbcType=VARCHAR} as PROVINCE,
            #{item.roleSource,jdbcType=VARCHAR} as ROLE_SOURCE,
            #{item.acceptType,jdbcType=VARCHAR} as ACCEPT_TYPE,
            #{item.complaintType,jdbcType=VARCHAR} as COMPLAINT_TYPE,
            #{item.complaintSubtype,jdbcType=VARCHAR} as COMPLAINT_SUBTYPE,
            #{item.refundReason,jdbcType=VARCHAR} as REFUND_REASON,
            #{item.describe,jdbcType=VARCHAR} as 'DESCRIBE',
            #{item.handleResult,jdbcType=VARCHAR} as HANDLE_RESULT,
            #{item.isRefunded,jdbcType=VARCHAR} as IS_REFUNDED,
            #{item.refundPnonenum,jdbcType=VARCHAR} as REFUND_PNONENUM,
            #{item.refundTimes,jdbcType=VARCHAR} as REFUND_TIMES,
            #{item.refundMoney,jdbcType=DOUBLE} as REFUND_MONEY,
            #{item.isHighRefund,jdbcType=VARCHAR} as IS_HIGH_REFUND,
            #{item.serviceNo,jdbcType=VARCHAR} as SERVICE_NO,
            to_date(#{item.finishTime,jdbcType=VARCHAR},'yyyy-MM-dd HH:mm:ss') as FINISH_TIME,
            to_date(#{item.bpAcceptTime,jdbcType=VARCHAR},'yyyy-MM-dd HH:mm:ss') as BP_ACCEPT_TIME,
            to_date(#{item.bpHandleTime,jdbcType=VARCHAR},'yyyy-MM-dd HH:mm:ss') as BP_HANDLE_TIME,
            #{item.bpSpendTime,jdbcType=VARCHAR} as BP_SPEND_TIME,
            #{item.feedbackLimit,jdbcType=VARCHAR} as FEEDBACK_LIMIT,
            #{item.feedback1,jdbcType=VARCHAR} as FEEDBACK1,
            #{item.feedback2,jdbcType=VARCHAR} as FEEDBACK2,
            #{item.feedback3,jdbcType=VARCHAR} as FEEDBACK3,
            #{item.notcloseOnetimes,jdbcType=VARCHAR} as NOTCLOSE_ONETIMES,
            #{item.notcloseReason,jdbcType=VARCHAR} as NOTCLOSE_REASON,
            #{item.no10010,jdbcType=VARCHAR} as NO10010,
            #{item.isUpgrade,jdbcType=VARCHAR} as IS_UPGRADE,
            #{item.complaint,jdbcType=VARCHAR} as COMPLAINT,
            #{item.workflowId,jdbcType=VARCHAR} as WORKFLOW_ID,
            #{item.bpHandlers,jdbcType=VARCHAR} as BP_HANDLERS,
            #{item.department,jdbcType=VARCHAR} as DEPARTMENT,
            #{item.mcomment,jdbcType=VARCHAR} as MCOMMENT
            from dual
        </foreach>
        ) a
    </insert>

    <update id="setThedayByAcceptTime">
        update FCT_CS_RECORDS set theday=to_char(accept_time,'yyyymmdd')
        where theday is null
    </update>

    <update id="setSpcode">
        update FCT_CS_RECORDS
        set spcode=substr(SERVICE_TYPE, instr(SERVICE_TYPE,'(')+1,5 )
        where instr(SERVICE_TYPE,'(')>0
        and spcode is null
    </update>

    <update id="setServiceType31">
        update FCT_CS_RECORDS
        set SERVICE_TYPE3='畅视互娱'
        where SERVICE_TYPE2 like '畅视互娱%'
        and SERVICE_TYPE3 is null
    </update>

    <update id="setServiceType32">
        update FCT_CS_RECORDS
        set SERVICE_TYPE3='WO+视频'
        where (SERVICE_TYPE2 not like '畅视互娱%' or SERVICE_TYPE2 is null)
        and SERVICE_TYPE3 is null
    </update>

    <update id="setSpidByThisMonth">
        update FCT_CS_RECORDS a
        set (spid,cptype)=( select min(spid),min(cptype) from ETL_TB_SP_INFO b where a.SPNAME=b.SPNAME)
        where theday <![CDATA[>=]]> #{sTime} and theday <![CDATA[<=]]> #{eTime} and spid is null
    </update>


    <update id="setOtherSpidByThisMonth">
        update FCT_CS_RECORDS a
        set spid= (select max(kc_spid) from DIM_CS_KC_SPNAME b where a.spname=b.CS_SPNAME)
        where spid is null
        and theday <![CDATA[>=]]> #{sTime} and theday <![CDATA[<=]]> #{eTime}
    </update>

    <update id="setShortName">
        update FCT_CS_RECORDS a
        set shortname=( select shortname from dim_product_category b where a.spid=b.spid )
        where theday <![CDATA[>=]]> #{sTime} and theday <![CDATA[<=]]> #{eTime} and shortname is null
    </update>


    <update id="setShopName">
        update FCT_CS_RECORDS a
        set shortname=spname
        where theday <![CDATA[>=]]> #{sTime} and theday <![CDATA[<=]]> #{eTime}
        and shortname is null
    </update>

    <update id="callData">
        call PD_CS_SLICE(#{sTime},'MONTH')
    </update>

    <delete id="removeThisMonthData">
        Delete from FCT_CS_RECORDS
        where ACCEPT_TIME <![CDATA[>=]]> to_date(#{month},'yyyymm')
        and ACCEPT_TIME <![CDATA[<]]> add_months(to_date(#{month},'yyyymm'),1)
    </delete>

</mapper>
