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

import com.jeeplus.modules.sg.managementcenter.activiti.entity.Construction;
import com.jeeplus.modules.sg.managementcenter.activiti.entity.EngineeringAmount;
import com.jeeplus.modules.sg.managementcenter.activiti.entity.OtherAmount;
import com.jeeplus.modules.sg.managementcenter.activiti.entity.Quantity;
import com.jeeplus.modules.sg.managementcenter.materialinfo.entity.MaterialInfo;
import com.jeeplus.modules.sg.managementcenter.project.entity.*;
import com.jeeplus.modules.sg.managementcenter.project.mapper.ProjectMapper;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.entity.PersonnelTurnover;
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;

import java.util.List;

/**
 * 项目基础信息表单
 *
 * @author lem
 * @version 2020-04-29
 */
@Service
@Transactional(readOnly = true)
public class ProjectService extends CrudService<ProjectMapper, Project> {
    @Autowired
    private ProjectMapper projectMapper;

    @Override
    public Page<Project> findPage(Page<Project> page, Project entity) {
        return super.findPage(page, entity);
    }

    public Page<Project> findBasicPage(Page<Project> page, Project project) {
        dataRuleFilter(project);
        project.setPage(page);
        page.setList(mapper.findBasicList(project));
        return page;
    }

    public List<Project> findBasicList(Project project){
        return mapper.findBasicList(project);
    }

    public Page<Project> findAssPage(Page<Project> page, Project project) {
        dataRuleFilter(project);
        project.setPage(page);
        page.setList(mapper.findAssList(project));
        return page;
    }

    public List<Project> findAssList(Project project){
        return mapper.findAssList(project);
    }

    /**
     * 交底计划安排
     */
    public Page<Project> findPlanPage(Page<Project> page, Project project) {
        dataRuleFilter(project);
        project.setPage(page);
        page.setList(mapper.findPlanList(project));
        return page;
    }

    public List<Project> findPlanList(Project project){
        return mapper.findPlanList(project);
    }

    /**
     * 项目立项信息
     */
    public Page<Project> findApprovalPage(Page<Project> page, Project project) {
        dataRuleFilter(project);
        project.setPage(page);
        page.setList(mapper.findApprovalList(project));
        return page;
    }

    /**
     * 项目拆旧信息
     */
    public Page<Project> findTearDownPage(Page<Project> page, Project project) {
        dataRuleFilter(project);
        project.setPage(page);
        page.setList(mapper.findTearDownList(project));
        return page;
    }

    /**
     * 项目关闭管理模块数据验证信息
     */
    public Page<ClosureManage> findClosureManagementData(Page<ClosureManage> page, ClosureManage closureManage) {
        dataRuleFilter(closureManage);
        closureManage.setPage(page);
        page.setList(mapper.findClosureManagementList(closureManage));
        return page;
    }

    /**
     * 修改项目前期基础信息
     */
    @Transactional(readOnly = false)
    public void insertBasic(Project project) {
        project.preInsert();
        projectMapper.insertBasic(project);
    }

    /**
     * 导入批量前期基础信息
     */
    @Transactional(readOnly = false)
    public void insertBasicList(List<Project> project) {
        for (Project p : project) {
            p.preInsert();
        }
        projectMapper.insertBasicList(project);
    }

    /**
     * 批量插入拆旧表信息 insertTearList
     * */
    @Transactional(readOnly = false)
    public void insertTearList(List<Project> project) {
        for (Project p : project) {
            p.preInsert();
        }
        projectMapper.insertTearList(project);
    }

    /**
     * 批量修改基础拆旧表信息
     */
    @Transactional(readOnly = false)
    public void updateTearList(List<Project> project) {
        projectMapper.updateTearList(project);
    }

    /**
     * 批量修改设计拆旧表信息
     */
    @Transactional(readOnly = false)
    public void updateDesTearList(List<Project> project) {
        projectMapper.updateDesTearList(project);
    }

    /**
     * 批量修改交底拆旧表信息
     */
    @Transactional(readOnly = false)
    public void updateConTearList(List<Project> project) {
        projectMapper.updateConTearList(project);
    }

