/**
 * 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.mapper.MaterialInfoMapper;
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;
	@Autowired
	private MaterialInfoMapper materialInfoMapper;

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

	public MaterialProject findByReserveName(String reserveName){
		return mapper.findByReserveName(reserveName);
	}

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

	public List<MaterialProject> findByList(List<MaterialProject> list){
		return materialProjectMapper.findByList(list);
	}
	
	@Override
	public Page<MaterialProject> findPage(Page<MaterialProject> page, MaterialProject materialProject) {
		return super.findPage(page, materialProject);
	}
	
	@Override
	@Transactional(readOnly = false)
	public void save(MaterialProject materialProject) {
		super.save(materialProject);
	}


	@Override
	@Transactional(readOnly = false)
	public void delete(MaterialProject materialProject) {
		//项目项目信息（包含多次涉电的信息）
		super.delete(materialProject);
		String reserveProjectName = materialProject.getReserveProjectName();
		//删除年度物资上报信息-中间态
		materialInfoMapper.deleteByPro(reserveProjectName);
		//删除年度物资上报信息
		materialInfoMapper.deleteByOld(reserveProjectName);
		/**
		 * 删除项目前期基础信息（xm_project_basicinformation）
		 */
		materialProjectMapper.deleteProjectBas(reserveProjectName);
		/**
		 * 删除施工任务分配信息（xm_project_assignment）
		 * */
		materialProjectMapper.deleteProjectAss(reserveProjectName);
		/**
		 * 删除交底计划安排信息（xm_project_disclosureplan）
		 * */
		materialProjectMapper.deleteProjectDis(reserveProjectName);
		/**
		 * 删除施工交底流程信息(xm_construction_clarificaiton)
		 * */
		materialProjectMapper.deleteConstruction(reserveProjectName);
		/**
		 * 删除拆旧表信息(xm_tear_down)
		 */
		materialProjectMapper.deleteXmTearDown(reserveProjectName);
		/**
		 * 删除土建工程量信息（xm_engineering_amount）
		 */
		materialProjectMapper.deleteEngineeringAmount(reserveProjectName);
		/**
		 * 删除其它工程量信息（xm_other_amount）
		 */
		materialProjectMapper.deleteOtherAmount(reserveProjectName);
		/**
		 * 删除物资领料流程信息(md_acquisition_process)
		 */
		materialProjectMapper.deleteAcqProcess(reserveProjectName);
		/**
		 * 删除土建流程信息(xm_nonpower_process)
		 */
		materialProjectMapper.deleteNonProcess(reserveProjectName);
		/**
		 * 删除施工验收（电气）流程信息(sg_construction_process)
		 */
		materialProjectMapper.deleteConProcess(reserveProjectName);
		/**
		 * 删除转资流程信息(xm_fund_process)
		 */
		materialProjectMapper.deleteFundProcess(reserveProjectName);
		/**
		 * 结算验收流程信息(xm_settlement_process)
		 */
		materialProjectMapper.deleteSettProcess(reserveProjectName);
		/**
		 * 删除施工验收流程-上传签字版政策处理清单/缴费单(xm_construction_upload)
		 */
		materialProjectMapper.deleteConUpload(reserveProjectName);
		/**
		 * 删除项目上传信息（xm_upload_images）
		 */
		materialProjectMapper.deleteUploadImages(reserveProjectName);
	}

	/**
	 * 重置项目
	 */
	@Transactional(readOnly = false)
	public void resetProject(MaterialProject materialProject) {
		materialProjectMapper.resetProject(materialProject);
		String reserveProjectName = materialProject.getReserveProjectName();
		/**
		 * 删除项目前期基础信息（xm_project_basicinformation）
		 */
		materialProjectMapper.deleteProjectBas(reserveProjectName);
		/**
		 * 删除施工任务分配信息（xm_project_assignment）
		 * */
		materialProjectMapper.deleteProjectAss(reserveProjectName);

		/**
		 * 删除交底计划安排信息（xm_project_disclosureplan）
		 * */
		materialProjectMapper.deleteProjectDis(reserveProjectName);

		/**
		 * 删除施工交底流程信息(xm_construction_clarificaiton)
		 * */
		materialProjectMapper.deleteConstruction(reserveProjectName);
		/**
		 * 删除拆旧表信息(xm_tear_down)
		 */
		materialProjectMapper.deleteXmTearDown(reserveProjectName);
		/**
		 * 删除土建工程量信息（xm_engineering_amount）
		 */
		materialProjectMapper.deleteEngineeringAmount(reserveProjectName);
		/**
		 * 删除其它工程量信息（xm_other_amount）
		 */
		materialProjectMapper.deleteOtherAmount(reserveProjectName);
		/**
		 * 删除物资领料流程信息(md_acquisition_process)
		 */
		materialProjectMapper.deleteAcqProcess(reserveProjectName);
		/**
		 * 删除土建流程信息(xm_nonpower_process)
		 */
		materialProjectMapper.deleteNonProcess(reserveProjectName);
		/**
		 * 删除施工验收（电气）流程信息(sg_construction_process)
		 */
		materialProjectMapper.deleteConProcess(reserveProjectName);
		/**
		 * 删除转资流程信息(xm_fund_process)
		 */
		materialProjectMapper.deleteFundProcess(reserveProjectName);
		/**
		 * 结算验收流程信息(xm_settlement_process)
		 */
		materialProjectMapper.deleteSettProcess(reserveProjectName);
		/**
		 * 删除施工验收流程-上传签字版政策处理清单/缴费单(xm_construction_upload)
		 */
		materialProjectMapper.deleteConUpload(reserveProjectName);
		/**
		 * 删除项目上传信息（xm_upload_images）
		 */
		materialProjectMapper.deleteUploadImages(reserveProjectName);
	}

	/**
	 * 文件导入
	 * */
	@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();
		if (StringUtils.isNotBlank(reserveCode)){
			String[] reserveCodes= reserveCode.split("\\s+");
			if(reserveCodes.length == 1){
				materialProject.setReserveCode(reserveCodes[0]);
			}else {
				materialProject.setReserveCodes(reserveCodes);
				materialProject.setReserveCode("");
			}
		}
		materialProject1.setProjectStatus("");
		materialProject1.setReserveProjectName(materialProject.getReserveProjectName());
		materialProject1.setProjectName(materialProject.getProjectName());
		materialProject1.setReserveCode(materialProject.getReserveCode());
		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.getReserveCode()&&!materialProject.getReserveCode().equals("")){
			strProject+=materialProject.getReserveCode()+",";
		}
		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");
		}
	}

	public List<String> findAllReserveName() {
		return materialProjectMapper.findAllReserveName();
	}

	@Transactional(readOnly = false)
	public void updateList(List<MaterialProject> materialProjectList) {
		materialProjectMapper.updateList(materialProjectList);
	}


	//根据项目名称进行查找
	public List<MaterialProject> findJugdeReserve(MaterialProject materialProject){
		return materialProjectMapper.findJugdeReserve(materialProject);
	}


}