package com.sw.mes.yb.plan.service.impl;


import cc.cczn.matrix.commons.extra.jpa.BaseExtraEntity;
import com.alibaba.fastjson.JSON;
import com.sw.mes.base.factory.init.dto.MachineCapacityInfoDataDto;
import com.sw.mes.base.factory.init.entity.Equipment;
import com.sw.mes.base.factory.init.entity.MachineCapacity;
import com.sw.mes.base.factory.init.entity.MachineCapacityInfo;
import com.sw.mes.base.factory.init.service.EquipmentService;
import com.sw.mes.base.factory.init.service.MachineCapacityInfoService;
import com.sw.mes.base.factory.init.service.MachineCapacityService;
import com.sw.mes.base.factory.material.entity.MaterialModel;
import com.sw.mes.base.factory.material.service.MaterialModelService;
import com.sw.mes.base.factory.product.entity.NewProductModel;
import com.sw.mes.base.factory.product.entity.NewProductModelDetail;
import com.sw.mes.base.factory.product.entity.ProductModel;
import com.sw.mes.base.factory.product.service.NewProductModelDetailService;
import com.sw.mes.base.factory.product.service.NewProductModelService;
import com.sw.mes.base.factory.product.service.ProductModelService;
import com.sw.mes.base.system.entity.DictionaryItem;
import com.sw.mes.base.system.service.DictionaryItemService;
import com.sw.mes.core.annotation.Jinko;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.enums.EnumValues;
import com.sw.mes.core.enums.EnumValuesFactory;
import com.sw.mes.core.utils.NumberUtil;
import com.sw.mes.enumvalue.EnumValuesServer;
import com.sw.mes.utils.StringUtils;
import com.sw.mes.yb.plan.dao.ProductionScheduleDao;
import com.sw.mes.yb.plan.dto.BatchSchedulingDto;
import com.sw.mes.yb.plan.dto.ProductionOrderCountDto;
import com.sw.mes.yb.plan.dto.SchedulingInfoDto;
import com.sw.mes.yb.plan.entity.ProductionOrder;
import com.sw.mes.yb.plan.entity.ProductionSchedule;
import com.sw.mes.yb.plan.service.ProductionOrderService;
import com.sw.mes.yb.plan.service.ProductionScheduleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@Service
@Transactional
@Slf4j
public class ProductionScheduleServiceImpl extends BaseServiceImpl<ProductionSchedule> implements ProductionScheduleService {
    ProductionScheduleDao dao;

    @Autowired
    public void setDao(ProductionScheduleDao dao) {
        super.setBaseDao(dao);
        this.dao = dao;
    }

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private MachineCapacityService machineCapacityService;

    @Autowired
    private ProductionOrderService productionOrderService;

    @Autowired
    private MachineCapacityInfoService machineCapacityInfoService;

    @Autowired
    private NewProductModelService modelService;

    @Autowired
    private NewProductModelDetailService modelDetailService;

    @Autowired
    private MaterialModelService materialModelService;

    @Autowired
    private DictionaryItemService dictionaryItemService;


