package com.qs.mp.web.controller.api.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.qs.mp.admin.domain.param.IndexQueryParam;
import com.qs.mp.admin.domain.param.IndexTicketBoxTopQueryParam;
import com.qs.mp.admin.domain.param.IndexTicketSiteTopQueryParam;
import com.qs.mp.admin.domain.vo.IndexVO;
import com.qs.mp.channel.domain.Channel;
import com.qs.mp.channel.domain.ChannelOrder;
import com.qs.mp.channel.service.IChannelOrderService;
import com.qs.mp.channel.service.IChannelService;
import com.qs.mp.common.core.domain.AjaxResult;
import com.qs.mp.common.utils.DateUtils;
import com.qs.mp.user.domain.UserTicketOrder;
import com.qs.mp.user.service.IUserTicketOrderService;
import com.qs.mp.utils.SecurityUtils;
import com.qs.mp.web.controller.common.BaseApiController;
import io.swagger.annotations.Api;

import java.math.BigDecimal;
import java.util.*;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @auther quanshu
 * @create 2022-03-31 16:17:48
 * @describe 首页渠道管理前端控制器
 */
@Api("首页管理API")
@RestController
@RequestMapping("/api/v1/mp/admin/index/*")
@Component
public class IndexMgrController extends BaseApiController {

	@Autowired
	private IChannelService channelService;

    @Autowired
    private IUserTicketOrderService userTicketOrderService;

	@Autowired
	private IChannelOrderService channelOrderService;



	/**
	 * 查询统计交易金额
	 * @return
	 */
	@PostMapping("/pay/amt")
	// @PreAuthorize("@ss.hasPermi('business:channel:list')")
	public AjaxResult listPayAmtData(@RequestBody IndexQueryParam indeQueryParam) {

		IndexQueryParam queryParam = getQueryParam(indeQueryParam);

		List<IndexVO> list = new ArrayList<IndexVO>();
		QueryWrapper<UserTicketOrder> queryWrapper = new QueryWrapper<>();
		queryWrapper.ge(null != queryParam && null != queryParam.getStartTime(), "t1.created_time", queryParam.getStartTime());
		queryWrapper.le(null != queryParam && null != queryParam.getEndTime(), "t1.created_time", queryParam.getEndTime());
		queryWrapper.gt("t1.`status`", 0);
//		Long channelId = SecurityUtils.getLoginUser().getChannelId();
//		if(null != channelId && 0 != channelId) {
//			Channel channel = channelService.getById(channelId);
//			if(null!=channel && StringUtils.isNotBlank(channel.getChannelNo())) {
//				queryWrapper.and(wrapper -> wrapper.likeRight("t2.channel_no", channel.getChannelNo())
//						.or().eq("t2.channel_no", channel.getChannelNo()));
//			}
//		}

		list = userTicketOrderService.selectIndexPayAmtList(queryWrapper);


		// 预付票信息
		List<IndexVO> channelOrderList = new ArrayList<>();
		QueryWrapper<ChannelOrder> channelOrderQueryWrapper1 = new QueryWrapper<>();
		channelOrderQueryWrapper1.ge(null != queryParam && null != queryParam.getStartTime(), "t1.created_time", queryParam.getStartTime());
		channelOrderQueryWrapper1.le(null != queryParam && null != queryParam.getEndTime(), "t1.created_time", queryParam.getEndTime());
		channelOrderQueryWrapper1.gt("t1.`status`", 0);
		channelOrderQueryWrapper1.gt("t1.type" , 1);
		channelOrderList = channelOrderService.selectIndexChannelOrderDailyInfoList(channelOrderQueryWrapper1);
		list = addPrePayOrderInfo(list, channelOrderList);

		return  AjaxResult.success(getPayAmtList(list, queryParam));
	}

