package com.hotent.portal.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.constants.JmsConstant;
import com.hotent.base.controller.BaseController;
import com.hotent.base.jms.JmsProducer;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.JsonUtil;
import com.hotent.portal.persistence.manager.AppModelManager;
import com.hotent.sys.persistence.manager.SysAppLogsManager;
import com.hotent.sys.persistence.manager.SysAppManager;
import com.hotent.sys.persistence.manager.SysLogsManager;
import com.hotent.sys.persistence.model.SysLogs;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 描述：App操作日志 控制器类
 * </pre>
 */
@RestController
@RequestMapping(value = "/sys/sysAppLogs/v1")
@Api(tags = "应用日志")
@ApiGroup(group = {ApiGroupConsts.GROUP_PORTAL})
public class SysAppLogsController extends BaseController<SysLogsManager, SysLogs> {
    private Logger logger = LoggerFactory.getLogger(SysAppLogsController.class);
    @Resource
    SysAppLogsManager sysAppLogsManager;
    @Resource
    SysAppManager sysAppManager;
    @Resource
    AppModelManager appModelManager;
    @Resource
    JmsProducer jmsProducer;

    @PostMapping(value = "/linkEventLog")
    @ApiOperation(value = "记录用户使用App", httpMethod = "POST", notes = "记录用户使用App")
    public CommonResult<?> linkEventLog(@RequestParam(value = "appId") String appId, @RequestParam(value = "appType") String appType,
                                        @RequestParam(value = "appName") String appName, @RequestParam(value = "appSource") String appSource,
                                        @RequestParam(value = "userId") String userId, @RequestParam(value = "orgId") String orgId,
                                        @RequestParam(value = "orgName") String orgName) throws Exception {
        ObjectNode data = JsonUtil.getMapper().createObjectNode();
        data.put("appId", appId);
        data.put("appName", appName);
        data.put("appType", appType);
        data.put("appSource", appSource);
        data.put("userId", userId);
        data.put("orgId", orgId);
        data.put("orgName", orgName);

        jmsProducer.sendToQueue(JsonUtil.toJson(data), JmsConstant.APP_LOG_QUEUE);
        return new CommonResult<>(true, "成功");
    }


    /**
     * 应用分类调用记录
     */
    @GetMapping(value = "/getAppStatic")
    @ApiOperation(value = "应用分类调用记录", httpMethod = "GET", notes = "应用分类调用记录")
    public CommonResult<?> getAppStatic(@ApiParam(name = "orgId", value = "组织id") @RequestParam(name = "orgId", required = false, defaultValue = "") String orgId,
                                        @ApiParam(name = "appSource", value = "应用来源") @RequestParam(name = "appSource", required = false, defaultValue = "") String appSource,
                                        @ApiParam(name = "appType", value = "应用分类") @RequestParam(name = "appType", required = false, defaultValue = "") String appType) throws Exception {

        JSONObject json = JSONUtil.createObj();
        int totalLocal = 0;
        int totalRemote = 0;
        int pcLocal = 0;
        int pcRemote = 0;
        int mobileLocal = 0;
        int mobileRemote = 0;
        List<Map<String, Object>> appStatic = sysAppManager.getAppStatic();
        for (Map<String, Object> appMap : appStatic) {
            Integer local_auth_code = (Integer)appMap.get("local_auth_code");
            if (local_auth_code.intValue() == 0) {
                pcLocal = Integer.parseInt(appMap.get("count").toString());
            } else {
                pcRemote = Integer.parseInt(appMap.get("count").toString());
            }
        }

        List<Map<String, Object>> appStatic1 = appModelManager.getAppStatic();
        for (Map<String, Object> appMap : appStatic1) {
            Long local_auth_code = (Long)appMap.get("local_auth_code");
            if (local_auth_code.intValue() == 0) {
                mobileLocal = Integer.parseInt(appMap.get("count").toString());
            } else {
                mobileRemote = Integer.parseInt(appMap.get("count").toString());
            }
        }
        totalLocal = pcLocal + mobileLocal;
        totalRemote = pcRemote + mobileRemote;
        json.putIfAbsent("totalLocal", totalLocal);
        json.putIfAbsent("totalRemote", totalRemote);
        json.putIfAbsent("pcLocal", pcLocal);
        json.putIfAbsent("pcRemote", pcRemote);
        json.putIfAbsent("mobileLocal", mobileLocal);
        json.putIfAbsent("mobileRemote", mobileRemote);
        return new CommonResult<>(true, "获取成功", json);
    }


    /**
     * 应用分类调用记录
     */
    @GetMapping(value = "/getAppTypeStatic")
    @ApiOperation(value = "应用分类调用记录", httpMethod = "GET", notes = "应用分类调用记录")
    public CommonResult<?> getAppTypeStatic(@ApiParam(name = "orgId", value = "组织id") @RequestParam(name = "orgId", required = false, defaultValue = "") String orgId,
                                            @ApiParam(name = "appSource", value = "应用来源") @RequestParam(name = "appSource", required = false, defaultValue = "") String appSource,
                                            @ApiParam(name = "appType", value = "应用分类") @RequestParam(name = "appType", required = false, defaultValue = "") String appType,
                                            @ApiParam(name = "type", value = "类型") @RequestParam(name = "type", required = false, defaultValue = "0") String type,
                                            @ApiParam(name = "year", value = "年") @RequestParam(name = "year", required = false, defaultValue = "") String year,
                                            @ApiParam(name = "data", value = "数据类型") @RequestParam(name = "data", required = false, defaultValue = "") String data) throws Exception {
        return new CommonResult<>(true, "获取成功", sysAppLogsManager.getAppTypeStatic(orgId, appType, appSource, type, year, data));
    }

    /**
     * 应用时间维度调用记录
     */
    @GetMapping(value = "/getAppTimeStatic")
    @ApiOperation(value = "应用时间维度调用记录", httpMethod = "GET", notes = "应用时间维度调用记录")
    public CommonResult<?> getAppTimeStatic(@ApiParam(name = "orgId", value = "组织id") @RequestParam(name = "orgId", required = false, defaultValue = "") String orgId,
                                            @ApiParam(name = "appSource", value = "应用来源") @RequestParam(name = "appSource", required = false, defaultValue = "") String appSource,
                                            @ApiParam(name = "appType", value = "应用分类") @RequestParam(name = "appType", required = false, defaultValue = "") String appType,
                                            @ApiParam(name = "type", value = "类型") @RequestParam(name = "type", required = false, defaultValue = "0") String type,
                                            @ApiParam(name = "year", value = "年") @RequestParam(name = "year", required = false, defaultValue = "") String year,
                                            @ApiParam(name = "data", value = "数据类型") @RequestParam(name = "data", required = false, defaultValue = "") String data) throws Exception {
        return new CommonResult<>(true, "获取成功", sysAppLogsManager.getAppTimeStatic(orgId, appType, appSource, type, year, data));
    }


}