    @Override
    public void batchScheduling(BatchSchedulingDto dto) {
        if (dto.getDeviceIds().size() >= 2){
            throw new RuntimeException("只能选择1台设备进行排程");
        }
        // 可排程订单类型
        List<DictionaryItem> itemList = dictionaryItemService.findByThemeValueAndEnable("scheduleOrderType", true);
        List<String> scheduleOrderTypes = itemList.stream().map(DictionaryItem::getValue).collect(Collectors.toList());

        ProductionOrder productionOrder = productionOrderService.findByAufnr(dto.getCode());
        if (productionOrder == null) {
            throw new RuntimeException("订单编号不存在");
        }
        if(productionOrder.getStatus().equals("CLOSE")){
            throw new RuntimeException("订单状态已关闭");
        }
        List<ProductionSchedule> normalList = dao.findNormalListByOrderId(productionOrder.getId());
        if (!CollectionUtils.isEmpty(normalList)){
            throw new RuntimeException("该订单已存在排程，不能重复排程");
        }
        Date now = new Date();
        if (now.compareTo(productionOrder.getGstrp()) <= 0 || now.compareTo(productionOrder.getGltrp()) >=0){
            throw new RuntimeException("当前时间不在订单生产周期内");
        }
        if (productionOrder.getMachineCapacityId() == null) {
            throw new RuntimeException("订单加工能力异常，请联系管理员");
        }
        MachineCapacityInfo capacityInfo = machineCapacityInfoService.get(productionOrder.getMachineCapacityId());
        if (capacityInfo == null){
            throw new RuntimeException("订单未关联加工能力，请联系管理员");
        }
        String orderType = productionOrder.getAuart();
        if (StringUtils.isEmpty(orderType)) {
            throw new RuntimeException("订单类型未知，请联系管理员");
        }
        if (!scheduleOrderTypes.contains(orderType)){
            throw new RuntimeException("订单类型【"+orderType+"】不可排程，请联系管理员");
        }
        if (!productionOrder.getProductionStatus()) {
            throw new RuntimeException("当前订单不是生产状态");
        }
        List<ProductionSchedule> productionSchedules = new ArrayList<>();
        for (Long deviceId : dto.getDeviceIds()) {
            Equipment equipment = equipmentService.get(deviceId);
            if (equipment == null) {
                throw new RuntimeException("设备ID【" + deviceId + "】不存在，请刷新页面重试");
            }
            if (!equipment.getSeries().equals(productionOrder.getField4())){
                throw new RuntimeException("设备【" + equipment.getCode() + "】所在片区与订单指定生产片区不一致，请检查");
            }
            if (!equipment.getEnableState()) {
                throw new RuntimeException("设备【" + equipment.getCode() + "】未启用");
            }
            if(equipment.getStatus() == 2){
                throw new RuntimeException("设备【" + equipment.getCode() + "】处于待机状态");
            }
            MachineCapacity machineCapacity = machineCapacityService.findByEquipmentId(equipment.getId());
            if (machineCapacity == null) {
                throw new RuntimeException("设备【" + equipment.getCode() + "】加工能力没有维护，不允许排程");
            }
            if (machineCapacity.getTheoryCapacity() == null || machineCapacity.getTheoryCapacity() <= 0){
                throw new RuntimeException("设备【" + equipment.getCode() + "】加工能力理论产能没有维护，不允许排程");
            }

            //创建即将开始的，下一个排产的
            ProductionSchedule productionSchedule = dao.findByDeviceIdAndStatus(deviceId, EnumValuesServer.ProductionScheduleStatus.未开始.getKey());

            //创建排程时不更新设备加工能力
            if(null == machineCapacity.getCapacityInfoNext()){
                if (EnumValuesServer.ProductionOrderType.ZRB4.getKey().equals(orderType)) {
                    machineCapacity.setRdlogo(true);
                } else {
                    machineCapacity.setRdlogo(false);
                }
                machineCapacity.setRdonlylogo(productionOrder.getRdonlylogo());
                machineCapacity.setProductType(capacityInfo.getProductType());
                machineCapacity.setCapacityInfo(capacityInfo);
                machineCapacityService.saveOrUpdate(machineCapacity);
            }

            productionSchedule = getProductionSchedule(productionOrder, equipment, productionSchedule);
            if (productionSchedule == null) {
                continue;
            }
            if (EnumValuesServer.ProductionOrderType.ZRB4.getKey().equals(orderType)) {
                productionSchedule.setFurnaceType(EnumValuesServer.FurnaceType.研发.getKey());
            } else {
                productionSchedule.setFurnaceType(EnumValuesServer.FurnaceType.常规.getKey());
            }
            productionSchedule.setProductionOrder(productionOrder);
            productionSchedule.setProductModel(productionOrder.getProductModel());
            productionSchedule.setCapacityInfo(new MachineCapacityInfo(productionOrder.getMachineCapacityId()));
            productionSchedule.setCount(machineCapacity.getTheoryCapacity());
            productionSchedule.setStatusInfo("手动排产");
            productionSchedule.setCreateUserId(BaseContextHandler.getUserID());
            productionSchedule.setCreateUserName(BaseContextHandler.getUsername());
            productionSchedule.setCreateTime(new Date());
            productionSchedule.setProductType(capacityInfo.getProductType());
            productionSchedule.setWitchOrder(dto.getWitchOrder());
            productionSchedules.add(productionSchedule);
        }
        dao.saveAll(productionSchedules);

        // 修改订单为已排程
        if (productionSchedules.size() > 0){
            this.setOrderSchedule(productionOrder, 1);
        }
    }

