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

import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.yb.alloy.dao.FeedingConfirmationDao;
import com.sw.mes.yb.alloy.dto.FeedingConfirmationDto;
import com.sw.mes.yb.alloy.dto.FurnaceCodeDto;
import com.sw.mes.yb.alloy.entity.AlloyMasterAlloy;
import com.sw.mes.yb.alloy.entity.FeedingConfirmation;
import com.sw.mes.yb.alloy.service.AlloyMasterAlloyService;
import com.sw.mes.yb.alloy.service.FeedingConfirmationService;
import com.sw.mes.yb.plan.entity.ProductionSchedule;
import com.sw.mes.yb.plan.service.ProductionScheduleService;
import com.sw.mes.yb.production.entity.AgainTask;
import com.sw.mes.yb.production.entity.LoadingTask;
import com.sw.mes.yb.production.service.AgainTaskService;
import com.sw.mes.yb.production.service.LoadingTaskService;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * @author lf
 * @description
 * @date 2024/3/5 9:59
 */
@Slf4j
@Service
@Transactional
public class FeedingConfirmationServiceImpl extends BaseServiceImpl<FeedingConfirmation> implements FeedingConfirmationService {
    FeedingConfirmationDao dao;

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

    @Autowired
    private ProductionScheduleService productionScheduleService;
    @Autowired
    private AgainTaskService againTaskService;
    @Autowired
    private LoadingTaskService loadingTaskService;
    @Autowired
    private AlloyMasterAlloyService alloyMasterAlloyService;
    @Autowired
    private ModelMapper modelMapper;

    @Override
    public FurnaceCodeDto findAgainNumByCode(String furnaceCode) {
        FurnaceCodeDto furnaceCodeDto = new FurnaceCodeDto();
        Integer againNum = 0;
        ProductionSchedule productionSchedule = productionScheduleService.findByDeviceNo(furnaceCode);
        Assert.notNull(productionSchedule, "炉台号不存在");
        String lotNo = productionSchedule.getLotNo();
        //        根据炉次号复投任务查如果没查到去配料任务查
        List<AgainTask> againTaskList = againTaskService.findSuccessByLotNo(lotNo);
        LoadingTask loadingTaskDb = loadingTaskService.findSuccessByLotNo(lotNo);
        if (CollectionUtils.isEmpty(againTaskList) && null == loadingTaskDb) {
            throw new RuntimeException("未查询到复投次数");
        }
        AgainTask againTaskDb = againTaskList.stream()
                .max(Comparator.comparing(AgainTask::getAgainNum))
                .orElse(null);
        if (null != againTaskDb) {
            againNum = againTaskDb.getAgainNum();
        }
        furnaceCodeDto.setFurnaceCode(furnaceCode);
        furnaceCodeDto.setLotNo(lotNo);
        furnaceCodeDto.setAddTime(againNum);
        return furnaceCodeDto;
    }

    @Override
    public void feedingConfirmation(FeedingConfirmationDto feedingConfirmationDto) {
        AlloyMasterAlloy alloyMasterAlloyDb = alloyMasterAlloyService.findByLotNoAndAddTime(feedingConfirmationDto.getLotNo(), String.valueOf(feedingConfirmationDto.getAddTime()));
        String s = alloyMasterAlloyDb.getWorkOrderNumber() + alloyMasterAlloyDb.getFurnaceCode() + alloyMasterAlloyDb.getAddTime();
        String substring = feedingConfirmationDto.getMotherAlloyCode().substring(0, feedingConfirmationDto.getMotherAlloyCode().length() - 4);
        if (!s.equals(substring)) {
            log.error("母合金单号:{}", substring);
            log.error("计算出来的单号:{}", s);
            throw new RuntimeException("母合金单号内的炉号、加料次数与原数据不一致");
        }
        FeedingConfirmation feedingConfirmation = fromDto(feedingConfirmationDto);
        super.saveOrUpdate(feedingConfirmation);
    }

    private FeedingConfirmation fromDto(FeedingConfirmationDto feedingConfirmationDto) {
        FeedingConfirmation feedingConfirmation = modelMapper.map(feedingConfirmationDto, FeedingConfirmation.class);
        feedingConfirmation.setCreateUser(BaseContextHandler.getUsername());
        feedingConfirmation.setCreateDate(new Date());
        return feedingConfirmation;
    }
}
