package com.sw.mes.crucible.service.impl;

import com.sw.mes.base.factory.init.entity.Equipment;
import com.sw.mes.base.factory.init.service.EquipmentService;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.crucible.dao.CrucibleFixedCastingDao;
import com.sw.mes.crucible.dto.CrucibleFixedCastingDto;
import com.sw.mes.crucible.dto.CrucibleFixedCastingSaveDto;
import com.sw.mes.crucible.entity.CrucibleFixedCasting;
import com.sw.mes.crucible.service.CrucibleFixedCastingService;
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.CollectionUtils;

import java.util.Date;

/**
 * @author lf
 * @description
 * @date 2024/1/8 9:09
 */
@Service
@Transactional
public class CrucibleFixedCastingServiceImpl extends BaseServiceImpl<CrucibleFixedCasting> implements CrucibleFixedCastingService {

    CrucibleFixedCastingDao dao;
    @Autowired
    private EquipmentService equipmentService;

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

    @Autowired
    ModelMapper modelMapper;

    @Override
    public CrucibleFixedCasting findByEquipmentCodeAndfixedCastingType(Integer equipmentCode, String fixedCastingType) {
        return dao.findByEquipmentIdAndfixedCastingType(equipmentCode, fixedCastingType);
    }

    @Override
    public CrucibleFixedCasting findByEquipmentIdAndfixedCastingTypeAndIdNot(Integer equipmentCode, String fixedCastingType, Integer id) {
        return dao.findByEquipmentIdAndfixedCastingTypeAndIdNot(equipmentCode, fixedCastingType, id);
    }

    @Override
    public void batchCreate(CrucibleFixedCastingSaveDto dto) {
        if (!CollectionUtils.isEmpty(dto.getEquipmentId())) {
            for (Integer id : dto.getEquipmentId()) {
                CrucibleFixedCasting castingTypeDb = dao.findByEquipmentIdAndfixedCastingType(id, dto.getFixedCastingType());
                if (null != castingTypeDb) {
                    throw new RuntimeException("该设备号与定投类型已存在!!!");
                }
                CrucibleFixedCasting crucibleFixedCasting = fromDto(dto);
                crucibleFixedCasting.setCreateDate(new Date());
                crucibleFixedCasting.setCreateUser(BaseContextHandler.getUsername());
                Equipment equipment = equipmentService.get((long)id);
                if (null != equipment) {
                    crucibleFixedCasting.setEquipment(equipment);
                    crucibleFixedCasting.setEquipmentCode(equipment.getCode());
                    super.saveOrUpdate(crucibleFixedCasting);
                }
            }
        }
    }

    @Override
    public CrucibleFixedCasting findByEquipment(String code) {
        return dao.findTopByEquipmentCodeOrderByIdDesc(code);
    }

    private CrucibleFixedCasting fromDto(CrucibleFixedCastingSaveDto entity) {
        CrucibleFixedCasting crucibleFixedCasting = modelMapper.map(entity, CrucibleFixedCasting.class);
        return crucibleFixedCasting;
    }

    @Override
    public CrucibleFixedCasting getCrucibleFixedCastingByEquipmentCode(String code) {
        //查询实验坩埚定投方案
        CrucibleFixedCasting syCrucible = dao.findByEquipmentCodeAndFixedCastingType(code, CrucibleFixedCastingDto.fixedCastingType_sy);
        if(syCrucible != null
                && syCrucible.getFixedCastingNumber() != null && syCrucible.getCompleteNumber() != null
                && syCrucible.getFixedCastingNumber() > syCrucible.getCompleteNumber()){
            return syCrucible;
        } else {
            syCrucible = dao.findByEquipmentCodeAndFixedCastingType(code, CrucibleFixedCastingDto.fixedCastingType_lc);
            if(syCrucible != null) return syCrucible;
        }
        return null;
    }
}
