package com.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.business.domain.ImputationData;
import com.business.service.ImputationDataService;
import com.sooka.common.core.controller.BaseController;
import com.sooka.common.core.domain.AjaxResult;
import com.sooka.framework.util.ShiroUtils;
import com.sooka.system.domain.SysDept;
import com.sooka.system.domain.SysDictData;
import com.sooka.system.domain.TUInterfaceinfo;
import com.sooka.system.service.ISysDeptService;
import com.sooka.system.service.ITUInterfaceinfoService;
import com.sooka.system.utils.DictUtils;
import com.util.StaticScheduleTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import sun.plugin2.util.SystemUtil;

import java.util.List;

/**
 * @author Administrator
 */
@Controller
@RequestMapping("visualization")
public class VisualizationController extends BaseController {

    @Autowired
    private ITUInterfaceinfoService interfaceinfoService;

    @Autowired
    private ImputationDataService imputationDataService;

    @Autowired
    private ISysDeptService sysDeptService;

    @GetMapping("index")
    public String index(ModelMap mmap){
        SysDept dept = new SysDept();
        dept.setParentId(214L);
        List<SysDept> depts = sysDeptService.selectDeptList(dept);
        mmap.put("depts",depts);
        return "/visualization/index";
    }

    @GetMapping("tk_iframe/{id}")
    public String tk_iframe(@PathVariable("id") Long id, ModelMap mmap){
        /**查询部门信息**/
        SysDept sysDept = sysDeptService.selectDeptById(id);
        /**查询接口列表**/
        TUInterfaceinfo interfaceinfo = new TUInterfaceinfo();
        interfaceinfo.setDeptId(id.toString());
        List<TUInterfaceinfo> interfaceList = interfaceinfoService.selectTUInterfaceinfoList(interfaceinfo);
        /**查询接口数量**/
        TUInterfaceinfo shareCount = interfaceinfoService.getShareCountByDeptId(id.toString());

        /**查询归集数据分析**/

        mmap.put("deptId",sysDept.getDeptId());
        mmap.put("deptName",sysDept.getDeptName());
        mmap.put("interfaceList",interfaceList);
        mmap.put("shareCount",shareCount);
        return "/visualization/tk_iframe";
    }

    /**
     * 查询 接口接口占比-子页
     * */
    @PostMapping("subPercent")
    @ResponseBody
    public JSONObject getSubPercent(String deptId,String year){
        JSONObject jsonObject = new JSONObject();
        List<TUInterfaceinfo> list_percent = interfaceinfoService.getSubPercent(deptId,year);
        jsonObject.put("percent",list_percent);
        return jsonObject;
    }

    /**
     * 查询 接口审批分析-子页 自动获取近三年
     * */
    @PostMapping("subJkspfx")
    @ResponseBody
    public JSONObject subJkspfx(String deptId){
        JSONObject jsonObject = new JSONObject();
        //年份数据
        jsonObject.put("years",getYears());
        //类别数组
        List<SysDictData> list = DictUtils.getDictCache("share_type");
        String[] types = new String[list.size()];
        for(int i=0;i<list.size();i++){
            types[i] = list.get(i).getDictLabel();
        }
        jsonObject.put("types",types);
        for(int i=0;i<getYears().length;i++){
            TUInterfaceinfo subJkspfx = interfaceinfoService.getSubJkspfx(deptId,getYears()[i]);
            String[] jkspfxData = new String[4];
            jkspfxData[0] = subJkspfx.getShareType1();
            jkspfxData[1] = subJkspfx.getShareType2();
            jkspfxData[2] = subJkspfx.getShareType3();
            jkspfxData[3] = subJkspfx.getShareType4();
            jsonObject.put(getYears()[i],jkspfxData);
        }
        return jsonObject;
    }

    /**获取当前年和前两年的年份**/
    private String[] getYears() {
        String[] arr = new String[3];
        for(int i=0;i<arr.length;i++){
            arr[i] = StaticScheduleTask.getYears(i-2);
        }
        return arr;
    }

    /**
     * 查询 接口数量和接口占比
     * */
    @PostMapping("countAndPercent")
    @ResponseBody
    public JSONObject getCountAndPercent(String year){
        JSONObject jsonObject = new JSONObject();
        List<TUInterfaceinfo> list_count = interfaceinfoService.getCount();
        List<TUInterfaceinfo> list_percent = interfaceinfoService.getPercent(year);
        jsonObject.put("count",list_count);
        jsonObject.put("percent",list_percent);
        return jsonObject;
    }

    /**
     * 查询省接口调用频次TOP5、TOP20
     * */
    @PostMapping("getProvinceFrequency")
    @ResponseBody
    public JSONObject getProvinceFrequency(){
        JSONObject jsonObject = new JSONObject();
        //省top5
        List<TUInterfaceinfo> list_top5 = interfaceinfoService.getProvinceTop5Frequency();
        //省top20
        List<TUInterfaceinfo> list_top20 = interfaceinfoService.getProvinceTop20Frequency();
        jsonObject.put("top5",list_top5);
        jsonObject.put("top20",list_top20);
        return jsonObject;
    }

    /**
     * 查询市接口调用频次TOP5、TOP20
     * */
    @PostMapping("getCityFrequency")
    @ResponseBody
    public JSONObject getCityFrequency(){
        JSONObject jsonObject = new JSONObject();
        //市top5
        List<TUInterfaceinfo> list_top5 = interfaceinfoService.getCityTop5Frequency();
        //市top20
        List<TUInterfaceinfo> list_top20 = interfaceinfoService.getCityTop20Frequency();
        jsonObject.put("top5",list_top5);
        jsonObject.put("top20",list_top20);
        return jsonObject;
    }

    /**
     * 查询归集和接口排名TOP10
     * */
    @PostMapping("getImputationDataAndInterfaceRanking")
    @ResponseBody
    public JSONObject getImputationDataAndInterfaceRanking(){
        JSONObject jsonObject = new JSONObject();
        //数据
        List<TUInterfaceinfo> list_data = interfaceinfoService.getImputationDataRanking();
        //接口
        List<TUInterfaceinfo> list_interface = interfaceinfoService.getImputationInterfaceRanking();
        jsonObject.put("data",list_data);
        jsonObject.put("interface",list_interface);
        return jsonObject;
    }

    /**
     * 查询归集和接口排名TOP10
     * */
    @PostMapping("getImputationDataList")
    @ResponseBody
    public String[] getImputationDataList(String year){
        String[] arr = new String[12];
        ImputationData id = new ImputationData();
        id.setYear(year);
        ImputationData data = imputationDataService.selectImputationData(id);
            arr[0] = data.getJan();
            arr[1] = data.getFeb();
            arr[2] = data.getMar();
            arr[3] = data.getApr();
            arr[4] = data.getMay();
            arr[5] = data.getJun();
            arr[6] = data.getJul();
            arr[7] = data.getAug();
            arr[8] = data.getSep();
            arr[9] = data.getOct();
            arr[10] = data.getNov();
            arr[11] = data.getDecb();
        return arr;
    }

    /**
     * 查询故障接口数量
     * */
    @PostMapping("getFaultInterfaceCount")
    @ResponseBody
    public Integer getFaultInterfaceCount(){
        return interfaceinfoService.getFaultInterfaceCount();
    }

}
