/**
 * 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.PickingRequisition;
import com.jeeplus.modules.sg.managementcenter.project.entity.Project;
import com.jeeplus.modules.sg.managementcenter.project.entity.ProjectManager;
import com.jeeplus.modules.sg.managementcenter.project.mapper.ProjectMapper;
import com.jeeplus.modules.sys.entity.User;
import io.swagger.models.auth.In;
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";//项目是否关闭
		Integer earlyMaterial = 0;//是否可以提前领料
		Integer preConstruction = 0;//提前施工
		Project project1 = new Project();
		List<Project> list = projectMapper.findList(project1);
		for(Project project:projects){
			project.setProjectType("");
			status = "0";
			acquisitionStatus="0";//领料状态
			constructionStatus="0";//施工状态
			whetherCloseStatus = "0";//项目是否关闭
			earlyMaterial = 0;//是否可以提前领料
			for (Project project2:list){
				if (project.getRequireName().equals(project2.getRequireName())){
					status = project2.getStatus();
					acquisitionStatus = project2.getAcquisitionStatus();
					constructionStatus = project2.getConstructionStatus();
					whetherCloseStatus = project2.getWhetherCloseStatus();
					earlyMaterial = project2.getEarlyMaterial();//是否可以提前领料
					preConstruction = project2.getPreConstruction();//提前施工
					project.setId(project2.getId());//项目id
					project.setCreateBy(project2.getCreateBy());
					project.setCreateDate(project2.getCreateDate());
					if (!status.equals("3")){
						if (!status.equals("0")||!acquisitionStatus.equals("0")
								||!constructionStatus.equals("0")||earlyMaterial!=0
								||preConstruction!=0){
							project.setType(project2.getType());
							project.setDemandUnit(project2.getDemandUnit());
							project.setBdzName(project2.getBdzName());
							project.setLineName(project2.getLineName());
							project.setRequireName(project2.getRequireName());
							project.setProjectId(project2.getProjectId());
							project.setProjectName(project2.getProjectName());
							project.setProjectNumber(project2.getProjectNumber());
							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.setConstructionUnitManage(project2.getConstructionUnitManage());//施工单位项目经理
							project.setConstructionUnitLeader(project2.getConstructionUnitLeader());//施工单位项目领导
							project.setDesignUnit(project2.getDesignUnit());//设计单位
							project.setDesignUnitManage(project2.getDesignUnitManage());//设计单位角色
							project.setDesignUnitLeader(project2.getDesignUnitLeader());//设计单位领导
							project.setClarificaitonType(project2.getClarificaitonType());//交底类型
							project.setBulidClassificationResons(project2.getBulidClassificationResons());//建设理由分类
							project.setBulbidResons(project2.getBulbidResons());//建设理由
							project.setAsicEquipment(project2.getAsicEquipment());//设备基本情况
							project.setRunTheClass(project2.getRunTheClass());//运行班
							project.setProjectTeam(project2.getProjectTeam());//项目组
							project.setCostEstimate(project2.getCostEstimate());//费用估算
							project.setWhetherRheUpwind(project2.getWhetherRheUpwind());//是否迎风期度夏项目
							project.setSureDaDate(project2.getSureDaDate());//确定下达时间
							project.setTextRemark(project2.getTextRemark());//备注
						}
					}
					project.setPowerLine(project2.getPowerLine());//停电线路
					project.setPowerScope(project2.getPowerScope());//停电范围
					project.setProjectType(project2.getProjectType());
					project.setPowerFlagPackage(project2.getPowerFlagPackage());
					project.setPreliminaryJudgment(project2.getPreliminaryJudgment());
					project.setFlagCivilEngineering(project2.getFlagCivilEngineering());
					project.setSteelTubePole(project2.getSteelTubePole());
					project.setFlagApprovalPlanning(project2.getFlagApprovalPlanning());
					project.setEquCreateType(project2.getEquCreateType());
					project.setEquCreateSize(project2.getEquCreateSize());
					project.setFlagNewEquName(project2.getFlagNewEquName());
					project.setIncludeBdzNews(project2.getIncludeBdzNews());
					project.setFlagNoElectrical(project2.getFlagNoElectrical());
					project.setDrawingsWork(project2.getDrawingsWork());
					project.setFlagAutomation(project2.getFlagAutomation());
					project.setTwoAndThreeRemote(project2.getTwoAndThreeRemote());
					project.setPhoneStyle(project2.getPhoneStyle());
					project.setStayOpenArea(project2.getStayOpenArea());
					project.setRingNetworkCabinet(project2.getRingNetworkCabinet());
					project.setStationDtu(project2.getStationDtu());
					break;
				}
			}
			if (null!=project.getId()){
				project.preUpdate();
			}else {
				project.preInsert();
			}
			if (!"".equals(project.getProjectType())){//项目类型
				project.setStatus(status);
				project.setAcquisitionStatus(acquisitionStatus);
				project.setConstructionStatus(constructionStatus);
				project.setWhetherCloseStatus(whetherCloseStatus);
				project.setEarlyMaterial(earlyMaterial);
				project.setPreConstruction(preConstruction);
			}else {
				project.setStatus("3");
				project.setAcquisitionStatus("3");
				project.setConstructionStatus("3");
				project.setWhetherCloseStatus(whetherCloseStatus);
				project.setEarlyMaterial(3);
				project.setPreConstruction(3);
			}
		}
		if (null!=projects&&projects.size()>0){
			projectMapper.insertList(projects);
		}
	}
		//项目经理导入
		@Transactional(readOnly = false)
		public void saveListManager(List<Project> projects,List<Project> projectList) {
			String status = "0";
			String acquisitionStatus="0";//领料状态
			String constructionStatus="0";//施工状态
			String whetherCloseStatus = "0";//项目是否关闭
			Integer earlyMaterial = 0;//是否可以提前领料
			Integer preConstruction = 0;//提前施工
			for(Project project:projects){
				status = "0";
				acquisitionStatus="0";//领料状态
				constructionStatus="0";//施工状态
				whetherCloseStatus = "0";//项目是否关闭
				earlyMaterial = 0;//是否可以提前领料
				for (Project project2:projectList){
					if (project.getRequireName().equals(project2.getRequireName())){
						status = project2.getStatus();
						acquisitionStatus = project2.getAcquisitionStatus();
						constructionStatus = project2.getConstructionStatus();
						whetherCloseStatus = project2.getWhetherCloseStatus();
						earlyMaterial = project2.getEarlyMaterial();//是否可以提前领料
						preConstruction = project2.getPreConstruction();//提前施工
						if (status.equals("3")){
							status = "0";
						}
						if (acquisitionStatus.equals("3")){
							acquisitionStatus = "0";
						}
						if (constructionStatus.equals("3")){
							constructionStatus = "0";
						}
						if (earlyMaterial==3){
							earlyMaterial = 0;
						}
						if (preConstruction == 3){
							preConstruction = 0;
						}
						project.setId(project2.getId());//项目id
						project.setCreateBy(project2.getCreateBy());
						project.setCreateDate(project2.getCreateDate());
						project.setType(project2.getType());
						project.setDemandUnit(project2.getDemandUnit());
						project.setBdzName(project2.getBdzName());
						project.setLineName(project2.getLineName());
						project.setRequireName(project2.getRequireName());
						project.setProjectId(project2.getProjectId());
						project.setProjectName(project2.getProjectName());
						project.setProjectNumber(project2.getProjectNumber());
						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.setConstructionUnitManage(project2.getConstructionUnitManage());//施工单位项目经理
						project.setConstructionUnitLeader(project2.getConstructionUnitLeader());//施工单位项目领导
						project.setDesignUnit(project2.getDesignUnit());//设计单位
						project.setDesignUnitManage(project2.getDesignUnitManage());//设计单位角色
						project.setDesignUnitLeader(project2.getDesignUnitLeader());//设计单位领导
						project.setClarificaitonType(project2.getClarificaitonType());//交底类型
						project.setBulidClassificationResons(project2.getBulidClassificationResons());//建设理由分类
						project.setBulbidResons(project2.getBulbidResons());//建设理由
						project.setAsicEquipment(project2.getAsicEquipment());//设备基本情况
						project.setRunTheClass(project2.getRunTheClass());//运行班
						project.setProjectTeam(project2.getProjectTeam());//项目组
						project.setCostEstimate(project2.getCostEstimate());//费用估算
						project.setWhetherRheUpwind(project2.getWhetherRheUpwind());//是否迎风期度夏项目
						project.setSureDaDate(project2.getSureDaDate());//确定下达时间
						project.setTextRemark(project2.getTextRemark());//备注
						break;
					}
				}
				if (null!=project.getId()){
					project.preUpdate();
				}
				project.setStatus(status);
				project.setAcquisitionStatus(acquisitionStatus);
				project.setConstructionStatus(constructionStatus);
				project.setWhetherCloseStatus(whetherCloseStatus);
				project.setEarlyMaterial(earlyMaterial);
				project.setPreConstruction(preConstruction);
			}
			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);
	}

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

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

	//根据项目id称查询人员
	public Project findUserById(String projectId) {
		return projectMapper.findUserById(projectId);
	}

	//修改提前施工状态0-未提前施工 2-提前施工
	public void updatePreConstruction(Project project){
		projectMapper.updatePreConstruction(project);
	}

	//根据项目定义号去查找项目
	public List<Project> findListProjectPicking(List<PickingRequisition> pickingRequisitions){
		return projectMapper.findListProjectRequisitions(pickingRequisitions);
	}
	//修改领料状态
	@Transactional(readOnly = false)
	public void getUpdateAcquisitionStatus(String acquisitionStatus,String id){
		projectMapper.updateAcquisitionStatus(acquisitionStatus,id);
	}
	//查找所有用户信息
	public List<String> findAllUser(){
		return projectMapper.findAllUser();
	}
}