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


import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.yb.alloy.dao.InspectionListDao;
import com.sw.mes.yb.alloy.dto.InspectionListDto;
import com.sw.mes.yb.alloy.entity.InspectionList;
import com.sw.mes.yb.alloy.service.InspectionListService;

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 InspectionListServiceImpl extends BaseServiceImpl<InspectionList> implements InspectionListService {
    @Autowired
    private ModelMapper modelMapper;
    InspectionListDao dao;

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

    @Override
    public void save(List<InspectionListDto> inspectionListDtoList, Integer id) {
        List<InspectionList> inspectionLists = fromDto(inspectionListDtoList, id);
        super.saveAll(inspectionLists);
    }

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

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

    private List<InspectionList> fromDto(List<InspectionListDto> inspectionListDtoList, Integer id) {
        ArrayList<InspectionList> inspectionList = new ArrayList<>();
        for (InspectionListDto inspectionListDto : inspectionListDtoList) {
            InspectionList map = modelMapper.map(inspectionListDto, InspectionList.class);
            map.setAlloyMasterAlloyId(id);
            inspectionList.add(map);
        }
        return inspectionList;
    }
}