    /**
     * 批量修改实际拆旧表信息
     */
    @Transactional(readOnly = false)
    public void updateFinTearList(List<Project> project) {
        projectMapper.updateFinTearList(project);
    }

    /**
     * 通过项目储备名称和设备材料名称查询是否已经存在
     * */
    public Boolean isNotByName(String reserveProjectName,String reservedName){
        int notByName = projectMapper.isNotByName(reserveProjectName, reservedName);
        if (notByName > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 批量删除基础拆旧信息
     */
    @Transactional(readOnly = false)
    public void deleteTearAll(String id) {
        projectMapper.deleteTearAll(id);
    }

    /**
     * 批量修改导入的项目信息
     */
    @Transactional(readOnly = false)
    public void updateBasicList(List<Project> project) {
        projectMapper.updateBasicList(project);
    }

    /**
     * 导入批量施工任务分配信息
     */
    @Transactional(readOnly = false)
    public void insertAssList(List<Project> project) {
        for (Project p : project) {
            p.preInsert();
        }
        projectMapper.insertAssList(project);
    }

    /**
     * 导入批量项目立项信息
     */
    @Transactional(readOnly = false)
    public void insertApprovalList(List<Project> project) {
        for (Project p : project) {
            p.preInsert();
        }
        projectMapper.insertApprovalList(project);
    }

    /**
     * 批量修改导入的施工任务分配信息
     */
    @Transactional(readOnly = false)
    public void updateAssList(List<Project> project) {
        projectMapper.updateAssList(project);
    }

    /**
     * 批量修改导入的项目立项信息
     */
    @Transactional(readOnly = false)
    public void updateApprovalList(List<Project> project) {
        projectMapper.updateApprovalList(project);
    }

    /**
     * 导入批量交底计划安排
     */
    @Transactional(readOnly = false)
    public void insertPlanList(List<Project> project) {
        for (Project p : project) {
            p.preInsert();
        }
        projectMapper.insertPlanList(project);
    }

    /**
     * 批量修改导入的交底计划安排
     */
    @Transactional(readOnly = false)
    public void updatePlanList(List<Project> project) {
        projectMapper.updatePlanList(project);
    }

    /**
     * 修改项目前期基本信息
     */
    @Transactional(readOnly = false)
    public void updateBasic(Project project) {
        projectMapper.updateBasic(project);
    }

    /**
     * 根据名称查询项目前期基本信息
     */
    public Project findProByName(String name) {
        return projectMapper.findProByName(name);
    }

    /**
     * 根据名称查询施工任务分配信息
     */
    public Project findAssByName(String name) {
        return projectMapper.findAssByName(name);
    }

    /**
     * 根据名称查询交底计划安排信息
     */
    public Project findPlanByName(String name) {
        return projectMapper.findPlanByName(name);
    }

    /**
     * 根据名称查询该计划的id，用于流程在表中添加procInsId
     */
    public String findPlanIdByName(String name) {
        return projectMapper.findPlanIdByName(name);
    }

    /**
     * 根据名称查询项目立项信息
     */
    public Project findApprovalByName(String name) {
        return projectMapper.findApprovalByName(name);
    }


    /**
     * 根据年度物资上报id获取施工任务分配信息
     */
    public Project getAss(Project project) {
        return projectMapper.getAss(project);
    }

    /**
     * 根据年度物资上报id获取交底计划安排
     */
    public Project getPlan(Project project) {
        return projectMapper.getPlan(project);
    }

    /**
     * 根据年度物资上报id获取项目立项信息信息
     */
    public Project getApproval(Project project) {
        return projectMapper.getApproval(project);
    }

    /**
     * 获取选择的基础拆旧表信息
     */
    public Project getTear(Project project) {
        return projectMapper.getTear(project);
    }

    /**
     * 基础项目拆旧信息修改
     */
    @Transactional(readOnly = false)
    public void tearEditSave(Project project) {
        projectMapper.tearEditSave(project);
    }

    /**
     * 修改项目关闭状态
     */
    @Transactional(readOnly = false)
    public void updateClosure(Project project) {
        projectMapper.updateClosure(project);
    }

    /**
     * 获取需求单位
     */
    public String findDemandUnit(String name) {
        return projectMapper.findDemandUnit(name);
    }

    /**
     * 通过流程定义号获取需求单位
     */
    public String demandByProcInsId(String procInsId) {
        return projectMapper.demandByProcInsId(procInsId);
    }


    /**
     * 根据id组查询所有项目信息
     */
    public List<Project> findListById(String[] projectIds) {
        return projectMapper.findListById(projectIds);
    }


    /**
     * 通过id获取项目暂定名称
     */
    public String reserveProjectNameById(String id) {
        return projectMapper.reserveProjectNameById(id);
    }

    /**
     * 通过流程定实例id查询项目流转人员信息
     */
    public Project findUser(String procInsId) {
        return projectMapper.findUser(procInsId);
    }

    /**
     * 通过流程定实例id查询储备人员信息
     */
    public PersonnelTurnover findCbUser(String procInsId) {
        return projectMapper.findCbUser(procInsId);
    }


    /**
     * 通过流程定义id查询项目流转人员信息-土建流程
     */
    public Project findTJUser(String procInsId) {
        return projectMapper.findTJUser(procInsId);
    }

    /**
     * 通过流程定义id查询项目流转人员信息-物资流程
     */
    public Project findWZUser(String procInsId) {
        return projectMapper.findWZUser(procInsId);
    }

    /**
     * 通过流程定义id查询项目流转人员信息-施工流程
     */
    public Project findSGUser(String procInsId) {
        return projectMapper.findSGUser(procInsId);
    }

    /**
     * 通过流程定义号查询项目的流转人员信息-转资流程
     */
    public Project findFundUser(String procInsId) {
        return projectMapper.findFundUser(procInsId);
    }

    /**
     * 根据储备项目名称查询项目的
     */
    @Transactional(readOnly = false)
    public void updateAdvCon(String reserveProjectName, String status) {
        projectMapper.updateAdvCon(reserveProjectName, status);
    }
/*	public Project get(String id) {
		Project project1 = super.get(id);
		return project1;
	}

	public List<Project> findList(Project project1) {
		return super.findList(project1);
	}

	public Page<Project> findPage(Page<Project> page, Project project1) {
		return super.findPage(page, project1);
	}

	@Transactional(readOnly = false)
	public void save(Project project1) {
		super.save(project1);
	}

	@Transactional(readOnly = false)
	public void delete(Project project1) {
		super.delete(project1);
	}

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

	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> project1s) {
		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 project1A : project1s){
			flag = true;
			for (Project project1B :list){
				if (project1A.getRequireName().equals(project1B.getRequireName())){
					project1A.setId(project1B.getId());
					project1A.setStatus(project1B.getStatus());
					project1A.setAcquisitionStatus(project1B.getAcquisitionStatus());
					project1A.setWhetherCloseStatus(project1B.getWhetherCloseStatus());
					project1A.setConstructionStatus(project1B.getConstructionStatus());
					project1A.setEarlyMaterial(project1B.getEarlyMaterial());
					project1A.setPreConstruction(project1B.getPreConstruction());
					project1A.setRezkey(project1B.getRezkey());
					updateList.add(project1A);
					flag = false;
					break;
				}
			}
			if (flag){
				project1A.setStatus(statusString);
				project1A.setAcquisitionStatus(statusString);
				project1A.setConstructionStatus(statusString);
				project1A.setWhetherCloseStatus(statusString);
				project1A.setEarlyMaterial(statusInt);
				project1A.setPreConstruction(statusInt);
				project1A.setRezkey("0");
				project1A.preInsert();
				insertList.add(project1A);
			}
		}
		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> project1s){
		return  projectMapper.findListProject(project1s);
	}



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

	public Project findJSUser(String procInsId) {
		return projectMapper.findJSUser(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 project1){
		projectMapper.updatePreConstruction(project1);
	}

	//根据项目定义号去查找项目
	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();
	}*/

    /**
     * 通过key值查找对应的流程id
     */
    public String findDefId(String key) {
        return projectMapper.findDefId(key);
    }

    /**
     * 根据流程定义号获取项目信息
     */
    public Project findByProcInsId(String procInsId) {
        return projectMapper.findByProcInsId(procInsId);
    }

    /**
     * 通过储备项目名称查询项目信息
     */
    public Project findByName(String reserveProjectName) {
        return projectMapper.findByName(reserveProjectName);
    }


    /**
     * 获取项目基础信息根据id
     */
    public Project findMaterialProject(String id) {
        return projectMapper.findMaterialProject(id);
    }

    /**
     * 通过项目需求名称修改土建状态
     */
    @Transactional(readOnly = false)
    public void updateAdvanceStart(String reserveProjectName, String numerical) {
        projectMapper.updateAdvanceStart(reserveProjectName, numerical);
    }

    //根据储备项目名称查询出所有的领料单数据
    public List<MaterialInfo> findAllInfo(String id) {
        return projectMapper.findAllInfo(id);
    }

    //根据储备项目名称查询出所有的领料单数据
    public List<MaterialInfo> findWzInfo(String procInsId) {
        return projectMapper.findWzInfo(procInsId);
    }

    //根据储备项目名称查询出所有的领料单数据-施工流程
    public List<MaterialInfo> findSgInfo(String id) {
        return projectMapper.findSgInfo(id);
    }

    //根据id查询项目的数据，包含停电线路，流转人员信息
    public Construction findBlockAss(String id) {
        return projectMapper.findBlockAss(id);
    }

    //根据id查询项目的数据，包含停电线路，流转人员信息
    public Construction findWzAss(String procInsId) {
        return projectMapper.findWzAss(procInsId);
    }

    //根据id查询项目的数据，包含停电线路，流转人员信息-施工流程
    public Construction findSgBlockAss(String id) {
        return projectMapper.findSgBlockAss(id);
    }

    //根据流程定义号获取项目信息添加项目二次涉电项目
    public Project findSgProject(String procInsId) {
        return projectMapper.findSgProject(procInsId);
    }

    @Transactional(readOnly = false)
    public void insertSgProject(Project project) {
        project.preInsert();
        projectMapper.insertSgProject(project);
    }

    public Assignment findSgAss(String procInsId) {
        return projectMapper.findSgAss(procInsId);
    }

    public Quantity findSgQuantity(String procInsId) {
        return projectMapper.findSgQuantity(procInsId);
    }

    @Transactional(readOnly = false)
    public void insertSgQuantity(Quantity quantity) {
        quantity.preInsert();
        projectMapper.insertSgQuantity(quantity);
    }

    @Transactional(readOnly = false)
    public void updateSgStatus(String status, String procInsId) {
        projectMapper.updateSgStatus(status, procInsId);
    }

    @Transactional(readOnly = false)
    public void updateTjStatus(String status, String procInsId) {
        projectMapper.updateTjStatus(status, procInsId);
    }

    @Transactional(readOnly = false)
    public void updateWzStatus(String status, String procInsId) {
        projectMapper.updateWzStatus(status, procInsId);
    }

    //通过流程定义号查询项目的土建及施工领料状态
    public Project findSgProcInsId(String procInsId) {
        return projectMapper.findSgProcInsId(procInsId);
    }

    //通过流程定义号查询项目的物资领料及项目进场状态
    public Project findWzProcInsId(String procInsId) {
        return projectMapper.findWzProcInsId(procInsId);
    }

    //通过流程定义号查询项目的物资领料及项目进场状态
    public Project findTjProcInsId(String procInsId) {
        return projectMapper.findTjProcInsId(procInsId);
    }

    @Transactional(readOnly = false)
    public void updateJsStatus(@Param("status") String status, @Param("reserveProjectName") String reserveProjectName) {
        projectMapper.updateJsStatus(status, reserveProjectName);
    }

    @Transactional(readOnly = false)
    public void wzStatusByName(@Param("status") String status, @Param("reserveProjectName") String reserveProjectName) {
        projectMapper.wzStatusByName(status, reserveProjectName);
    }

    /**
     * 项目基础信息所有数据
     * */
     public List<BeforeExport> findBtnExport(BeforeExport beforeExport) {
         return projectMapper.findBtnExport(beforeExport);
     }

}