package com.business.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.business.domain.ImputationData;
import com.business.mapper.ImputationDataMapper;
import com.business.service.ImputationDataService;
import com.sooka.apply.service.IIntRecordService;
import com.sooka.common.core.controller.BaseController;
import com.sooka.common.core.domain.AjaxResult;
import com.sooka.common.utils.StringUtils;
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 javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

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

    @Autowired
    private ITUInterfaceinfoService interfaceinfoService;

    @Autowired
    private IIntRecordService iIntRecordService;

    @Autowired
    private ImputationDataService imputationDataService;

    @Resource
    private ImputationDataMapper imputationDataMapper;

    @Autowired
    private ISysDeptService sysDeptService;

    private static final String FORMAT_STRING = "0000000000";

    @GetMapping("index")
    public String index(ModelMap mmap){
        SysDept dept = new SysDept();
        dept.setParentId(214L);
        dept.setStatus("0");//不查询状态禁用的部门
        List<SysDept> depts = sysDeptService.selectDeptList(dept);
        mmap.put("depts",depts);
        mmap.put("deptCount",depts.size());
        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());
        /**部门归集数据量**/
        Long guijiCount = interfaceinfoService.getGuijiCountByDeptId(id.toString());
        String guijiCountResult = guijiCount!=null?guijiCount.toString():"0";
        mmap.put("deptId",sysDept.getDeptId());
        mmap.put("deptName",sysDept.getDeptName());
        mmap.put("interfaceList",interfaceList);
        mmap.put("shareCount",shareCount);
        mmap.put("guijiCount",formatWithMakingUp(guijiCountResult));
        return "/visualization/tk_iframe";
    }


    /**
     * 字符串前用零补齐
     *
     * @param src 原始字符串
     * @return 补齐后的字符串
     */
    public static String formatWithMakingUp(String src) {
        if (null == src) {
            return null;
        }
        int delta = FORMAT_STRING.length() - src.length();
        if (delta <= 0) {
            return src;
        }
        return FORMAT_STRING.substring(0, delta) + src;
    }

    /**
     * 查询 接口接口占比-子页
     * */
    @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("refreshData")
    @ResponseBody
    public JSONObject refreshData(){
        JSONObject jsonObject = new JSONObject();
        interfaceinfoService.refreshData();
        jsonObject.put("data", "well done");
        return jsonObject;
    }

    /**
     * 更新归集数据分析
     * */
    @PostMapping("imputationData")
    @ResponseBody
    public JSONObject imputationData(){
        JSONObject jsonObject = new JSONObject();
//        interfaceinfoService.imputationData();
        interfaceinfoService.imputationDataByMonth();
        jsonObject.put("data", "well done");
        return jsonObject;
    }

    /**
     * 审批分析 - 可视化二级页（右下） 自动获取近三年数据
     * 目前显示接口应用审批、数据应用审批、证照应用审批。按照这三类进行查询。
     * */
    @PostMapping("subSpfx")
    @ResponseBody
    public JSONObject subSpfx(String deptId){
        JSONObject jsonObject = new JSONObject();
        //年份数据
        jsonObject.put("years", getYears());//获取当前年和前两年的年份
        //类别数组
        String[] types = {"接口应用审批", "数据应用审批", "证照应用审批"};
        jsonObject.put("types", types);
        for(int i = 0; i < getYears().length; i++){
            TUInterfaceinfo subJkspfx = interfaceinfoService.getSubSpfx(deptId, getYears()[i]);
            String[] jkspfxData = new String[3];
            jkspfxData[0] = subJkspfx.getShareType1();//接口应用审批
            jkspfxData[1] = subJkspfx.getShareType2();//数据应用审批
            jkspfxData[2] = subJkspfx.getShareType3();//证照应用审批
            jkspfxData[2] = "0";//证照应用审批 假数据
            jsonObject.put(getYears()[i], jkspfxData);
        }
        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;
    }

    /**
     * 查询月度归集数据分析
     * */
    @PostMapping("getImputationDataList")
    @ResponseBody
    public String[] getImputationDataList(String year){
        String[] arr = new String[12];
        Long[] calendar = new Long[12];
        calendar[0] = 0L;
        calendar[1] = 0L;
        calendar[2] = 0L;
        calendar[3] = 0L;
        calendar[4] = 0L;
        calendar[5] = 0L;
        calendar[6] = 0L;
        calendar[7] = 0L;
        calendar[8] = 0L;
        calendar[9] = 0L;
        calendar[10] = 0L;
        calendar[11] = 0L;
        ImputationData param = new ImputationData();
        param.setYear(year);
        List<ImputationData> imputationDataList = imputationDataMapper.selectImputationInterface(param);
        for (int i = 0; i < imputationDataList.size(); i++) {
            ImputationData imputationData = imputationDataList.get(i);
            calendar[0] += Long.valueOf(imputationData.getJan());
            calendar[1] += Long.valueOf(imputationData.getFeb());
            calendar[2] += Long.valueOf(imputationData.getMar());
            calendar[3] += Long.valueOf(imputationData.getApr());
            calendar[4] += Long.valueOf(imputationData.getMay());
            calendar[5] += Long.valueOf(imputationData.getJun());
            calendar[6] += Long.valueOf(imputationData.getJul());
            calendar[7] += Long.valueOf(imputationData.getAug());
            calendar[8] += Long.valueOf(imputationData.getSep());
            calendar[9] += Long.valueOf(imputationData.getOct());
            calendar[10] += Long.valueOf(imputationData.getNov());
            calendar[11] += Long.valueOf(imputationData.getDecb());
        }
        arr[0] = calendar[0].toString();
        arr[1] = calendar[1].toString();
        arr[2] = calendar[2].toString();
        arr[3] = calendar[3].toString();
        arr[4] = calendar[4].toString();
        arr[5] = calendar[5].toString();
        arr[6] = calendar[6].toString();
        arr[7] = calendar[7].toString();
        arr[8] = calendar[8].toString();
        arr[9] = calendar[9].toString();
        arr[10] = calendar[10].toString();
        arr[11] = calendar[11].toString();
        return arr;
    }

    /**
     * 查询月度归集数据分析
     * */
    @PostMapping("getImputationDataList_old")
    @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.getInterfaceCount("1");
    }

    /**
     * 接口申请数量
     * @param deptId 部门ID
     * @return 结果
     */
    @PostMapping("applyCount")
    @ResponseBody
    public String[] applyCount(String year, String deptId){
        return iIntRecordService.applyCount(year,deptId);
    }


    /**
     * 查询归集和接口排名TOP10
     * */
    @PostMapping("getCallNumByDept/{deptId}")
    @ResponseBody
    public JSONArray getCallNumByDept(@PathVariable("deptId") Long deptId){
        JSONArray jsonArray = new JSONArray();
        //数据
        List<TUInterfaceinfo> list_data = interfaceinfoService.getCallNumByDept(deptId);
        for(TUInterfaceinfo info:list_data){
            JSONObject object = new JSONObject();
            object.put("name",info.getDeptName());
            object.put("value",info.getCount());
            jsonArray.add(object);
        }
        if(list_data.size() == 0){
            JSONObject object = new JSONObject();
            object.put("name", "无数据");
            object.put("value", 0);
            jsonArray.add(object);
        }
        return jsonArray;
    }

}
