<template>
	<view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				regionList: [],
				totalData: {},
				busniessTypePie:[],
				busniessTradePie:[]
			}
		},
		methods: {
			async getData(obj) {
				let _this = this
				/* ts 数据类型声明
					type serverpartRegion = {
						serverpartname:string,
						crashpay: string
					}
					tyepy reginListModel = {
						name:string,
						crashpay:number,
						child: serverpartRegion[]
					}
				*/
				// let regionList = [] // 营收上传列表 reginListModel[]
				const requestParamas = {
					Statistics_Date: obj.date || '2021-12-12',
					pushProvinceCode: obj.provinceid || '340000',
					Serverpart_ID: obj.serverpartid || '',
					SPRegionType_ID: obj.regionid || ''
					// Revenue_Include: 1
				}
				// 营收数据
				const data = await this.$request.$webGet('CommercialApi/Revenue/GetRevenuePushList', requestParamas)
				console.log(data)
				if (data.Result_Code != 100) return
				
				// 在营门店数量统计
				const busniessCounts = await this.$request.$webGet('CommercialApi/BaseInfo/GetSPShopCountList', requestParamas)
				if(busniessCounts.Result_Code != 100) return
				
				// 在营门店数量统计
				const tradeData = await this.$request.$webPost('CommercialApi/BaseInfo/GetBusinessTradeList', {
				  "SearchParameter": JSON.stringify({
					"PROVINCE_CODE": 340000
				  }),
				  "PageIndex": 1,
				  "PageSize": 999,
				  "SortStr": ""
				})
				if(tradeData.Result_Code != 100) return
				
				const [reginList, totalData, busniessTypePie,busniessTradePie] = _this.getReginList(data.Result_Data.List, busniessCounts.Result_Data.List)
				_this.regionList = reginList // 营收上传列表 reginListModel[]
				_this.totalData = totalData
				_this.busniessTypePie = busniessTypePie
				_this.busniessTradePie = busniessTradePie
			},
			getTotalShowData(newData, oldData) {
				var data = {} // <TotalModel>
				/*
				type TotalModel = {
					ticketCount: number // 客单数量 ,
					totalCount: number// 总数量 ,
					totalOffAmount: number// 优惠金额 ,
					mobilePayment: number// 移动支付金额 ,
					cashPay: number// 实收金额 ,
					different_Price_Less: number// 短款金额 ,
					different_Price_More: number// 长款金额 ,
				}
				*/

				if (oldData && oldData.ticketCount) {
					data.ticketCount = oldData.ticketCount + newData.TicketCount
					data.totalCount = oldData.totalCount + newData.TotalCount
					data.totalOffAmount = oldData.totalOffAmount + newData.TotalOffAmount
					data.mobilePayment = oldData.mobilePayment + newData.MobilePayment
					data.cashPay = oldData.cashPay + newData.CashPay
					data.diffLessPrice = oldData.diffLessPrice + newData.Different_Price_Less
					data.diffMorePrice = oldData.diffMorePrice + newData.Different_Price_More

				} else {
					data.ticketCount = newData.TicketCount
					data.totalCount = newData.TotalCount
					data.totalOffAmount = newData.TotalOffAmount
					data.mobilePayment = newData.MobilePayment
					data.cashPay = newData.CashPay
					data.diffLessPrice = newData.Different_Price_Less
					data.diffMorePrice = newData.Different_Price_More
				}
				return data
			},
			getBusniessPie(typeName, item,list){ // 经营模式数据
				const oldData = list.find(m=>m.name === item[typeName])
				if(oldData){
					oldData.data +=  item.CashPay
					
					
				}else{
					list.push({
						name: item[typeName],
						data: item.CashPay,
					
					})
				}
			
				return [...list]
			},
			
			getReginList(data,shopCountList) {
				let _this = this
				let totalShow = {} // 总营收数据统计
				let list = [] // 上传营收列表数据
				let busniessTypePie = [] // 经营模式饼图统计数据
				let busniessTradePie = [] // 经营业态饼图统计数据
				data.map(async (n,index) => {
					n.show = false
					totalShow = _this.getTotalShowData(n, totalShow);// 总营收数据统计
				
					// 上传营收情况
					let regin = list.find(m => m.name === n.SPRegionType_Name)
					let [reginShopCount,serverpartShopCount] = [0,0]
					shopCountList.forEach( shop => {
						if(shop.SPREGIONTYPE_NAME == n.SPRegionType_Name){
							reginShopCount += shop.SHOP_BUSINESSCOUNT
						}
						if(shop.SERVERPART_ID==n.Serverpart_ID) {
							serverpartShopCount = shop.SHOP_BUSINESSCOUNT
						}
					})
					
					if (!regin) {
						list.push({
							name: n.SPRegionType_Name, // 区域名称
							crashpay: n.CashPay,
							uploadcount: 1, // 区域上传数量
							totalcount: reginShopCount,
							child: [{
								serverpart_Id: n.Serverpart_ID,
								serverpart_Name: n.Serverpart_Name,
								crashpay: n.CashPay,
								uploadcount: 1,
								totalcount: serverpartShopCount
							}]
						})
					} else {
						regin.crashpay += n.CashPay
						regin.uploadcount += 1
						let serverpart = regin.child.find(m => m.serverpart_Name === n.Serverpart_Name)
						if (serverpart) {
							serverpart.crashpay += n.CashPay
							serverpart.uploadcount += 1
						} else {
							regin.child.push({
								serverpart_Id: n.Serverpart_ID,
								serverpart_Name: n.Serverpart_Name,
								crashpay: n.CashPay,
								uploadcount: 1,
								totalcount: serverpartShopCount
							})
							
						}
						regin.child.sort((a,b)=> b.crashpay- a.crashpay)
					}
					list.sort((a,b)=> b.crashpay -a.crashpay)
					busniessTypePie  = _this.getBusniessPie('Business_TypeName',n,busniessTypePie)
					busniessTradePie  = _this.getBusniessPie('BusinessTrade_Name',n,busniessTradePie)
				})
				
				return [list, totalShow,busniessTypePie,busniessTradePie]
			}
		},
		onLoad(option) {
			this.getData(option)
		}
	}
</script>

<style>

</style>
