package cn.ezhizao.project.business.da.controller;

import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.project.business.da.domain.EquipmentCurrentError;
import cn.ezhizao.project.business.da.domain.EquipmentCurrentStatus;
import cn.ezhizao.project.business.daywork.domain.BizDaywork;
import cn.ezhizao.project.business.daywork.domain.BizDayworkItem;
import cn.ezhizao.project.business.daywork.service.IBizDayworkItemService;
import cn.ezhizao.project.business.daywork.service.IBizDayworkService;
import cn.ezhizao.project.business.daywork.service.IBizLotService;
import cn.ezhizao.project.business.equipment.domain.BizEquipmentDetail;
import cn.ezhizao.project.business.equipment.service.IBizEquipmentDetailService;
import cn.ezhizao.project.business.process.domain.BizProcess;
import cn.ezhizao.project.business.process.service.IBizProcessService;
import cn.ezhizao.project.business.product.domain.BizProduct;
import cn.ezhizao.project.business.product.service.IBizProductService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/da")
public class DataAcquistionController extends BaseController {
    @Resource
    IBizDayworkItemService dayworkItemService;
    @Resource
    IBizDayworkService dayworkService;
    @Resource
    IBizLotService lotService;
    @Resource
    IBizProductService productService;
    @Resource
    IBizEquipmentDetailService equipmentDetailService;
    @Resource
    IBizProcessService processService;
    @Log(title = "数采查询,单个设备加工情况", businessType = BusinessType.SELECT)
    @GetMapping("/getCurrentProductionByEquipCode")
    public AjaxResult getCurrentProductionByEquipCode(@RequestParam("equipCode") String equipCode) {
        List<BizEquipmentDetail> equipmentDetails = equipmentDetailService.query().eq("equipment_code", equipCode).list();
        if (equipmentDetails.isEmpty()) {
            return AjaxResult.error(202, "设备" + equipCode + "不存在");
        }
        BizDayworkItem item = dayworkItemService.query().eq("equipment_detail_code", equipCode).in("status", Arrays.asList("1", "2", "3")).orderByDesc("create_time").last("limit 1").one();
        if (item == null) {
            return AjaxResult.error(201, "该设备没有加工内容");
        }
        BizDaywork bizDaywork = dayworkService.query().eq("id", item.getDayworkId()).one();
        BizProduct product = productService.query().eq("id", bizDaywork.getProductId()).one();
        BizProcess process = processService.query().eq("id", item.getProcessId()).one();
        EquipmentCurrentStatus status = new EquipmentCurrentStatus();
        status.setStartTime(item.getStartTime());
        status.setEndTime(item.getEndTime());
        status.setProductDescription(product == null ? "" : product.getDescription());
        status.setLotCode(bizDaywork.getLotCode());
        status.setProcessAlias(process == null ? "" : process.getProcessAlias());
        return AjaxResult.success("查询成功", status);
    }
    @Log(title = "数采查询,多个设备加工情况", businessType = BusinessType.SELECT)
    @GetMapping("getCurrentProductionByEquipCodes")
    public AjaxResult getCurrentProductionByEquipCodes(@RequestParam("equipCode") String equipCodes) {
        List<String> equipCodeList = Arrays.stream(equipCodes.split(",")).distinct().collect(Collectors.toList());
        List<BizEquipmentDetail> equipmentDetails = equipmentDetailService.query().in("equipment_code", equipCodeList).list();
//        if (equipmentDetails.isEmpty()) {
//            return AjaxResult.error(202, "设备" + equipCodes + "不存在");
//        }
        List<BizDayworkItem> items = dayworkItemService.query().in("equipment_detail_code", equipCodeList).in("status", Arrays.asList("1", "2", "3")).list();
        List<BizDaywork> bizDayworks = dayworkService.query().in("id", items.isEmpty() ? Collections.singletonList(0L) : items.stream().map(BizDayworkItem::getDayworkId).collect(Collectors.toList())).list();
        List<BizProduct> products = productService.query().in("id", bizDayworks.isEmpty() ? Collections.singletonList(0L) : bizDayworks.stream().map(BizDaywork::getProductId).collect(Collectors.toList())).list();
        List<BizProcess> processes = processService.query().in("id", items.isEmpty() ? Collections.singletonList(0L): items.stream().map(BizDayworkItem::getProcessId).collect(Collectors.toList())).list();
//        if (items.isEmpty()) {
//            return AjaxResult.error(201, "查询设备没有加工内容");
//        }
        Map<String, Object> result = new HashMap<>();
        equipCodeList.forEach(e -> {
            BizEquipmentDetail l = equipmentDetails.stream().filter(v -> v.getEquipmentCode().equals(e)).findFirst().orElse(null);
            if (l == null) {
                EquipmentCurrentError error = new EquipmentCurrentError();
                error.setErrMsg("设备" + e + "不存在");
                error.setErrCode(202);
                result.put(e, error);
            } else {
                BizDayworkItem item = items.stream().filter(v -> v.getEquipmentDetailId().equals(l.getId())).max(Comparator.comparing(BizDayworkItem::getCreateTime)).orElse(null);
                if (item == null) {
                    EquipmentCurrentError error = new EquipmentCurrentError();
                    error.setErrMsg("设备 " + e + "没有加工内容");
                    error.setErrCode(201);
                    result.put(l.getEquipmentCode(), error);
                } else {
                    EquipmentCurrentStatus status = new EquipmentCurrentStatus();
                    status.setStartTime(item.getStartTime());
                    status.setEndTime(item.getEndTime());
                    BizDaywork bizDaywork = bizDayworks.stream().filter(v -> v.getId().equals(item.getDayworkId())).findFirst().orElse(new BizDaywork());
                    BizProduct product = products.stream().filter(v -> v.getId().equals(bizDaywork.getProductId())).findFirst().orElse(new BizProduct());
                    BizProcess process = processes.stream().filter(v -> v.getId().equals(item.getProcessId())).findFirst().orElse(new BizProcess());
                    status.setProductDescription(product.getDescription());
                    status.setLotCode(bizDaywork.getLotCode());
                    status.setProcessAlias(process.getProcessAlias());
                    result.put(l.getEquipmentCode(), status);
                }
            }
        });
//        if (item == null) {
//            return AjaxResult.error(201, "该设备没有加工内容");
//        }
        EquipmentCurrentStatus status = new EquipmentCurrentStatus();
        return AjaxResult.success("查询成功", result);
    }
}
