<?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.UploadSignStatusMapper">

    <resultMap type="UploadSignStatus" id="UploadSignStatusResultMap">
        <id column="id" property="id" />
        <result column="acc_id" property="accId" />
        <result column="channel_id" property="channelId" />
        <result column="sign" property="sign" />
        <result column="port" property="port" />
        <result column="sub_port" property="subPort" />
        <result column="province_name" property="provinceName" />
        <result column="city_name" property="cityName" />
        <result column="company_name" property="companyName" />
        <result column="company_num" property="companyNum" />
        <result column="company_card_type" property="companyCardType" />
        <result column="regist_name" property="registName" />
        <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="sub_port_usage" property="subPortUsage" />
        <result column="usage_category" property="usageCategory" />
        <result column="port_type" property="portType" />
        <result column="sign_source" property="signSource" />
        <result column="location_server_room" property="locationServerRoom" />
        <result column="sms_templates" property="smsTemplates" />
        <result column="status" property="status" />
        <result column="error_msg" property="errorMsg" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="task_id" property="taskId" />
        <result column="domain" property="domain" />
        <result column="phones" property="phones" />
        <result column="is_sync" property="isSync" />
        <result column="is_deleted" property="isDeleted"/>
        <result column="app_name" property="appName"/>
        <result column="auth_start_time" property="authStartTime"/>
        <result column="auth_end_time" property="authEndTime"/>
        <result column="sub_port_service_type" property="subPortServiceType"/>
        <result column="sub_port_service_detail" property="subPortServiceDetail"/>
        <result column="otype_server_room" property="otypeServerRoom"/>
        <result column="m_upload_company_name" property="mUploadCompanyName"/>
        <result column="m_operation_type" property="mOperationType"/>
        <result column="m_port_range" property="mPortRange"/>
        <result column="m_port_type" property="mPortType"/>
        <result column="m_gcl_id" property="mGclId"/>
        <result column="m_legal_name" property="mLegalName"/>
        <result column="m_sign_check_file" property="mSignCheckFile"/>
        <result column="m_legal_mdn" property="mLegalMdn"/>
        <result column="m_agency_mdn" property="mAgencyMdn"/>
        <result column="t_service_type" property="tServiceType"/>
        <result column="t_letter_of_auth" property="tLetterOfAuth"/>
        <result column="t_base_sp_id" property="tBaseSpId"/>
        <result column="t_net_in_time" property="tNetInTime"/>
        <result column="t_service_big_type" property="tServiceBigType"/>
        <result column="t_service_big_detail" property="tServiceBigDetail"/>
        <result column="t_contain_yl_mdn" property="tContainYlMdn"/>
        <result column="t_contain_yl_url" property="tContainYlUrl"/>
        <result column="file_batch_id" property="fileBatchId"/>
    </resultMap>

    <select id="listPageUploadSignStatus" parameterType="UploadSignStatus"
            resultMap="UploadSignStatusResultMap">
        SELECT
        a.id, a.acc_id, a.channel_id, a.sign, a.port, a.sub_port, a.province_name, a.city_name, a.company_name,
        company_num, company_card_type, regist_name, legal_cert_type, legal_cert_id,
        agency_name, agency_cert_type, agency_cert_id, sub_port_usage, usage_category,
        port_type, sign_source, location_server_room, sms_templates, a.status, error_msg,
        a.create_time, a.update_time, task_id, domain, phones, a.is_sync, a.is_deleted,
        app_name, auth_start_time, auth_end_time, sub_port_service_type,
        sub_port_service_detail, otype_server_room, m_upload_company_name,
        m_operation_type, m_port_range, m_port_type, m_gcl_id, m_legal_name,
        m_sign_check_file, m_legal_mdn, m_agency_mdn, t_service_type, t_letter_of_auth,
        t_base_sp_id, t_net_in_time, t_service_big_type, t_service_big_detail,
        t_contain_yl_mdn, t_contain_yl_url,file_batch_id,b.channel_name as channelName
        FROM upload_sign_status a left join sms_send_channel_info b on a.channel_id = b.id
        <where>
            a.is_deleted = 0
            <if test="accId != null">
                AND a.acc_id = #{accId}
            </if>
            <if test="port != null and port != ''">
                AND a.port = #{port}
            </if>
            <if test="subPort != null and subPort != ''">
                AND a.sub_port = #{subPort}
            </if>
            <if test="sign != null and sign != ''">
                AND a.sign like concat('%',#{sign},'%')
            </if>
            <if test="status != null">
                AND a.status = #{status}
            </if>
            <if test="createTimeStart != null and createTimeStart != ''">
                AND a.create_time &gt;= #{createTimeStart}
            </if>
            <if test="createTimeEnd != null and createTimeEnd != ''">
                AND a.create_time &lt;= #{createTimeEnd}
            </if>
            <if test="taskId != null and taskId != ''">
                AND a.task_id = #{taskId}
            </if>
            <if test="isSync != null">
                AND a.is_sync = #{isSync}
            </if>

            <if test="fileBatchId != null and fileBatchId != ''">
                AND a.file_batch_id = #{fileBatchId}
            </if>

            <if test="channelId != null">
                AND a.channel_id = #{channelId}
            </if>
<!--            <if test="isDeleted != null and isDeleted != ''">-->
<!--                AND is_deleted = #{isDeleted}-->
<!--            </if>-->
        </where>
        order by a.id desc
    </select>

    <select id="queryUploadSignStatusById" parameterType="java.lang.Integer"
            resultMap="UploadSignStatusResultMap">
        SELECT
            a.id, acc_id, a.channel_id, sign, a.port, sub_port, province_name, city_name, company_name,
            company_num, company_card_type, regist_name, legal_cert_type, legal_cert_id,
            agency_name, agency_cert_type, agency_cert_id, sub_port_usage, usage_category,
            port_type, sign_source, location_server_room, sms_templates, status, error_msg,
            a.create_time, update_time, task_id, domain, phones, is_sync, a.is_deleted,
            app_name, auth_start_time, auth_end_time, sub_port_service_type,
            sub_port_service_detail, otype_server_room, m_upload_company_name,
            m_operation_type, m_port_range, m_port_type, m_gcl_id, m_legal_name,
            m_sign_check_file, m_legal_mdn, m_agency_mdn, t_service_type, t_letter_of_auth,
            t_base_sp_id, t_net_in_time, t_service_big_type, t_service_big_detail,
            t_contain_yl_mdn, t_contain_yl_url,file_batch_id, b.type as accType
        FROM upload_sign_status a left join upload_sign_acc b on a.acc_id = b.id
        WHERE a.id = #{id}
    </select>

    <select id="getUploadSignStatusBySubPort"
                resultMap="UploadSignStatusResultMap">
            SELECT
                id, acc_id, channel_id, sign, port, sub_port, province_name, city_name, company_name,
                company_num, company_card_type, regist_name, legal_cert_type, legal_cert_id,
                agency_name, agency_cert_type, agency_cert_id, sub_port_usage, usage_category,
                port_type, sign_source, location_server_room, sms_templates, status, error_msg,
                create_time, update_time, task_id, domain, phones, is_sync, is_deleted,
                app_name, auth_start_time, auth_end_time, sub_port_service_type,
                sub_port_service_detail, otype_server_room, m_upload_company_name,
                m_operation_type, m_port_range, m_port_type, m_gcl_id, m_legal_name,
                m_sign_check_file, m_legal_mdn, m_agency_mdn, t_service_type, t_letter_of_auth,
                t_base_sp_id, t_net_in_time, t_service_big_type, t_service_big_detail,
                t_contain_yl_mdn, t_contain_yl_url,file_batch_id
            FROM upload_sign_status
            WHERE acc_id = #{channelId}
            AND sub_port = #{subPort}
            AND is_deleted = 0
            AND status in (1,2,3)
        </select>


    <insert id="insertUploadSignStatus" parameterType="UploadSignStatus" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO upload_sign_status
        (
            acc_id, channel_id, sign, port, sub_port, province_name, city_name,
            company_name, company_num, company_card_type, regist_name, legal_cert_type,
            legal_cert_id, agency_name, agency_cert_type, agency_cert_id, sub_port_usage,
            usage_category, port_type, sign_source, location_server_room, sms_templates,
            status, error_msg, create_time, update_time, task_id, domain, phones,
            is_sync, is_deleted, app_name, auth_start_time, auth_end_time,
            sub_port_service_type, sub_port_service_detail, otype_server_room,
            m_upload_company_name, m_operation_type, m_port_range, m_port_type, m_gcl_id,
            m_legal_name, m_sign_check_file, m_legal_mdn, m_agency_mdn,
            t_service_type, t_letter_of_auth, t_base_sp_id, t_net_in_time,
            t_service_big_type, t_service_big_detail, t_contain_yl_mdn, t_contain_yl_url,file_batch_id
        )
        VALUES
            (
                #{accId}, #{channelId}, #{sign}, #{port}, #{subPort}, #{provinceName}, #{cityName},
                #{companyName}, #{companyNum}, #{companyCardType}, #{registName}, #{legalCertType},
                #{legalCertId}, #{agencyName}, #{agencyCertType}, #{agencyCertId}, #{subPortUsage},
                #{usageCategory}, #{portType}, #{signSource}, #{locationServerRoom}, #{smsTemplates},
                #{status}, #{errorMsg}, #{createTime}, #{updateTime}, #{taskId}, #{domain}, #{phones},
                #{isSync}, #{isDeleted}, #{appName}, #{authStartTime}, #{authEndTime},
                #{subPortServiceType}, #{subPortServiceDetail}, #{otypeServerRoom},
                #{mUploadCompanyName}, #{mOperationType}, #{mPortRange}, #{mPortType}, #{mGclId},
                #{mLegalName}, #{mSignCheckFile}, #{mLegalMdn}, #{mAgencyMdn},
                #{tServiceType}, #{tLetterOfAuth}, #{tBaseSpId}, #{tNetInTime},
                #{tServiceBigType}, #{tServiceBigDetail}, #{tContainYlMdn}, #{tContainYlUrl},#{fileBatchId}
            )
    </insert>

    <update id="updateUploadSignStatus" parameterType="UploadSignStatus">
        UPDATE upload_sign_status
        SET
            acc_id = #{accId},
            channel_id = #{channelId}
            sign = #{sign},
            port = #{port},
            sub_port = #{subPort},
            province_name = #{provinceName},
            city_name = #{cityName},
            company_name = #{companyName},
            company_num = #{companyNum},
            company_card_type = #{companyCardType},
            regist_name = #{registName},
            legal_cert_type = #{legalCertType},
            legal_cert_id = #{legalCertId},
            agency_name = #{agencyName},
            agency_cert_type = #{agencyCertType},
            agency_cert_id = #{agencyCertId},
            sub_port_usage = #{subPortUsage},
            usage_category = #{usageCategory},
            port_type = #{portType},
            sign_source = #{signSource},
            location_server_room = #{locationServerRoom},
            sms_templates = #{smsTemplates},
            status = #{status},
            error_msg = #{errorMsg},
            create_time = #{createTime},
            update_time = #{updateTime},
            task_id = #{taskId},
            domain = #{domain},
            phones = #{phones},
            is_sync = #{isSync},
            is_deleted = #{isDeleted},
            app_name = #{appName},
            auth_start_time = #{authStartTime},
            auth_end_time = #{authEndTime},
            sub_port_service_type = #{subPortServiceType},
            sub_port_service_detail = #{subPortServiceDetail},
            otype_server_room = #{otypeServerRoom},
            m_upload_company_name = #{mUploadCompanyName},
            m_operation_type = #{mOperationType},
            m_port_range = #{mPortRange},
            m_port_type = #{mPortType},
            m_gcl_id = #{mGclId},
            m_legal_name = #{mLegalName},
            m_sign_check_file = #{mSignCheckFile},
            m_legal_mdn = #{mLegalMdn},
            m_agency_mdn = #{mAgencyMdn},
            t_service_type = #{tServiceType},
            t_letter_of_auth = #{tLetterOfAuth},
            t_base_sp_id = #{tBaseSpId},
            t_net_in_time = #{tNetInTime},
            t_service_big_type = #{tServiceBigType},
            t_service_big_detail = #{tServiceBigDetail},
            t_contain_yl_mdn = #{tContainYlMdn},
            t_contain_yl_url = #{tContainYlUrl},
            file_batch_id = #{fileBatchId}
        WHERE id = #{id}
    </update>

    <update id="editUploadSignStatus" parameterType="UploadSignStatus">
        UPDATE upload_sign_status
        <set>
            <if test="sign != null and sign != ''">sign = #{sign},</if>
            <if test="provinceName != null and provinceName != ''">province_name = #{provinceName},</if>
            <if test="cityName != null and cityName != ''">city_name = #{cityName},</if>
            <if test="companyName != null and companyName != ''">company_name = #{companyName},</if>
            <if test="companyNum != null and companyNum != ''">company_num = #{companyNum},</if>
            <if test="registName != null and registName != ''">regist_name = #{registName},</if>
            <if test="agencyName != null and agencyName != ''">agency_name = #{agencyName},</if>
            <if test="legalCertType != null and legalCertType != ''">legal_cert_type = #{legalCertType},</if>
            <if test="agencyCertType != null and agencyCertType != ''">agency_cert_type = #{agencyCertType},</if>
            <if test="legalCertId != null and legalCertId != ''">legal_cert_id = #{legalCertId},</if>
            <if test="agencyCertId != null and agencyCertId != ''">agency_cert_id = #{agencyCertId},</if>
            <if test="smsTemplates != null and smsTemplates != ''">sms_templates = #{smsTemplates},</if>
            <if test="domain != null and domain != ''">domain = #{domain},</if>
            <if test="phones != null and phones != ''">phones = #{phones},</if>
            update_time = #{updateTime}
        </set>
        WHERE id = #{id}
    </update>

    <update id="deleteUploadSignStatus" parameterType="UploadSignStatus">
        UPDATE upload_sign_status set is_deleted = 1
        WHERE id = #{id}
    </update>

    <select id="quertSubPortList" parameterType="UploadSignChannel" resultType="java.lang.String">
        select sub_port FROM upload_sign_status
        WHERE acc_id = #{id} and port = #{port} and is_deleted = 0 and status in (1,2,3)
    </select>

    <select id="quertSubPortListByChannel" parameterType="UploadSignStatus" resultType="java.lang.String">
        select sub_port FROM upload_sign_status
        WHERE channel_id = #{copyChannel} and is_deleted = 0 and status in (1,2,3)
    </select>

    <select id="queryExportUploadSignStatus" resultMap="UploadSignStatusResultMap">
        select
            id, acc_id, channel_id, sign, port, sub_port, province_name, city_name, company_name,
            company_num, company_card_type, regist_name, legal_cert_type, legal_cert_id,
            agency_name, agency_cert_type, agency_cert_id, sub_port_usage, usage_category,
            port_type, sign_source, location_server_room, sms_templates, status, error_msg,
            create_time, update_time, task_id, domain, phones, is_sync, is_deleted,
            app_name, auth_start_time, auth_end_time, sub_port_service_type,
            sub_port_service_detail, otype_server_room, m_upload_company_name,
            m_operation_type, m_port_range, m_port_type, m_gcl_id, m_legal_name,
            m_sign_check_file, m_legal_mdn, m_agency_mdn, t_service_type, t_letter_of_auth,
            t_base_sp_id, t_net_in_time, t_service_big_type, t_service_big_detail,
            t_contain_yl_mdn, t_contain_yl_url,file_batch_id
        FROM upload_sign_status
        WHERE acc_id = #{accId} and status = #{status} and is_deleted = 0
    </select>

    <select id="queryExportUploadSignStatusExport" resultMap="UploadSignStatusResultMap">
        select
            id, acc_id, channel_id, sign, port, sub_port, province_name, city_name, company_name,
            company_num, company_card_type, regist_name, legal_cert_type, legal_cert_id,
            agency_name, agency_cert_type, agency_cert_id, sub_port_usage, usage_category,
            port_type, sign_source, location_server_room, sms_templates, status, error_msg,
            create_time, update_time, task_id, domain, phones, is_sync, is_deleted,
            app_name, auth_start_time, auth_end_time, sub_port_service_type,
            sub_port_service_detail, otype_server_room, m_upload_company_name,
            m_operation_type, m_port_range, m_port_type, m_gcl_id, m_legal_name,
            m_sign_check_file, m_legal_mdn, m_agency_mdn, t_service_type, t_letter_of_auth,
            t_base_sp_id, t_net_in_time, t_service_big_type, t_service_big_detail,
            t_contain_yl_mdn, t_contain_yl_url,file_batch_id
        FROM upload_sign_status
        <where>
            is_deleted = 0
            <if test="accId != null">
                AND acc_id = #{accId}
            </if>
            <if test="status != null">
                AND status = #{status}
            </if>
            <if test="fileBatchId != null and fileBatchId != ''">
                AND file_batch_id = #{fileBatchId}
            </if>
        </where>
    </select>

    <update id="updateTaskIdByChannelAndStatus" parameterType="UploadSignStatus">
        UPDATE upload_sign_status set task_id = #{taskId}
        where acc_id = #{accId} and status = #{status} and (task_id is null or task_id = '') and is_deleted = 0
        <if test="fileBatchId!=null and fileBatchId!=''">
            and file_batch_id = #{fileBatchId}
        </if>
    </update>

    <!-- 批量插入 UploadSignStatus 对象列表 -->
    <insert id="insertUploadSignStatusList" parameterType="java.util.List">
        INSERT INTO upload_sign_status
        (
        acc_id, channel_id, sign, port, sub_port, province_name, city_name,
        company_name, company_num, company_card_type, regist_name, legal_cert_type,
        legal_cert_id, agency_name, agency_cert_type, agency_cert_id, sub_port_usage,
        usage_category, port_type, sign_source, location_server_room, sms_templates,
        status, error_msg, create_time, update_time, task_id, domain, phones,
        is_sync, is_deleted, app_name, auth_start_time, auth_end_time,
        sub_port_service_type, sub_port_service_detail, otype_server_room,
        m_upload_company_name, m_operation_type, m_port_range, m_port_type, m_gcl_id,
        m_legal_name, m_sign_check_file, m_legal_mdn, m_agency_mdn,
        t_service_type, t_letter_of_auth, t_base_sp_id, t_net_in_time,
        t_service_big_type, t_service_big_detail, t_contain_yl_mdn, t_contain_yl_url,file_batch_id
        )
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.accId}, #{item.channelId}, #{item.sign}, #{item.port}, #{item.subPort}, #{item.provinceName}, #{item.cityName},
            #{item.companyName}, #{item.companyNum}, #{item.companyCardType}, #{item.registName}, #{item.legalCertType},
            #{item.legalCertId}, #{item.agencyName}, #{item.agencyCertType}, #{item.agencyCertId}, #{item.subPortUsage},
            #{item.usageCategory}, #{item.portType}, #{item.signSource}, #{item.locationServerRoom}, #{item.smsTemplates},
            #{item.status}, #{item.errorMsg}, #{item.createTime}, #{item.updateTime}, #{item.taskId}, #{item.domain}, #{item.phones},
            #{item.isSync}, #{item.isDeleted}, #{item.appName}, #{item.authStartTime}, #{item.authEndTime},
            #{item.subPortServiceType}, #{item.subPortServiceDetail}, #{item.otypeServerRoom},
            #{item.mUploadCompanyName}, #{item.mOperationType}, #{item.mPortRange}, #{item.mPortType}, #{item.mGclId},
            #{item.mLegalName}, #{item.mSignCheckFile}, #{item.mLegalMdn}, #{item.mAgencyMdn},
            #{item.tServiceType}, #{item.tLetterOfAuth}, #{item.tBaseSpId}, #{item.tNetInTime},
            #{item.tServiceBigType}, #{item.tServiceBigDetail}, #{item.tContainYlMdn}, #{item.tContainYlUrl},#{item.fileBatchId}
            )
        </foreach>
    </insert>

    <update id="batchUpdateUploadSignStatus">
        update upload_sign_status set status=#{status}
        <if test="errorMsg!=null and errorMsg!=''">
            ,error_msg = #{errorMsg}
        </if>
        where task_id = #{taskId} and acc_id = #{accId} and sub_port in (${subPorts}) and is_deleted = 0
    </update>

    <update id="coverUploadSignStatusByIds">
        UPDATE upload_sign_status
        SET is_deleted = 2,
            cover_file_batch_id = #{batchId}
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="queryStatusAllName" resultMap="UploadSignStatusResultMap">
        SELECT
            id, company_name, company_num, company_card_type,
            regist_name, legal_cert_type, legal_cert_id,
            agency_name, agency_cert_type, agency_cert_id
        FROM upload_sign_status
        WHERE is_deleted = 0
        AND status in (1,2,3)
    </select>
</mapper>