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


	<resultMap id="BaseResultMap" type="cn.superdesk.uniorder.dao.entity._dashboard.TempDashboard">
		<id column="id" property="id" jdbcType="BIGINT"/>
	</resultMap>
	<resultMap id="DashboardProjectPurchaseResultMap" type="cn.superdesk.uniorder.dto.vo._dashboard.UnifiedDashboardProjectPurchaseVo">
		<result column="projectId" property="projectId" jdbcType="BIGINT"/>
		<result column="projectName" property="projectName" jdbcType="VARCHAR"/>
		<result column="orderAmount" property="orderAmount" jdbcType="DECIMAL"/>
		<result column="lng" property="lng" jdbcType="DECIMAL"/>
		<result column="lat" property="lat" jdbcType="DECIMAL"/>
	</resultMap>

	<!-- 项目采购情况 -->
	<select id="getDashboardProjectPurchase" resultMap="DashboardProjectPurchaseResultMap">

		SELECT
			uo.project_id as projectId,
			pro.organization_name as projectName,
			sum(IFNULL(uo.amount,0)) as orderAmount,
			pro.lng,
			pro.lat
		from
			unified_order uo
			LEFT JOIN sys_organization pro on uo.project_id = pro.organization_id
		<where>
			and uo.project_id != 0
			and uo.complet_state = 1
			and uo.service_no = 'QYNG'

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

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

	</select>

	<resultMap id="DashboardYearPurchaseResultMap" type="cn.superdesk.uniorder.dto.vo._dashboard.UnifiedDashboardYearPurchaseVo">
		<result column="monthNum" property="monthNum" jdbcType="INTEGER"/>
		<result column="orderNum" property="orderNum" jdbcType="INTEGER"/>
		<result column="orderAmount" property="orderAmount" jdbcType="DECIMAL"/>
	</resultMap>


	<!--年度采购情况-->
	<select id="getDashboardYearPurchase" resultMap="DashboardYearPurchaseResultMap">

		SELECT
			CONVERT(DATE_FORMAT(uo.create_time,'%m'),SIGNED) monthNum,
			count(uo.id) orderNum,
			sum(IFNULL(uo.amount,0)) orderAmount
		from
			unified_order uo
			LEFT JOIN sys_organization pro on uo.project_id = pro.organization_id
		<where>
			and uo.project_id !=0
			and uo.service_no = 'QYNG'
			and uo.complet_state = 1


		<if test="queryYear != null and queryYear != ''">
			and DATE_FORMAT(uo.create_time,'%Y') = #{queryYear}
		</if>

		<if test="projectId != null and projectId != ''">
			and uo.project_id = #{projectId}
		</if>

		<if test="virtId != null and virtId != ''">
			and uo.purchase_virt_id = #{virtId}
		</if>

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

		GROUP BY DATE_FORMAT(uo.create_time,'%Y-%m')
		ORDER BY DATE_FORMAT(uo.create_time,'%m')

	</select>




	<resultMap id="DashboardYearPurchaseCompareResultMap" type="cn.superdesk.uniorder.dto.vo._dashboard.UnifiedDashboardProjectPurchaseCompareVo">
		<result column="projectId" property="projectId" jdbcType="BIGINT"/>
		<result column="projectName" property="projectName" jdbcType="VARCHAR"/>
		<result column="orderAmount" property="orderAmount" jdbcType="DECIMAL"/>
	</resultMap>

	<!--项目采购对比分析-->
	<select id="getDashboardProjectPurchaseCompare" resultMap="DashboardYearPurchaseCompareResultMap">

		SELECT
			uo.project_id as projectId,
			pro.organization_name as projectName,
			sum(IFNULL(uo.amount,0)) as orderAmount
		from
			unified_order uo
			LEFT JOIN sys_organization pro on uo.project_id = pro.organization_id
		<where>
			and uo.project_id !=0
			and uo.service_no = 'QYNG'
			and uo.complet_state = 1

		<if test="checkedProjectIds != null and checkedProjectIds != ''">
			and uo.project_id in (${checkedProjectIds})
		</if>
		<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>
		</where>

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

	</select>


	<resultMap id="DashboardPurchaseCateCompareResultMap" type="cn.superdesk.uniorder.dto.vo._dashboard.UnifiedDashboardPurchaseCateCompareVo">
		<result column="cateName" property="cateName" jdbcType="VARCHAR"/>
		<result column="orderAmount" property="orderAmount" jdbcType="DECIMAL"/>
	</resultMap>


	<!--采购分类分析-->
	<select id="getDashboardPurchaseCateCompare" resultMap="DashboardPurchaseCateCompareResultMap">

		SELECT
			uog.cate_name_lv1 as cateName,
			sum(uog.price*uog.quantity) as orderAmount
		from
			unified_order_goods uog
			LEFT JOIN unified_order uo ON uog.order_sn = uo.order_sn
		<where>
			and uo.project_id !=0
			and uo.complet_state = 1
			and uo.service_no = 'QYNG'
			and cate_name_lv1 is not null

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

		<if test="projectId != null and projectId != ''">
			and uo.project_id = #{projectId}
		</if>

		<if test="virtId != null and virtId != ''">
			and uo.purchase_virt_id = #{virtId}
		</if>

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

		GROUP BY uog.cate_name_lv1
		ORDER BY sum(uog.price*uog.quantity) desc

	</select>







	<resultMap id="DashboardGoodsPurchaseRankingResultMap" type="cn.superdesk.uniorder.dto.vo._dashboard.UnifiedDashboardGoodsPurchaseRankingVo">

		<result column="cateName" property="cateName" jdbcType="VARCHAR"/>
		<result column="orderAmount" property="orderAmount" jdbcType="DECIMAL"/>
	</resultMap>


	<!--商品采购情况排名-->
	<select id="getDashboardGoodsPurchaseRanking" resultMap="DashboardGoodsPurchaseRankingResultMap">

		SELECT
			uog.goods_name as goodsName,
			sum(uog.price*uog.quantity) as orderAmount
		from
			unified_order_goods uog
			LEFT JOIN unified_order uo ON uog.order_sn = uo.order_sn
		<where>
			and uo.project_id !=0
			and uo.service_no = 'QYNG'
			and uo.complet_state = 1

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

		<if test="projectId != null and projectId != ''">
			and uo.project_id = #{projectId}
		</if>

		<if test="virtId != null and virtId != ''">
			and uo.purchase_virt_id = #{virtId}
		</if>

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

		GROUP BY uog.goods_name
		ORDER BY sum(uog.price*uog.quantity) desc
		LIMIT 0,10

	</select>



	<resultMap id="DashboardHeadResultMap" type="cn.superdesk.uniorder.dto.vo._dashboard.UnifiedDashboardHeadDataVo">
		<result column="orderNum" property="orderNum" jdbcType="INTEGER"/>
		<result column="orderAmount" property="orderAmount" jdbcType="DECIMAL"/>
	</resultMap>

	<!--看板头部数据-->
	<select id="getDashboardHeadData" resultMap="DashboardHeadResultMap">

		SELECT
			count(uo.id) orderNum,
			sum(IFNULL(uo.amount,0)) orderAmount
		from
			unified_order uo
			LEFT JOIN sys_organization pro on uo.project_id = pro.organization_id
		<where>
			and uo.project_id !=0
			and uo.service_no = 'QYNG'
			and uo.complet_state = 1

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

	</select>

</mapper>