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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.*;
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")
public class StatisticController extends CommonCtrl {

    @Autowired private PayOrderService payOrderService;
    @Autowired private AgentInfoService agentInfoService;
    @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_CHANNEL')")
    @RequestMapping(value="", method = RequestMethod.GET)
    public ApiRes list() {
        JSONObject paramJSON = getReqParamJSON();
        PayOrder payOrder = getObject(PayOrder.class);
        List<String> agentSubList = null;
        // 交易报表
        if (StringUtils.isEmpty(payOrder.getAgentNo())) {
            // 查询服务商所有下级
            agentSubList = agentInfoService.queryAllSubAgentNo(getCurrentAgentNo());
        }
        String method = getValStringRequired("method");
        String mchNo = null;
        Page<PayOrderCount> mapPage = new Page<>();
        switch (method) {
            case CS.STATISTIC_TYPE.MCH:
                // 商户
                mapPage = statsTradeService.selectMchStatsList(paramJSON, agentSubList);
                break;
            case CS.STATISTIC_TYPE.AGENT:
                // 服务商
                mapPage = statsTradeService.selectAgentStatsList(paramJSON, agentSubList);
                break;
            case CS.STATISTIC_TYPE.TRANSACTION:
                mapPage = statsTradeService.selectTransactionStatsList(payOrder, paramJSON, agentSubList);
                break;
            case CS.STATISTIC_TYPE.STORE:
                // 门店
                getValStringRequired("mchNo");
                mapPage = statsTradeService.selectStoreStatsList(paramJSON);
                break;
            case CS.STATISTIC_TYPE.WAY_CODE:
                // 支付方式
                getValStringRequired("mchNo");
                mapPage = statsPayWayService.selectPayWayStatsList(paramJSON);
                break;
            case CS.STATISTIC_TYPE.WAY_CODE_TYPE:
                // 支付类型
                getValStringRequired("mchNo");
                mapPage = statsPayWayService.selectWayCodeTypeStatsList(paramJSON);
                break;
            case CS.STATISTIC_TYPE.DEVICE:
                // 设备
                mapPage = statsDeviceService.selectStatsList(paramJSON, agentSubList);
                break;
            default:
                break;
        }
        // 解决hasnext不显示的问题
        mapPage.setCurrent(paramJSON.getIntValue("pageNumber"));
        mapPage.setSize(paramJSON.getIntValue("pageSize"));
        return ApiRes.page(mapPage);
    }
}
