<?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="cn.roadm.mapper.ApiSignReportMapper">
    <resultMap id="ApiSignReportDO" type="cn.roadm.bo.SmsApiSignReportDO">
        <id column="id" property="id"/>
        <result column="account" property="account"/>
        <result column="signature" property="signature"/>
        <result column="ext_code" property="extCode"/>
        <result column="priority" property="priority"/>
        <result column="create_time" property="createTime"/>
        <result column="complete_time" property="completeTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="flow_id" property="flowId"/>
        <result column="sing_source" property="singSource"/>
        <result column="produce_type" property="produceType"/>
        <result column="use_desc" property="useDesc"/>
        <result column="company_name" property="companyName"/>
        <result column="company_code" property="companyCode"/>
        <result column="register_name" property="registerName"/>
        <result column="register_cert_type" property="registerCertType"/>
        <result column="register_cert_id" property="registerCertId"/>
        <result column="legal_name" property="legalName"/>
        <result column="legal_cert_type" property="legalCertType"/>
        <result column="legal_cert_id" property="legalCertId"/>
        <result column="agency_name" property="agencyName"/>
        <result column="agency_cert_type" property="agencyCertType"/>
        <result column="agency_cert_id" property="agencyCertId"/>
        <result column="register_type" property="registerType"/>
        <result column="otype_status" property="otypeStatus"/>
        <result column="data_source" property="dataSource"/>
        <result column="source_json_content" property="sourceJson"/>
        <result column="client_result" property="clientResult"/>
    </resultMap>

    <resultMap id="aliMmsSignReportDO" type="cn.roadm.bo.AliMmsSignReportDO">
        <id column="id" property="id"/>
        <result column="username" property="username"/>
        <result column="flow_id" property="flowId"/>
        <result column="sign" property="sign"/>
        <result column="otype" property="otype"/>
        <result column="status" property="status"/>
        <result column="content" property="content"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="report_status" property="reportStatus"/>
    </resultMap>

    <select id="searchPageSelectApiSignReportData" resultMap="ApiSignReportDO">
        select r.id,
        ANY_VALUE(r.account) AS account,
        ANY_VALUE(r.signature) AS signature,
        ANY_VALUE(r.ext_code) AS ext_code,
        ANY_VALUE(r.priority) AS priority,
        DATE_FORMAT(ANY_VALUE(r.create_time), '%Y-%m-%d %H:%i:%s') AS create_time,
        DATE_FORMAT(ANY_VALUE(r.complete_time), '%Y-%m-%d %H:%i:%s') AS complete_time,
        DATE_FORMAT(COALESCE(ANY_VALUE(r.complete_time), ANY_VALUE(r.update_time)), '%Y-%m-%d %H:%i:%s') AS update_time,
        ANY_VALUE(r.`status`) AS `status`,
        ANY_VALUE(r.data_source) AS data_source,
        ANY_VALUE(r.client_result) as client_result from sms_api_sign_report r
        <where>
            <if test="id != null and id != ''">
                r.id = #{id}
            </if>
            <if test="sign != null and sign != ''">
                and r.signature like concat('%', #{sign}, '%')
            </if>
            <if test="extCode != null and extCode != ''">
                and r.ext_code like concat('%', #{extCode}, '%')
            </if>
            <if test="account != null and account != ''">
                and r.account = #{account}
            </if>
            <if test="dataSource != null and dataSource != ''">
                and r.data_source = #{dataSource}
            </if>
            <if test="startTime != null and startTime != ''">
                and r.create_time &gt;= str_to_date(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and r.create_time &lt;= DATE_ADD(str_to_date(#{endTime},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>
            <if test="status != null">
                and `status` = #{status}
            </if>
        </where>
        group by r.id
        order by r.id desc
    </select>

    <select id="listQuerySignReportOtypeByIds" resultType="cn.roadm.bo.SmsApiSignDetailBO">
        select pid as id, otype, ext_code as extCode, ext_status as extStatus, vendor_status as vendorStatus, `desc`,
        DATE_FORMAT(vendor_time, '%Y-%m-%d %H:%i:%s') as vendorTime from sms_api_sign_detail
        <where>
            pid in
            <foreach collection="list" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </where>
    </select>

    <select id="getApiSignReportTaskDetail" resultMap="ApiSignReportDO">
        select r.id,
            ANY_VALUE(r.account) AS account,
            ANY_VALUE(r.signature) AS signature,
            ANY_VALUE(r.ext_code) AS ext_code,
            ANY_VALUE(r.priority) AS priority,
            DATE_FORMAT(ANY_VALUE(r.create_time), '%Y-%m-%d %H:%i:%s') AS create_time,
            DATE_FORMAT(ANY_VALUE(r.complete_time), '%Y-%m-%d %H:%i:%s') AS complete_time,
            ANY_VALUE(r.`status`) AS `status`,
            ANY_VALUE(r.data_source) AS data_source,
            ANY_VALUE(r.flow_id) AS flow_id,
            ANY_VALUE(r.sing_source) AS sing_source,
            ANY_VALUE(r.produce_type) as produce_type,
            ANY_VALUE(r.use_desc) as use_desc,
            ANY_VALUE(r.company_name) as company_name,
            ANY_VALUE(r.company_code) as company_code,
            ANY_VALUE(r.register_name) as register_name,
            ANY_VALUE(r.register_cert_type) as register_cert_type,
            ANY_VALUE(r.register_cert_id) as register_cert_id,
            ANY_VALUE(r.legal_name) as legal_name,
            ANY_VALUE(r.legal_cert_type) as legal_cert_type,
            ANY_VALUE(r.legal_cert_id) as legal_cert_id,
            ANY_VALUE(r.agency_name) as agency_name,
            ANY_VALUE(r.agency_cert_type) as agency_cert_type,
            ANY_VALUE(r.agency_cert_id) as agency_cert_id,
            ANY_VALUE(r.register_type) as register_type,
            ANY_VALUE(r.client_result) as client_result,
            GROUP_CONCAT(
                       CONCAT(o.otype, ':', o.vendor_status)
                       ORDER BY o.otype ASC
                       SEPARATOR ','
               )                                                 AS otype_status,
        ANY_VALUE(r.source_json_content) as source_json_content
        from sms_api_sign_report r
                 LEFT JOIN sms_api_sign_report_otype o ON r.id = o.pid
        where r.id = #{id}
        group by r.id
    </select>

    <select id="getApiSignReportDetailInfoById" resultType="cn.roadm.bo.SmsApiSignDetailBO">
        select otype, vendor_status as vendorStatus
        from sms_api_sign_detail
        where pid = #{id}
    </select>

    <select id="selectApiSignReportIdsByForm" resultType="long">
        select count(1) from sms_api_sign_report
        <where>
            status = 0
            <if test="sign != null and sign != ''">
                and signature like concat('%', #{sign}, '%')
            </if>
            <if test="extCode != null and extCode != ''">
                and ext_code like concat('%', #{extCode}, '%')
            </if>
            <if test="account != null and account != ''">
                and `account` = #{account}
            </if>
            <if test="startTime != null and startTime != ''">
                and create_time &gt;= str_to_date(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and create_time &lt;= DATE_ADD(str_to_date(#{endTime},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>
        </where>
    </select>

    <select id="selectQuerySignReportByIds" resultMap="ApiSignReportDO">
        select id, account, flow_id, signature, ext_code, sing_source, produce_type, use_desc, company_name,
        company_code, register_name, register_cert_type, register_cert_id, legal_name, legal_cert_type, legal_cert_id,
        agency_name, agency_cert_type, agency_cert_id, register_type, priority, DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%s') as create_time,
        status, data_source from sms_api_sign_report where id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="listSelectSmsApiSignReport" resultMap="ApiSignReportDO">
        select id, account, flow_id, signature, ext_code, sing_source, produce_type, use_desc, company_name,
        company_code, register_name, register_cert_type, register_cert_id, legal_name, legal_cert_type, legal_cert_id,
        agency_name, agency_cert_type, agency_cert_id, register_type, priority, DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%s') as create_time,
        `status`, data_source, source_json_content from sms_api_sign_report
        <where>
            status = 0
            <if test="sign != null and sign != ''">
                and signature like concat('%', #{sign}, '%')
            </if>
            <if test="extCode != null and extCode != ''">
                and ext_code like concat('%', #{extCode}, '%')
            </if>
            <if test="account != null and account != ''">
                and `account` = #{account}
            </if>
            <if test="dataSource != null and dataSource != ''">
                and data_source = #{dataSource}
            </if>
            <if test="startTime != null and startTime != ''">
                and create_time &gt;= str_to_date(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and create_time &lt;= DATE_ADD(str_to_date(#{endTime},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>
            <if test="lastId != null">
                and id &gt; #{lastId}
            </if>
        </where>
        order by id
        limit 1000
    </select>

    <update id="updateTaskStatus">
        update sms_api_sign_report
        set status      = 1,
            update_time = now()
        where id = #{id}
    </update>

    <select id="listQueryWaitingStatusTask" resultMap="ApiSignReportDO">
        select id,
               flow_id,
               account,
               data_source,
               ext_code
        from sms_api_sign_report
        where status = #{status}
        <if test="lastId != null and lastId > 0">
            and id &gt; #{lastId}
        </if>
        limit 10000
    </select>

    <update id="updateTaskPushSuccess">
        update sms_api_sign_report
        set status        = 1,
            complete_time = now()
        where id = #{id}
    </update>

    <select id="listSelectOtypeStatus" resultType="cn.roadm.bo.SmsApiSignDetailBO">
        select id,
               pid,
               otype,
               vendor_status                                 as vendorStatus,
               DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%s') as createTime,
               DATE_FORMAT(update_time, '%Y-%m-%d %H:%i:%s') as updateTime,
               push_state                                    as pushState,
               push_id                                       as pushId
        from sms_api_sign_report_otype
        where pid = #{id}
    </select>

    <update id="updateOtypeVendorStatus">
        update sms_api_sign_report_otype
        set vendor_status   = #{vendorStatus},
            reported_status = #{reportedStatus},
            reported_code   = #{extCode},
            reason          = #{reason},
            update_time     = now(),
            all_otype_result = #{allOtypeResult}
        where pid = #{id}
          and otype = #{otype}
    </update>

    <update id="updateOtypePushSuccess">
        update sms_api_sign_report_otype
        set push_state = 2,
            push_id    = #{pushId}
        where pid = #{id}
          and otype = #{otype}
    </update>

    <select id="getApiSignReportOtypeDetail" resultType="cn.roadm.bo.SmsApiSignDetailBO">
        select id, pid, otype, vendor_status, push_state, push_id, reported_code, reported_status, reason, create_time, update_time from sms_api_sign_report_otype where pid = #{id} and otype = #{otype}
    </select>

    <select id="getApiSignReportTaskCount" resultType="java.lang.Long">
        select count(1) from sms_api_sign_report r
        <where>
            <if test="id != null and id != ''">
                r.id = #{id}
            </if>
            <if test="sign != null and sign != ''">
                and r.signature like concat('%', #{sign}, '%')
            </if>
            <if test="extCode != null and extCode != ''">
                and r.ext_code like concat('%', #{extCode}, '%')
            </if>
            <if test="account != null and account != ''">
                and r.account = #{account}
            </if>
            <if test="startTime != null and startTime != ''">
                and r.create_time &gt;= str_to_date(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and r.create_time &lt;= DATE_ADD(str_to_date(#{endTime},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>
            <if test="status != null">
                and `status` = #{status}
            </if>
        </where>
    </select>

    <update id="updateClientVendorStatus">
        update sms_api_sign_report set status = 1, complete_time = now(), update_time = now(), client_result = #{allOtypeResult} where id = #{id}
    </update>

    <select id="queryByteDanceSignReportOtypeStatus" resultType="cn.roadm.bo.AloneSignReportStateBO">
        select id as taskId, all_otype_result as otypeStatus, push_state from sms_api_sign_report_otype where pid = #{id}
    </select>

    <select id="queryCommonSignReportOtypeStatus" resultType="cn.roadm.bo.AloneSignReportStateBO">
        select id as taskId, otype, vendor_status as otypeStatus, push_state from sms_api_sign_report_otype where pid = #{id}
    </select>

    <select id="searchPageSelectAliMmsSignReportData" resultMap="aliMmsSignReportDO">
        select id, username, flow_id, sign, otype, status, content, create_time, update_time,report_status from mms_customer_sign_report
        <where>
            <if test="id != null and id != ''">
                id = #{id}
            </if>
            <if test="account != null and account != ''">
                and username = #{account}
            </if>
            <if test="sign != null and sign != ''">
                and sign like concat('%', #{sign}, '%')
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="otype != null">
                and otype = #{otype}
            </if>
            <if test="startTime != null and startTime != ''">
                and create_time &gt;= str_to_date(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and create_time &lt;= DATE_ADD(str_to_date(#{endTime},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>
        </where>
    </select>

    <select id="getAliMmsSignReportContent" resultMap="aliMmsSignReportDO">
        select id, username, flow_id, sign, otype, status, content, create_time, update_time from mms_customer_sign_report where id = #{id}
    </select>

    <select id="getAliMmsSignReportCallback" resultType="cn.roadm.bo.AliMmsSignReportCallbackDO">
        select id, sign, flow_id as flowId, otype, request_id as requestId, report_status as reportStatus, call_back_content as callbackContent, call_back_result as callbackResult, create_time as createTime, update_time as updateTime from mms_customer_sign_report where id = #{id}
    </select>

    <select id="getAliMmsSignReportTaskCount" resultType="java.lang.Long">
        select count(1) from mms_customer_sign_report
        <where>
            <if test="id != null and id != ''">
                id = #{id}
            </if>
            <if test="account != null and account != ''">
                and username = #{account}
            </if>
            <if test="sign != null and sign != ''">
                and sign like concat('%', #{sign}, '%')
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="otype != null">
                and otype = #{otype}
            </if>
            <if test="startTime != null and startTime != ''">
                and create_time &gt;= str_to_date(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and create_time &lt;= DATE_ADD(str_to_date(#{endTime},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>
        </where>
    </select>

    <update id="updateAliMmsSignReportStatus">
        update mms_customer_sign_report set status = 1, report_status = #{status} where id = #{id}
    </update>

    <select id="getAliMmsSignReportCount" resultType="java.lang.Long">
        select count(1) from mms_customer_sign_report
        <where>
            status = 0
            <if test="id != null and id != ''">
                and id = #{id}
            </if>
            <if test="account != null and account != ''">
                and username = #{account}
            </if>
            <if test="sign != null and sign != ''">
                and sign like concat('%', #{sign}, '%')
            </if>
            <if test="otype != null">
                and otype = #{otype}
            </if>
            <if test="startTime != null and startTime != ''">
                and create_time &gt;= str_to_date(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and create_time &lt;= DATE_ADD(str_to_date(#{endTime},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>
        </where>
    </select>

    <select id="exportPageSelectAliMmsSignReportData" resultMap="aliMmsSignReportDO">
        select id, username, flow_id, sign, otype, status, content, create_time, update_time from mms_customer_sign_report
        <where>
            status = 0
            <if test="id != null and id != ''">
                and id = #{id}
            </if>
            <if test="account != null and account != ''">
                and username = #{account}
            </if>
            <if test="sign != null and sign != ''">
                and sign like concat('%', #{sign}, '%')
            </if>
            <if test="otype != null">
                and otype = #{otype}
            </if>
            <if test="startTime != null and startTime != ''">
                and create_time &gt;= str_to_date(#{startTime}, '%Y-%m-%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and create_time &lt;= DATE_ADD(str_to_date(#{endTime},'%Y-%m-%d'),INTERVAL 1 DAY)
            </if>
            <if test="lastId != null">
                and id &lt; #{lastId}
            </if>
        </where>
        order by id desc
        limit 1000
    </select>
</mapper>
