package com.jeequan.jeepay.mgr.ctrl.statistics;

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.PayOrderCount;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.PayOrder;
import com.jeequan.jeepay.mgr.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.StatsChannelService;
import com.jeequan.jeepay.service.impl.StatsDeviceService;
import com.jeequan.jeepay.service.impl.StatsPayWayService;
import com.jeequan.jeepay.service.impl.StatsTradeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 总计接口
 *
 * @author xiaoyu
 *
 * @date 2022/5/23 16:52
 */
@Slf4j
@RestController
@RequestMapping("/api/statistic/total")
public class TotalStatisticController extends CommonCtrl {

    @Autowired private StatsTradeService statsTradeService;
    @Autowired private StatsChannelService statsChannelService;
    @Autowired private StatsPayWayService statsPayWayService;
    @Autowired private StatsDeviceService statsDeviceService;


    /**
     * @author: xiaoyu
     * @date: 2022/5/23 17:15
     * @describe: 总计
     */
    @PreAuthorize("hasAnyAuthority('ENT_STATISTIC_TRANSACTION', 'ENT_STATISTIC_MCH', 'ENT_STATISTIC_MCH_STORE', 'ENT_STATISTIC_MCH_WAY', 'ENT_STATISTIC_MCH_TYPE', 'ENT_STATISTIC_AGENT', 'ENT_STATISTIC_ISV', 'ENT_STATISTIC_CHANNEL')")
    @RequestMapping(value="", method = RequestMethod.GET)
    public ApiRes total() {
        JSONObject paramJSON = getReqParamJSON();
        String method = getValStringRequired("method");
        PayOrderCount orderCount = new PayOrderCount();
        switch (method) {
            case CS.STATISTIC_TYPE.MCH:
                // 商户统计
                // resMap = payOrderService.totalByMch(payOrder, paramJSON, null);
                orderCount = statsTradeService.selectTotalByMch(paramJSON, null);
                break;
            case CS.STATISTIC_TYPE.AGENT:
                // 服务商统计
                // resMap = payOrderService.totalByAgent(payOrder, paramJSON, null);
                orderCount = statsTradeService.selectTotalByAgent(paramJSON, null);
                break;
            case CS.STATISTIC_TYPE.ISV:
                // 服务商统计
                // resMap = payOrderService.totalByIsv(payOrder, paramJSON);
                orderCount = statsTradeService.selectTotalByIsv(paramJSON);
                break;
            case CS.STATISTIC_TYPE.CHANNEL:
                // 通道统计
                // resMap = payOrderService.totalByChannel(payOrder, paramJSON);
                orderCount = statsChannelService.selectTotalByChannel(paramJSON);
                break;
            case CS.STATISTIC_TYPE.TRANSACTION:
                // 交易报表统计
                // resMap = payOrderService.totalByTransaction(payOrder, paramJSON, null);
                orderCount = statsTradeService.selectTotalByTransaction(paramJSON, null);
                break;
            case CS.STATISTIC_TYPE.STORE:
                // 门店统计
                getValStringRequired("mchNo");
                // payOrder.setMchNo(mchNo);
                // resMap = payOrderService.totalByStore(payOrder, paramJSON);
                orderCount = statsTradeService.selectTotalByStore(paramJSON, null);
                break;
            case CS.STATISTIC_TYPE.WAY_CODE:
                // 支付方式统计
                getValStringRequired("mchNo");
                // payOrder.setMchNo(mchNo);
                // resMap = payOrderService.totalByWayCode(payOrder, paramJSON);
                orderCount = statsPayWayService.selectTotalByPayWay(paramJSON);
                break;
            case CS.STATISTIC_TYPE.WAY_CODE_TYPE:
                // 支付类型统计
                getValStringRequired("mchNo");
                // payOrder.setMchNo(mchNo);
                // resMap = payOrderService.totalByWayCodeType(payOrder, paramJSON);
                orderCount = statsPayWayService.selectTotalByWayCodeType(paramJSON);
                break;
            case CS.STATISTIC_TYPE.DEVICE:
                // 设备统计
                // resMap = payOrderService.totalByDevice(payOrder, paramJSON, null);
                orderCount = statsDeviceService.selectTotalByDeviceNo(paramJSON, null);
                break;
            default:
                break;
        }
        return ApiRes.ok(orderCount);
    }

    public List<String> getAgentSubList(JSONObject paramJSON, PayOrder payOrder) {
        List<String> allAgentNoList = null;
        if (StringUtils.isNotEmpty(payOrder.getAgentNo())) {
            Boolean onlyOne = paramJSON.getBoolean("onlyOne");
            if (onlyOne) {
                // 仅查询单个服务商
                allAgentNoList = Arrays.asList(payOrder.getAgentNo());
            }else {
                // 查询服务商所有下级
                allAgentNoList = agentInfoService.queryAllSubAgentNo(payOrder.getAgentNo());
            }
        }
        return allAgentNoList;
    }
}
