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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.core.utils.DateKit;
import com.jeequan.jeepay.db.entity.MchInfo;
import com.jeequan.jeepay.db.entity.ToDoListModel;
import com.jeequan.jeepay.mgr.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.MainChartService;
import com.jeequan.jeepay.service.impl.PayOrderService;
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.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 首页统计类
 *
 * @author pangxiaoyu
 *
 * @date 2021-06-07 07:15
 */
@Slf4j
@RestController
@RequestMapping("/api/mainChart")
public class MainChartController extends CommonCtrl {

    @Autowired private PayOrderService payOrderService;
    @Autowired private StatsTradeService statsTradeService;
    @Autowired private MainChartService mainChartService;

    /**
     * @author: xiaoyu
     * @date: 2021/12/28 14:53
     * @describe: 平台商户、服务商数量统计
     */
    @PreAuthorize("hasAuthority('ENT_C_MAIN_ISV_MCH_COUNT')")
    @RequestMapping(value="/isvAndMchCount", method = RequestMethod.GET)
    public ApiRes isvAndMchCount() {
        JSONObject resJson = payOrderService.isvAndMchCount();
        return ApiRes.ok(resJson);
    }

    /**
     * @author: xiaoyu
     * @date: 2021/12/28 14:53
     * @describe: 今日/昨日交易统计
     */
    @PreAuthorize("hasAuthority('ENT_C_MAIN_PAY_DAY_COUNT')")
    @RequestMapping(value="/payDayCount", method = RequestMethod.GET)
    public ApiRes payDayCount() {
        JSONObject reqParamJSON = getReqParamJSON();
        JSONObject resJson = statsTradeService.payDayCount(null, null, reqParamJSON);

        return ApiRes.ok(resJson);
    }

    /**
     * @author: xiaoyu
     * @date: 2021/12/28 14:53
     * @describe: 趋势图数据
     */
    @PreAuthorize("hasAuthority('ENT_C_MAIN_PAY_TREND_COUNT')")
    @RequestMapping(value="/payTrendCount", method = RequestMethod.GET)
    public ApiRes payTrendCount() {
        JSONObject paramJSON = getReqParamJSON();
        int recentDay = paramJSON.getIntValue("recentDay");
        if (recentDay == 0){
            recentDay = 30;   // 默认30天
        }
        paramJSON.put("recentDay", recentDay);
        JSONObject resJson = payOrderService.payTrendCount(paramJSON, null, null);
        return ApiRes.ok(resJson);
    }

    /**
     * @author: pangxiaoyu
     * @date: 2021/6/7 16:18
     * @describe: 交易统计
     */
    @PreAuthorize("hasAuthority('ENT_C_MAIN_PAY_COUNT')")
    @RequestMapping(value="/payCount", method = RequestMethod.GET)
    public ApiRes payCount() {
        JSONObject paramJSON = getReqParamJSON();
        JSONObject resJson = payOrderService.mainPagePayCount(null, null, paramJSON);
        return ApiRes.ok(resJson);
    }

    /**
     * @author: pangxiaoyu
     * @date: 2021/6/7 16:18
     * @describe: 支付方式统计
     */
    @PreAuthorize("hasAuthority('ENT_C_MAIN_PAY_TYPE_COUNT')")
    @RequestMapping(value="/payTypeCount", method = RequestMethod.GET)
    public ApiRes payWayCount() {
        JSONObject paramJSON = getReqParamJSON();
        ArrayList arrayResult = payOrderService.mainPagePayTypeCount(null, null, paramJSON);
        return ApiRes.ok(arrayResult);
    }

    /**
     * @author: xiaoyu
     * @date: 2022/3/23 16:39
     * @describe: 拓展员商户统计数据
     */
    @RequestMapping(value="/userChart", method = RequestMethod.GET)
    public ApiRes userChart() {

        Long sysUserId = getValLongRequired("sysUserId");
        String dateRange = getValStringRequired("queryDateRange");

        // 返回数据
        JSONObject result = new JSONObject();
        result.put("mchAllCount", 0);
        result.put("mchTodayAddCount", 0);
        result.put("mchOnNetCount", 0);
        result.put("mchOnNetNewCount", 0);
        result.put("payAllAmount", 0);
        result.put("payAmount", 0);

        // 拓展员邀请的商户
        List<MchInfo> list = mchInfoService.list(MchInfo.gw().eq(MchInfo::getCreatedUid, sysUserId));
        if (CollUtil.isEmpty(list)) {
            return ApiRes.ok(result);
        }

        // 获取查询时间请求参数  [开始时间,  结束时间]
        Date[] queryDateRangeArray = DateKit.getQueryDateRange(dateRange);

        String createdStart = DateUtil.formatDateTime(queryDateRangeArray[0]);
        String createdEnd = DateUtil.formatDateTime(queryDateRangeArray[1]);

        // 商户总数
        long mchAllCount = mchInfoService.mchCount(null, sysUserId, null, null);
        result.put("mchAllCount", mchAllCount);

        // 按时间搜索商户数
        long mchTodayAddCount = mchInfoService.mchCount(null, sysUserId, createdStart, createdEnd);
        result.put("mchTodayAddCount", mchTodayAddCount);

        // 商户入网总数
        long mchOnNetCount = mchInfoService.mchApplyCount(null, sysUserId, null, null);
        result.put("mchOnNetCount", mchOnNetCount);

        // 按时间搜索商户入网数
        long mchOnNetNewCount = mchInfoService.mchApplyCount(null, sysUserId, createdStart, createdEnd);
        result.put("mchOnNetNewCount", mchOnNetNewCount);

        BigDecimal payAllAmount = payOrderService.totalAmount(null, sysUserId, null, null);
        result.put("payAllAmount", payAllAmount);

        BigDecimal payAmount = payOrderService.totalAmount(null, sysUserId, createdStart, createdEnd);
        result.put("payAmount", payAmount);

        return ApiRes.ok(result);
    }

    /**
     *
     * @describe （待办事项）汇总待审核或者待操作的事件数量
     *
     * @return
     */
    @RequestMapping(value = "/getWaitOperationNumber",method = RequestMethod.GET)
    public ApiRes getWaitOperationNumber(){
        List<ToDoListModel> waitOperationNumber = mainChartService.getWaitOperationNumber();
        return ApiRes.ok(waitOperationNumber);
    }

}
