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.RawMaterialLevelImportDto;
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.RawMaterialLevel;
import com.sw.mes.yb.alloy.dao.RawMaterialLevelDao;
import com.sw.mes.yb.alloy.service.RawMaterialLevelService;
import org.springframework.util.CollectionUtils;

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


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@Service
@Transactional
public class RawMaterialLevelServiceImpl extends BaseServiceImpl<RawMaterialLevel> implements RawMaterialLevelService {
    RawMaterialLevelDao dao;

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

    @Autowired
    private ModelMapper modelMapper;

    @Override
    public void importRawMaterialLevel(List<RawMaterialLevelImportDto> rawMaterialLevelImportDtoList) {
        if (CollectionUtils.isEmpty(rawMaterialLevelImportDtoList)) {
            throw new RuntimeException("原料导入信息不存在");
        }
        for (RawMaterialLevelImportDto rawMaterialLevelDto : rawMaterialLevelImportDtoList) {
            RawMaterialLevel rawMaterialLevel = new RawMaterialLevel();
            RawMaterialLevel entityfromFto = fromDto(rawMaterialLevelDto);
            BeanUtils.copyProperties(entityfromFto, rawMaterialLevel, BeanUtil.getIgnoreProperties(entityfromFto, null, null));
            RawMaterialLevel rawMaterialLevelDb = dao.findByMaterialCodeAndResistanceGear(rawMaterialLevelDto.getMaterialCode(), rawMaterialLevel.getResistanceGear());
            if (null == rawMaterialLevelDb) {
                rawMaterialLevel.setCreateUser(BaseContextHandler.getUsername());
                rawMaterialLevel.setCreateDate(new Date());
                super.saveOrUpdate(rawMaterialLevel);
            } else {
                rawMaterialLevelDb.setUpdateUser(BaseContextHandler.getUsername());
                rawMaterialLevelDb.setUpdateDate(new Date());
                rawMaterialLevelDb.setResistanceValue(new BigDecimal(rawMaterialLevelDto.getResistanceValue()));
                super.saveOrUpdate(rawMaterialLevelDb);
            }
        }
    }

    @Override
    public RawMaterialLevel findByMaterialCode(String code) {
        return dao.findByMaterialCode(code);
    }

    @Override
    public RawMaterialLevel findByMaterialCodeAndResistanceGear(String code, String materialGear) {
        return dao.findByMaterialCodeAndResistanceGear(code, materialGear);
    }

    @Override
    public RawMaterialLevel findByMaterialCodeAndResistanceGearAndIdNot(String materialCode, String resistanceGear, Integer id) {
        return dao.findByMaterialCodeAndResistanceGearAndIdNot(materialCode, resistanceGear, id);
    }

    private RawMaterialLevel fromDto(RawMaterialLevelImportDto dto) {
        RawMaterialLevel map = modelMapper.map(dto, RawMaterialLevel.class);
        if (null != dto.getResistanceGear()) {
            map.setResistanceGear(dto.getResistanceGear());

        }
        if (null != dto.getResistanceValue()) {
            map.setResistanceValue(new BigDecimal(dto.getResistanceValue()));
        }
        return map;
    }


}
