package cn.superdesk.uniorder.controller._dashboard;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.openapi.interceptor.AuthorityVerify.AuthorityVerify;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dto.param._dashboard.TvHeadDataQueryCriteria;
import cn.superdesk.uniorder.dto.param._dashboard.TvMapDataQueryCriteria;
import cn.superdesk.uniorder.dto.vo._dashboard.TvHeadDataVo;
import cn.superdesk.uniorder.dto.vo._dashboard.TvMapDataVo;
import cn.superdesk.uniorder.dubbo.consumer._dashboard.TvDashboardDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer._permission.PermissionDubboCustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(value = "Dashboard-电视", tags = "Dashboard-电视")
@Slf4j
@RestController
@RequestMapping("/dashboard/tv")
public class TvDashboardController extends BaseController {

	@Autowired
	private TvDashboardDubboConsumerService tvDashboardDubboConsumerService;

	@Autowired
	private PermissionDubboCustomerService permissionDubboCustomerService;


	//	@AvoidRepeatableCommit
	@AuthorityVerify
	@ApiOperation(value = "1.每隔5分钟统计以下数据:\n" +
			"1.1 时间范围:当前统计时间往前推31天(含当天);\n" +
			"1.2 统计对象:超级前台统一订单所有业务的订单数据,不含`已取消/已作废`状态的订单;\n" +
			"1.3 统计字段:\n" +
			"(1)下单楼宇数:取当前统计时间范围内所有符合条件的业务订单数据所属服务之窗的项目数≥0,项目数据去重;\n" +
			"(2)下单企业数:取当前统计时间范围内所有符合条件的业务订单数据所属服务之窗的企业数≥0,同项目下企业数据去重,含项目下游客企业;\n" +
			"(3)下单员工数:取当前统计时间范围内所有符合条件的业务订单数据的下单人数≥0,人数数据去重;\n" +
			"(3)订单数:取当前统计时间范围内所有符合条件的业务订单数据的订单总数≥0,订单数据去重,与`订单管理-订单数``字段的计算公式一致;\n" +
			"~~(4)订单金额:取当前统计时间范围内所有符合条件的业务订单数据的订单金额总和,金额保留小数点两位,与`订单管理-订单金额``字段的计算公式一致;~~")
	@PostMapping("/getHeadData")
	@ResponseBody
	public R<TvHeadDataVo> getHeadData(@RequestBody TvHeadDataQueryCriteria queryCriteria) {

		R<TvHeadDataVo> result = new R<>();

		DateTime minus31day = DateUtil.offsetDay(DateUtil.date(), -31);

		String todayStr      = DateUtil.today();
		String minus31dayStr = DateUtil.formatDate(minus31day);

		if (StrUtil.isBlank(queryCriteria.getEndTime())) {
			queryCriteria.setEndTime(todayStr);
		}

		if (StrUtil.isEmpty(queryCriteria.getStartTime())) {
			queryCriteria.setStartTime(minus31dayStr);
		}

		log.info("{}", AnJsonUtils.toJson(queryCriteria));


		try {

			TvHeadDataVo resultVo = tvDashboardDubboConsumerService.getHeadData(queryCriteria);

			result.setSuccess(true);
			result.setData(resultVo);


		} catch (Exception e) {

			result.setCode(String.valueOf(e.hashCode()));
			result.setSuccess(false);
			result.setMessage(e.getMessage());

			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}

		return result;
	}