	private List<Map<String,Object>> getPayAmtList(List<IndexVO> list, IndexQueryParam queryParam){
		List<Map<String,Object>> mapList = new ArrayList<>();
		if(null != queryParam && queryParam.getLevel().equals("month")) {  // 月粒度
			List<String> monthList = DateUtils.addMonths(queryParam.getStartTime(), queryParam.getEndTime());
			if(null != monthList && monthList.size() > 0) {
				for(String month : monthList) {
					if(StringUtils.isNotBlank(month)) {
						Map<String,Object> map = new HashMap<String,Object>();
						map.put("x", month);
						map.put("y", getMonthPayAmt(month, list));
						mapList.add(map);
					}
				}
			}
		}else if(null != queryParam && queryParam.getLevel().equals("week")) {  // 周粒度
			List<Map<String,Object>> weekList = DateUtils.getDateOfWeek(queryParam.getStartTime(), queryParam.getEndTime());
			if(null != weekList && weekList.size() > 0) {
				for(Map<String,Object> weekMap : weekList) {
					if(null != weekMap && weekMap.containsKey("start")
							&& weekMap.containsKey("end")
							&& weekMap.containsKey("dayList")) {
						String start = weekMap.get("start").toString();
						String end = weekMap.get("end").toString();
						List<String> dayList = (List<String>) weekMap.get("dayList");
						Map<String,Object> map = new HashMap<String,Object>();
						map.put("x", start +"-"+end);
						map.put("y", getWeekPayAmt(dayList, list));
						mapList.add(map);
					}
				}
			}
		}else {  // 日粒度
			List<String> dayList = DateUtils.addDates(DateUtils.dateTime(queryParam.getStartTime()), DateUtils.dateTime(queryParam.getEndTime()));
			if(null != dayList && dayList.size() > 0) {
				for(String day : dayList) {
					int payAmt = 0;
					if(null != list && list.size() > 0) {
						if(StringUtils.isNotBlank(day)) {
							for(IndexVO indexVo : list) {
								if(null != indexVo
										&& null != indexVo.getPayAmt()
										&& StringUtils.isNotBlank(indexVo.getTime())
										&& indexVo.getTime().equals(day)) {
									payAmt = indexVo.getPayAmt();
									break;
								}
							}
						}
					}
					Map<String,Object> map = new HashMap<String,Object>();
					map.put("x", day);
					map.put("y", payAmt);
					mapList.add(map);
				}
			}
		}
		return mapList;
	}


	private int getWeekPayAmt(List<String> dayList, List<IndexVO> list) {
		BigDecimal payAmt = new BigDecimal(0);
		if(null != list && list.size() > 0
				&& null != dayList && dayList.size() > 0) {
			for(IndexVO indexVo : list) {
				if(null != indexVo
						&& null != indexVo.getPayAmt()
						&& StringUtils.isNotBlank(indexVo.getTime())
						&& dayList.contains(indexVo.getTime())) {
					payAmt = payAmt.add(new BigDecimal(indexVo.getPayAmt()));
				}
			}
			return payAmt.intValue();
		}
		return 0;
	}

	private int getMonthPayAmt(String month, List<IndexVO> list) {
		BigDecimal payAmt = new BigDecimal(0);
		if(null != list && list.size() > 0
				&& StringUtils.isNotBlank(month)) {
			for(IndexVO indexVo : list) {
				if(null != indexVo
						&& null != indexVo.getPayAmt()
						&& StringUtils.isNotBlank(indexVo.getTime())
						&& indexVo.getTime().indexOf(month) >= 0) {
					payAmt = payAmt.add(new BigDecimal(indexVo.getPayAmt()));
				}
			}
			return payAmt.intValue();
		}
		return 0;
	}


	/**
	 * 查询统计交易用户数
	 * @return
	 */
	@PostMapping("/pay/user")
	// @PreAuthorize("@ss.hasPermi('business:channel:list')")
	public AjaxResult listPayUserData(@RequestBody IndexQueryParam indeQueryParam) {

		IndexQueryParam queryParam = getQueryParam(indeQueryParam);

		List<IndexVO> list = new ArrayList<IndexVO>();
		QueryWrapper<UserTicketOrder> queryWrapper = new QueryWrapper<>();
		queryWrapper.ge(null != queryParam && null != queryParam.getStartTime(), "t1.created_time", queryParam.getStartTime());
		queryWrapper.le(null != queryParam && null != queryParam.getEndTime(), "t1.created_time", queryParam.getEndTime());
		queryWrapper.gt("t1.`status`", 0);
//		Long channelId = SecurityUtils.getLoginUser().getChannelId();
//		if(null != channelId && 0 != channelId) {
//			Channel channel = channelService.getById(channelId);
//			if(null!=channel && StringUtils.isNotBlank(channel.getChannelNo())) {
//				queryWrapper.and(wrapper -> wrapper.likeRight("t2.channel_no", channel.getChannelNo())
//						.or().eq("t2.channel_no", channel.getChannelNo()));
//			}
//		}

		list = userTicketOrderService.selectIndexPayUserCntList(queryWrapper);
		return  AjaxResult.success(getPayUserList(list, queryParam));
	}

