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

import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.yb.alloy.dao.CorrectingRecordDao;
import com.sw.mes.yb.alloy.dto.CorrectingRecordDto;
import com.sw.mes.yb.alloy.entity.CorrectingRecord;
import com.sw.mes.yb.alloy.service.CorrectingRecordService;
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.ArrayList;
import java.util.List;


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@Service
@Transactional
public class CorrectingRecordServiceImpl extends BaseServiceImpl<CorrectingRecord> implements CorrectingRecordService {
    CorrectingRecordDao dao;

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

    @Autowired
    private ModelMapper modelMapper;

    @Override
    public void save(List<CorrectingRecordDto> correctingRecordDtoList, Integer id) {
        List<CorrectingRecord> correctingRecordList = fromDto1(correctingRecordDtoList, id);
        super.saveAll(correctingRecordList);
    }

    @Override
    public List<CorrectingRecord> findByAlloyMaterAlloyId(Integer id) {
        return dao.findByAlloyMaterAlloyId(id);
    }

    @Override
    public void deleteByIds(Integer id) {
        List<CorrectingRecord> correctingRecordList = dao.findByAlloyMaterAlloyId(id);
        if (!CollectionUtils.isEmpty(correctingRecordList)) {
            dao.deleteInBatch(correctingRecordList);
        }
    }

    private List<CorrectingRecord> fromDto1(List<CorrectingRecordDto> correctingRecordDtoList, Integer id) {
        ArrayList<CorrectingRecord> recordArrayList = new ArrayList<>();
        for (CorrectingRecordDto correctingRecordDto : correctingRecordDtoList) {
            CorrectingRecord map = modelMapper.map(correctingRecordDto, CorrectingRecord.class);
            map.setAlloyMasterAlloyId(id);
            recordArrayList.add(map);
        }
        return recordArrayList;
    }
}
