<?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.base.UnifiedOrderMapperExCustomerDimGoods">


    <!--  autoMapping="true"  -->
    <resultMap id="CustomerUnAccountingDimGoodsVoResultMap"
               type="cn.superdesk.uniorder.dto.vo.accounting.unified.customer.notyet.CustomerUnAccountingDimGoodsVo">

        <!--        uog.id as unified_order_goods_id,-->
        <id column="unified_order_goods_id" property="unifiedOrderGoodsId" jdbcType="BIGINT"/>
        <!--        uo.id as unified_order_id,-->
        <result column="unified_order_id" property="unifiedOrderId" jdbcType="BIGINT"/>
        <!--        uog.order_sn,-->
        <result column="order_sn" property="orderSn" jdbcType="VARCHAR"/>

        <!-- uo.amount as order_amount,-->
        <result column="order_amount" property="orderAmount" jdbcType="DECIMAL"/>
        <!-- uo.cost_amount as order_cost_amount,-->
        <result column="order_cost_amount" property="orderCostAmount" jdbcType="DECIMAL"/>
        <!-- uo.transport_amount as order_transport_amount,-->
        <result column="order_transport_amount" property="orderTransportAmount" jdbcType="DECIMAL"/>

        <!-- uo.project_id as out_org_id,-->
        <!-- uo.project_name as out_org_name,-->
        <result column="out_org_id" property="outOrgId" jdbcType="BIGINT"/>
        <result column="out_org_name" property="outOrgName" jdbcType="VARCHAR"/>

        <result column="purchase_virt_id" property="purchaseVirtId" jdbcType="BIGINT"/>
        <result column="purchase_virt_name" property="purchaseVirtName" jdbcType="VARCHAR"/>
        <result column="purchase_user_id" property="purchaseUserId" jdbcType="BIGINT"/>
        <result column="purchase_user_name" property="purchaseUserName" jdbcType="VARCHAR"/>
        <result column="purchase_phone" property="purchasePhone" jdbcType="VARCHAR"/>

        <result column="express_sn" property="expressSn" jdbcType="VARCHAR"/>
        <result column="express" property="express" jdbcType="VARCHAR"/>
        <result column="express_company" property="expressCompany" jdbcType="VARCHAR"/>

        <result column="sale_id" property="saleId" jdbcType="BIGINT"/>
        <result column="sale_name" property="saleName" jdbcType="VARCHAR"/>

        <result column="order_state" property="orderState" jdbcType="INTEGER"/>
        <result column="order_state_name" property="orderStateName" jdbcType="VARCHAR"/>
        <result column="pay_type" property="payType" jdbcType="INTEGER"/>
        <result column="pay_type_name" jdbcType="VARCHAR" property="payTypeName" />
        <result column="pay_state" property="payState" jdbcType="INTEGER"/>
        <result column="pay_state_name" jdbcType="VARCHAR" property="payStateName" />

        <!-- uo.service_name, 弃用 改用定义 mark by linjinyu at 20220624 -->
        <result column="service_name" property="serviceName" jdbcType="VARCHAR"/>
        <!-- uo.service_no,-->
        <result column="service_no" property="serviceNo" jdbcType="VARCHAR"/>

        <result column="rec_user_name" property="recUserName" jdbcType="VARCHAR"/>
        <result column="rec_user_id" property="recUserId" jdbcType="BIGINT"/>
        <result column="rec_user_phone" property="recUserPhone" jdbcType="VARCHAR"/>
        <result column="rec_address" property="recAddress" jdbcType="VARCHAR"/>


        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="pay_time"     property="payTime"    jdbcType="TIMESTAMP"/>
        <result column="send_time"    property="sendTime"   jdbcType="TIMESTAMP"/>
        <result column="finish_time"  property="finishTime" jdbcType="TIMESTAMP"/>

        <result column="remarks" property="remarks" jdbcType="VARCHAR"/>

        <result column="goods_id" jdbcType="BIGINT" property="goodsId" />
        <result column="out_snapshot_order_goods_id" jdbcType="BIGINT" property="outSnapshotOrderGoodsId" />
        <result column="goods_name" property="goodsName" jdbcType="VARCHAR"/>
        <result column="goods_alias" jdbcType="VARCHAR" property="goodsAlias" />

        <result column="quantity" property="quantity" jdbcType="INTEGER"/>
        <result column="price" property="price" jdbcType="DECIMAL"/>
        <result column="cost_price" property="costPrice" jdbcType="DECIMAL"/>
        <!-- uog.price * uog.quantity as goods_amount,-->
        <!-- uog.cost_price * uog.quantity as goods_cost_amount,-->
        <result column="goods_amount" property="goodsAmount" jdbcType="DECIMAL"/>
        <result column="goods_cost_amount" property="goodsCostAmount" jdbcType="DECIMAL"/>