	private List<Map<String,Object>> getPayUserList(List<IndexVO> list, IndexQueryParam queryParam){
		List<Map<String,Object>> mapList = new ArrayList<>();
		if(null != queryParam && queryParam.getLevel().equals("month")) {  // 月粒度
			List<String> monthList = DateUtils.addMonths(queryParam.getStartTime(), queryParam.getEndTime());
			if(null != monthList && monthList.size() > 0) {
				for(String month : monthList) {
					if(StringUtils.isNotBlank(month)) {
						Map<String,Object> map = new HashMap<String,Object>();
						map.put("x", month);
						map.put("y", getMonthPayUser(month, list));
						mapList.add(map);
					}
				}
			}
		}else if(null != queryParam && queryParam.getLevel().equals("week")) {  // 周粒度
			List<Map<String,Object>> weekList = DateUtils.getDateOfWeek(queryParam.getStartTime(), queryParam.getEndTime());
			if(null != weekList && weekList.size() > 0) {
				for(Map<String,Object> weekMap : weekList) {
					if(null != weekMap && weekMap.containsKey("start")
							&& weekMap.containsKey("end")
							&& weekMap.containsKey("dayList")) {
						String start = weekMap.get("start").toString();
						String end = weekMap.get("end").toString();
						List<String> dayList = (List<String>) weekMap.get("dayList");
						Map<String,Object> map = new HashMap<String,Object>();
						map.put("x", start +"-"+end);
						map.put("y", getWeekPayUser(dayList, list));
						mapList.add(map);
					}
				}
			}
		}else {  // 日粒度
			List<String> dayList = DateUtils.addDates(DateUtils.dateTime(queryParam.getStartTime()), DateUtils.dateTime(queryParam.getEndTime()));
			if(null != dayList && dayList.size() > 0) {
				for(String day : dayList) {
					int payUser = 0;
					if(null != list && list.size() > 0) {
						if(StringUtils.isNotBlank(day)) {
							for(IndexVO indexVo : list) {
								if(null != indexVo
										&& null != indexVo.getPayUserCnt()
										&& StringUtils.isNotBlank(indexVo.getTime())
										&& indexVo.getTime().equals(day)) {
									payUser = indexVo.getPayUserCnt();
									break;
								}
							}
						}
					}
					Map<String,Object> map = new HashMap<String,Object>();
					map.put("x", day);
					map.put("y", payUser);
					mapList.add(map);
				}
			}
		}
		return mapList;
	}


	private int getWeekPayUser(List<String> dayList, List<IndexVO> list) {
		int payUserCnt = 0;
		if(null != list && list.size() > 0
				&& null != dayList && dayList.size() > 0) {
			for(IndexVO indexVo : list) {
				if(null != indexVo
						&& null != indexVo.getPayUserCnt()
						&& StringUtils.isNotBlank(indexVo.getTime())
						&& dayList.contains(indexVo.getTime())) {
					payUserCnt = payUserCnt + indexVo.getPayUserCnt();
				}
			}
			return payUserCnt;
		}
		return 0;
	}

	private int getMonthPayUser(String month, List<IndexVO> list) {
		int payUserCnt = 0;
		if(null != list && list.size() > 0
				&& StringUtils.isNotBlank(month)) {
			for(IndexVO indexVo : list) {
				if(null != indexVo
						&& null != indexVo.getPayUserCnt()
						&& StringUtils.isNotBlank(indexVo.getTime())
						&& indexVo.getTime().indexOf(month) >= 0) {
					payUserCnt = payUserCnt + indexVo.getPayUserCnt();
				}
			}
			return payUserCnt;
		}
		return 0;
	}


	/**
	 * 初始化查询参数
	 * @param indeQueryParam
	 * @return
	 */
	private IndexQueryParam getQueryParam(IndexQueryParam indeQueryParam) {
		IndexQueryParam queryParam = new IndexQueryParam();
		queryParam.setDays(null != indeQueryParam && null != indeQueryParam.getDays()?indeQueryParam.getDays(): 7);
		queryParam.setLevel(null != indeQueryParam && StringUtils.isNotBlank(indeQueryParam.getLevel())?indeQueryParam.getLevel():"day");
		if(queryParam.getDays() == 0) { // 自定义
			if(null != indeQueryParam.getStartTime() && null != indeQueryParam.getEndTime()) {
				queryParam.setStartTime(indeQueryParam.getStartTime());
				queryParam.setEndTime(DateUtils.getEndTimeOfNow(indeQueryParam.getEndTime()));
			}else {
				queryParam.setStartTime(DateUtils.getPreDayOfNowZero(6));
				queryParam.setEndTime(new Date());
			}
		}else {
			queryParam.setStartTime(DateUtils.getPreDayOfNowZero(queryParam.getDays() - 1));
			queryParam.setEndTime(new Date());
		}
		return queryParam;
	}


