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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.MchInfo;
import com.jeequan.jeepay.db.entity.SysUserEntity;
import com.jeequan.jeepay.db.entity.ToDoListModel;
import com.jeequan.jeepay.mch.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.*;
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.ArrayList;
import java.util.List;

/**
 * 主页数据类
 *
 * @author pangxiaoyu
 *
 * @date 2021-04-27 15:50
 */
@Slf4j
@RestController
@RequestMapping("api/mainChart")
public class MainChartController extends CommonCtrl {

    @Autowired private PayOrderService payOrderService;

    @Autowired private SysUserService sysUserService;

    @Autowired private MchInfoService mchInfoService;
    @Autowired private StatsTradeService statsTradeService;
    @Autowired private MainChartService mainChartService;
    /**
     * @author: xiaoyu
     * @date: 2021/12/28 14:54
     * @describe: 今日/昨日交易统计
     */
    @PreAuthorize("hasAuthority('ENT_MCH_MAIN_PAY_DAY_COUNT')")
    @RequestMapping(value="/payDayCount", method = RequestMethod.GET)
    public ApiRes payDayCount() {
        JSONObject reqParamJSON = getReqParamJSON();

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

        // JSONObject resJson = payOrderService.payDayCount(getCurrentMchNo(), null, null, paramJSON);
        JSONObject resJson = statsTradeService.payDayCount(getCurrentMchNo(), null, reqParamJSON);
        return ApiRes.ok(resJson);
    }

    /**
     * @author: xiaoyu
     * @date: 2021/12/28 14:53
     * @describe: 趋势图数据
     */
    @PreAuthorize("hasAuthority('ENT_MCH_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);

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

        JSONObject resJson = payOrderService.payTrendCount(paramJSON, getCurrentMchNo(), null);
        return ApiRes.ok(resJson);
    }

    /**
     * @author: xiaoyu
     * @date: 2021/12/28 14:54
     * @describe: 交易统计
     */
    @PreAuthorize("hasAuthority('ENT_MCH_MAIN_PAY_COUNT')")
    @RequestMapping(value="/payCount", method = RequestMethod.GET)
    public ApiRes payCount() {
        JSONObject paramJSON = getReqParamJSON();
        // 门店ID条件
        List<String> storeIdList = getCurrentUser().getStoreIdList();
        paramJSON.put("storeIdList", storeIdList);

        // 获取传入参数
        JSONObject resJson = payOrderService.mainPagePayCount(getCurrentMchNo(), null, paramJSON);
        return ApiRes.ok(resJson);
    }

    /**
     * @author: xiaoyu
     * @date: 2021/12/28 14:54
     * @describe: 支付方式统计
     */
    @PreAuthorize("hasAuthority('ENT_MCH_MAIN_PAY_TYPE_COUNT')")
    @RequestMapping(value="/payTypeCount", method = RequestMethod.GET)
    public ApiRes payWayCount() {
        JSONObject paramJSON = getReqParamJSON();
        // 门店ID条件
        List<String> storeIdList = getCurrentUser().getStoreIdList();
        paramJSON.put("storeIdList", storeIdList);

        ArrayList arrayResult = payOrderService.mainPagePayTypeCount(getCurrentMchNo(), null, paramJSON);
        return ApiRes.ok(arrayResult);
    }

    /**
     * @author: xiaoyu
     * @date: 2021/12/28 14:54
     * @describe: 商户基本信息、用户基本信息
     */
    @PreAuthorize("hasAuthority('ENT_MCH_INFO')")
    @RequestMapping(value="", method = RequestMethod.GET)
    public ApiRes userDetail() {
        SysUserEntity sysUserEntity = sysUserService.getById(getCurrentUser().getSysUser().getSysUserId());
        MchInfo mchInfo = mchInfoService.getById(getCurrentMchNo());
        JSONObject json = (JSONObject) JSON.toJSON(mchInfo);
        json.put("loginUsername", sysUserEntity.getLoginUsername());
        json.put("realname", sysUserEntity.getRealname());
        return ApiRes.ok(json);
    }
    /**
     *
     * @describe （待办事项）汇总待审核或者待操作的事件数量
     *
     * @return
     */
    @RequestMapping(value = "/getWaitOperationNumber",method = RequestMethod.GET)
    public ApiRes getWaitOperationNumber(){
        List<ToDoListModel> waitOperationNumber = new ArrayList<>();
        return ApiRes.ok(waitOperationNumber);
    }
}