	@AuthorityVerify
	@PostMapping("/getHeadDataOct")
	@ResponseBody
	public R<TvHeadDataVo> getHeadDataOct(@RequestBody TvHeadDataQueryCriteria queryCriteria) {

		R<TvHeadDataVo> result = new R<>();

		DateTime minus31day = DateUtil.offsetDay(DateUtil.date(), -31);


		String todayStr      = DateUtil.today();
		String minus31dayStr = DateUtil.formatDate(minus31day);

		if (StrUtil.isBlank(queryCriteria.getEndTime())) {
			queryCriteria.setEndTime(todayStr);
		}

		if (StrUtil.isEmpty(queryCriteria.getStartTime())) {
			queryCriteria.setStartTime(minus31dayStr);
		}

		log.info("{}", AnJsonUtils.toJson(queryCriteria));


		try {

			TvHeadDataVo resultVo = tvDashboardDubboConsumerService.getHeadDataOct(queryCriteria);

			result.setSuccess(true);
			result.setData(resultVo);


		} catch (Exception e) {

			result.setCode(String.valueOf(e.hashCode()));
			result.setSuccess(false);
			result.setMessage(e.getMessage());

			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}

		return result;
	}

	@AuthorityVerify
	@ApiOperation(value = "2. 地图显示:\n" +
			"(1)显示对象:当前时间范围内查询出所有的下单项目,以项目的经纬度定位展示在地图上;\n" +
			"(2)对象数据:按`下单项目`维度,统计当前时间范围所有下单项目的订单数;\n" +
			"(3)地图显示:\n" +
			"①以所有下单项目的订单数,依次显示其下单项目的圆圈大小;\n" +
			"②每隔5分钟,下单项目的订单数发生变化时,其圆圈需持续闪动10s左右;\n" +
			"~~③鼠标放置下单项目圆圈上,即悬浮框显示其内容,离开即关闭悬浮框:~~\n" +
			"~~下单项目名称~~\n" +
			"~~下单项目的订单金额,以`元`为单位;~~")
	@PostMapping("/getMapData")
	@ResponseBody
	public R<List<TvMapDataVo>> getMapData(@RequestBody TvMapDataQueryCriteria queryCriteria) {

		R<List<TvMapDataVo>> result = new R<>();

		DateTime minus31day = DateUtil.offsetDay(DateUtil.date(), -31);


		String todayStr      = DateUtil.today();
		String minus31dayStr = DateUtil.formatDate(minus31day);

		if (StrUtil.isBlank(queryCriteria.getEndTime())) {
			queryCriteria.setEndTime(todayStr);
		}

		if (StrUtil.isEmpty(queryCriteria.getStartTime())) {
			queryCriteria.setStartTime(minus31dayStr);
		}


		log.info("{}", AnJsonUtils.toJson(queryCriteria));

		try {

			List<TvMapDataVo> resultList = tvDashboardDubboConsumerService.getMapData(queryCriteria);

			result.setSuccess(true);
			result.setData(resultList);

		} catch (Exception e) {

			result.setCode(String.valueOf(e.hashCode()));
			result.setSuccess(false);
			result.setMessage(e.getMessage());

			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}

		return result;
	}

	/**
	 * {
	 * "startTime": "2021-01-01",
	 * "endTime": "2021-11-01"
	 * }
	 *
	 * @param queryCriteria
	 * @return
	 */
	@AuthorityVerify
	@PostMapping("/getMapDataOct")
	@ResponseBody
	public R<List<TvMapDataVo>> getMapDataOct(@RequestBody TvMapDataQueryCriteria queryCriteria) {

		R<List<TvMapDataVo>> result = new R<>();

		DateTime minus31day = DateUtil.offsetDay(DateUtil.date(), -31);

		String todayStr      = DateUtil.today();
		String minus31dayStr = DateUtil.formatDate(minus31day);

		if (StrUtil.isBlank(queryCriteria.getEndTime())) {
			queryCriteria.setEndTime(todayStr);
		}

		if (StrUtil.isEmpty(queryCriteria.getStartTime())) {
			queryCriteria.setStartTime(minus31dayStr);
		}


		log.info("{}", AnJsonUtils.toJson(queryCriteria));

		try {

			List<TvMapDataVo> resultList = tvDashboardDubboConsumerService.getMapDataOct(queryCriteria);

			result.setSuccess(true);
			result.setData(resultList);

		} catch (Exception e) {

			result.setCode(String.valueOf(e.hashCode()));
			result.setSuccess(false);
			result.setMessage(e.getMessage());

			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}

		return result;
	}

}