	/**
	 * 查询统计新增经销商数量
	 * @return
	 */
	@PostMapping("/site/increase")
	// @PreAuthorize("@ss.hasPermi('business:channel:list')")
	public AjaxResult listSiteIncreaseData(@RequestBody IndexQueryParam indeQueryParam) {

		IndexQueryParam queryParam = getQueryParam(indeQueryParam);

		List<IndexVO> list = new ArrayList<IndexVO>();
		QueryWrapper<Channel> queryWrapper = new QueryWrapper<>();
		queryWrapper.ge(null != queryParam && null != queryParam.getStartTime(), "t1.created_time", queryParam.getStartTime());
		queryWrapper.le(null != queryParam && null != queryParam.getEndTime(), "t1.created_time", queryParam.getEndTime());
		queryWrapper.eq("t1.`level`", 0);
//		Long channelId = SecurityUtils.getLoginUser().getChannelId();
//		if(null != channelId && 0 != channelId) {
//			Channel channel = channelService.getById(channelId);
//			if(null!=channel && StringUtils.isNotBlank(channel.getChannelNo())) {
//				queryWrapper.and(wrapper -> wrapper.likeRight("t2.channel_no", channel.getChannelNo())
//						.or().eq("t2.channel_no", channel.getChannelNo()));
//			}
//		}

		list = channelService.selectIndexSiteIncreaseList(queryWrapper);
		return  AjaxResult.success(getSiteIncreaseList(list, queryParam));
	}

	private List<Map<String,Object>> getSiteIncreaseList(List<IndexVO> list, IndexQueryParam queryParam){
		List<Map<String,Object>> mapList = new ArrayList<>();
		if(null != queryParam && queryParam.getLevel().equals("month")) {  // 月粒度
			List<String> monthList = DateUtils.addMonths(queryParam.getStartTime(), queryParam.getEndTime());
			if(null != monthList && monthList.size() > 0) {
				for(String month : monthList) {
					if(StringUtils.isNotBlank(month)) {
						Map<String,Object> map = new HashMap<String,Object>();
						map.put("x", month);
						map.put("y", getMonthSiteIncrease(month, list));
						mapList.add(map);
					}
				}
			}
		}else if(null != queryParam && queryParam.getLevel().equals("week")) {  // 周粒度
			List<Map<String,Object>> weekList = DateUtils.getDateOfWeek(queryParam.getStartTime(), queryParam.getEndTime());
			if(null != weekList && weekList.size() > 0) {
				for(Map<String,Object> weekMap : weekList) {
					if(null != weekMap && weekMap.containsKey("start")
							&& weekMap.containsKey("end")
							&& weekMap.containsKey("dayList")) {
						String start = weekMap.get("start").toString();
						String end = weekMap.get("end").toString();
						List<String> dayList = (List<String>) weekMap.get("dayList");
						Map<String,Object> map = new HashMap<String,Object>();
						map.put("x", start +"-"+end);
						map.put("y", getWeekSiteIncrease(dayList, list));
						mapList.add(map);
					}
				}
			}
		}else {  // 日粒度
			List<String> dayList = DateUtils.addDates(DateUtils.dateTime(queryParam.getStartTime()), DateUtils.dateTime(queryParam.getEndTime()));
			if(null != dayList && dayList.size() > 0) {
				for(String day : dayList) {
					int newSiteCnt = 0;
					if(null != list && list.size() > 0) {
						if(StringUtils.isNotBlank(day)) {
							for(IndexVO indexVo : list) {
								if(null != indexVo
										&& null != indexVo.getNewSiteCnt()
										&& StringUtils.isNotBlank(indexVo.getTime())
										&& indexVo.getTime().equals(day)) {
									newSiteCnt = indexVo.getNewSiteCnt();
									break;
								}
							}
						}
					}

					Map<String,Object> map = new HashMap<String,Object>();
					map.put("x", day);
					map.put("y", newSiteCnt);
					mapList.add(map);
				}
			}
		}
		return mapList;
	}


	private int getWeekSiteIncrease(List<String> dayList, List<IndexVO> list) {
		int newSiteCnt = 0;
		if(null != list && list.size() > 0
				&& null != dayList && dayList.size() > 0) {
			for(IndexVO indexVo : list) {
				if(null != indexVo
						&& null != indexVo.getNewSiteCnt()
						&& StringUtils.isNotBlank(indexVo.getTime())
						&& dayList.contains(indexVo.getTime())) {
					newSiteCnt = newSiteCnt + indexVo.getNewSiteCnt();
				}
			}
			return newSiteCnt;
		}
		return 0;
	}

