package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.constant.CtopRoleCodeConstant;
import cn.com.ctop.common.module.service.ISysRoleExtService;
import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import cn.com.ctop.common.module.vo.PangolinReportAppMonthVO;
import cn.com.ctop.crawler.modules.pangolin.service.*;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("pangolin")
public class PangolinController {
    @Autowired
    private PangolinReportAppHourService pangolinReportAppHourService;
    @Autowired
    private PangolinReportChannelDailyService pangolinReportChannelDailyService;
    @Autowired
    private PangolinReportBillMonthService pangolinReportBillMonthService;
    @Autowired
    private PangolinAppService pangolinAppService;
    @Autowired
    private PangolinChannelService pangolinChannelService;
    @Autowired
    private ISysRoleExtService sysRoleService;

    @PostMapping("api/channel/list")
    public Map<String, Object> channelList(@RequestBody JSONObject data) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = user.getId();
        String roleCode = sysRoleService.getRoleCodeByUserId(userId);
        if(null!=roleCode&&(roleCode.equals(CtopRoleCodeConstant.COMMON_ROLE_CODE_ADMIN))){
            userId = null;
        }
        return pangolinChannelService.getListByUserId(userId);
    }

    @PostMapping("api/app/list")
    public Map<String, Object> appList(@RequestBody JSONObject data) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = user.getId();
        String roleCode = sysRoleService.getRoleCodeByUserId(userId);
        if(null!=roleCode&&(roleCode.equals(CtopRoleCodeConstant.COMMON_ROLE_CODE_ADMIN))){
            userId = null;
        }
        return pangolinAppService.getListByUserId(userId,data);
    }


    @PostMapping("api/memberList")
    public Map<String,Object> memberList(){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            String userId = user.getId();
            String roleCode = sysRoleService.getRoleCodeByUserId(userId);
            if (null!=roleCode&&CtopRoleCodeConstant.COMMON_ROLE_CODE_ADMIN.equals(roleCode)) {
                userId = null;
            }
            resultMap = pangolinAppService.getMemberList(userId);
            return resultMap;
        } catch (Exception e) {
            ResultMapUtils.setResultMap(resultMap,StatusCode.COMMON_SERVER_ERROR);
            return resultMap;
        }
    }


    /**
     * 实时数据
     */
    @PostMapping("api/realtime")
    public Map<String, Object> realTime(@RequestBody JSONObject data) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = user.getId();
        String roleCode = sysRoleService.getRoleCodeByUserId(userId);
        if(null!=roleCode&&(roleCode.equals(CtopRoleCodeConstant.COMMON_ROLE_CODE_ADMIN))){
            userId = null;
        }
        return pangolinReportAppHourService.getRealTimeData(userId,data);
    }

    /**
     * 激活统计
     */
    @PostMapping("api/activation/list")
    public Map<String, Object> activationList(@RequestBody JSONObject data) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = user.getId();
        String roleCode = sysRoleService.getRoleCodeByUserId(userId);
        if(null!=roleCode&&(roleCode.equals(CtopRoleCodeConstant.COMMON_ROLE_CODE_ADMIN))){
            userId = null;
        }
        return pangolinReportChannelDailyService.getActivationList(userId, data);
    }

    /**
     * 对账单
     */
    @PostMapping("/api/checkApps/list")
    public Map<String,Object> checkAppList(@RequestBody JSONObject data) {
        Map<String,Object> result = new HashMap<>();
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = user.getId();
        String roleCode = sysRoleService.getRoleCodeByUserId(userId);
        if(null!=roleCode&&(roleCode.equals(CtopRoleCodeConstant.COMMON_ROLE_CODE_ADMIN))){
            userId = null;
        }
        PageInfo<PangolinReportAppMonthVO> pageInfo = pangolinReportBillMonthService.checkAppList(userId, data);
        result.put("data",pageInfo);
        ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
        return result;
    }

}
