package cn.superdesk.uniorder.controller._statistics;


import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.numerical.AnDateHelper;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dto.param.accounting.statistics.ReceivableDetailDataQueryParam;
import cn.superdesk.uniorder.dto.vo._dashboard.*;
import cn.superdesk.uniorder.dto.vo._function.select.SelectOptionOrg;
import cn.superdesk.uniorder.dto.vo._function.select.SelectOptionVirt;
import cn.superdesk.uniorder.dubbo.consumer._dashboard.BackendDashboardDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer._permission.PermissionDubboCustomerService;
import cn.superdesk.uniorder.dubbo.consumer.statistics.ReceivableStatisticsDubboService;
import com.alibaba.fastjson2.JSON;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 看板
 * 查询 已完成 且 企业采购 订单
 */
@Api(value = "Dashboard-后端-首页", tags = "Dashboard-后端-首页")
@Slf4j
@RestController
@RequestMapping("/statistics/test")
public class ReceivableStatisticsTestController extends BaseController {

	@Autowired
	private BackendDashboardDubboConsumerService unifiedDashboardDubboConsumerService;

	@Autowired
	private ReceivableStatisticsDubboService receivableStatisticsDubboService;

	@Autowired
	private PermissionDubboCustomerService permissionDubboCustomerService;



	/**
	 * 应收统计
	 *
	 * @return
	 */
	@PostMapping("/receivableStatistics")
	@ResponseBody
	public R<Map> receivableStatistics(ReceivableDetailDataQueryParam queryParam) {

		R<Map> result    = new R<>();
		Map    returnMap = new HashMap();

		try {


			Map<String, Object> params = new HashMap<>();


			if(StringUtils.isEmpty(queryParam.getAccountingOrderSn())){
				result.setCode(500);
				result.setSuccess(false);
				result.setMessage("对账单编号不能为空");
				return result;
			}

			receivableStatisticsDubboService.saveReceivableDetailData(queryParam,getToken());


		} 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;
	}


	/**
	 * 应收明细数据定时任务到每日数据
	 *
	 * @return
	 */
	@PostMapping("/saveReceivableDetailDataToDayData")
	@ResponseBody
	public R<Map> saveReceivableDetailDataToDayData(ReceivableDetailDataQueryParam queryParam) {

		R<Map> result    = new R<>();
		Map    returnMap = new HashMap();

		try {


			if(StringUtils.isEmpty(queryParam.getAccountingOrderSn())){
				result.setCode(500);
				result.setSuccess(false);
				result.setMessage("对账单编号不能为空");
				return result;
			}

			receivableStatisticsDubboService.saveReceivableDetailDataToDayData(queryParam,getToken());



		} 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;
	}






	/**
	 * 积分结算_对账单状态变更为“已取消”时
	 *
	 * @return
	 */
	@PostMapping("/receivableCancelAccounting")
	@ResponseBody
	public R<Map> receivableCancelAccounting(ReceivableDetailDataQueryParam queryParam) {

		R<Map> result    = new R<>();
		Map    returnMap = new HashMap();

		try {


			Map<String, Object> params = new HashMap<>();

			receivableStatisticsDubboService.receivableCancelAccounting(queryParam,getToken());



		} 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;
	}

	/**
	 * 实收_积分结算_对账单_回款状态变更为“已回款”时
	 *
	 * @return
	 */
	@PostMapping("/receivableReturnedMoney")
	@ResponseBody
	public R<Map> receivableReturnedMoney(ReceivableDetailDataQueryParam queryParam) {

		R<Map> result    = new R<>();
		Map    returnMap = new HashMap();

		try {


			Map<String, Object> params = new HashMap<>();

			receivableStatisticsDubboService.receivableReturnedMoney(queryParam,getToken());



		} 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;
	}


}