	private int getMonthSiteIncrease(String month, List<IndexVO> list) {
		int newSiteCnt = 0;
		if(null != list && list.size() > 0
				&& StringUtils.isNotBlank(month)) {
			for(IndexVO indexVo : list) {
				if(null != indexVo
						&& null != indexVo.getNewSiteCnt()
						&& StringUtils.isNotBlank(indexVo.getTime())
						&& indexVo.getTime().indexOf(month) >= 0) {
					newSiteCnt = newSiteCnt + indexVo.getNewSiteCnt();
				}
			}
			return newSiteCnt;
		}
		return 0;
	}


	/**
	 * 查询统计交易金额
	 * @return
	 */
	@PostMapping("/daily")
	// @PreAuthorize("@ss.hasPermi('business:channel:list')")
	public AjaxResult listDailyData(@RequestBody IndexQueryParam indeQueryParam) {

		// 今天
		IndexQueryParam queryParam = new IndexQueryParam();
		queryParam.setStartTime(DateUtils.getPreDayOfNowZero(0));  // 今天0点
		queryParam.setEndTime(new Date());  // 今天

		List<IndexVO> todayList = new ArrayList<IndexVO>();
		List<IndexVO> yestodayList = new ArrayList<IndexVO>();
		List<IndexVO> todaySiteList = new ArrayList<IndexVO>();
		List<IndexVO> yetodaySiteList = new ArrayList<IndexVO>();

		List<IndexVO> channelOrderTodayList = new ArrayList<>();
		List<IndexVO> channelOrderYestodayList= new ArrayList<>();

		QueryWrapper<UserTicketOrder> queryWrapper1 = new QueryWrapper<>();
		queryWrapper1.ge(null != queryParam && null != queryParam.getStartTime(), "t1.created_time", queryParam.getStartTime());
		queryWrapper1.le(null != queryParam && null != queryParam.getEndTime(), "t1.created_time", queryParam.getEndTime());
		queryWrapper1.gt("t1.`status`", 0);
		todayList = userTicketOrderService.selectIndexDailyInfoList(queryWrapper1);

		QueryWrapper<ChannelOrder> channelOrderQueryWrapper1 = new QueryWrapper<>();
		channelOrderQueryWrapper1.ge(null != queryParam && null != queryParam.getStartTime(), "t1.created_time", queryParam.getStartTime());
		channelOrderQueryWrapper1.le(null != queryParam && null != queryParam.getEndTime(), "t1.created_time", queryParam.getEndTime());
		channelOrderQueryWrapper1.gt("t1.`status`", 0);
		channelOrderQueryWrapper1.gt("t1.type" , 1);
		channelOrderTodayList = channelOrderService.selectIndexChannelOrderDailyInfoList(channelOrderQueryWrapper1);
		todayList = addPrePayOrderInfo(todayList, channelOrderTodayList);

		QueryWrapper<Channel> siteQueryWrapper1 = new QueryWrapper<>();
		siteQueryWrapper1.ge(null != queryParam && null != queryParam.getStartTime(), "t1.created_time", queryParam.getStartTime());
		siteQueryWrapper1.le(null != queryParam && null != queryParam.getEndTime(), "t1.created_time", queryParam.getEndTime());
		siteQueryWrapper1.eq("t1.`level`", 0);
		todaySiteList = channelService.selectIndexSiteIncreaseList(siteQueryWrapper1);

		// 昨天
		queryParam.setStartTime(DateUtils.getPreDayOfNowZero(1));  // 昨天0点
		queryParam.setEndTime(DateUtils.getPreDayOfNow(1));  // 今天减去24小时

		QueryWrapper<UserTicketOrder> queryWrapper2 = new QueryWrapper<>();
		queryWrapper2.ge(null != queryParam && null != queryParam.getStartTime(), "t1.created_time", queryParam.getStartTime());
		queryWrapper2.le(null != queryParam && null != queryParam.getEndTime(), "t1.created_time", queryParam.getEndTime());
		queryWrapper2.gt("t1.`status`", 0);
		yestodayList = userTicketOrderService.selectIndexDailyInfoList(queryWrapper2);

		QueryWrapper<ChannelOrder> channelOrderQueryWrapper2 = new QueryWrapper<>();
		channelOrderQueryWrapper2.ge(null != queryParam && null != queryParam.getStartTime(), "t1.created_time", queryParam.getStartTime());
		channelOrderQueryWrapper2.le(null != queryParam && null != queryParam.getEndTime(), "t1.created_time", queryParam.getEndTime());
		channelOrderQueryWrapper2.gt("t1.`status`", 0);
		channelOrderQueryWrapper2.gt("t1.type" , 1);
		channelOrderYestodayList = channelOrderService.selectIndexChannelOrderDailyInfoList(channelOrderQueryWrapper2);
		yestodayList = addPrePayOrderInfo(yestodayList, channelOrderYestodayList);


		QueryWrapper<Channel> siteQueryWrapper2 = new QueryWrapper<>();
		siteQueryWrapper2.ge(null != queryParam && null != queryParam.getStartTime(), "t1.created_time", queryParam.getStartTime());
		siteQueryWrapper2.le(null != queryParam && null != queryParam.getEndTime(), "t1.created_time", queryParam.getEndTime());
		siteQueryWrapper2.eq("t1.`level`", 0);
		yetodaySiteList = channelService.selectIndexSiteIncreaseList(siteQueryWrapper2);

		return  AjaxResult.success(calcDailyInfoData(todayList, yestodayList, todaySiteList, yetodaySiteList));
	}

