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

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sg.financial.settlement.util.ExcelWriter;
import com.jeeplus.modules.sg.managementcenter.materialinfo.entity.MaterialInfo;
import com.jeeplus.modules.sg.managementcenter.materialinfo.mapper.MaterialInfoMapper;
import com.jeeplus.modules.sg.managementcenter.materialinfo.util.MaterialInfoUtil;
import com.jeeplus.modules.sg.managementcenter.materialinfo.util.MaterialReplaceUtil;
import com.jeeplus.modules.sg.managementcenter.materialproject.entity.MaterialProject;
import com.jeeplus.modules.sg.managementcenter.materialproject.service.MaterialProjectService;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
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 javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


@Service
@Transactional(readOnly = true)
public class MaterialInfoService extends CrudService<MaterialInfoMapper, MaterialInfo> {

	@Autowired
	private MaterialInfoMapper materialInfoMapper;
	@Autowired
	private MaterialProjectService materialProjectService;

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

	@Override
	public List<MaterialInfo> findList(MaterialInfo materialInfo) {
		return super.findList(materialInfo);
	}


	@Override
	public Page<MaterialInfo> findPage(Page<MaterialInfo> page, MaterialInfo materialInfo) {
		return super.findPage(page, materialInfo);
	}
	
	@Transactional(readOnly = false)
	@Override
	public void save(MaterialInfo materialInfo) {
		super.save(materialInfo);
	}

	@Transactional(readOnly = false)
	public void saveList(List<MaterialInfo> list) {
		for(MaterialInfo maintainData : list){
			maintainData.preInsert();
		}
		materialInfoMapper.insertList(list);
	}

	@Transactional(readOnly = false)
	public void insertOldList(List<MaterialInfo> list) {
		for(MaterialInfo maintainData : list){
			maintainData.preInsert();
		}
		materialInfoMapper.insertOldList(list);
	}
	
	@Transactional(readOnly = false)
	@Override
	public void delete(MaterialInfo materialInfo) {
		super.delete(materialInfo);
	}

	/**
	 *根据项目名称删除物料信息
	 */
	@Transactional(readOnly = false)
	public void deleteByPro(String projectName) {
		materialInfoMapper.deleteByPro(projectName);
	}



//	/**
//	 *物料信息合并保存
//	 */
//    @Transactional(readOnly = false)
//	public void export(MultipartFile file,String batch,String itemBatch) throws IOException, InvalidFormatException {
//		ImportExcel mo = new ImportExcel(file,1,"模块表");
//		ImportExcel importExcel = new ImportExcel(file,1,"领料清单");
//			Row row = mo.getRow(0);
//		String projectNam1 = (String) mo.getCellValue(row, 1);
//		String projectName = projectNam1.trim();
//		MaterialProject materialProject1 = new MaterialProject();
//		materialProject1.setReserveProjectName(projectName);
//		List<MaterialProject> materialProjects = materialProjectService.findList(materialProject1);
//		if (materialProjects != null && materialProjects.size() > 0) {
//			materialInfoMapper.deleteByPro(projectName);
//			materialInfoMapper.deleteByOld(projectName);
//		} else {
//			MaterialProject materialProject = new MaterialProject();
//			materialProject.setReserveProjectName(projectName);
//			materialProjectService.save(materialProject);
//		}
//		List<MaterialInfo> materialInfos = getData(importExcel,projectName);   //sheet页数据
//		List<MaterialInfo> list = getNewData(materialInfos);      //合并后数据
//		saveList(list);
//		insertOldList(list);
//	}

	/**
	 *在年度物资上报统计表中保存项目信息，在年度上报明细表中保存物资信息
	 */
	@Transactional(readOnly = false)
	public void saveData(MultipartFile file,String batch,String itemBatch) throws IOException, InvalidFormatException {
		ImportExcel module = new ImportExcel(file,1,"模块表");
		Row row = module.getRow(0);
		Object obj = module.getCellValue(row, 1);
		if(obj==null){
			throw new RuntimeException("找不到项目名称！");
		}
		String projectName = ((String) obj).trim();
		MaterialProject project = materialProjectService.findByReserveName(projectName);
		//若年度物资上报统计表中存在该项目 则删除对应物资明细数据，否则在上报统计表中新增一条项目数据
		if(project!=null){
			materialInfoMapper.deleteByPro(projectName);
			materialInfoMapper.deleteByOld(projectName);
		}else{
			MaterialProject materialProject = new MaterialProject();
			materialProject.setReserveProjectName(projectName);
			materialProjectService.save(materialProject);
		}
		//保存物资明细数据
		ImportExcel pick = new ImportExcel(file,1,"领料清单");
		List<MaterialInfo> materialInfos = MaterialInfoUtil.getData(pick,projectName);   //获取领料清单数据
		List<MaterialInfo> list = MaterialInfoUtil.combineData(materialInfos);      //合并数据
		saveList(list);
		insertOldList(list);
	}

	/**
	 *根据项目名称获取物料信息
	 */
	public List<MaterialInfo> findByBatch(List<MaterialProject> list){
		List<MaterialInfo> list1 = materialInfoMapper.findByList(list);
		 List<MaterialInfo> list2 = MaterialInfoUtil.combineData(list1); //合并数据
		Collections.sort(list2, new Comparator<MaterialInfo>() {
			@Override
			public int compare(MaterialInfo o1, MaterialInfo o2) {
				return o1.getMaterialCode().compareTo(o2.getMaterialCode());
			}
		});
		 return list2;
	}
//
//	/**
//	 * 对比替换数据
//	 * @param list
//	 * @return
//	 */
//	private List<MaterialInfo> replaceDate(List<MaterialInfo> list) {
//		for(MaterialInfo materialInfo : list){
//			String code = materialInfo.getMaterialCode();
//			String name = materialInfo.getMaterialName();
//			String des = materialInfo.getExtendDescription();
//			for(int i=0;i< MaterialReplaceUtil.exportCode.length;i++){
//				if(code.equals(MaterialReplaceUtil.exportCode[i]) && name.equals(MaterialReplaceUtil.exportName[i])
//						&& des.equals(MaterialReplaceUtil.exportDes[i])){
//					materialInfo.setMaterialCode(MaterialReplaceUtil.replaceCode[i]);
//					materialInfo.setMaterialName(MaterialReplaceUtil.replaceName[i]);
//					materialInfo.setExtendDescription(MaterialReplaceUtil.replaceDes[i]);
//					break;
//				}
//			}
//		}
//		return list;
//	}
//
//	/**
//	 * 数据和标准库比对
//	 * @param list
//	 * @param standardList
//	 * @return
//	 */
//	private List<MaterialInfo> compareData(List<MaterialInfo> list, List<MaterialInfo> standardList) {
//		for(MaterialInfo materialInfo : list){
//			String code = materialInfo.getMaterialCode();
//			String name = materialInfo.getMaterialName();
//			String des = materialInfo.getExtendDescription();
//			materialInfo.setState("0");
//			for(MaterialInfo materialInfo1 : standardList){
//				String code1 = materialInfo1.getMaterialCode();
//				String name1 = materialInfo1.getMaterialName();
//				String des1 = materialInfo1.getExtendDescription();
//				if(code.equals(code1) && name.equals(name1) && des.equals(des1)){
//					materialInfo.setState("1");
//					break;
//				}
//			}
//		}
//		return list;
//	}



}