package com.jeequan.jeepay.mch.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.mch.ctrl.CommonCtrl;
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.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 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_STORE', 'ENT_STATISTIC_MCH_WAY', 'ENT_STATISTIC_MCH_TYPE')")
    @RequestMapping(value="", method = RequestMethod.GET)
    public ApiRes total() {
        JSONObject paramJSON = getReqParamJSON();
        PayOrder payOrder = getObject(PayOrder.class);
        String method = getValStringRequired("method");
        String mchNo = getCurrentMchNo();
        payOrder.setMchNo(mchNo);
        paramJSON.put("mchNo", mchNo);
        // 门店ID条件
        List<String> storeIdList = getCurrentUser().getStoreIdList();
        paramJSON.put("storeIdList", storeIdList);

        PayOrderCount orderCount = null;
        switch (method) {
            case CS.STATISTIC_TYPE.TRANSACTION:
                // 交易报表统计
                orderCount = statsTradeService.selectTotalByTransaction(paramJSON, null);
                break;
            case CS.STATISTIC_TYPE.STORE:
                // 门店统计
                orderCount = statsTradeService.selectTotalByStore(paramJSON, null);
                break;
            case CS.STATISTIC_TYPE.WAY_CODE:
                // 支付方式统计
                orderCount = statsPayWayService.selectTotalByPayWay(paramJSON);
                break;
            case CS.STATISTIC_TYPE.WAY_CODE_TYPE:
                // 支付类型统计
                orderCount = statsPayWayService.selectTotalByWayCodeType(paramJSON);
                break;
            case CS.STATISTIC_TYPE.DEVICE:
                // 设备统计
                orderCount = statsDeviceService.selectTotalByDeviceNo(paramJSON, null);
                break;
            case CS.STATISTIC_TYPE.MCH:
                // 商户统计
                orderCount = statsTradeService.selectTotalByTransaction(paramJSON, null);
                break;
            default:
                break;
        }
        return ApiRes.ok(orderCount);
    }
}
