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


import com.sw.mes.base.factory.init.entity.Equipment;
import com.sw.mes.base.factory.init.service.EquipmentService;
import com.sw.mes.base.system.entity.DictionaryItem;
import com.sw.mes.base.system.service.DictionaryItemService;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.yb.report.dto.CrystalPullingInfoRunTimeDto;
import com.sw.mes.yb.report.entity.CrystalPullingInfo;
import com.sw.mes.yb.report.service.CrystalPullingInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.sw.mes.yb.report.entity.CrystalPullingInfoLog;
import com.sw.mes.yb.report.dao.CrystalPullingInfoLogDao;
import com.sw.mes.yb.report.service.CrystalPullingInfoLogService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@Service
@Transactional
public class CrystalPullingInfoLogServiceImpl extends BaseServiceImpl<CrystalPullingInfoLog> implements CrystalPullingInfoLogService {
    CrystalPullingInfoLogDao dao;

    @Autowired
    private CrystalPullingInfoService crystalPullingInfoService;

    @Autowired
    private DictionaryItemService dictionaryItemService;

    @Autowired
    private EquipmentService equipmentService;

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

    @Override
    public CrystalPullingInfoLog getCrystalPullingInfoLogByLotNo(String lotNo) {
        return dao.getOneByLotNoOrderByIdDesc(lotNo);
    }

    @Override
    public List<CrystalPullingInfoLog> getCrystalPullingInfoLogByLotNos(List<String> lotNos) {
        return dao.getCrystalPullingInfoLogByLotNos(lotNos);
    }

    @Override
    public CrystalPullingInfoLog getCrystalPullingInfoLogByDeviceNo(String deviceNo) {
        return dao.getCrystalPullingInfoLogByDeviceNo(deviceNo);
    }

    @Override
    public CrystalPullingInfoLog getCrystalPullingInfoLogByDeviceNoAndLotNo(String deviceNo, String lotNo) {

        return dao.getCrystalPullingInfoLogByDeviceNoAndLotNo(deviceNo,lotNo);
    }

    @Override
    public BigDecimal getCrystalPullingInfoLogByTimeAndSeries(String startTime, String endTime, List<String> seriesList) {
        return dao.getCrystalPullingInfoLogByTimeAndSeries(startTime, endTime, seriesList);
    }

    @Override
    public Result<CrystalPullingInfoRunTimeDto> getRunTimeInfo(String lotNo, String deviceNo) {


        //状态卡控，判断能否终止炉次
        Equipment equipment = equipmentService.findByCode(deviceNo);
        if(null != equipment && equipment.getStatus() == 2){
            throw new RuntimeException("设备："+deviceNo+"处于待机状态请先结束待机！");
        }

        //上一炉次信息
        CrystalPullingInfoLog log = this.getCrystalPullingInfoLogByDeviceNo(deviceNo);
//		if (null == log){
//			throw new RuntimeException("上一炉次拉晶情况记录表信息为空，无法计算运行时长！当前炉次号："+lotNo);
//		}
        //当前拉晶情况
        CrystalPullingInfo crystalPullingInfoByLotNo = crystalPullingInfoService.getCrystalPullingInfoByLotNo(lotNo);
        if (null == crystalPullingInfoByLotNo){
             throw new RuntimeException("实时拉晶情况为空，炉次号："+lotNo);
            /*CrystalPullingInfoRunTimeDto dto = new CrystalPullingInfoRunTimeDto();
            dto.setIsAbnamalPulling(false);
            return ResultGenerator.success(dto);*/
        }
        DictionaryItem timeItem = dictionaryItemService.findByThemeValueAndNameAndEnable("shortestDuration ", "shortestTime", true);

        if (null != log && null != log.getStopTime()){
            //判断运行时间
            //运行时间=当前炉次的停炉时间(当前时间 new date()) - 上一炉次停炉时间 - 异常停炉时间
            long runTimeLen1 = new Date().getTime() - log.getStopTime().getTime();
            double _runTime1 = runTimeLen1 / 1000 / 60 / 60d;
            BigDecimal runTimeActual;
            if (null == crystalPullingInfoByLotNo.getStandbyTime()){
                runTimeActual = new BigDecimal(_runTime1).setScale(2, BigDecimal.ROUND_DOWN);
            }else {
                BigDecimal runTimeActualFirst = new BigDecimal(_runTime1).setScale(2, BigDecimal.ROUND_DOWN);
                runTimeActual = runTimeActualFirst.subtract(crystalPullingInfoByLotNo.getStandbyTime());
            }
            if (null != timeItem.getValue() &&  !timeItem.getValue().isEmpty()){
                BigDecimal shortestDuration = new BigDecimal(timeItem.getValue());
                CrystalPullingInfoRunTimeDto crystalPullingInfoRunTimeDto = new CrystalPullingInfoRunTimeDto();
                if (runTimeActual.compareTo(shortestDuration)<0){
                    crystalPullingInfoRunTimeDto.setIsAbnamalPulling(true);
                    return ResultGenerator.success(crystalPullingInfoRunTimeDto);
                }else {
                    crystalPullingInfoRunTimeDto.setIsAbnamalPulling(false);
                    return ResultGenerator.success(crystalPullingInfoRunTimeDto);
                }
            }
        }else {
            //新开炉次情况，没有上一炉次信息，开始时间取开炉时间
            if (null != crystalPullingInfoByLotNo.getStartTime()){
                //判断运行时间
                //运行时间=当前炉次的停炉时间(当前时间 new date()) - 上一炉次停炉时间 - 异常停炉时间
                long runTimeLen1 = new Date().getTime() - crystalPullingInfoByLotNo.getStartTime().getTime();
                double _runTime1 = runTimeLen1 / 1000 / 60 / 60d;
                BigDecimal runTimeActual;
                if (null == crystalPullingInfoByLotNo.getStandbyTime()){
                    runTimeActual = new BigDecimal(_runTime1).setScale(2, BigDecimal.ROUND_DOWN);
                }else {
                    BigDecimal runTimeActualFirst = new BigDecimal(_runTime1).setScale(2, BigDecimal.ROUND_DOWN);
                    runTimeActual = runTimeActualFirst.subtract(crystalPullingInfoByLotNo.getStandbyTime());
                }
                if (null != timeItem.getValue() &&  !timeItem.getValue().isEmpty()){
                    BigDecimal shortestDuration = new BigDecimal(timeItem.getValue());
                    CrystalPullingInfoRunTimeDto crystalPullingInfoRunTimeDto = new CrystalPullingInfoRunTimeDto();
                    if (runTimeActual.compareTo(shortestDuration)<0){
                        crystalPullingInfoRunTimeDto.setIsAbnamalPulling(true);
                        return ResultGenerator.success(crystalPullingInfoRunTimeDto);
                    }else {
                        crystalPullingInfoRunTimeDto.setIsAbnamalPulling(false);
                        return ResultGenerator.success(crystalPullingInfoRunTimeDto);
                    }
                }
            }

        }
        return null;
    }
}
