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

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sg.managementcenter.materialinfo.service.MaterialInfoService;
import com.jeeplus.modules.sg.managementcenter.materialproject.entity.MaterialProject;
import com.jeeplus.modules.sg.managementcenter.materialproject.mapper.MaterialProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional(readOnly = true)
public class MaterialProjectService extends CrudService<MaterialProjectMapper, MaterialProject> {
	@Autowired
	private MaterialProjectMapper materialProjectMapper;
	@Autowired
	private MaterialInfoService materialInfoService;

	public MaterialProject get(String id) {
		return super.get(id);
	}
	
	public List<MaterialProject> findList(MaterialProject materialProject) {
		return super.findList(materialProject);
	}

	public List<MaterialProject> findBatch(){
		return materialProjectMapper.findBatch();
	}

	public List<MaterialProject> findByList(List<MaterialProject> list){
		return materialProjectMapper.findByList(list);
	}
	
	public Page<MaterialProject> findPage(Page<MaterialProject> page, MaterialProject materialProject) {
		return super.findPage(page, materialProject);
	}
	
	@Transactional(readOnly = false)
	public void save(MaterialProject materialProject) {
		super.save(materialProject);
	}
	
	@Transactional(readOnly = false)
	public void delete(MaterialProject materialProject) {
		super.delete(materialProject);
		materialInfoService.deleteByPro(materialProject.getReserveProjectName());
	}
	//文件导入
	@Transactional(readOnly = false)
	public void savaList(List<MaterialProject> list){
		for (MaterialProject materialProject:list){
			materialProject.preInsert();
		}
		materialProjectMapper.insertList(list);
	}
	//文件导入
	@Transactional(readOnly = false)
	public void replaceList(List<MaterialProject> list){
		for (MaterialProject materialProject:list){
			materialProject.preInsert();
		}
		materialProjectMapper.replaceList(list);
	}
	public void getSearch(MaterialProject materialProject,MaterialProject materialProject1){
		String reserveCode = materialProject.getReserveCode();
		String projectCode = materialProject.getProjectCode();
		if (StringUtils.isNotBlank(reserveCode)){
			String[] reserveCodes= reserveCode.split("\\s+");
			if(reserveCodes.length == 1){
				materialProject.setReserveCode(reserveCodes[0]);
			}else {
				materialProject.setReserveCodes(reserveCodes);
				materialProject.setReserveCode("");
			}
		}
		if (StringUtils.isNotBlank(projectCode)){
			String[] projectCodes = projectCode.split("\\s+");
			if(projectCodes.length == 1){
				materialProject.setProjectCode(projectCodes[0]);
			}else {
				materialProject.setProjectCodes(projectCodes);
				materialProject.setProjectCode("");
			}
		}
		materialProject1.setProjectStatus("");
		materialProject1.setReserveProjectName(materialProject.getReserveProjectName());
		materialProject1.setProjectName(materialProject.getProjectName());
		materialProject1.setProjectCode(materialProject.getProjectCode());
		materialProject1.setReserveCode(materialProject.getReserveCode());
		materialProject1.setProjectAttribute(materialProject.getProjectAttribute());
		materialProject1.setProjectDefine(materialProject.getProjectDefine());
		materialProject1.setProjectBatch(materialProject.getProjectBatch());
		materialProject1.setReserveCodes(materialProject.getReserveCodes());
		materialProject1.setProjectCodes(materialProject.getProjectCodes());
		String strProject = "";
		if (null!=materialProject.getReserveProjectName()&&!materialProject.getReserveProjectName().equals("")){
			strProject+=materialProject.getReserveProjectName()+",";
		}
		if (null!=materialProject.getProjectName()&&!materialProject.getProjectName().equals("")){
			strProject+=materialProject.getReserveProjectName()+",";
		}
		if (null!=materialProject.getProjectCode()&&!materialProject.getProjectCode().equals("")){
			strProject+=materialProject.getProjectCode()+",";
		}
		if (null!=materialProject.getReserveCode()&&!materialProject.getReserveCode().equals("")){
			strProject+=materialProject.getReserveCode()+",";
		}
		if (null!=materialProject.getProjectAttribute()&&!materialProject.getProjectAttribute().equals("")){
			strProject+=materialProject.getProjectAttribute()+",";
		}
		if (null!=materialProject.getProjectDefine()&&!materialProject.getProjectDefine().equals("")){
			strProject+=materialProject.getProjectDefine()+",";
		}
		if (null!=materialProject.getProjectBatch()&&!materialProject.getProjectBatch().equals("")){
			strProject+=materialProject.getProjectBatch()+",";
		}
		if (null!=materialProject.getProjectCodes()){
			strProject+=materialProject.getProjectCodes();
		}
		if (null!=materialProject.getReserveCodes()){
			strProject+=materialProject.getReserveCodes();
		}
		String[] split = strProject.split(",");
		if (split.length>1){
			materialProject.setProjectStatus("0");
			materialProject1.setProjectStatus("0");
		}
	}

}