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

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.agent.ctrl.CommonCtrl;
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.service.impl.AgentInfoService;
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.List;

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

    @Autowired private AgentInfoService agentInfoService;
    @Autowired private StatsTradeService statsTradeService;
    @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_CHANNEL')")
    @RequestMapping(value="", method = RequestMethod.GET)
    public ApiRes total() {
        JSONObject paramJSON = getReqParamJSON();
        PayOrder payOrder = getObject(PayOrder.class);

        List<String> agentSubList = null;

        if (StringUtils.isEmpty(payOrder.getAgentNo())) {
            // 查询服务商所有下级
            agentSubList = agentInfoService.queryAllSubAgentNo(getCurrentAgentNo());
        }else {

        }
        String method = getValStringRequired("method");
        String mchNo = null;
        PayOrderCount orderCount = new PayOrderCount();
        switch (method) {
            case CS.STATISTIC_TYPE.MCH:
                // 商户统计
                // resMap = payOrderService.totalByMch(payOrder, paramJSON, agentSubList);
                orderCount = statsTradeService.selectTotalByTransaction(paramJSON, agentSubList);
                break;
            case CS.STATISTIC_TYPE.AGENT:
                // 服务商统计
                // resMap = payOrderService.totalByAgent(payOrder, paramJSON, agentSubList);
                orderCount = statsTradeService.selectTotalByAgent(paramJSON, agentSubList);
                break;
            case CS.STATISTIC_TYPE.TRANSACTION:
                // 交易报表统计
                // resMap = payOrderService.totalByTransaction(payOrder, paramJSON, agentSubList);
                orderCount = statsTradeService.selectTotalByTransaction(paramJSON, agentSubList, false);
                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, agentSubList);
                orderCount = statsDeviceService.selectTotalByDeviceNo(paramJSON, agentSubList);
                break;
            default:
                break;
        }
        return ApiRes.ok(orderCount);
    }
}
