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.core.utils.BeanUtil;
import com.sw.mes.yb.alloy.dto.ResistanceOfThisRodImportDto;
import org.modelmapper.ModelMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.sw.mes.yb.alloy.entity.ResistanceOfThisRod;
import com.sw.mes.yb.alloy.dao.ResistanceOfThisRodDao;
import com.sw.mes.yb.alloy.service.ResistanceOfThisRodService;
import org.springframework.util.CollectionUtils;

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


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@Service
@Transactional
public class ResistanceOfThisRodServiceImpl extends BaseServiceImpl<ResistanceOfThisRod> implements ResistanceOfThisRodService {
    ResistanceOfThisRodDao dao;

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

    @Autowired
    private ModelMapper modelMapper;

    @Override
    public List<ResistanceOfThisRod> findAllByResistanceOfThisRod(ResistanceOfThisRod resistanceOfThisRod) {
        return dao.findAllByResistanceOfThisRod(resistanceOfThisRod.getFurnaceCode(), resistanceOfThisRod.getHeatField(),
                resistanceOfThisRod.getAddTime(), resistanceOfThisRod.getMaterialShortName(), resistanceOfThisRod.getResistanceOfThisRod());
    }

    @Override
    public void importResistanceOfThisRod(List<ResistanceOfThisRodImportDto> resistanceOfThisRodImportDtoList) {

        if (CollectionUtils.isEmpty(resistanceOfThisRodImportDtoList)) {
            throw new RuntimeException("本棒电阻导入信息不存在");
        }
        ArrayList<ResistanceOfThisRod> resistanceOfThisRodArrayList = new ArrayList<>();
        for (ResistanceOfThisRodImportDto resistanceOfThisRodImportDto : resistanceOfThisRodImportDtoList) {
            ResistanceOfThisRod resistanceOfThisRod = new ResistanceOfThisRod();
            ResistanceOfThisRod entityfromFto = fromDto(resistanceOfThisRodImportDto);
            BeanUtils.copyProperties(entityfromFto, resistanceOfThisRod, BeanUtil.getIgnoreProperties(entityfromFto, null, null));
            resistanceOfThisRod.setCreateUser(BaseContextHandler.getUsername());
            resistanceOfThisRod.setCreateDate(new Date());

            List<ResistanceOfThisRod> resistanceOfThisRodList = dao.findAllByResistanceOfThisRod(resistanceOfThisRod.getFurnaceCode(), resistanceOfThisRod.getHeatField(),
                    resistanceOfThisRod.getAddTime(), resistanceOfThisRod.getMaterialShortName(),
                    resistanceOfThisRod.getResistanceOfThisRod());
            if (!CollectionUtils.isEmpty(resistanceOfThisRodList)) {
//                throw new RuntimeException("本棒信息电阻已存在、炉台号:" + resistanceOfThisRod.getFurnaceCode()
//                        + "热场:" + resistanceOfThisRod.getHeatField() + "加次:" + resistanceOfThisRod.getAddTime()
//                        + "产品简称" + resistanceOfThisRod.getMaterialShortName() + "本棒电阻" + resistanceOfThisRod.getResistanceOfThisRod());
                for (ResistanceOfThisRod resistance : resistanceOfThisRodList) {
                    super.saveOrUpdate(resistance);
                }
            }else {
                resistanceOfThisRodArrayList.add(resistanceOfThisRod);
            }
        }
        dao.saveAll(resistanceOfThisRodArrayList);
    }

    @Override
    public ResistanceOfThisRod findByFurnaceCodeAndAddTime(String lotNo, int addTime) {
        return dao.findByFurnaceCodeAndAddTime(lotNo,addTime);
    }

    private ResistanceOfThisRod fromDto(ResistanceOfThisRodImportDto dto) {
        ResistanceOfThisRod map = modelMapper.map(dto, ResistanceOfThisRod.class);
        if (null != dto.getResistanceOfThisRod()) {
            map.setResistanceOfThisRod(new BigDecimal(dto.getResistanceOfThisRod()));
        }
        return map;
    }
}
