package com.wms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wms.entity.app.monitor.Analysis7Days;
import com.wms.entity.app.monitor.FinishDetail;
import com.wms.entity.app.monitor.LocationUseDetail;
import com.wms.entity.app.monitor.WorkInfoByStand;
import com.wms.entity.table.Location;
import com.wms.entity.table.Stand;
import com.wms.entity.table.WorkFlow;
import com.wms.entity.table.WorkSummary;
import com.wms.service.LocationService;
import com.wms.service.StandService;
import com.wms.service.WorkFlowService;
import com.wms.service.WorkSummaryService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static com.wms.utils.StringUtils.convertJsonString;

/**
 * 大屏监控类
 */
@Controller
@CrossOrigin
@RequestMapping(value = "/monitor")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MonitorController {
    private final LocationService locationService;
    private final WorkFlowService workFlowService;
    private final WorkSummaryService workSummaryService;
    private final StandService standService;

    /**
     * 请求库位使用情况
     * @return 库位使用情况
     */
    @GetMapping("/getLocationUseDetail")
    @ResponseBody
    public String getLocationUseDetail() {
        LocationUseDetail response = new LocationUseDetail();
        try {
            int allSize = (int) locationService.count();
            int emptySize = (int) locationService.count(new LambdaQueryWrapper<Location>()
                    .eq(Location::getIsLock, 0)
                    .eq(Location::getLocationStatus, 0));
            response.setUsed(allSize - emptySize);
            response.setEmpty(emptySize);
        } catch (Exception e) {
            response.setUsed(0);
            response.setEmpty(0);
        }
        return convertJsonString(response);
    }

    /**
     * 请求最近7天的工作量统计
     * @return 最近7天的工作量统计
     */
    @GetMapping("/getAnalysis7Days")
    @ResponseBody
    public String getAnalysis7Days() {
        Analysis7Days response = new Analysis7Days();
        List<String> dateList = new ArrayList<>();
        WorkInfoByStand works = new WorkInfoByStand();
        try {
            // 获取当前日期
            LocalDate now = LocalDate.now();
            // 创建一个列表来存储日期
            List<LocalDate> dates = new ArrayList<>();
            // 添加最近7天的日期到列表中
            for (int i = 6; i >= 0; i--) {
                dates.add(now.minusDays(i));
            }
            // 9个站台的信息
            List<Integer> stand1 = new ArrayList<>();
            List<Integer> stand2 = new ArrayList<>();
            List<Integer> stand3 = new ArrayList<>();
            List<Integer> stand4 = new ArrayList<>();
            List<Integer> stand5 = new ArrayList<>();
            List<Integer> stand6 = new ArrayList<>();
            List<Integer> stand7 = new ArrayList<>();
            List<Integer> stand8 = new ArrayList<>();
            List<Integer> stand9 = new ArrayList<>();
            // 获取所有的工作总结
            List<WorkSummary> allWorkSummaries = workSummaryService.list();
            // 循环日期
            for (LocalDate date : dates) {
                dateList.add(date.format(DateTimeFormatter.ofPattern("MM-dd")));
                // 获取当日的工作总结
                List<WorkSummary> dayWorkSummaryList = allWorkSummaries.stream().filter(workSummary -> Objects.equals(workSummary.getWorkDate().toLocalDate(), date)).toList();
                // 获取1号站台当日的工作总结
                List<WorkSummary> stand1WorkSummaryList = dayWorkSummaryList.stream().filter(workSummary -> workSummary.getWorkStation().contains("#1")).toList();
                stand1.add(stand1WorkSummaryList.size());
                // 获取2号站台当日的工作总结
                List<WorkSummary> stand2WorkSummaryList = dayWorkSummaryList.stream().filter(workSummary -> workSummary.getWorkStation().contains("#2")).toList();
                stand2.add(stand2WorkSummaryList.size());
                // 获取3号站台当日的工作总结
                List<WorkSummary> stand3WorkSummaryList = dayWorkSummaryList.stream().filter(workSummary -> workSummary.getWorkStation().contains("#3")).toList();
                stand3.add(stand3WorkSummaryList.size());
                // 获取4号站台当日的工作总结
                List<WorkSummary> stand4WorkSummaryList = dayWorkSummaryList.stream().filter(workSummary -> workSummary.getWorkStation().contains("#4")).toList();
                stand4.add(stand4WorkSummaryList.size());
                // 获取5号站台当日的工作总结
                List<WorkSummary> stand5WorkSummaryList = dayWorkSummaryList.stream().filter(workSummary -> workSummary.getWorkStation().contains("#5")).toList();
                stand5.add(stand5WorkSummaryList.size());
                // 获取6号站台当日的工作总结
                List<WorkSummary> stand6WorkSummaryList = dayWorkSummaryList.stream().filter(workSummary -> workSummary.getWorkStation().contains("#6")).toList();
                stand6.add(stand6WorkSummaryList.size());
                // 获取7号站台当日的工作总结
                List<WorkSummary> stand7WorkSummaryList = dayWorkSummaryList.stream().filter(workSummary -> workSummary.getWorkStation().contains("#7")).toList();
                stand7.add(stand7WorkSummaryList.size());
                // 获取8号站台当日的工作总结
                List<WorkSummary> stand8WorkSummaryList = dayWorkSummaryList.stream().filter(workSummary -> workSummary.getWorkStation().contains("#8")).toList();
                stand8.add(stand8WorkSummaryList.size());
                // 获取9号站台当日的工作总结
                List<WorkSummary> stand9WorkSummaryList = dayWorkSummaryList.stream().filter(workSummary -> workSummary.getWorkStation().contains("#9")).toList();
                stand9.add(stand9WorkSummaryList.size());
            }
            // 设定站台信息
            works.setStand1(stand1);
            works.setStand2(stand2);
            works.setStand3(stand3);
            works.setStand4(stand4);
            works.setStand5(stand5);
            works.setStand6(stand6);
            works.setStand7(stand7);
            works.setStand8(stand8);
            works.setStand9(stand9);
        } catch (Exception e) {
            dateList = Collections.emptyList();
            works = new WorkInfoByStand();
        }
        response.setWorks(works);
        response.setDate(dateList);
        return convertJsonString(response);
    }

    /**
     * 请求工单完成情况
     * @return 工单完成情况
     */
    @GetMapping("/getFinishDetail")
    @ResponseBody
    public String getFinishDetail() {
        FinishDetail response = new FinishDetail();
        List<Integer> total = new ArrayList<>();
        List<Integer> complete = new ArrayList<>();
        try {
            // 获取当前站台所有的工作流信息
            List<WorkFlow> allWorkFlows = workFlowService.list();
            List<Stand> stands = standService.list(new LambdaQueryWrapper<Stand>()
                    .eq(Stand::getStandType, 2)
                    .orderByDesc(Stand::getStandId));
            for (Stand workStation : stands) {
                // 查询到当前的工作量
                List<WorkFlow> currentStationWorkFlowList = allWorkFlows.stream().filter(workFlow -> Objects.equals(workFlow.getWorkStation(), workStation.getStandId())).toList();
                int finishingRows = 0;
                for (WorkFlow workFlow : currentStationWorkFlowList) {
                    if (workFlow.getWorkStatus() == 2) {
                        finishingRows++;
                    }
                }
                total.add(currentStationWorkFlowList.size());
                complete.add((finishingRows * 100)/currentStationWorkFlowList.size());
            }
        } catch (Exception e) {
            total = Collections.emptyList();
            complete = Collections.emptyList();
        }

        response.setComplete(complete);
        response.setTotal(total);
        return convertJsonString(response);
    }
}