	// 增加预付票的信息
	private List<IndexVO> addPrePayOrderInfo(List<IndexVO> indexList, List<IndexVO> channelOrderIndexList) {
		List<IndexVO> newIndexList = new ArrayList<>();
		if (CollectionUtils.isNotEmpty(indexList) && CollectionUtils.isNotEmpty(channelOrderIndexList)){
			TreeMap<String, IndexVO> treeMap = new TreeMap<>();
			indexList.addAll(channelOrderIndexList);
			for (IndexVO indexVO : indexList) {
				IndexVO index = treeMap.get(indexVO.getTime());
				if (Objects.isNull(index)) {
					treeMap.put(indexVO.getTime(), indexVO);
				} else {
					index.setPayAmt(index.getPayAmt() + indexVO.getPayAmt());
				}
			}
			for (String key : treeMap.keySet()) {
				newIndexList.add(treeMap.get(key));
			}
			return newIndexList;
		}
		if (CollectionUtils.isEmpty(indexList) && CollectionUtils.isNotEmpty(channelOrderIndexList)) {
			return channelOrderIndexList;
		}
		return indexList;
	}

	private Map<String,Object> getDailyInfoData(List<IndexVO> list,List<IndexVO> siteList){
		Map<String,Object> payAmt = new HashMap<String,Object>();
		Map<String,Object> payUser = new HashMap<String,Object>();
		Map<String,Object> ticketNum = new HashMap<String,Object>();
		Map<String,Object> newSite = new HashMap<String,Object>();
		String today = DateUtils.parseDateToStr(DateUtils.YYYYMMDD,new Date());

		int todayPayAmt = 0;
		int todayPayUserCnt = 0;
		int todayTicketNum = 0;
		int todayNewSiteCnt = 0;

		int yesterdayPayAmt = 0;
		int yesterdayPayUserCnt = 0;
		int yesterdayTicketNum = 0;
		int yesterdayNewSiteCnt = 0;

		if(null != list && list.size() > 0) {
			for(IndexVO indexVo : list) {
				if(null != indexVo
						&& StringUtils.isNotBlank(indexVo.getTime())) {
					if(today.equals(indexVo.getTime())) {
						todayPayAmt =  null != indexVo.getPayAmt()?indexVo.getPayAmt():0;
						todayPayUserCnt =  null != indexVo.getPayUserCnt()?indexVo.getPayUserCnt():0;
						todayTicketNum = null != indexVo.getTicketNum()?indexVo.getTicketNum():0;
					}else {
						yesterdayPayAmt = null != indexVo.getPayAmt()?indexVo.getPayAmt():0;
						yesterdayPayUserCnt = null != indexVo.getPayUserCnt()?indexVo.getPayUserCnt():0;
						yesterdayTicketNum =  null != indexVo.getTicketNum()?indexVo.getTicketNum():0;
					}
				}
			}
		}
		if(null != siteList && siteList.size() > 0) {
			for(IndexVO indexVo : siteList) {
				if(null != indexVo
						&& null != indexVo.getNewSiteCnt()
						&& StringUtils.isNotBlank(indexVo.getTime())) {
					if(today.equals(indexVo.getTime())) {
						todayNewSiteCnt = null != indexVo.getNewSiteCnt()?indexVo.getNewSiteCnt():0;
					}else {
						yesterdayNewSiteCnt = null != indexVo.getNewSiteCnt()?indexVo.getNewSiteCnt():0;
					}
				}
			}
		}
		payAmt.put("today", todayPayAmt);
		payAmt.put("yesterday", yesterdayPayAmt);
		payUser.put("today", todayPayUserCnt);
		payUser.put("yesterday", yesterdayPayUserCnt);
		ticketNum.put("today", todayTicketNum);
		ticketNum.put("yesterday", yesterdayTicketNum);
		newSite.put("today", todayNewSiteCnt);
		newSite.put("yesterday", yesterdayNewSiteCnt);

		Map<String,Object> map = new HashMap<String,Object>();
		map.put("payAmt", payAmt);
		map.put("payUser", payUser);
		map.put("ticketNum", ticketNum);
		map.put("newSite", newSite);
		return map;
	}


