package com.ruoyi.web.controller.buyerapi;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.buyerapi.domain.*;
import com.ruoyi.buyerapi.service.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysLogininfor;
import com.ruoyi.system.service.ISysLogininforService;
import com.sun.javafx.collections.MappingChange;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * 工作台Controller
 *
 * @author ruoyi
 * @date 2022-09-19
 */
@Api(tags = "首页")
@RestController
@RequestMapping("/home/page")
public class BuyerHomePageController extends BaseController {
    @Autowired
    private IBuyerDeptEmployeeService buyerDeptEmployeeService;

    @Autowired
    private IBuyerDeptDeviceService buyerDeptDeviceService;

    @Autowired
    private IBuyerProjectService buyerProjectService;

    @Autowired
    private IBuyerProjectAcceptService buyerProjectAcceptService;

    @Autowired
    private IBuyerProcessFileService buyerProcessFileService;

    /**
     * 统计首页消息提醒
     */
    @ApiOperation(value = "统计首页消息提醒")
    @GetMapping("/message")
    public AjaxResult message() {
        List<BuyerProject> newBuyerProjects = new ArrayList<>();
        // 查询过程文件
        BuyerProject project = new BuyerProject();
        List<BuyerProject> buyerProjects = buyerProjectService.selectBuyerProjectProcessFileList(project);
        if (buyerProjects != null && buyerProjects.size() > 0) {
            for (BuyerProject buyerProject : buyerProjects) {
                Date nowDate = DateUtils.getNowDate();
                Date middleDate = buyerProject.getMiddleDate();
                Date endDate = buyerProject.getEndDate();
                int middleCompare = DateUtil.compare(nowDate, middleDate);
                int compare = DateUtil.compare(endDate, nowDate);
                if (middleCompare > 0 && compare > 0) {
                    // 查询过程文件
                    if (buyerProject.getLinkId() == null) {
                        buyerProject.setType("过程文件");
                        buyerProject.setMessage("已过半");
                        buyerProject.setDate(buyerProject.getMiddleDate());
                        newBuyerProjects.add(buyerProject);
                    }
                }
            }
        }
        // 查询验收资料
        BuyerProject projectEntity = new BuyerProject();
        List<BuyerProject> buyerProjectList = buyerProjectService.selectBuyerProjectAcceptFileList(projectEntity);
        if (buyerProjectList != null && buyerProjectList.size() > 0) {
            for (BuyerProject buyerProject : buyerProjectList) {
                Date endDate = buyerProject.getEndDate();
                Date nowDate = DateUtil.date();
                Date nowDateNextMonth = DateUtil.nextMonth();
                int endCompare = DateUtil.compare(nowDateNextMonth, endDate);
                int compare = DateUtil.compare(endDate, nowDate);
                if (endCompare > 0 && compare > 0) {
                    // 查询验收资料
                    if (buyerProject.getLinkId() == null) {
                        buyerProject.setType("验收资料");
                        buyerProject.setMessage("结束");
                        buyerProject.setDate(buyerProject.getEndDate());
                        newBuyerProjects.add(buyerProject);
                    }
                }
            }
        }
        return AjaxResult.success(newBuyerProjects);

//        // 效率低下
//        List<BuyerProject> newBuyerProjects = new ArrayList<>();
//        // 查询过程文件
//        BuyerProject project = new BuyerProject();
//        project.setTodayStatu("0");
//        List<BuyerProject> buyerProjects = buyerProjectService.selectBuyerProjectList(project);
//        if (buyerProjects != null && buyerProjects.size() > 0) {
//            for (BuyerProject buyerProject : buyerProjects) {
//                Date nowDate = DateUtils.getNowDate();
//                Date middleDate = buyerProject.getMiddleDate();
//                int middleCompare = DateUtil.compare(nowDate, middleDate);
//                if (middleCompare > 0) {
//                    // 查询过程文件
//                    BuyerProcessFile buyerProcessFile = new BuyerProcessFile();
//                    buyerProcessFile.setProjectId(buyerProject.getId());
//                    List<BuyerProcessFile> buyerProcessFiles = buyerProcessFileService.selectBuyerProcessFileList(buyerProcessFile);
//                    if (buyerProcessFiles != null && buyerProcessFiles.size() > 0) {
//                        for (BuyerProcessFile processFile : buyerProcessFiles) {
//                            if (processFile.getBuyerFileInfos() == null || processFile.getBuyerFileInfos().size() == 0) {
//                                buyerProject.setType("过程文件");
//                                buyerProject.setMessage("已过半");
//                                newBuyerProjects.add(buyerProject);
//                            }
//                        }
//                    }
//                }
//            }
//        }
//        // 查询验收资料
//        BuyerProject projectEntity = new BuyerProject();
//        projectEntity.setTodayStatus("0");
//        List<BuyerProject> buyerProjectList = buyerProjectService.selectBuyerProjectList(projectEntity);
//        if (buyerProjectList != null && buyerProjectList.size() > 0) {
//            for (BuyerProject buyerProject : buyerProjectList) {
//                Date endDate = buyerProject.getEndDate();
//                Date nowDateNextMonth = DateUtil.nextMonth();
//                int endCompare = DateUtil.compare(nowDateNextMonth, endDate);
//                if (endCompare > 0) {
//                    // 查询验收资料
//                    BuyerProjectAccept buyerProjectAccept = new BuyerProjectAccept();
//                    buyerProjectAccept.setProjectId(buyerProject.getId());
//                    List<BuyerProjectAccept> buyerProjectAccepts = buyerProjectAcceptService.selectBuyerProjectAcceptList(buyerProjectAccept);
//                    if (buyerProjectAccepts != null && buyerProjectAccepts.size() > 0) {
//                        for (BuyerProjectAccept projectAccept : buyerProjectAccepts) {
//                            if (projectAccept.getBuyerFileInfos() == null || projectAccept.getBuyerFileInfos().size() == 0) {
//                                buyerProject.setType("验收资料");
//                                buyerProject.setMessage("结束");
//                                newBuyerProjects.add(buyerProject);
//                            }
//                        }
//                    }
//                }
//            }
//        }
//        return AjaxResult.success(newBuyerProjects);
    }