<!--        <result column="direction" jdbcType="TINYINT" property="direction" />-->

        <result column="cate_id_lv1" property="cateIdLv1" jdbcType="BIGINT"/>
        <result column="cate_name_lv1" property="cateNameLv1" jdbcType="VARCHAR"/>
        <result column="cate_id_lv2" property="cateIdLv2" jdbcType="BIGINT"/>
        <result column="cate_name_lv2" property="cateNameLv2" jdbcType="VARCHAR"/>
        <result column="cate_id_lv3" property="cateIdLv3" jdbcType="BIGINT"/>
        <result column="cate_name_lv3" property="cateNameLv3" jdbcType="VARCHAR"/>
        <result column="self_option" jdbcType="VARCHAR" property="selfOption" />
        <result column="unit" property="unit" jdbcType="VARCHAR"/>

        <result column="fiscal_code" jdbcType="VARCHAR" property="fiscalCode" />
        <result column="tax_code" jdbcType="VARCHAR" property="taxCode" />
        <result column="tax_rate" jdbcType="DECIMAL" property="taxRate" />
        <result column="merch_tax_id" jdbcType="BIGINT" property="merchTaxId" />
        <result column="merch_tax_code" jdbcType="VARCHAR" property="merchTaxCode" />
        <result column="merch_tax_rate" jdbcType="DECIMAL" property="merchTaxRate" />
        <result column="operation_tax_id" jdbcType="BIGINT" property="operationTaxId" />
        <result column="operation_tax_code" jdbcType="VARCHAR" property="operationTaxCode" />
        <result column="operation_tax_rate" jdbcType="DECIMAL" property="operationTaxRate" />