	@SuppressWarnings("unused")
	private Map<String,Object> calcDailyInfoData(List<IndexVO> todayList, List<IndexVO> yetodayList,  List<IndexVO> todaySiteList, List<IndexVO> yetodaySiteList){
		Map<String,Object> payAmt = new HashMap<String,Object>();
		Map<String,Object> payUser = new HashMap<String,Object>();
		Map<String,Object> ticketNum = new HashMap<String,Object>();
		Map<String,Object> newSite = new HashMap<String,Object>();

		String today = DateUtils.parseDateToStr(DateUtils.YYYYMMDD,new Date());

		int todayPayAmt = 0;
		int todayPayUserCnt = 0;
		int todayTicketNum = 0;
		int todayNewSiteCnt = 0;

		int yesterdayPayAmt = 0;
		int yesterdayPayUserCnt = 0;
		int yesterdayTicketNum = 0;
		int yesterdayNewSiteCnt = 0;

		if(null != todayList && todayList.size() > 0) {
			for(IndexVO indexVo : todayList) {
				if(null != indexVo
						&& StringUtils.isNotBlank(indexVo.getTime())) {
					todayPayAmt +=  null != indexVo.getPayAmt()?indexVo.getPayAmt():0;
					todayPayUserCnt +=  null != indexVo.getPayUserCnt()?indexVo.getPayUserCnt():0;
					todayTicketNum += null != indexVo.getTicketNum()?indexVo.getTicketNum():0;
				}
			}
		}

		if(null != yetodayList && yetodayList.size() > 0) {
			for(IndexVO indexVo : yetodayList) {
				if(null != indexVo) {
					yesterdayPayAmt += null != indexVo.getPayAmt()?indexVo.getPayAmt():0;
					yesterdayPayUserCnt += null != indexVo.getPayUserCnt()?indexVo.getPayUserCnt():0;
					yesterdayTicketNum +=  null != indexVo.getTicketNum()?indexVo.getTicketNum():0;
				}
			}
		}

		if(null != todaySiteList && todaySiteList.size() > 0) {
			for(IndexVO indexVo : todaySiteList) {
				if(null != indexVo
						&& null != indexVo.getNewSiteCnt()) {
					todayNewSiteCnt += null != indexVo.getNewSiteCnt()?indexVo.getNewSiteCnt():0;
				}
			}
		}

		if(null != yetodaySiteList && yetodaySiteList.size() > 0) {
			for(IndexVO indexVo : yetodaySiteList) {
				if(null != indexVo
						&& null != indexVo.getNewSiteCnt()) {
					yesterdayNewSiteCnt += null != indexVo.getNewSiteCnt()?indexVo.getNewSiteCnt():0;
				}
			}
		}

		payAmt.put("today", todayPayAmt);
		payAmt.put("yesterday", yesterdayPayAmt);
		payUser.put("today", todayPayUserCnt);
		payUser.put("yesterday", yesterdayPayUserCnt);
		ticketNum.put("today", todayTicketNum);
		ticketNum.put("yesterday", yesterdayTicketNum);
		newSite.put("today", todayNewSiteCnt);
		newSite.put("yesterday", yesterdayNewSiteCnt);

		Map<String,Object> map = new HashMap<String,Object>();
		map.put("payAmt", payAmt);
		map.put("payUser", payUser);
		map.put("ticketNum", ticketNum);
		map.put("newSite", newSite);
		return map;
	}

