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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.agent.ctrl.CommonCtrl;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.core.model.DBOEMConfig;
import com.jeequan.jeepay.core.service.ISysConfigService;
import com.jeequan.jeepay.db.entity.AgentInfo;
import com.jeequan.jeepay.db.entity.SysUserEntity;
import com.jeequan.jeepay.db.entity.ToDoListModel;
import com.jeequan.jeepay.service.impl.AgentInfoService;
import com.jeequan.jeepay.service.impl.PayOrderService;
import com.jeequan.jeepay.service.impl.StatsTradeService;
import com.jeequan.jeepay.service.impl.SysUserService;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主页数据类
 *
 * @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 AgentInfoService agentInfoService;
    @Autowired private ISysConfigService sysConfigService;
    @Autowired private StatsTradeService statsTradeService;

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

        List<String> agentSubList = agentInfoService.queryAllSubAgentNo(getCurrentAgentNo());
        // Map<String, String> agentNoMap = getAgentNo(reqParamJSON);
        // JSONObject resJson = payOrderService.payDayCount(null, agentNoMap.get("agentNo"), agentNoMap.get("topAgentNo"), reqParamJSON);
        JSONObject resJson = statsTradeService.payDayCount(null, agentSubList, reqParamJSON);
        return ApiRes.ok(resJson);
    }

    /**
     * @author: xiaoyu
     * @date: 2021/12/28 14:53
     * @describe: 趋势图数据
     */
    @PreAuthorize("hasAuthority('ENT_AGENT_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);
        List<String> agentSubList = agentInfoService.queryAllSubAgentNo(getCurrentAgentNo());

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

    /**
     * @author: xiaoyu
     * @date: 2021/12/28 14:54
     * @describe: 交易统计
     */
    @PreAuthorize("hasAuthority('ENT_AGENT_MAIN_PAY_COUNT')")
    @RequestMapping(value="/payCount", method = RequestMethod.GET)
    public ApiRes payCount() {
        Map<String, String> agentNoMap = getAgentNo(getReqParamJSON());
        List<String> agentSubList = agentInfoService.queryAllSubAgentNo(getCurrentAgentNo());
        // 获取传入参数
        JSONObject resJson = payOrderService.mainPagePayCount(null, agentSubList, getReqParamJSON());
        return ApiRes.ok(resJson);
    }

    /**
     * @author: xiaoyu
     * @date: 2021/12/28 14:54
     * @describe: 支付方式统计
     */
    @PreAuthorize("hasAuthority('ENT_AGENT_MAIN_PAY_TYPE_COUNT')")
    @RequestMapping(value="/payTypeCount", method = RequestMethod.GET)
    public ApiRes payWayCount() {
        Map<String, String> agentNoMap = getAgentNo(getReqParamJSON());
        List<String> agentSubList = agentInfoService.queryAllSubAgentNo(getCurrentAgentNo());
        ArrayList arrayResult = payOrderService.mainPagePayTypeCount(null, agentSubList, getReqParamJSON());
        return ApiRes.ok(arrayResult);
    }

    /**
     * @author: xiaoyu
     * @date: 2021/12/28 14:54
     * @describe: 服务商基本信息、用户基本信息
     */
    @PreAuthorize("hasAuthority('ENT_C_USERINFO')")
    @RequestMapping(value="", method = RequestMethod.GET)
    public ApiRes userDetail() {
        SysUserEntity sysUserEntity = sysUserService.getById(getCurrentUser().getSysUser().getSysUserId());
        AgentInfo agentInfo = agentInfoService.getById(getCurrentAgentNo());
        Byte infoState = getCurrentUser().getInfoState();
        DBOEMConfig dboemConfig = sysConfigService.getOemConfig();

        JSONObject json = (JSONObject) JSON.toJSON(agentInfo);
        json.put("promiseFile", dboemConfig.getPromiseFile());
        json.put("loginUsername", sysUserEntity.getLoginUsername());
        json.put("realname", sysUserEntity.getRealname());
        json.put("infoState", infoState);
        return ApiRes.ok(json);
    }

    /**
     * @author: xiaoyu
     * @date: 2022/3/18 16:20
     * @describe: 与app共用接口返回服务商号
     */
    public Map<String, String> getAgentNo(JSONObject paramJSON) {
        HashMap<String, String> AgentNoMap = new HashMap<>();
        String agentNo = null;
        String topAgentNo = getCurrentAgentNo();
        if (StringUtils.isNotEmpty(paramJSON.getString("agentNo"))) {
            agentNo = paramJSON.getString("agentNo");
        }
        AgentNoMap.put("agentNo", agentNo);
        AgentNoMap.put("topAgentNo", topAgentNo);
        return AgentNoMap;
    }
    /**
     *
     * @describe （待办事项）汇总待审核或者待操作的事件数量
     *
     * @return
     */
    @RequestMapping(value = "/getWaitOperationNumber",method = RequestMethod.GET)
    public ApiRes getWaitOperationNumber(){
        List<ToDoListModel> waitOperationNumber = new ArrayList<>();
        return ApiRes.ok(waitOperationNumber);
    }
}
