package com.ruoyi.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.cs.domain.CsResourceOrderApply;
import com.ruoyi.cs.domain.dto.SatisfactionStatisticsDTO;
import com.ruoyi.cs.domain.vo.AnalysisLineVO;
import com.ruoyi.cs.domain.vo.ExportOrderResponseTimeStatisticsVO;
import com.ruoyi.cs.domain.vo.SatisfactionStatisticsVO;
import com.ruoyi.cs.mapper.CsResourceOrderApplyMapper;
import com.ruoyi.cs.service.ICsAchievementInfoDetailsService;
import com.ruoyi.cs.service.ICsOrderMemberUserService;
import com.ruoyi.cs.service.ICsResourceOrderApplyService;
import io.swagger.annotations.Api;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.*;


/**
 * 管理端统计
 *
 * @author ruoyi
 * @date 2024-04-23
 */
@Api("管理端统计")
@RestController
@RequestMapping("/cs/csStatistics")
public class CsStatisticsController extends BaseController
{
    @Resource
    private ICsResourceOrderApplyService csResourceOrderApplyService;
    @Resource
    private ICsOrderMemberUserService csOrderMemberUserService;
    @Resource
    private ICsAchievementInfoDetailsService csAchievementInfoDetailsService;

    @Resource
    private CsResourceOrderApplyMapper csResourceOrderApplyMapper;

    /**
     * 获取管理端工作台统计
     */
    @GetMapping( "/getStatistics")
    public AjaxResult getStatistics(String beginAt, String endAt)
    {
        Map<String,Object> map = new HashMap<>();

        map.put("HPCCPU", "16");//HPC CPU核数
        map.put("HPCMemory", "3");//HPC 内存使用量
        map.put("cloudVirtualMachine", "20");//云计算 虚拟机数量
        map.put("cloudMemory", "64");//云计算 内存
        map.put("cloudStorage", "300");//云计算 存储空间
        map.put("storageCapacity", "500");//存储 存储容量
        map.put("storageIO", "50");//存储 I/O带宽
        map.put("nodeVirtualMachine", "60");//裸节点 虚拟机数量
        map.put("nodeMemory", "32");//裸节点 内存
        map.put("nodeStorage", "512");//裸节点 存储空间
        map.put("VPNCPU", "2.8");//网络(VPN) CPU使用
        // 创建Random类实例
        Random random = new Random();
        // 生成一个[?, ?)之间的随机整数
        int randomNumber = random.nextInt(100);
        map.put("VPN", randomNumber+"");//网络(VPN) 流量

        map.put("companyNum", "5");//新增合作单位

        // 新增成果数量
        map.putAll(csAchievementInfoDetailsService.selectAchievementInfoDetailsStatistics(beginAt, endAt));
        // 查询管理端人员相关统计
        map.putAll(csOrderMemberUserService.selectUserStatistics(beginAt, endAt));
        // 查询管理端资源、工单相关统计
        map.putAll(csResourceOrderApplyService.selectResourceOrderStatistics(beginAt, endAt));
        return AjaxResult.success(map);
    }

    /**
     * 查询部门列表
     *
     * @return
     */
    @GetMapping("/searchDeptList")
    public AjaxResult searchDeptList(){
        return AjaxResult.success(csResourceOrderApplyService.searchDeptList());
    }

    /**
     * 查询工单数量统计信息
     *
     * @param params
     * @return
     */
    @GetMapping("/searchResourceOrderApplyStatisticsInfo")
    public AjaxResult searchResourceOrderApplyStatisticsInfo(@RequestParam Map<String, Object> params){
        return AjaxResult.success(csResourceOrderApplyService.searchResourceOrderApplyStatisticsInfo(params));
    }

    /**
     * 工单响应时长统计
     *
     * @param params   year 年
     *                 quarter 季度
     *                 month 月
     *                 startTime 开始时间
     *                 endTime 结束时间
     *                 acceptance 受理部门
     *                 companyName 申请单位
     *                 deptName 二级单位
     * @return ok
     */
    @GetMapping( "/getOrderResponseTimeStatistics")
    public AjaxResult getOrderResponseTimeStatistics(@RequestParam Map<String, Object> params)
    {
        return AjaxResult.success(csResourceOrderApplyMapper.getOrderResponseTimeStatistics(params));
    }

    /**
     * 导出工单响应时长数据
     */
    @PostMapping("/exportOrderResponseTimeStatistics")
    public void exportOrderResponseTimeStatistics(HttpServletResponse response,SatisfactionStatisticsDTO dto) {
        List<ExportOrderResponseTimeStatisticsVO> list = csResourceOrderApplyService.exportOrderResponseTimeStatistics(dto);
        ExcelUtil<ExportOrderResponseTimeStatisticsVO> util = new ExcelUtil<ExportOrderResponseTimeStatisticsVO>(ExportOrderResponseTimeStatisticsVO.class);
        util.exportExcel(response, list, "工单响应时长数据");
    }