	/**
	 * 查询统计统计票组销售TOP10
	 * @return
	 */
	@PostMapping("/ticket/box/top")
	// @PreAuthorize("@ss.hasPermi('business:channel:list')")
	public AjaxResult ticketBoxTopData(@RequestBody IndexQueryParam indeQueryParam) {

		IndexQueryParam queryParam = getQueryParam(indeQueryParam);
		List<IndexVO> list = new ArrayList<IndexVO>();
//		QueryWrapper<UserTicketOrder> queryWrapper = new QueryWrapper<>();
//		queryWrapper.ge(null != queryParam && null != queryParam.getStartTime(), "t1.created_time", queryParam.getStartTime());
//		queryWrapper.le(null != queryParam && null != queryParam.getEndTime(), "t1.created_time", queryParam.getEndTime());
//		queryWrapper.gt("t1.`status`", 0);

//		Long channelId = SecurityUtils.getLoginUser().getChannelId();
//		if(null != channelId && 0 != channelId) {
//			Channel channel = channelService.getById(channelId);
//			if(null!=channel && StringUtils.isNotBlank(channel.getChannelNo())) {
//				queryWrapper.and(wrapper -> wrapper.likeRight("t2.channel_no", channel.getChannelNo())
//						.or().eq("t2.channel_no", channel.getChannelNo()));
//
//			}
//		}

		IndexTicketBoxTopQueryParam indexTicketBoxTopQueryParam = new IndexTicketBoxTopQueryParam();
		indexTicketBoxTopQueryParam.setStartTime(queryParam.getStartTime());
		indexTicketBoxTopQueryParam.setEndTime(queryParam.getEndTime());
		indexTicketBoxTopQueryParam.setTicketOrderStatus(0);
		indexTicketBoxTopQueryParam.setChannelOrderStatus(0);
		indexTicketBoxTopQueryParam.setChannelOrderType(1);

//		list = userTicketOrderService.selectIndexTicketBoxTop(queryWrapper);

		list = userTicketOrderService.selectIndexTicketBoxTopByTicketOrderAndChannelOrder(indexTicketBoxTopQueryParam);

		List<Map<String,Object>> mapList = new ArrayList<>();
		if(null != list && list.size() > 0) {
			for(IndexVO indexVo : list) {
				if(null != indexVo
						&& StringUtils.isNotBlank(indexVo.getTitle())
						&& null != indexVo.getPayAmt()) {
					Map<String,Object> map = new HashMap<String,Object>();
					map.put("x", indexVo.getTitle());
					map.put("y", indexVo.getPayAmt());
					mapList.add(map);
				}
			}
		}
		return  AjaxResult.success(mapList);
	}


	/**
	 * 统计经销商交易额TOP10
	 * @return
	 */
	@PostMapping("/ticket/site/top")
	// @PreAuthorize("@ss.hasPermi('business:channel:list')")
	public AjaxResult ticketSiteTopData(@RequestBody IndexQueryParam indeQueryParam) {

		IndexQueryParam queryParam = getQueryParam(indeQueryParam);
		List<IndexVO> list = new ArrayList<IndexVO>();
//		QueryWrapper<UserTicketOrder> queryWrapper = new QueryWrapper<>();
//		queryWrapper.ge(null != queryParam && null != queryParam.getStartTime(), "t1.created_time", queryParam.getStartTime());
//		queryWrapper.le(null != queryParam && null != queryParam.getEndTime(), "t1.created_time", queryParam.getEndTime());
//		queryWrapper.gt("t1.`status`", 0);
//		queryWrapper.isNotNull("t1.channel_id");

//		Long channelId = SecurityUtils.getLoginUser().getChannelId();
//		if(null != channelId && 0 != channelId) {
//			Channel channel = channelService.getById(channelId);
//			if(null!=channel && StringUtils.isNotBlank(channel.getChannelNo())) {
//				queryWrapper.and(wrapper -> wrapper.likeRight("t2.channel_no", channel.getChannelNo())
//						.or().eq("t2.channel_no", channel.getChannelNo()));
//
//			}
//		}

		IndexTicketSiteTopQueryParam indexTicketSiteTopQueryParam = new IndexTicketSiteTopQueryParam();
		indexTicketSiteTopQueryParam.setStartTime(queryParam.getStartTime());
		indexTicketSiteTopQueryParam.setEndTime(queryParam.getEndTime());
		indexTicketSiteTopQueryParam.setTicketOrderStatus(0);
		indexTicketSiteTopQueryParam.setChannelOrderStatus(0);
		indexTicketSiteTopQueryParam.setChannelOrderType(1);
		list = userTicketOrderService.selectIndexTicketSiteTopByTicketOrderAndChannelOrder(indexTicketSiteTopQueryParam);

//		list = userTicketOrderService.selectIndexTicketSiteTop(queryWrapper);
		List<Map<String,Object>> mapList = new ArrayList<>();
		if(null != list && list.size() > 0) {
			for(IndexVO indexVo : list) {
				if(null != indexVo
						&& StringUtils.isNotBlank(indexVo.getName())
						&& null != indexVo.getPayAmt()) {
					Map<String,Object> map = new HashMap<String,Object>();
					map.put("x", indexVo.getName());
					map.put("y", indexVo.getPayAmt());
					mapList.add(map);
				}
			}
		}
		return  AjaxResult.success(mapList);
	}


}
