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


import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.utils.BeanUtil;
import com.sw.mes.yb.alloy.dto.FixLogicDto;
import com.sw.mes.yb.alloy.entity.FixLogicHisLog;
import com.sw.mes.yb.alloy.service.FixLogicHisLogService;
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.FixLogic;
import com.sw.mes.yb.alloy.dao.FixLogicDao;
import com.sw.mes.yb.alloy.service.FixLogicService;
import org.springframework.util.Assert;

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


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@Service
@Transactional
public class FixLogicServiceImpl extends BaseServiceImpl<FixLogic> implements FixLogicService {
    FixLogicDao dao;

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

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private FixLogicHisLogService fixLogicHisLogService;

    @Override
    public List<FixLogic> findAllByFixLogic(FixLogic fixLogic) {
        return dao.findAllByFixLogic(fixLogic.getHeatField(), fixLogic.getMaterialShortName(), fixLogic.getAddTime());
    }

    @Override
    public void update(FixLogicDto dto) {
        Assert.notNull(dto.getId(), "id不能为空");
        FixLogic fixLogicDb = super.get(dto.getId());
        Assert.notNull(fixLogicDb, "原信息不存在");
        FixLogicHisLog fixLogicHisLog = new FixLogicHisLog();
        BeanUtils.copyProperties(fixLogicDb, fixLogicHisLog, BeanUtil.getIgnoreProperties(fixLogicDb, null, null));
        fixLogicHisLog.setFixLogicId(fixLogicDb.getId());
        fixLogicHisLogService.saveOrUpdate(fixLogicHisLog);
        FixLogic entityfromFto = fromDto(dto);
        BeanUtils.copyProperties(entityfromFto, fixLogicDb, BeanUtil.getIgnoreProperties(entityfromFto, null, null));
        super.saveOrUpdate(fixLogicDb);
    }

    @Override
    public FixLogic findByFixUpperLimit(BigDecimal tResistivity) {
        return dao.findByFixUpperLimit(tResistivity);
    }

    @Override
    public FixLogic findByHeatFieldAndMaterialShortNameAndAddTime(String heatField, String productName, Integer againNum) {
        return dao.findByHeatFieldAndMaterialShortNameAndAddTime(heatField,productName,againNum);
    }

    private FixLogic fromDto(FixLogicDto dto) {
        return modelMapper.map(dto, FixLogic.class);
    }
}
