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


import com.alibaba.excel.EasyExcel;
import com.sw.mes.base.factory.utils.excel.EasyExcelUtil;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.exception.BusinessException;
import com.sw.mes.core.utils.DynamicSpecifications;
import com.sw.mes.yb.check.dao.CrystallonDataDao;
import com.sw.mes.yb.check.dto.CrystallonDataDto;
import com.sw.mes.yb.check.entity.CrystallonData;
import com.sw.mes.yb.check.service.CrystallonDataService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@Service
@Transactional
public class CrystallonDataServiceImpl extends BaseServiceImpl<CrystallonData> implements CrystallonDataService {
    CrystallonDataDao dao;

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

    @Autowired
    ModelMapper modelMapper;

    @Override
    public CrystallonData findByCrystallonNo(String crystallonNo) {
        List<CrystallonData> crystallonDataList = dao.findByCrystallonNo(crystallonNo);
        if (null != crystallonDataList && crystallonDataList.size() > 0) {
            return crystallonDataList.get(0);
        }
        return null;
    }

    @Override
    public List<CrystallonData> findByCrystalNo(String crystalNo) {
        return dao.findByCrystalNo(crystalNo);
    }

    @Override
    public void save(List<CrystallonData> crystallonDataList) {
        for (CrystallonData crystallonData : crystallonDataList) {
            CrystallonData crystallonData1 = findByCrystallonNo(crystallonData.getCrystallonNo());
            if (null == crystallonData1) {
                saveOrUpdate(crystallonData);
            } else {
                throw new BusinessException("籽晶编号重复：" + crystallonData.getCrystallonNo());
            }

        }
    }

    @Override
    public void exportExcel(HttpServletRequest request, HttpServletResponse resp) {
        Specification<CrystallonData> specification = DynamicSpecifications.bySearchFilter(request, CrystallonData.class);
        List<CrystallonData> crystallonDataList = findByExampleSort(specification, null);
        try {
            List<CrystallonDataDto> crystallonDataDtoList = new ArrayList<>();
            if (!crystallonDataList.isEmpty()) {
                crystallonDataList.stream().forEach(crystallonData -> {
                    CrystallonDataDto dto = modelMapper.map(crystallonData, CrystallonDataDto.class);
                    crystallonDataDtoList.add(dto);
                });
            }
            EasyExcelUtil.setResponse(resp);
            EasyExcel.write(resp.getOutputStream(), CrystallonDataDto.class).sheet("籽晶检验记录").doWrite(crystallonDataDtoList);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
