<?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._dashboard.TvDashboardMapper">



    <resultMap id="HeadDataResultMap" type="cn.superdesk.uniorder.dto.vo._dashboard.TvHeadDataVo">

        <result column="org_count" property="orgCount" jdbcType="BIGINT"/>
        <result column="org_name_count" property="orgNameCount" jdbcType="BIGINT"/>

        <result column="virt_count" property="virtCount" jdbcType="BIGINT"/>
        <result column="user_count" property="userCount" jdbcType="BIGINT"/>

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

        <result column="order_transport_amount" property="orderTransportAmount" jdbcType="DECIMAL"/>

        <result column="order_refund_amount" property="orderRefundAmount" jdbcType="DECIMAL"/>
        <result column="order_refund_cost_amount" property="orderRefundCostAmount" jdbcType="DECIMAL"/>

        <result column="goods_amount" property="goodsAmount" jdbcType="DECIMAL"/>
        <result column="goods_cost_amount" property="goodsCostAmount" jdbcType="DECIMAL"/>

        <result column="goods_transport_amount" property="goodsTransportAmount" jdbcType="DECIMAL"/>

        <result column="goods_refund_amount" property="goodsRefundAmount" jdbcType="DECIMAL"/>
        <result column="goods_refund_cost_amount" property="goodsRefundCostAmount" jdbcType="DECIMAL"/>

        <result column="order_count" property="orderCount" jdbcType="BIGINT"/>

    </resultMap>

    <!-- 项目采购情况 getOrderManagerGoodsPageHead -->
    <select id="getHeadData" resultMap="HeadDataResultMap">

        SELECT

            COUNT(DISTINCT uo.project_id) as org_count,
            COUNT(DISTINCT uo.project_name) as org_name_count,
            COUNT(DISTINCT uo.purchase_virt_id) as virt_count,
            COUNT(DISTINCT uo.purchase_user_id) as user_count,

            SUM(IFNULL(uo.amount,0)) as order_amount_sum_if,

            IFNULL(sum(uo.amount),0) as order_amount,
            IFNULL(sum(uo.cost_amount),0) as order_cost_amount,

            IFNULL(sum(uor.order_refund_amount),0) as order_refund_amount,
            IFNULL(sum(uor.order_refund_cost_amount),0) as order_refund_cost_amount,

            IFNULL(sum(uo.transport_amount),0) as order_transport_amount,

            ifnull(sum(uog1.goods_amount),0) as goods_amount,
            ifnull(sum(uog1.goods_cost_amount),0) as goods_cost_amount,

            ifnull(sum(uog2.goods_amount),0) as goods_refund_amount,
            ifnull(sum(uog2.goods_cost_amount),0) as goods_refund_cost_amount,

            ifnull(sum(uog0.goods_amount),0) as goods_transport_amount,

            COUNT(uo.id) as order_count
        FROM
            unified_order uo

            RIGHT JOIN (
                SELECT
                    sum(price * quantity) as goods_amount,
                    sum(cost_price * quantity) as goods_cost_amount,
                    order_sn
                FROM
                    unified_order_goods
                <where>
                    AND direction = 1
                    AND goods_id != 0
                    <include refid="cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderMapperEx.orderMamagerWhereIfGoodsName"></include>
                </where>
            GROUP by order_sn
            ) uog1 on uo.order_sn = uog1.order_sn

            LEFT JOIN (
                SELECT
                    sum(price * quantity) as goods_amount,
                    sum(cost_price * quantity) as goods_cost_amount,
                    order_sn
                FROM
                    unified_order_goods
                <where>
                    AND direction = -1
                    <include refid="cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderMapperEx.orderMamagerWhereIfGoodsName"></include>
                </where>
                GROUP by order_sn
            ) uog2 on uo.order_sn = uog2.order_sn

            LEFT JOIN (
                SELECT
                    sum(price * quantity) as goods_amount,
                    sum(cost_price * quantity) as goods_cost_amount,
                    order_sn
                FROM
                    unified_order_goods
                <where>
                    AND direction = 1
                    AND goods_id = 0
                    <include refid="cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderMapperEx.orderMamagerWhereIfGoodsName"></include>
                </where>
                GROUP by order_sn
            ) uog0 on uo.order_sn = uog0.order_sn

            LEFT JOIN (
                SELECT
                    sum(amount) order_refund_amount,
                    0 as order_refund_cost_amount,
                    order_sn
                from
                    unified_order_return
                GROUP BY order_sn
            ) uor on uo.order_sn = uor.order_sn

        <where>
            AND uo.complet_state in (1,2)

<!--        <if test="virtIds != null and virtIds != ''">-->
<!--            and uo.purchase_virt_id in (${virtIds})-->
<!--        </if>-->

        <if test="queryYearMonth != null and queryYearMonth != ''">
            and DATE_FORMAT(uo.create_time,'%Y-%m') = #{queryYearMonth}
        </if>
        <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>

            <include refid="cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderMapperEx.orderMamagerWhereIf"></include>
        </where>

    </select>




    <resultMap id="MapDataResultMap" type="cn.superdesk.uniorder.dto.vo._dashboard.TvMapDataVo">
        <result column="org_id" property="orgId" jdbcType="BIGINT"/>
        <result column="org_name" property="orgName" jdbcType="VARCHAR"/>
        <result column="order_amount" property="orderAmount" jdbcType="DECIMAL"/>
        <result column="order_count" property="orderCount" jdbcType="BIGINT"/>
        <result column="lng" property="lng" jdbcType="DECIMAL"/>
        <result column="lat" property="lat" jdbcType="DECIMAL"/>
    </resultMap>

    <!-- 项目采购情况 -->
    <select id="getMapData" resultMap="MapDataResultMap">

        SELECT
            uo.project_id as org_id,
            pro.organization_name as org_name,
            sum(IFNULL(uo.amount,0)) as order_amount,
            COUNT(uo.id) as order_count,
            pro.lng,
            pro.lat
        from
            unified_order uo
            LEFT JOIN sys_organization pro on uo.project_id = pro.organization_id
        <where>
            and uo.complet_state in ( 1,2)

            <if test="virtIds != null and virtIds != ''">
                and uo.purchase_virt_id in (${virtIds})
            </if>
            <if test="queryYearMonth != null and queryYearMonth != ''">
                and DATE_FORMAT(uo.create_time,'%Y-%m') = #{queryYearMonth}
            </if>

            <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>
        </where>

        GROUP BY uo.project_id
        ORDER BY sum(IFNULL(uo.amount,0)) desc

    </select>
</mapper>