<?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.superdesk.uniorder.dao.mapper.thirdpart.jd.SysQuartzScheduleTaskStatementOfAccountJdMapperEx">

    <resultMap id="queryGenericParamResultMap"
               type="cn.superdesk.uniorder.dto.vo.thirdpart.jd.SysQuartzScheduleTaskStatementOfAccountJdListItemVo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="dim_create_by" jdbcType="TINYINT" property="dimCreateBy"/>
        <result column="statement_of_account_name" jdbcType="VARCHAR" property="statementOfAccountName"/>
        <result column="statement_of_account_type" jdbcType="TINYINT" property="statementOfAccountType"/>
        <result column="third_part_accounting_order_sn" jdbcType="VARCHAR" property="thirdPartAccountingOrderSn"/>
        <result column="service_no" jdbcType="VARCHAR" property="serviceNo"/>
        <result column="sys_org_id" jdbcType="BIGINT" property="sysOrgId"/>
        <result column="sys_virt_id" jdbcType="BIGINT" property="sysVirtId"/>
        <result column="out_org_id" jdbcType="BIGINT" property="outOrgId"/>
        <result column="out_virt_id" jdbcType="BIGINT" property="outVirtId"/>
        <result column="sys_merchant_id" jdbcType="BIGINT" property="sysMerchantId"/>
        <result column="out_merchant_id" jdbcType="BIGINT" property="outMerchantId"/>
        <result column="cycle_begin_time" jdbcType="TIMESTAMP" property="cycleBeginTime"/>
        <result column="cycle_end_time" jdbcType="TIMESTAMP" property="cycleEndTime"/>
        <result column="is_pause" jdbcType="TINYINT" property="isPause"/>
        <result column="job_id" jdbcType="BIGINT" property="jobId"/>
        <result column="batch_id" jdbcType="VARCHAR" property="batchId"/>
        <result column="run_state" jdbcType="TINYINT" property="runState"/>
        <result column="run_time" jdbcType="TIMESTAMP" property="runTime"/>
        <result column="process_state" jdbcType="TINYINT" property="processState"/>
        <result column="process_time" jdbcType="TIMESTAMP" property="processTime"/>
        <result column="accounting_status" jdbcType="TINYINT" property="accountingStatus"/>
        <result column="accounting_time" jdbcType="TIMESTAMP" property="accountingTime"/>
        <result column="check_state" jdbcType="TINYINT" property="checkState"/>
        <result column="check_time" jdbcType="TIMESTAMP" property="checkTime"/>
        <result column="finish_time" jdbcType="TIMESTAMP" property="finishTime"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="create_user_id" jdbcType="BIGINT" property="createUserId"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="update_user_id" jdbcType="BIGINT" property="updateUserId"/>
        <result column="accounting_order_sn" jdbcType="VARCHAR" property="accountingOrderSn"/>
        <result column="statistics_amount_dim_order_outsider" jdbcType="DECIMAL" property="statisticsAmountDimOrderOutsider" />
        <result column="statistics_amount_dim_order_ownsider" jdbcType="DECIMAL" property="statisticsAmountDimOrderOwnsider" />
        <result column="statistics_quantity_dim_order_total" jdbcType="BIGINT" property="statisticsQuantityDimOrderTotal" />
        <result column="statistics_quantity_dim_order_normal" jdbcType="BIGINT" property="statisticsQuantityDimOrderNormal" />
        <result column="statistics_quantity_dim_order_exception" jdbcType="BIGINT" property="statisticsQuantityDimOrderException" />

        <result column="result" jdbcType="LONGVARCHAR" property="result"/>
        <result column="params" jdbcType="LONGVARCHAR" property="params"/>
        <result column="params_snapshot" jdbcType="LONGVARCHAR" property="paramsSnapshot"/>

        <result column="time_consuming_for_import" jdbcType="BIGINT" property="timeConsumingForImport" />
        <result column="time_consuming_for_compare" jdbcType="BIGINT" property="timeConsumingForCompare" />

        <!-- 供应商结算 对账单 部分 -->

        <result column="ausu_accounting_order_sn" jdbcType="VARCHAR" property="ausuAccountingOrderSn"/>
        <result column="ausu_accounting_status" jdbcType="TINYINT" property="ausuAccountingStatus"/>
        <result column="ausu_invoice_status" jdbcType="TINYINT" property="ausuInvoiceStatus"/>
        <result column="ausu_back_account_status" jdbcType="TINYINT" property="ausuBackAccountStatus" />
        <result column="ausu_sale_id" jdbcType="BIGINT" property="ausuSaleId"/>
        <result column="ausu_sale_name" jdbcType="VARCHAR" property="ausuSaleName"/>
        <result column="ausu_service_no" jdbcType="VARCHAR" property="ausuServiceNo"/>
        <result column="ausu_source_from" jdbcType="VARCHAR" property="ausuSourceFrom"/>

    </resultMap>


    <sql id="conditionPart">



        <if test="sysQuartzScheduleTaskStatementOfAccountJdId != null and sysQuartzScheduleTaskStatementOfAccountJdId != '' ">
            and sqstsofaj.id = #{sysQuartzScheduleTaskStatementOfAccountJdId}
        </if>

        <!-- 1.2.4.1 (5)对账单号:对当前列表数据`对账单号`字段内容,`精确`查询; 批次Id -->
        <if test="batchId != null and batchId != '' ">
            and sqstsofaj.batch_id = #{batchId}
        </if>


        <!-- 1.2.4.1 (6)结算中心账单编号:对当前列表数据`结算中心账单编号`字段内容,`精确`查询; -->
        <if test="accountingOrderSn != null and accountingOrderSn != '' ">
            and sqstsofaj.accounting_order_sn = #{accountingOrderSn}
        </if>
        <!-- 1.2.4.1 (7)第三方账单编号:对当前列表数据`第三方账单编号`字段内容,`精确`查询,多条对账任务数据可能存在同样的账单编号; 第三方账单编号 202103010000900180,202103010000900180 -->
        <if test="thirdPartAccountingOrderSn != null and thirdPartAccountingOrderSn != '' ">
            and FIND_IN_SET(#{thirdPartAccountingOrderSn},sqstsofaj.third_part_accounting_order_sn)
        </if>


        <!-- 1.2.4.1 (8)第三方订单编号:对当前列表对账任务中导入第三方账单中的`订单编号`内容,`精确`查询,多条对账任务数据可能存在同样的第三方订单编号; -->
        <!-- quoteOrderSn -->
        <!-- 1.2.4.1 (9)结算中心订单编号:对当前列表对账任务中比对的结算中心的`订单编号`内容,`精确`查询,多条对账任务数据可能存在同样的结算中心订单编号; -->
        <!-- referenceOrderSn -->


        <!-- 1.2.4.1 (10)创建人:对当前列表数据`创建人`字段内容,`模糊`查询; -->
        <if test="createUserId != null">
            and sqstsofaj.create_user_id = #{createUserId}
        </if>
        <!-- 1.2.4.1 (11)更新人:对当前列表数据`更新人`字段内容,`模糊`查询; -->
        <if test="updateUserId != null">
            and sqstsofaj.update_user_id = #{updateUserId}
        </if>

        <!-- 1.2.4.9 账单日期 -->
        <if test="cycleBeginTime != null and cycleBeginTime != '' ">
            and DATE_FORMAT(sqstsofaj.cycle_begin_time,'%Y-%m-%d') = #{cycleBeginTime}
        </if>
        <if test="cycleEndTime != null and cycleEndTime != '' ">
            and DATE_FORMAT(sqstsofaj.cycle_end_time,'%Y-%m-%d') = #{cycleEndTime}
        </if>


        <!-- 1.2.4.10 创建日期 -->
        <if test="createTimeStartTime != null and createTimeStartTime != ''">
            and DATE_FORMAT(sqstsofaj.create_time,'%Y-%m-%d') &gt;= #{createTimeStartTime,jdbcType=VARCHAR}
        </if>
        <if test="createTimeEndTime != null and createTimeEndTime != ''">
            and DATE_FORMAT(sqstsofaj.create_time,'%Y-%m-%d') &lt;= #{createTimeEndTime,jdbcType=VARCHAR}
        </if>

        <!-- 1.2.4.10 账单周期 -->
        <if test="cycleTime != null and cycleTime != ''">
            and #{cycleTime,jdbcType=VARCHAR} &gt;= DATE_FORMAT(sqstsofaj.cycle_begin_time,'%Y-%m-%d')
            and #{cycleTime,jdbcType=VARCHAR} &lt;= DATE_FORMAT(sqstsofaj.cycle_end_time,'%Y-%m-%d')
        </if>


        <!--     1.2.4.2 供应商
        (1)初始:默认显示为`请选择`,即全部;
        (2)数据:
        ①来源:取侨城汇结算中心供应商管理中所有的供应商,含已删除|已禁用的供应商,
        数据内容:供应商名称(服务编号)
        ②若无数据,即下拉列表为空显示,
        ③排序:按结算中心该供应商ID从小到大依次排列显示,
        (3)显示:供应商名称若当前名称内容显示不全时,末尾`...`省略显示,
        (4)下拉选框:支持按供应商名称内容`模糊`搜索,单选, -->
        <if test="sysMerchantId != null">
            and sqstsofaj.sys_merchant_id = #{sysMerchantId}
        </if>
        <if test="outMerchantId != null">
            and sqstsofaj.out_merchant_id = #{outMerchantId}
        </if>




        <!-- and runState != '' -->
        <if test="runState != null">
            and sqstsofaj.run_state = #{runState}
        </if>

        <if test="statementOfAccountType != null and statementOfAccountType != '' ">
            and sqstsofaj.statement_of_account_type = #{statementOfAccountType}
        </if>


        <!-- 1.2.4.3 导入状态
        (1)初始:默认`全部`,显示为`请选择`,
        (2)选择:单选,`导入中|导入成功|导入失败`,
        导入中:指该对账任务正在上传服务端中的状态,对账任务创建的初始状态,
        导入成功:指该对账任务上传服务端成功And符合数据检查条件的状态,
        导入失败:指该对账任务上传服务失败或其他异常或不符合数据检查条件的状态, -->
        <if test="processState != null">
            and sqstsofaj.process_state = #{processState}
        </if>

        <!--     1.2.4.4 对账状态
        (1)初始:默认`全部`,显示为`请选择`,
        (2)选择:单选,`待对账|对账中|对账完成`,
        待对账:指该对账任务还未开始进行与结算中心供应商未结算商品订单对账的状态,
        对账中:指该对账任务正在与结算中心供应商未结算商品订单进行对账,还未完成进行中的状态,
        对账完成:指该对账任务已全部对账完成的状态, -->
        <if test="accountingStatus != null">
            and sqstsofaj.accounting_status = #{accountingStatus}
        </if>
        <!--     1.2.4.5 对账结果
        (1)初始:默认`全部`,显示为`请选择`,
        (2)选择:单选,`正常|异常|待对账`,
        正常:指该对账任务下所有第三方订单的对账结果全部为`正常`的状态,
        异常:指该对账任务下所有第三方订单的对账结果存在部分`异常`或`待对账`的状态,
        待对账:指该对账任务下所有第三方订单的对账结果全部为`待对账`的状态, -->
        <if test="checkState != null">
            and sqstsofaj.check_state = #{checkState}
        </if>


        <!-- 供应商结算 对账单 部分 start -->

        <!-- 1.2.4.6 对账单状态
        (1)初始:默认`全部`,显示为`请选择`;
        (2)选择:单选,`待确认|已确认|已取消`;
        指该对账任务确认对账后在`结算中心-供应商结算-对账单`列表该对账单状态;
        若该对账任务的对账单状态初始均为`待确认`; -->
        <if test="ausuAccountingStatus != null and ausuAccountingStatus != '' ">
            and ausu.accounting_status = #{ausuAccountingStatus}
        </if>
        <if test="ausuAccountingStatus != null and ausuAccountingStatus == 0 ">
            and ausu.accounting_status is NULL
        </if>
        <!--     1.2.4.7 申请状态
        (1)初始:默认`全部`,显示为`请选择`;
        (2)选择:单选,`未申请|待开票|待收票|待付款|已完成|已作废`;
        指该对账任务确认对账后在`结算中心-供应商结算-对账单`列表该对账单的申请状态;
        若该对账任务对账单的申请状态初始均为`未申请`; -->
        <if test="ausuInvoiceStatus != null and ausuInvoiceStatus != '' ">
            and ausu.invoice_status = #{ausuInvoiceStatus}
        </if>
        <if test="ausuInvoiceStatus != null and ausuInvoiceStatus == 0 ">
            and ausu.invoice_status is NULL
        </if>
        <!--     1.2.4.8 付款状态
        (1)初始:默认`全部`,显示为`请选择`;
        (2)选择:单选,`未付款|付款中|已付款`;
        指该对账任务确认对账后在`结算中心-供应商结算-对账单`列表该对账单的付款状态;
        若该对账任务对账单的付款状态初始均为`未付款`; -->
        <if test="ausuBackAccountStatus != null and ausuBackAccountStatus != '' ">
            and ausu.back_account_status = #{ausuBackAccountStatus}
        </if>
        <if test="ausuBackAccountStatus != null and ausuBackAccountStatus == 0 ">
            and ausu.back_account_status is NULL
        </if>


        <!-- 供应商结算 对账单 部分   end -->

        <!-- 1.2.4.1 (8)第三方订单编号:对当前列表对账任务中导入第三方账单中的`订单编号`内容,`精确`查询,多条对账任务数据可能存在同样的第三方订单编号; -->
        <if test="quoteOrderSn != null and quoteOrderSn != '' ">
            and sqstsofaj.batch_id IN (
                <include refid="subQuery"></include>
            )
        </if>
        <!-- 1.2.4.1 (9)结算中心订单编号:对当前列表对账任务中比对的结算中心的`订单编号`内容,`精确`查询,多条对账任务数据可能存在同样的结算中心订单编号; -->
        <if test="referenceOrderSn != null and referenceOrderSn != '' ">
            and sqstsofaj.batch_id IN (
            <include refid="subQuery"></include>
            )
        </if>



    </sql>

    <sql id="subQuery">
        SELECT
            DISTINCT batch_id
        FROM
            accounting_unified_supplier_order_jd_check_up_report
        <where>

            <if test="batchId != null and batchId != '' ">
                and batch_id = #{batchId}
            </if>

            <if test="quoteOrderSn != null and quoteOrderSn != '' ">
                and jd_order_sn = #{quoteOrderSn}
            </if>

            <if test="referenceOrderSn != null and referenceOrderSn != '' ">
                and order_sn = #{referenceOrderSn}
            </if>

        </where>

    </sql>


    <select id="queryGenericParam" resultMap="queryGenericParamResultMap">

        SELECT
            sqstsofaj.id,
            sqstsofaj.dim_create_by,
            sqstsofaj.statement_of_account_name,
            sqstsofaj.statement_of_account_type,
            sqstsofaj.third_part_accounting_order_sn,
            sqstsofaj.service_no,
            sqstsofaj.sys_org_id,
            sqstsofaj.sys_virt_id,
            sqstsofaj.out_org_id,
            sqstsofaj.out_virt_id,
            sqstsofaj.sys_merchant_id,
            sqstsofaj.out_merchant_id,
            sqstsofaj.cycle_begin_time,
            sqstsofaj.cycle_end_time,
            sqstsofaj.is_pause,
            sqstsofaj.job_id,
            sqstsofaj.batch_id,
            sqstsofaj.run_state,
            sqstsofaj.run_time,
            sqstsofaj.process_state,
            sqstsofaj.process_time,
            sqstsofaj.accounting_status,
            sqstsofaj.accounting_time,
            sqstsofaj.check_state,
            sqstsofaj.check_time,
            sqstsofaj.finish_time,
            sqstsofaj.remark,
            sqstsofaj.create_time,
            sqstsofaj.create_user_id,
            sqstsofaj.update_time,
            sqstsofaj.update_user_id,
            sqstsofaj.accounting_order_sn,
            sqstsofaj.statistics_amount_dim_order_outsider,
            sqstsofaj.statistics_amount_dim_order_ownsider,
            sqstsofaj.statistics_quantity_dim_order_total,
            sqstsofaj.statistics_quantity_dim_order_normal,
            sqstsofaj.statistics_quantity_dim_order_exception,
            sqstsofaj.result,
            sqstsofaj.params,
            sqstsofaj.params_snapshot,

            sqstsofaj.time_consuming_for_import,
            sqstsofaj.time_consuming_for_compare,

            ausu.accounting_order_sn as ausu_accounting_order_sn,
            ausu.accounting_status as ausu_accounting_status,
            ausu.invoice_status as ausu_invoice_status,
            ausu.back_account_status as ausu_back_account_status,
            ausu.sale_id as ausu_sale_id,
            ausu.sale_name as ausu_sale_name,
            ausu.service_no as ausu_service_no,
            ausu.source_from as ausu_source_from
        FROM
            sys_quartz_schedule_task_statement_of_account_jd sqstsofaj
            LEFT JOIN accounting_unified_supplier ausu ON sqstsofaj.accounting_order_sn = ausu.accounting_order_sn
        <where>
            <include refid="conditionPart"></include>
        </where>
        <choose>
            <when test="orderByClause != null and orderByClause != '' ">
                order by ${orderByClause}
            </when>
            <otherwise>
                order by sqstsofaj.update_time DESC
            </otherwise>
        </choose>

    </select>

    <resultMap id="queryHeadGenericParamResultMap" type="cn.superdesk.uniorder.dto.vo.thirdpart.jd.SysQuartzScheduleTaskStatementOfAccountJdHeadVo">

        <result column="jd_accounting_quantity" jdbcType="INTEGER" property="jdAccountingQuantity"/>

        <result column="statistics_amount_dim_order_outsider" jdbcType="DECIMAL" property="statisticsAmountDimOrderOutsider" />
        <result column="statistics_amount_dim_order_ownsider" jdbcType="DECIMAL" property="statisticsAmountDimOrderOwnsider" />
        <result column="statistics_quantity_dim_order_total" jdbcType="BIGINT" property="statisticsQuantityDimOrderTotal" />
        <result column="statistics_quantity_dim_order_normal" jdbcType="BIGINT" property="statisticsQuantityDimOrderNormal" />
        <result column="statistics_quantity_dim_order_exception" jdbcType="BIGINT" property="statisticsQuantityDimOrderException" />
    </resultMap>

    <select id="queryHeadGenericParam" resultMap="queryHeadGenericParamResultMap">
        SELECT
            count(sqstsofaj.id) AS jd_accounting_quantity,
            SUM(sqstsofaj.statistics_amount_dim_order_outsider) as statistics_amount_dim_order_outsider,
            SUM(sqstsofaj.statistics_amount_dim_order_ownsider) as statistics_amount_dim_order_ownsider,
            SUM(sqstsofaj.statistics_quantity_dim_order_total) as statistics_quantity_dim_order_total,
            SUM(sqstsofaj.statistics_quantity_dim_order_normal) as statistics_quantity_dim_order_normal,
            SUM(sqstsofaj.statistics_quantity_dim_order_exception) as statistics_quantity_dim_order_exception
        FROM
            sys_quartz_schedule_task_statement_of_account_jd sqstsofaj
            LEFT JOIN accounting_unified_supplier ausu ON sqstsofaj.accounting_order_sn = ausu.accounting_order_sn
        <where>
            <include refid="conditionPart"></include>
        </where>
    </select>

</mapper>