    /**
     * 统计首页企业信息模块
     */
    @ApiOperation(value = "企业信息")
    @GetMapping("/company")
    public Map company() {
        Map map = new HashMap<String, Object>();
        if (StrUtil.isEmpty(SecurityUtils.getCompanyId())) {
            map.put("onTotal", 0);
            map.put("acceptTotal", 0);
            map.put("overtimeTotal", 0);
            return map;
        } else {
            BuyerDeptEmployee buyerDeptEmployee = new BuyerDeptEmployee();
            // 设置公司id
            buyerDeptEmployee.setDeptId(SecurityUtils.getCompanyId());
            // 统计企业人数
            List<BuyerDeptEmployee> buyerDeptEmployees = buyerDeptEmployeeService.selectBuyerDeptEmployeeList(buyerDeptEmployee);
            Integer employeeTotal = buyerDeptEmployees.size();
            // 统计设备数量
            BuyerDeptDevice buyerDeptDevice = new BuyerDeptDevice();
            buyerDeptDevice.setDeptId(SecurityUtils.getCompanyId());
            List<BuyerDeptDevice> buyerDeptDevices = buyerDeptDeviceService.selectBuyerDeptDeviceList(buyerDeptDevice);
            Integer deviceTotal = buyerDeptDevices.size();
            // 统计专用设备数量
            buyerDeptDevice.setProfessionalDevice("1");
            List<BuyerDeptDevice> buyerDeptDeviceList = buyerDeptDeviceService.selectBuyerDeptDeviceList(buyerDeptDevice);
            Integer professionalDeviceTotal = buyerDeptDeviceList.size();
            map.put("employeeTotal", employeeTotal);
            map.put("deviceTotal", deviceTotal);
            map.put("professionalDeviceTotal", professionalDeviceTotal);
        }
        return map;
    }

    /**
     * 统计首页项目概况模块
     */
    @ApiOperation(value = "项目概况")
    @GetMapping("/project")
    public Map project() {
        Map map = new HashMap<String, Object>();
        if (StrUtil.isEmpty(SecurityUtils.getCompanyId())) {
            map.put("onTotal", 0);
            map.put("acceptTotal", 0);
            map.put("overtimeTotal", 0);
            return map;
        } else {
            BuyerProject buyerProject = new BuyerProject();
            // 设置公司id
            buyerProject.setCompanyId(SecurityUtils.getCompanyId());
            // 进行中项目数量
            buyerProject.setStatus("0");
            List<BuyerProject> onList = buyerProjectService.selectBuyerProjectList(buyerProject);
            int onTotal = onList.size();
            // 已验收项目数量
//            BuyerProjectAccept buyerProjectAccept = new BuyerProjectAccept();
//            buyerProjectAccept.setDeptId(SecurityUtils.getCompanyId());
//            buyerProjectAccept.setStatus("1");
//            List<BuyerProjectAccept> endList = buyerProjectAcceptService.selectBuyerProjectAcceptList(buyerProjectAccept);
            buyerProject.setStatus("1");
            List<BuyerProject> endList = buyerProjectService.selectBuyerProjectList(buyerProject);
            int acceptTotal = endList.size();
            // 已超时项目数量
            buyerProject.setStatus("2");
            List<BuyerProject> overtimeList = buyerProjectService.selectBuyerProjectList(buyerProject);
            int overtimeTotal = overtimeList.size();
            map.put("onTotal", onTotal);
            map.put("acceptTotal", acceptTotal);
            map.put("overtimeTotal", overtimeTotal);
        }
        return map;
    }
}
