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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 统计列表接口
 *
 * @author xiaoyu
 *
 * @date 2022/5/23 16:52
 */
@Slf4j
@RestController
@RequestMapping("/api/statistic")
public class StatisticController 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 list() {
        JSONObject paramJSON = getReqParamJSON();
        PayOrder payOrder = getObject(PayOrder.class);
        String method = getValStringRequired("method");
        String mchNo = getCurrentMchNo();
        payOrder.setMchNo(mchNo);

        // 门店ID条件
        List<String> storeIdList = getCurrentUser().getStoreIdList();
        paramJSON.put("storeIdList", storeIdList);
        paramJSON.put("mchNo", mchNo);


        Page<PayOrderCount> countPage = new Page<>();
        switch (method) {
            case CS.STATISTIC_TYPE.TRANSACTION:
                // 交易报表
                countPage = statsTradeService.selectTransactionStatsList(payOrder, paramJSON, null);
                break;
            case CS.STATISTIC_TYPE.STORE:
                // 门店
                countPage = statsTradeService.selectStoreStatsList(paramJSON);
                break;
            case CS.STATISTIC_TYPE.WAY_CODE:
                // 支付方式
                countPage = statsPayWayService.selectPayWayStatsList(paramJSON);
                break;
            case CS.STATISTIC_TYPE.WAY_CODE_TYPE:
                // 支付类型
                countPage = statsPayWayService.selectWayCodeTypeStatsList(paramJSON);
                //对返回数据进行指定排序
                List<PayOrderCount> records = countPage.getRecords();
                Collections.sort(records, new Comparator<PayOrderCount>() {
                    @Override
                    public int compare(PayOrderCount o1, PayOrderCount o2) {
                        String wayTypeName1 = o1.getName();
                        String wayTypeName2 = o2.getName();
                        List<String> orderList = Arrays.asList("微信", "支付宝", "云闪付","银联","数字人民币","其他");
                        int indexOf1 = orderList.indexOf(wayTypeName1);
                        int indexOf2 = orderList.indexOf(wayTypeName2);
                        return indexOf1 - indexOf2;
                    }
                });
                break;
            case CS.STATISTIC_TYPE.DEVICE:
                // 设备
                countPage = statsDeviceService.selectStatsList(paramJSON, null);
                break;
            case CS.STATISTIC_TYPE.MCH:
                //商户统计
                countPage = statsTradeService.selectByMch(paramJSON,null);
                break;
            default:
                break;
        }
        // 解决hasnext不显示的问题
        countPage.setCurrent(paramJSON.getIntValue("pageNumber"));
        countPage.setSize(paramJSON.getIntValue("pageSize"));
        return ApiRes.page(countPage);
    }
}