    /**
     * 导出工单处理时长
     */
    @PostMapping("/exportWorkOrderProcessingTime")
    public void exportWorkOrderProcessingTime(HttpServletResponse response,SatisfactionStatisticsDTO dto) {
        List<CsResourceOrderApply> list = csResourceOrderApplyService.exportWorkOrderProcessingTime(dto);
        for (CsResourceOrderApply resourceOrderApply : list) {
            if (StringUtils.isEmpty(resourceOrderApply.getTackleTime())){
                resourceOrderApply.setTackleTime("未处理完成");
            }
        }
        ExcelUtil<CsResourceOrderApply> util = new ExcelUtil<CsResourceOrderApply>(CsResourceOrderApply.class);
        util.exportExcel(response, list, "工单处理时长数据");
    }

    /**
     * 工单处理时长统计
     *
     * @param params
     *
     * @return ok
     */
    @GetMapping( "/getOrderHandleTimeStatistics")
    public AjaxResult getOrderHandleTimeStatistics(@RequestParam Map<String, Object> params)
    {
        HashMap<String, Object> map = new HashMap<>();
        params.put("days", 30);
        List<Map<String, Object>> mapList = csResourceOrderApplyMapper.nOrderHandleTime(params);
        map.put("handleTimeList", mapList);
        return AjaxResult.success(map);
    }

