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

import java.util.List;
import java.util.UUID;

import com.jeeplus.modules.sg.financial.settlement.entity.MaintainData;
import com.jeeplus.modules.sg.managementcenter.project.entity.Project;
import com.jeeplus.modules.sg.managementcenter.project.mapper.ProjectMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;

/**
 * 请假表单Service
 * @author lgf
 * @version 2018-06-12
 */
@Service
@Transactional(readOnly = true)
public class ProjectService extends CrudService<ProjectMapper, Project> {
    @Autowired
	private ProjectMapper projectMapper;

	public Project get(String id) {
		Project project = super.get(id);
		return project;
	}
	
	public List<Project> findList(Project project) {
		return super.findList(project);
	}
	
	public Page<Project> findPage(Page<Project> page, Project project) {
		return super.findPage(page, project);
	}
	
	@Transactional(readOnly = false)
	public void save(Project project) {
		super.save(project);
	}
	
	@Transactional(readOnly = false)
	public void delete(Project project) {
		super.delete(project);
	}

	@Transactional
	public void updateStatus(Project project){
	    projectMapper.updateStatus(project);
    }

	public List<Project> findListById(String[] proListId) {
		return projectMapper.findListById(proListId);
	}

	/**
	 * 根据项目需求名称修改项目施工交底状态0未发起施工交底，1施工交底过程中，2完成施工交底
	 */
	@Transactional(readOnly = false)
	public void updateStatusBy(String status, String procInsId) {
		projectMapper.updateStatusBy(status,procInsId);
	}

	/**
	 * 根据项目需求名称修改项目关闭状态
	 */
	@Transactional(readOnly = false)
	public void updateStatusByClose(String close,String procInsId){
		projectMapper.updateStatusByClose(close,procInsId);
	}

	@Transactional(readOnly = false)
	public void saveList(List<Project> projects) {
		String status = "0";
		String acquisitionStatus="0";//领料状态
		String constructionStatus="0";//施工状态
		String whetherCloseStatus = "0";//项目是否关闭
		Project project1 = new Project();
		List<Project> list = projectMapper.findList(project1);
		for(Project project:projects){
			status = "0";
			acquisitionStatus="0";//领料状态
			constructionStatus="0";//施工状态
			whetherCloseStatus = "0";//项目是否关闭
			for (Project project2:list){
				if (project.getRequireName().equals(project2.getRequireName())){
					status = project2.getStatus();
					acquisitionStatus = project2.getAcquisitionStatus();
					constructionStatus = project2.getConstructionStatus();
					whetherCloseStatus = project2.getWhetherCloseStatus();
					project.setId(project2.getId());//项目id
					project.setCreateBy(project2.getCreateBy());
					project.setCreateDate(project2.getCreateDate());
					project.setProjectManageRole(project2.getProjectManageRole());//项目经理角色
					project.setDistributionEngineering(project2.getDistributionEngineering());//配网工程专职角色
					project.setDistributionMaterials(project2.getDistributionMaterials());//配网物资专职角色
					project.setDistributionSystem(project2.getDistributionSystem());//配网系统专职角色
					project.setDistributionPolicy(project2.getDistributionPolicy());//配网政策处理专职角色
					project.setManagementLeader(project2.getManagementLeader());//项目管理中心领导角色
					project.setRunUnit(project2.getRunUnit());//运行单位
					project.setRunUnitRole(project2.getRunUnitRole());//运行单位角色
					project.setThroughResearchRole(project2.getThroughResearchRole());//经研所角色
					project.setConstructionUnit(project2.getConstructionUnit());//施工单位
					project.setConstructionUnitRole(project2.getConstructionUnitRole());//施工单位角色
					project.setDesignUnit(project2.getDesignUnit());//设计单位
					project.setDesignUnitRole(project2.getDesignUnitRole());//设计单位角色
					project.setClarificaitonType(project2.getClarificaitonType());//交底类型
					break;
				}
			}
			if (null!=project.getId()){
				project.preUpdate();
			}else {
				project.preInsert();
			}

			project.setStatus(status);
			project.setAcquisitionStatus(acquisitionStatus);
			project.setConstructionStatus(constructionStatus);
			project.setWhetherCloseStatus(whetherCloseStatus);
		}
		if (null!=projects&&projects.size()>0){
			projectMapper.insertList(projects);
		}
	}
	//根据id查找项目
	public List<Project> getListProject(List<Project> projects){
		return  projectMapper.findListProject(projects);
	}

	public Project findUser(String procInsId) {
		return projectMapper.findUser(procInsId);
	}
}