/**
 * 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.ArrayList;
import java.util.List;
import java.util.UUID;

import com.jeeplus.common.json.AjaxJson;
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) {
		Boolean flag = false;
		String statusString = "0";
		Integer statusInt = 0;
		Project project1 = new Project();
		List<Project> list = projectMapper.findList(project1);
		List<Project> updateList = new ArrayList<Project>();//添加状态改变的
		List<Project> insertList = new ArrayList<Project>();//添加状态不变的
		for (Project projectA:projects){
			flag = true;
			for (Project projectB:list){
				if (projectA.getRequireName().equals(projectB.getRequireName())){
					projectA.setId(projectB.getId());
					projectA.setStatus(projectB.getStatus());
					projectA.setAcquisitionStatus(projectB.getAcquisitionStatus());
					projectA.setWhetherCloseStatus(projectB.getWhetherCloseStatus());
					projectA.setConstructionStatus(projectB.getConstructionStatus());
					projectA.setEarlyMaterial(projectB.getEarlyMaterial());
					projectA.setPreConstruction(projectB.getPreConstruction());
					projectA.setRezkey(projectB.getRezkey());
					updateList.add(projectA);
					flag = false;
					break;
				}
			}
			if (flag){
				projectA.setStatus(statusString);
				projectA.setAcquisitionStatus(statusString);
				projectA.setConstructionStatus(statusString);
				projectA.setWhetherCloseStatus(statusString);
				projectA.setEarlyMaterial(statusInt);
				projectA.setPreConstruction(statusInt);
				projectA.setRezkey("0");
				projectA.preInsert();
				insertList.add(projectA);
			}
		}
		if (updateList.size()>0){
			for (Project project:updateList){
				project.preUpdate();
				projectMapper.update(project);
			}
		}
		if (insertList.size()>0){
			projectMapper.insertList(insertList);
		}
	}

	//根据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();
	}
}