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

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sg.managementcenter.constructionProject.entity.ConstructionProject;
import com.jeeplus.modules.sg.managementcenter.constructionProject.mapper.ConstructionProjectMapper;
import com.jeeplus.modules.sg.managementcenter.constructionProject.util.RecursiveUtil;
import com.jeeplus.modules.sg.managementcenter.constructionProject.util.TreeTableUtil;
import com.jeeplus.modules.sg.managementcenter.project.entity.Project1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Transactional(readOnly = true)
public class ConstructionProjectService extends CrudService<ConstructionProjectMapper, ConstructionProject> {

	@Autowired
	private ConstructionProjectMapper constructionProjectMapper;

	public ConstructionProject get(String id) {
		return super.get(id);
	}
	
	public List<ConstructionProject> findList(List<Project1> allListProject, ConstructionProject constructionProject) {
		return super.findList(constructionProject);
	}

	//修改项目
	@Transactional(readOnly = false)
	public void updateListProject(List<Project1> projects){
		constructionProjectMapper.updateListProject(projects);
	}

    //查询安排计划项目
    public List<Project1> findListAllProject(List<Project1> allListProject, ConstructionProject constructionProject){
        String ids = "";
        List<Project1> projects = null;
        if (null!=constructionProject && null!=constructionProject.getProjectNumber()){
            ids = constructionProject.getProjectNumber();
        }
        if (!"".equals(ids)){
            List<String> stringList = new ArrayList<String>();
            String[] split = ids.split(",");
            for (String str : split){
                stringList.add(str);
            }
            projects = RecursiveUtil.getAllProjectRecursive(allListProject, stringList);
        }
        return projects;
    }

    public List<String> getProjectNumber(ConstructionProject constructionProject){
		String ids = "";
		List<String> stringList = null;
		if (null!=constructionProject && null!=constructionProject.getProjectNumber()){
			ids = constructionProject.getProjectNumber();
		}
		if (!"".equals(ids)){
			stringList = new ArrayList<String>();
			String[] split = ids.split(",");
			for (String str : split){
				stringList.add(str);
			}
		}
		return stringList;
	}
	//查询安排计划项目getAllProjectNo
	public List<Project1> getAllProjectNo(List<Project1> allListProject, ConstructionProject constructionProject){
		String ids = "";
		List<Project1> projects = null;
		if (null!=constructionProject && null!=constructionProject.getProjectNumber()){
			ids = constructionProject.getProjectNumber();
		}
		if (!"".equals(ids)){
			List<String> stringList = new ArrayList<String>();
			String[] split = ids.split(",");
			for (String str : split){
				stringList.add(str);
			}
			projects = RecursiveUtil.getAllProjectNo(allListProject, stringList);
		}
		return projects;
	}

	public List<Project1> allListProject(ConstructionProject constructionProject){
		Project1 project = new Project1();
		project.setRezkey("0");
		List<Project1> allListProject = constructionProjectMapper.findAllListProject(project);
		return allListProject;
	}

	//查询安排计划项目
	public List<Project1> findListProject(ConstructionProject constructionProject){
        List<String> stringList = new ArrayList<String>();
		String ids = "";
		List<Project1> projects = null;
		if (null!=constructionProject && null!=constructionProject.getProjectNumber()){
			ids = constructionProject.getProjectNumber();
		}
		if (!"".equals(ids)){
			String[] split = ids.split(",");
			for (String str : split){
				stringList.add(str);
			}
			projects = constructionProjectMapper.findListProject(stringList);
		}
		return projects;
	}

	public Page<ConstructionProject> findPage(Page<ConstructionProject> page, ConstructionProject constructionProject) {
		return super.findPage(page, constructionProject);
	}
	
	@Transactional(readOnly = false)
	public void save(ConstructionProject constructionProject) {
		super.save(constructionProject);
	}

	//文件导入
	@Transactional(readOnly = false)
	public void savaList(List<ConstructionProject> list){
		for (ConstructionProject constructionProject:list){
			constructionProject.preInsert();
		}
		constructionProjectMapper.insertList(list);
	}
	//修改计划安排事件状态
	@Transactional(readOnly = false)
	public void updateStatus(ConstructionProject constructionProject){
		constructionProjectMapper.updateStatus(constructionProject);
	}

	//安排具体时间
	@Transactional(readOnly = false)
	public void updateSpecificTime(ConstructionProject constructionProject,Date historyTime){
		String status = TreeTableUtil.getConstructionId(constructionProject);
		if ("".equals(status)){
			constructionProject.preUpdate();
			constructionProjectMapper.updateSpecificTime(constructionProject);
		}
		if ("line".equals(status)){
			Date specificTime = constructionProject.getSpecificTime();
			constructionProject.setId("");
			constructionProject.setArrangeTime(null);
			List<ConstructionProject> list = findList(constructionProject);
			for (ConstructionProject constructionProject1:list){
				constructionProject1.preUpdate();
				constructionProject1.setSpecificTime(specificTime);
			}
			constructionProjectMapper.updateSpecificLine(list);
		}
	}

}