    /**
     * 资源数量对比统计
     *
     * @param params
     *
     * @return ok
     */
    @GetMapping( "/getResourcesContrastNumStatistics")
    public AjaxResult getResourcesContrastNumStatistics(@RequestParam Map<String, Object> params)
    {
        HashMap<String, Object> map = new HashMap<>();
        LocalDate currentDate = LocalDate.now();
        if("year".equals(String.valueOf(params.get("searchType")))){
            LocalDate lastYearDate = currentDate.minusYears(1);
            int currentYear = currentDate.getYear();
            int lastYear = lastYearDate.getYear();
            List<String> yearList = new ArrayList<>();
            yearList.add(lastYear+"年");
            yearList.add(currentYear+"年");
            map.put("timeList",yearList);
            params.put("year",currentYear);
            List<Map<String, Object>> nowMapList = csResourceOrderApplyMapper.getResourcesContrastNumStatistics(params);
            List<String> classList = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(nowMapList)){
                for(int i = 0;i < nowMapList.size();i++){
                    classList.add(String.valueOf(nowMapList.get(i).get("className")));
                }
            }
            params.put("year",lastYear);
            List<Map<String, Object>> lastMapList = csResourceOrderApplyMapper.getResourcesContrastNumStatistics(params);
            if(CollectionUtils.isNotEmpty(lastMapList)){
                for(int i = 0;i < lastMapList.size();i++){
                    if(!classList.contains(String.valueOf(lastMapList.get(i).get("className")))){
                        classList.add(String.valueOf(lastMapList.get(i).get("className")));
                    }
                }
            }
            List<Integer> nowList = new ArrayList<>();
            List<Integer> lastList = new ArrayList<>();
            for(int i = 0;i < classList.size();i++){
                nowList.add(checkList(nowMapList,classList.get(i)));
                lastList.add(checkList(lastMapList,classList.get(i)));

            }
            map.put("classList", classList);
            map.put("nowList", nowList);
            map.put("lastList", lastList);
        }
        if("month".equals(String.valueOf(params.get("searchType")))){
            LocalDate lastMonthDate = currentDate.minusMonths(1);
            int currentMonth = currentDate.getMonthValue();
            int lastMonth = lastMonthDate.getMonthValue();
            List<String> monthList = new ArrayList<>();
            monthList.add(lastMonth+"月");
            monthList.add(currentMonth+"月");
            map.put("timeList",monthList);
            params.put("month",currentMonth);
            List<Map<String, Object>> nowMapList = csResourceOrderApplyMapper.getResourcesContrastNumStatistics(params);
            List<String> classList = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(nowMapList)){
                for(int i = 0;i < nowMapList.size();i++){
                    classList.add(String.valueOf(nowMapList.get(i).get("className")));
                }
            }
            params.put("month",lastMonth);
            List<Map<String, Object>> lastMapList = csResourceOrderApplyMapper.getResourcesContrastNumStatistics(params);
            if(CollectionUtils.isNotEmpty(lastMapList)){
                for(int i = 0;i < lastMapList.size();i++){
                    if(!classList.contains(String.valueOf(lastMapList.get(i).get("className")))){
                        classList.add(String.valueOf(lastMapList.get(i).get("className")));
                    }
                }
            }
            List<Integer> nowList = new ArrayList<>();
            List<Integer> lastList = new ArrayList<>();
            for(int i = 0;i < classList.size();i++){
                nowList.add(checkList(nowMapList,classList.get(i)));
                lastList.add(checkList(lastMapList,classList.get(i)));
            }
            map.put("classList", classList);
            map.put("nowList", nowList);
            map.put("lastList", lastList);
        }
        return AjaxResult.success(map);
    }

    private Integer checkList(List<Map<String, Object>> list,String className){
        int result = 0;
        for(int i = 0;i < list.size();i++){
            if(className.equals(String.valueOf(list.get(i).get("className")))){
                result = Integer.parseInt(String.valueOf(list.get(i).get("nowNum")));
            }
        }
        return result;
    }

    /**
     * 工单申请类型统计
     *
     * @param params
     *
     * @return ok
     */
    @GetMapping( "/getOrderContrastNumStatistics")
    public AjaxResult getOrderContrastNumStatistics(@RequestParam Map<String, Object> params)
    {
        HashMap<String, Object> map = new HashMap<>();
        LocalDate currentDate = LocalDate.now();
        if("year".equals(String.valueOf(params.get("searchType")))){
            LocalDate lastYearDate = currentDate.minusYears(1);
            int currentYear = currentDate.getYear();
            int lastYear = lastYearDate.getYear();
            List<String> yearList = new ArrayList<>();
            yearList.add(lastYear+"年");
            yearList.add(currentYear+"年");
            map.put("timeList",yearList);
            params.put("year",currentYear);
            List<Map<String, Object>> nowMapList = csResourceOrderApplyMapper.getOrderContrastNumStatistics(params);
            List<String> classList = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(nowMapList)){
                for(int i = 0;i < nowMapList.size();i++){
                    classList.add(String.valueOf(nowMapList.get(i).get("className")));
                }
            }
            params.put("year",lastYear);
            List<Map<String, Object>> lastMapList = csResourceOrderApplyMapper.getOrderContrastNumStatistics(params);
            if(CollectionUtils.isNotEmpty(lastMapList)){
                for(int i = 0;i < lastMapList.size();i++){
                    if(!classList.contains(String.valueOf(lastMapList.get(i).get("className")))){
                        classList.add(String.valueOf(lastMapList.get(i).get("className")));
                    }
                }
            }
            List<Integer> nowList = new ArrayList<>();
            List<Integer> lastList = new ArrayList<>();
            for(int i = 0;i < classList.size();i++){
                nowList.add(checkList(nowMapList,classList.get(i)));
                lastList.add(checkList(lastMapList,classList.get(i)));

            }
            map.put("classList", classList);
            map.put("nowList", nowList);
            map.put("lastList", lastList);
        }
        if("month".equals(String.valueOf(params.get("searchType")))){
            LocalDate lastMonthDate = currentDate.minusMonths(1);
            int currentMonth = currentDate.getMonthValue();
            int lastMonth = lastMonthDate.getMonthValue();
            List<String> monthList = new ArrayList<>();
            monthList.add(lastMonth+"月");
            monthList.add(currentMonth+"月");
            map.put("timeList",monthList);
            params.put("month",currentMonth);
            List<Map<String, Object>> nowMapList = csResourceOrderApplyMapper.getOrderContrastNumStatistics(params);
            List<String> classList = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(nowMapList)){
                for(int i = 0;i < nowMapList.size();i++){
                    classList.add(String.valueOf(nowMapList.get(i).get("className")));
                }
            }
            params.put("month",lastMonth);
            List<Map<String, Object>> lastMapList = csResourceOrderApplyMapper.getOrderContrastNumStatistics(params);
            if(CollectionUtils.isNotEmpty(lastMapList)){
                for(int i = 0;i < lastMapList.size();i++){
                    if(!classList.contains(String.valueOf(lastMapList.get(i).get("className")))){
                        classList.add(String.valueOf(lastMapList.get(i).get("className")));
                    }
                }
            }
            List<Integer> nowList = new ArrayList<>();
            List<Integer> lastList = new ArrayList<>();
            for(int i = 0;i < classList.size();i++){
                nowList.add(checkList(nowMapList,classList.get(i)));
                lastList.add(checkList(lastMapList,classList.get(i)));
            }
            map.put("classList", classList);
            map.put("nowList", nowList);
            map.put("lastList", lastList);
        }
        return AjaxResult.success(map);
    }

    /**
     * 工单/资源满意度统计
     * <p>
     * quarter 季度
     * month 月
     * startTime 开始时间
     * endTime 结束时间
     * acceptance 受理部门
     * companyName 申请单位
     * deptName 二级单位
     * type 1
     *
     * @return ok
     */
    @PostMapping("/getSatisfactionStatistics")
    public AjaxResult getSatisfactionStatistics(@RequestBody SatisfactionStatisticsDTO dto) {
        Map<String,Object> resultMap = csResourceOrderApplyService.getSatisfactionStatistics(dto);
        return AjaxResult.success(resultMap);
    }

    /**
     * 单位工单提交统计
     * <p>
     * quarter 季度
     * month 月
     * startTime 开始时间
     * endTime 结束时间
     * acceptance 受理部门
     * companyName 申请单位
     * deptName 二级单位
     * type 1
     *
     * @return ok
     */
    @PostMapping("/getWorOrderSubmissionStatistics")
    public AjaxResult getWorOrderSubmissionStatistics(@RequestBody SatisfactionStatisticsDTO dto) {
        Map<String,Object> resultMap = csResourceOrderApplyService.getWorOrderSubmissionStatistics(dto);
        return AjaxResult.success(resultMap);
    }

}