    @Override
    public Map<Long, ProductionOrderCountDto> getProductionScheduleData() {
        List<Map<String, Object>> productionOrderCountsObj = dao.getProductionScheduleData();
        List<ProductionOrderCountDto> productionOrderCountDtos = JSON.parseArray(JSON.toJSONString(productionOrderCountsObj), ProductionOrderCountDto.class);
        Map<Long, ProductionOrderCountDto> map = new HashMap<>();
        for (ProductionOrderCountDto productionOrderCountDto : productionOrderCountDtos) {
            map.put(productionOrderCountDto.getProductionOrderId(), productionOrderCountDto);
        }
        return map;
    }

    private ProductionSchedule getProductionSchedule(ProductionOrder productionOrder, Equipment equipment, ProductionSchedule productionSchedule) {
        if (productionSchedule == null) {
            productionSchedule = new ProductionSchedule();
            productionSchedule.setDeviceId(equipment.getId());
            productionSchedule.setDeviceNo(equipment.getCode());
            productionSchedule.setStatus(EnumValuesServer.ProductionScheduleStatus.未开始.getKey());
        } else {
            if (productionSchedule.getProductionOrder().getId().intValue() == productionOrder.getId()) {
                return null;
            }
            productionSchedule.setProductionOrder(productionOrder);
        }
        return productionSchedule;
    }