<!--        <result column="goods_params" property="goodsParams" jdbcType="VARCHAR"/>-->

        <!-- 发票申请状态 是否已申请 0-未申请;1-已申请; -->
        <!--		uog.invoice_apply_status,-->
        <!--        <result column="invoice_apply_status" property="invoiceApplyStatus" jdbcType="INTEGER"/>-->
        <!-- 开票状态 是否已登记开票 0-未登记;1-已登记; -->
        <!--		uog.invoice_make_status,-->
        <!--        <result column="invoice_make_status" property="invoiceMakeStatus" jdbcType="INTEGER"/>-->
        <!-- 回款状态 是否回款 0-未回款;1-已回款; -->
        <!--		uog.back_status,-->
        <!--        <result column="back_status" property="backStatus" jdbcType="INTEGER"/>-->

        <!-- 客户对账:1-已对账;2-未对账 -->
        <result column="accounting_state_customer" property="accountingStateCustomer" jdbcType="INTEGER"/>
        <!-- 供应商对账:1-已对账;2-未对账 -->
        <result column="accounting_state_supplier" property="accountingStateSupplier" jdbcType="INTEGER"/>

    </resultMap>

    <!-- 客户对账 -> 未对账商品 共用 -->
    <sql id="getCustomerUnAccountingDimGoodsWhereIf">

        and uo.complet_state != 3
        <!--        <choose>-->
        <!--            <when test="payType != null and payType != '' or payType == 0">-->
        <!--                and uoc.pay_type = #{payType}-->
        <!--            </when>-->
        <!--            <otherwise>-->

        <!--            </otherwise>-->
        <!--        </choose>-->



        <!-- 订单编号 uo.order_sn -->
        <if test="orderSn != null and orderSn != '' ">
            and uo.order_sn like CONCAT('%',#{orderSn},'%')
        </if>

        <if test="supplierId != null and supplierId != '' ">
            and uo.sale_id = #{supplierId}
        </if>
        <if test="supplierName != null and supplierName != '' ">
            and uo.sale_name like CONCAT('%',#{supplierName},'%')
        </if>

        <if test="ids != null and ids != ''">
            and uog.id in
            <foreach collection="ids" index="index" item="unified_order_goods_id" open="(" close=")" separator=",">
                #{unified_order_goods_id}
            </foreach>
        </if>

        <if test="outVirtIds != null and outVirtIds != ''"><!-- 已修正 数据权限 by linjinyu at 2020090818 -->
            <!-- project_id -->
            and uo.purchase_virt_id in
            <foreach collection="outVirtIds" index="index" item="pid" open="(" close=")" separator=",">
                #{pid}
            </foreach>
        </if>

        <if test="outServices != null and outServices.size >0 ">
            and uo.service_no in
            <foreach collection="outServices" index="index" item="serviceNo" open="(" close=")" separator=",">
                #{serviceNo}
            </foreach>
        </if>
        <if test="serviceNo != null and serviceNo != '' ">
            and uo.service_no = #{serviceNo}
        </if>


        <if test="payType != null">
            and uo.pay_type = #{payType}
        </if>
        <if test="payTypeName != null and payTypeName != '' ">
            and uo.pay_type_name like CONCAT('%',#{payTypeName},'%')
        </if>

        <if test="orderState != null">
            and uo.order_state = #{orderState}
        </if>
        <if test="orderStateName != null and orderStateName != '' ">
            and uo.order_state_name like CONCAT('%',#{orderStateName},'%')
        </if>

        <!-- 下单时间 uo.create_time -->
        <if test="startTime != null and startTime != ''">
            and DATE_FORMAT(uo.create_time,'%Y-%m-%d') &gt;= #{startTime,jdbcType=VARCHAR}
        </if>
        <if test="endTime != null and endTime != ''">
            and DATE_FORMAT(uo.create_time,'%Y-%m-%d') &lt;= #{endTime,jdbcType=VARCHAR}
        </if>

        <!-- 下单人姓名 uo.purchase_user_name -->
        <if test="purchaseUserId != null and purchaseUserId != '' ">
            and uo.purchase_user_id = #{purchaseUserId}
        </if>
        <if test="purchaseUserName != null and purchaseUserName != '' ">
            and uo.purchase_user_name like CONCAT('%',#{purchaseUserName},'%')
        </if>
        <if test="purchasePhone != null and purchasePhone != '' ">
            and uo.purchase_phone = #{purchasePhone}
        </if>


        <!-- 企业名称 uo.purchase_virt_name -->
        <if test="purchaseVirtId != null and purchaseVirtId != '' ">
            and uo.purchase_virt_id = #{purchaseVirtId}
        </if>
        <if test="purchaseVirtName != null and purchaseVirtName != '' ">
            and uo.purchase_virt_name like CONCAT('%',#{purchaseVirtName},'%')
        </if>





        <!-- TODO 标记 20201021 and uog.id in  () 不见了  order by -->
        <if test="unifiedOrderGoodsIds != null and unifiedOrderGoodsIds.size > 0">
            and uog.id in
            <foreach collection="unifiedOrderGoodsIds" index="index" item="unified_order_goods_id" open="(" close=")" separator=",">
                #{unified_order_goods_id}
            </foreach>
        </if>


    </sql>

    <sql id="getCustomerUnAccountingDimGoodsWhereIfAccountingStateCustomer">
        <choose>
            <when test="accountingStateCustomer != null and accountingStateCustomer != '' or accountingStateCustomer == 0">
                and accounting_state_customer = #{accountingStateCustomer}
            </when>
            <otherwise>
                <!--
                    and uog.accounting_state_customer = 2
                -->
            </otherwise>
        </choose>
    </sql>

    <!-- 客户对账 -> 未对账商品 列表 | 此页面是以商品流水的维度去展示的 opt 搜索 下单时间 只能以订单维度 最终 -->
    <select id="getCustomerUnAccountingDimGoodsList" parameterType="Map"
            resultMap="CustomerUnAccountingDimGoodsVoResultMap">
        SELECT


            uog.id as unified_order_goods_id,
            uo.id as unified_order_id,
            uog.order_sn,
            <!--
            uo.order_sn,
            -->

            uo.amount as order_amount,
            uo.cost_amount as order_cost_amount,
            uo.transport_amount as order_transport_amount,

            uo.project_id as out_org_id,
            uo.project_name as out_org_name,

            uo.purchase_virt_id,
            uo.purchase_virt_name,

            uo.purchase_user_id,
            uo.purchase_user_name,
            uo.purchase_phone,

            uo.express_sn,
            uo.express,
            uo.express_company,

            uo.order_state,
            uo.order_state_name,

            uo.sale_id,
            uo.sale_name,

            uo.pay_type,
            uo.pay_type_name,
            uo.pay_state,
            uo.pay_state_name,

            <!-- 弃用 改用定义 mark by linjinyu at 20220624 -->
            uo.service_name,
            uo.service_no,

            uo.rec_user_name,
            uo.rec_user_id,
            uo.rec_user_phone,
            uo.rec_address,

            uo.create_time,
            uo.update_time,
            uo.pay_time,
            uo.send_time,
            uo.finish_time,

            uo.remarks,

            uog.goods_id,
            uog.out_snapshot_order_goods_id,
            uog.goods_name,
            uog.goods_alias,

            uog.price,
            uog.cost_price,
            uog.quantity,
            uog.price * uog.quantity as goods_amount,
            uog.cost_price * uog.quantity as goods_cost_amount,

            uog.cate_id_lv1,
            uog.cate_name_lv1,
            uog.cate_id_lv2,
            uog.cate_name_lv2,
            uog.cate_id_lv3,
            uog.cate_name_lv3,
            uog.self_option,
            uog.unit,

            uog.fiscal_code,
            uog.tax_code,
            uog.tax_rate,
            uog.merch_tax_id,
            uog.merch_tax_code,
            uog.merch_tax_rate,
            uog.operation_tax_id,
            uog.operation_tax_code,
            uog.operation_tax_rate,

            <!-- 客户对账:1-已对账;2-未对账 -->
            uog.accounting_state_customer,
            <!-- 供应商对账:1-已对账;2-未对账 -->
            uog.accounting_state_supplier

            <!-- 发票申请状态 是否已申请 0-未申请;1-已申请;
            uog.invoice_apply_status,
            -->
            <!-- 开票状态 是否已登记开票 0-未登记;1-已登记;
            uog.invoice_make_status,
             -->
            <!-- 回款状态 是否回款 0-未回款;1-已回款;
            uog.back_status,
            -->


        FROM
            unified_order_goods uog
            LEFT JOIN unified_order uo on uo.order_sn = uog.order_sn
        <where>
            <include refid="getCustomerUnAccountingDimGoodsWhereIf"></include>
            <!-- 客户对账 状态:1-已对账;2-未对账 -->
            <choose>
                <when test="accountingStateCustomer != null and accountingStateCustomer != ''">
                    and uog.accounting_state_customer = #{accountingStateCustomer}
                </when>
                <otherwise>
                    <!--
                        and uog.accounting_state_customer = 2
                    -->
                </otherwise>
            </choose>
            <choose>
                <when test="hasOutVirtId != null and hasOutVirtId != '' and hasOutVirtId == 1">
                    and uo.purchase_virt_id IS NOT NULL
                </when>
                <when test="hasOutVirtId != null and hasOutVirtId != '' and hasOutVirtId == 2">
                    and uo.purchase_virt_id IS NULL
                </when>
                <otherwise>

                </otherwise>
            </choose>

            <choose>
                <when test="orderBy != null and orderBy != '' ">
                    order by ${orderBy}
                </when>
                <otherwise>
                    order by uo.create_time desc, uo.order_sn desc, uog.id asc
                </otherwise>
            </choose>
        </where>


    </select>



    <resultMap id="UnifiedOrderCustomerHeadSumResultMap" type="cn.superdesk.uniorder.dto.vo.accounting.unified.customer.notyet.UnifiedOrderCustomerHeadSumVo">

        <result column="total" property="total" jdbcType="INTEGER"/>

        <result column="order_amount" property="orderAmount" jdbcType="DECIMAL"/>
        <result column="order_cost_amount" property="orderCostAmount" jdbcType="DECIMAL"/>

        <result column="goods_cost_amount" property="goodsCostAmount" jdbcType="DECIMAL"/>
        <result column="transport_cost_amount" property="transportCostAmount" jdbcType="DECIMAL"/>
        <result column="refund_cost_amount" property="refundCostAmount" jdbcType="DECIMAL"/>

    </resultMap>

    <select id="getCustomerUnAccountingDimGoodsHeadSum" resultMap="UnifiedOrderCustomerHeadSumResultMap">
        SELECT
            count(uo.id) as total,
            IFNULL(sum(uo.amount),0) order_amount,
            IFNULL(sum(uo.cost_amount),0) order_cost_amount,
            IFNULL(sum(uog1.goods_cost_amount),0) as goods_cost_amount,
            IFNULL(sum(uog0.goods_cost_amount),0) as transport_cost_amount,
            IFNULL(sum(uog2.goods_cost_amount),0) as refund_cost_amount
        from
            unified_order uo
            LEFT JOIN (
                SELECT
                    order_sn,
                    sum(cost_price * quantity) as goods_cost_amount,
                    sum(price * quantity) as goods_amount
                FROM unified_order_goods
                <where>
                    AND direction = 1
                    AND goods_id != 0
                    <include refid="getCustomerUnAccountingDimGoodsWhereIfAccountingStateCustomer"></include>
                </where>
                GROUP by order_sn
            ) uog1 on uo.order_sn = uog1.order_sn
            LEFT JOIN (
                SELECT
                    order_sn,
                    sum(cost_price * quantity) as goods_cost_amount,
                    sum(price * quantity) as goods_amount
                FROM unified_order_goods
                <where>
                    AND direction = -1
                    <include refid="getCustomerUnAccountingDimGoodsWhereIfAccountingStateCustomer"></include>
                </where>
                GROUP by order_sn
            ) uog2 on uo.order_sn = uog2.order_sn
            LEFT JOIN (
                SELECT
                    order_sn,
                    sum(cost_price * quantity) as goods_cost_amount,
                    sum(price * quantity) as goods_amount
                FROM unified_order_goods
                <where>
                    AND direction = 1
                    AND goods_id = 0
                    <include refid="getCustomerUnAccountingDimGoodsWhereIfAccountingStateCustomer"></include>
                </where>
                GROUP by order_sn
            ) uog0 on uo.order_sn = uog0.order_sn
        <where>
            <include refid="getCustomerUnAccountingDimGoodsWhereIf"></include>
            <choose>
                <when test="hasOutVirtId != null and hasOutVirtId != '' and hasOutVirtId == 1">
                    and uo.purchase_virt_id IS NOT NULL
                </when>
                <when test="hasOutVirtId != null and hasOutVirtId != '' and hasOutVirtId == 2">
                    and uo.purchase_virt_id IS NULL
                </when>
                <otherwise>

                </otherwise>
            </choose>
        </where>
    </select>
</mapper>