/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sg.annualmaterialreport.service;

import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sg.annualmaterialreport.entity.NewMaterialReport;
import com.jeeplus.modules.sg.annualmaterialreport.entity.ReportDetails;
import com.jeeplus.modules.sg.annualmaterialreport.mapper.NewMaterialReportMapper;
import com.jeeplus.modules.sg.annualmaterialreport.mapper.ReportDetailsMapper;
import com.jeeplus.modules.sg.audit.report.mapper.ReportMapper;
import com.jeeplus.modules.sg.managementcenter.materialinfo.entity.MaterialInfo;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 新年度物资上报services
 *
 * @author QY
 */
@Service
@Transactional(readOnly = true)
public class ReportDetailsService extends CrudService<ReportDetailsMapper, ReportDetails> {

	@Autowired
	private ReportDetailsMapper reportDetailsMapper;

	@Autowired
	private NewMaterialReportMapper newMaterialReportMapper;

	@Override
	public ReportDetails get(String id) {
		return super.get(id);
	}

	@Override
	public List<ReportDetails> findList(ReportDetails entity) {
		return super.findList(entity);
	}

	@Override
	public Page<ReportDetails> findPage(Page<ReportDetails> page, ReportDetails entity) {
		return super.findPage(page, entity);
	}

	@Override
	@Transactional(readOnly = false)
	public void save(ReportDetails entity) {
		super.save(entity);
	}


	/**
	 * 在年度物资上报统计表中保存项目信息，在年度上报明细表中保存物资信息
	 */
	@Transactional(readOnly = false)
	public void saveData(MultipartFile file, String reserveProjectName) throws IOException, InvalidFormatException {
		ImportExcel importExcel = new ImportExcel(file, 1, "甲供材清单");
		List<ReportDetails> reportDetailsList = new ArrayList<>();
		int lastRow = importExcel.getLastDataRowNum();

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
		String format = simpleDateFormat.format(new Date());
		//判断项目是否存在，存在删除年度物资上报明细信息，不存在直接插入信息
		NewMaterialReport byName = newMaterialReportMapper.findByName(reserveProjectName);
		if (null == byName) {
			NewMaterialReport newMaterialReport = new NewMaterialReport();
			newMaterialReport.setReserveProjectName(reserveProjectName);
			newMaterialReport.setAttributionYear(format);
			newMaterialReport.preInsert();
			newMaterialReportMapper.insert(newMaterialReport);
		} else {
			//项目存在，删除该项目的详细信息，重新添加
			reportDetailsMapper.delByName(reserveProjectName);
		}
		for (int i = 1; i < lastRow; i++) {
			Row row = importExcel.getRow(i);
			ReportDetails reportDetails = new ReportDetails();
			String cellValue = (String) importExcel.getCellValue(row, 4);
			if (null != cellValue && !"".equals(cellValue)) {
				reportDetails.setMaterialCode(cellValue);
				reportDetails.setMaterialDescription((String) importExcel.getCellValue(row, 5));
				reportDetails.setNewExtendedCodeBar((String) importExcel.getCellValue(row, 6));
				reportDetails.setExtendedDescription((String) importExcel.getCellValue(row, 7));
				reportDetails.setCompany((String) importExcel.getCellValue(row, 8));
				reportDetails.setUnitPrice((String) importExcel.getCellValue(row, 9));
				reportDetails.setQuantity((String) importExcel.getCellValue(row, 10));
				reportDetails.setReserveProjectName(reserveProjectName);
				reportDetails.preInsert();
				reportDetailsList.add(reportDetails);
			}
		}
		reportDetailsMapper.insertList(reportDetailsList);
/*		String projectName = ((String) obj).trim();
		MaterialProject project = mapper.findByReserveName(projectName);
		//若年度物资上报统计表中存在该项目 则删除对应物资明细数据，否则在上报统计表中新增一条项目数据
		if(project!=null){
			mapper.deleteByPro(projectName);
		}else{
			MaterialProject materialProject = new MaterialProject();
			materialProject.setReserveProjectName(projectName);
			mapper.sa(materialProject);
		}
		//保存物资明细数据
		ImportExcel pick = new ImportExcel(file,1,"领料清单");
		List<MaterialInfo> materialInfos = MaterialInfoUtil.getData(pick,projectName);   //获取领料清单数据
		List<MaterialInfo> list = MaterialInfoUtil.combineData(materialInfos);      //合并数据
		saveList(list);
		insertOldList(list);*/
	}

	@Transactional(readOnly = false)
	public void delByName(NewMaterialReport newMaterialReport) {
		mapper.delByName(newMaterialReport.getReserveProjectName());
	}

	public List<ReportDetails> exportList(List<NewMaterialReport> list) {
		return mapper.exportList(list);
	}
}