    // 自动排程
    @Override
    public void autoScheduling() {
        // 检查已有未开始状态的排程，删除不符合规则的排程
        List<ProductionSchedule> productionSchedules = dao.findByStatus(EnumValuesServer.ProductionScheduleStatus.未开始.getKey());
        for (ProductionSchedule productionSchedule : productionSchedules) {
            if (productionSchedule == null) {
                continue;
            }
            ProductionOrder productionOrder = productionSchedule.getProductionOrder();
            //检查订单信息
            if (productionOrder == null) {
                dao.delete(productionSchedule);
                continue;
            }
            //检查订单生产状态
            if (!productionOrder.getProductionStatus()) {
                dao.delete(productionSchedule);
                this.setOrderSchedule(productionOrder, 0);
                continue;
            }
            //检查订单开始结束时间
            Date now = new Date();
            if (now.compareTo(productionOrder.getGstrp()) <= 0 || now.compareTo(productionOrder.getGltrp()) >=0){
                dao.delete(productionSchedule);
                this.setOrderSchedule(productionOrder, 0);
                continue;
            }
            Equipment equipment = equipmentService.get(productionSchedule.getDeviceId());
            // 检查设备属性
            if (equipment == null || !equipment.getEnableState() || equipment.getDevice() == null
                    || !equipment.getDevice().getCategory().equals(EnumValues.EquipmentCategory.单晶炉.getKey())) {
                dao.delete(productionSchedule);
                this.setOrderSchedule(productionOrder, 0);
                continue;
            }
            // 检查设备加工能力配置
            MachineCapacity machineCapacity = machineCapacityService.findByEquipmentId(equipment.getId());
            if (machineCapacity == null) {
                dao.delete(productionSchedule);
                this.setOrderSchedule(productionOrder, 0);
                continue;
            }
            // 检查设备加工能力
            if (!machineCapacity.getCapacityInfo().getId().equals(productionSchedule.getCapacityInfo().getId())) {
                dao.delete(productionSchedule);
                this.setOrderSchedule(productionOrder, 0);
                continue;
            }
            // 理论产能
            if (machineCapacity.getTheoryCapacity() < productionSchedule.getCount()){
                dao.delete(productionSchedule);
                this.setOrderSchedule(productionOrder, 0);
                continue;
            }
            // 判断订单研发状态和设备研发状态是否一致
            if (EnumValuesServer.ProductionOrderType.ZRB4.getKey().equals(productionOrder.getAuart()) !=
                    machineCapacity.getRdlogo()) {
                dao.delete(productionSchedule);
                this.setOrderSchedule(productionOrder, 0);
                continue;
            }
            // 研发订单的研发标识不一样
            if (EnumValuesServer.ProductionOrderType.ZRB4.getKey().equals(productionOrder.getAuart())
                    &&(     (StringUtils.isBlank(machineCapacity.getRdonlylogo()) && StringUtils.isBlank(productionOrder.getRdonlylogo()))  //判断研发唯一标识不一样，哪怕一个为null一个有值
                            || (StringUtils.isBlank(machineCapacity.getRdonlylogo()) != StringUtils.isBlank(productionOrder.getRdonlylogo()))
                            || (!machineCapacity.getRdonlylogo().equals(productionOrder.getRdonlylogo()))   )
            ) {
                dao.delete(productionSchedule);
                this.setOrderSchedule(productionOrder, 0);
                continue;
            }
        }

        // 可排程订单类型
        List<DictionaryItem> itemList = dictionaryItemService.findByThemeValueAndEnable("scheduleOrderType", true);
        List<String> scheduleOrderTypes = itemList.stream().map(DictionaryItem::getValue).collect(Collectors.toList());

        // 获取订单信息，执行排程
        List<Map<String, Object>> maps = dao.getProductionScheduleData();
        List<ProductionOrderCountDto> productionOrderCounts = JSON.parseArray(JSON.toJSONString(maps), ProductionOrderCountDto.class);
        List<ProductionSchedule> productionScheduleList = new ArrayList<>();
        for (ProductionOrderCountDto orderCount : productionOrderCounts) {
            // 订单类型不可排程
            if (!scheduleOrderTypes.contains(orderCount.getOrderType())) continue;

            int scheduleCount = 0;

            // 剩余排程数量
            long residueCount = orderCount.getDemandCount() - orderCount.getSumCount();
            if (residueCount > 0) {
                // 获取设备加工能力集合
                List<MachineCapacity> machineCapacities = machineCapacityService.getLjSeriesDevicesA(orderCount.getMachineCapacityId());
                if(machineCapacities == null || machineCapacities.size() == 0) continue;
                // 获取单晶炉设备集合
                List<Equipment> equipmentList = equipmentService.findByFactoryCodeAndCategory(orderCount.getWorkShop(), String.valueOf(EnumValuesFactory.DeviceCategory.单晶炉.getKey()));
                if (equipmentList == null || equipmentList.size() == 0) continue;
                Map<Long, Equipment> equipmentMap = equipmentList.stream().collect(Collectors.toMap(Equipment::getId, Equipment -> Equipment));

                // 删除不匹配设备的加工能力
                for (int i = 0; i < machineCapacities.size(); i++) {
                    MachineCapacity capacity = machineCapacities.get(i);
                    if (equipmentMap.get(capacity.getEquipment().getId()) == null){
                        machineCapacities.remove(i);
                        i--;
                    }
                }

                for (MachineCapacity machineCapacity : machineCapacities) {
                    Integer theoryCapacity = machineCapacity.getTheoryCapacity();
                    if (theoryCapacity <= 0) continue;      // 理论产能没有维护，排除该设备
                    try {
                        // 当前订单数量已排完，退出当前订单排程
                        if (residueCount <= 0) {
                            break;
                        }
                        // 检查设备加工能力配置
                        ProductionSchedule productionSchedule = getProductionSchedule(new ProductionOrder(orderCount.getProductionOrderId()), machineCapacity.getEquipment(), null);
                        if (EnumValuesServer.ProductionOrderType.ZRB4.getKey().equals(orderCount.getOrderType())) {
                            if (machineCapacity.getRdlogo() && StringUtils.isNotBlank(machineCapacity.getRdonlylogo())
                                    && machineCapacity.getRdonlylogo().equals(orderCount.getRdonlylogo())) {
                                assert productionSchedule != null;
                                productionSchedulesSet(orderCount, machineCapacity, theoryCapacity, productionSchedule);
                                productionSchedule.setFurnaceType(EnumValuesServer.FurnaceType.研发.getKey());
                                productionScheduleList.add(productionSchedule);
                                residueCount -= theoryCapacity;
                            }
                        } else {
                            if (!machineCapacity.getRdlogo() && StringUtils.isBlank(machineCapacity.getRdonlylogo())) {
                                assert productionSchedule != null;
                                productionSchedulesSet(orderCount, machineCapacity, theoryCapacity, productionSchedule);
                                productionSchedule.setFurnaceType(EnumValuesServer.FurnaceType.常规.getKey());
                                productionScheduleList.add(productionSchedule);
                                residueCount -= theoryCapacity;
                            }
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    // 当前订单排程超过100，则保存一次
                    if (productionScheduleList.size() >= 100) {
                        dao.saveAll(productionScheduleList);
                        scheduleCount += productionScheduleList.size();
                        productionScheduleList = new ArrayList<>();
                    }
                }

                // 保存当前订单剩余排程
                if (productionScheduleList.size() > 0) {
                    dao.saveAll(productionScheduleList);
                    scheduleCount += productionScheduleList.size();
                    productionScheduleList = new ArrayList<>();
                }
            }

            // 修改订单为已经排程
            if (scheduleCount > 0){
                ProductionOrder order = productionOrderService.get(orderCount.getProductionOrderId());
                this.setOrderSchedule(order, 1);
            }
        }
    }

    @Override
    public void setOrderSchedule(ProductionOrder order, int state){
        order.setScheduled(state);
        productionOrderService.saveOrUpdate(order);
    }

    private void productionSchedulesSet(ProductionOrderCountDto productionOrderCountDto, MachineCapacity machineCapacity, Integer theoryCapacity, ProductionSchedule productionSchedule) {
        productionSchedule.setProductionOrder(new ProductionOrder(productionOrderCountDto.getProductionOrderId()));
        productionSchedule.setProductModel(new ProductModel(productionOrderCountDto.getProductModelId()));
        productionSchedule.setCapacityInfo(machineCapacity.getCapacityInfo());
        productionSchedule.setCreateTime(new Date());
        productionSchedule.setCount(theoryCapacity);
        productionSchedule.setStatusInfo("自动排程");
        productionSchedule.setCreateUserId(BaseContextHandler.getUserID());
        productionSchedule.setCreateUserName("缺省用户".equals(BaseContextHandler.getUsername()) ? "" : BaseContextHandler.getUsername());
        productionSchedule.setCreateTime(new Date());
        productionSchedule.setProductType(machineCapacity.getProductType());
    }

    @Override
    public List<SchedulingInfoDto> getSchedulingInfo() {
        List<Map<String, Object>> mapDatas = dao.getSchedulingInfo();
        Map<String, SchedulingInfoDto> schedulingInfoDtoMap = new LinkedHashMap<>();
        for (Map<String, Object> map1 : mapDatas) {
            SchedulingInfoDto schedulingInfoDto = schedulingInfoDtoMap.get(map1.get("area"));
            if (schedulingInfoDto == null) {
                schedulingInfoDto = new SchedulingInfoDto();
                schedulingInfoDto.setArea(map1.get("area").toString());
                schedulingInfoDto.setScheduleCount(0);
                schedulingInfoDto.setFreeCount(0);
                schedulingInfoDtoMap.put(map1.get("area").toString(), schedulingInfoDto);
            }
            if (NumberUtil.Object2Integer(map1.get("type"), 0) == 0) { // 空闲
                schedulingInfoDto.setFreeCount(NumberUtil.Object2Integer(map1.get("counts"), 0));
            } else {
                schedulingInfoDto.setScheduleCount(NumberUtil.Object2Integer(map1.get("counts"), 0));
            }
        }
        List<SchedulingInfoDto> schedulingInfoDtos = new LinkedList<>();
        for (Map.Entry<String, SchedulingInfoDto> mp : schedulingInfoDtoMap.entrySet()) {
            schedulingInfoDtos.add(mp.getValue());
        }
        return schedulingInfoDtos;
    }

    @Override
    public ProductionSchedule findTopByDeviceIdAndStatusOrderByIdDesc(Long equipmentId, Integer status) {
        return dao.findTopByDeviceIdAndStatusOrderByIdDesc(equipmentId, status);
    }


    @Override
    public ProductionSchedule findTopByDeviceNoAndStatusOrderByIdDesc(String equipmentCode, List<Integer> status) {
        List<ProductionSchedule> list = dao.findByDeviceNoAndStatusInOrderByIdDesc(equipmentCode, status);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<ProductionSchedule> findByDeviceNoAndStatus(String equipmentCode, List<Integer> status) {
        return dao.findByDeviceNoAndStatusInOrderByIdDesc(equipmentCode, status);
    }

    @Override
    public List<ProductionSchedule> findNormalListByOrderId(Long orderId) {
        return dao.findNormalListByOrderId(orderId);
    }

    @Override
    public List<ProductionSchedule> findByStatus1(Integer status) {
        List<ProductionSchedule> list = dao.findByStatus1(status);
        if (list.size() > 0) {
            return list;
        }
        return null;
    }

    @Override
    public ProductionSchedule findByLotNo(String lotNo) {
        List<ProductionSchedule> list = dao.findByLotNo(lotNo);
        if (list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public ProductionSchedule findByLotNoC(String lotNo) {
        List<ProductionSchedule> list = dao.findByLotNoC(lotNo);
        if (list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<ProductionSchedule> findAllByLotNo(String lotNo) {
        List<ProductionSchedule> list = dao.findAllByLotNo(lotNo);
        if (list.size() > 0){
            return list;
        }
        return null;
    }

    @Override
    public ProductionSchedule findDataByLotNo(String lotNo) {
        return dao.findDataByLotNo(lotNo);
    }

    @Override
    public ProductionSchedule findDataByDeviceNo(String deviceNo,List<Integer> status) {
        return dao.findDataByDeviceNo(deviceNo,status);
    }

    @Override
    public List<ProductionSchedule> findByOderId(Long orderId) {
        return dao.findByOderId(orderId);
    }

    @Override
    public List<String> queryLotNoByDeviceNo(String deviceNo) {
        return dao.findByDeviceNoAndStatus(deviceNo);
    }
    @Override
    public String queryLotNosByDeviceNo(String deviceNo) {
        return dao.queryLotNosByDeviceNo(deviceNo);
    }

    @Override
    public List<String> findTop3LotNoByEquipmentCode(String equipmentCode) {
        return dao.findTop3LotNoByEquipmentCode(equipmentCode);
    }

    @Jinko(value = "设备加工能力", author = "wxl")
    @Override
    public void updateMachineCapacity(Integer id) {
        // 获取设备加工能力
        MachineCapacity capacity = machineCapacityService.get(id);

        // 获取正在进行的排程
        ProductionSchedule schedule = dao.findTopByDeviceIdAndStatusOrderByIdDesc(capacity.getEquipment().getId(),
                EnumValuesServer.ProductionScheduleStatus.进行中.getKey());
        if (schedule == null) return;

        // 获取对应料号
        NewProductModel model = modelService.findByCode(schedule.getProductModel().getCode());
        List<NewProductModelDetail> detailList = modelDetailService.findByProductModelId(model.getId());
        for (NewProductModelDetail detail : detailList) {
            if (detail.getCode().equals(model.getCode())) continue;
            MaterialModel materialModel = materialModelService.findByCode(detail.getCode());
            if (materialModel == null) continue;

            MachineCapacityInfoDataDto capacityInfoDto = materialModelService.getMachineCapacityInfoDataDto(materialModel.getId());
            // 比较加工能力属性是否一致
            MachineCapacityInfo capacityInfo = capacity.getCapacityInfo();
            if (capacityInfo.getConductionType().equals(capacityInfoDto.getConductionType())
                && capacityInfo.getSpecification().equals(capacityInfoDto.getSpecification())
                && capacityInfo.getDopant().equals(capacityInfoDto.getDopant())
                && capacityInfo.getSimpleName().equals(capacityInfoDto.getSimpleName())){

                // 属性一致，修改料号
                schedule.setProductModel(new ProductModel(materialModel.getId()));
                dao.save(schedule);
            }
        }
    }

    @Override
    public void updateStatus(Long scheduleId, Integer status) {
        ProductionSchedule schedule = dao.getOne(scheduleId);
        schedule.setStatus(status);
        dao.save(schedule);
    }

    @Override
    public ProductionSchedule findByDeviceAndStatusList(Long deviceId, List<Integer> statusList) {
        return dao.findByDeviceAndStatusList(deviceId, statusList);
    }

    @Override
    public ProductionSchedule findPlanProductionById(Long id) {
        return dao.findPlanProductionById(id);
    }

    @Override
    public ProductionSchedule findCloseByEquipmentCode(String code, int status) {
        return dao.findCloseByEquipmentCode(code,status);
    }

    @Override
    public ProductionSchedule findByEquipmentCode(String code) {
        return dao.findByEquipmentCode(code);
    }

    @Override
    public ProductionSchedule findByDeviceNo(String furnaceCode) {
        return dao.findByDeviceNo(furnaceCode);
    }



}
