package com.yanzhu.jh.project.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.yanzhu.jh.project.domain.vo.SurProjectBuildNodeDataExport;
import com.yanzhu.jh.project.mapper.SurProjectMapper;
import com.yanzhu.jh.project.domain.BaseBuildNode;
import com.yanzhu.jh.project.mapper.BaseBuildNodeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanzhu.jh.project.mapper.SurProjectBuildNodeDataMapper;
import com.yanzhu.jh.project.domain.SurProjectBuildNodeData;
import com.yanzhu.jh.project.service.ISurProjectBuildNodeDataService;
import com.ruoyi.common.utils.SecurityUtils;
/**
 * 项目建设节点数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-07
 */
@Service
public class SurProjectBuildNodeDataServiceImpl implements ISurProjectBuildNodeDataService 
{
    @Autowired
    private SurProjectBuildNodeDataMapper surProjectBuildNodeDataMapper;

    @Autowired
    private SurProjectMapper surProjectMapper;

    @Autowired
    private BaseBuildNodeMapper baseBuildNodeMapper;

    /**
     * 查询项目建设节点数据
     * 
     * @param id 项目建设节点数据主键
     * @return 项目建设节点数据
     */
    @Override
    public SurProjectBuildNodeData selectSurProjectBuildNodeDataById(Long id)
    {
        return surProjectBuildNodeDataMapper.selectSurProjectBuildNodeDataById(id);
    }

    /**
     * 查询项目建设节点数据列表
     * 
     * @param surProjectBuildNodeData 项目建设节点数据
     * @return 项目建设节点数据
     */
    @Override
    public List<SurProjectBuildNodeData> selectSurProjectBuildNodeDataList(SurProjectBuildNodeData surProjectBuildNodeData)
    {
        return surProjectBuildNodeDataMapper.selectSurProjectBuildNodeDataList(surProjectBuildNodeData);
    }

    /**
     * 新增项目建设节点数据
     * 
     * @param surProjectBuildNodeData 项目建设节点数据
     * @return 结果
     */
    @Override
    public int insertSurProjectBuildNodeData(SurProjectBuildNodeData surProjectBuildNodeData)
    {
        if(!SecurityUtils.getAuthentication().getPrincipal().equals("anonymousUser")) {
            surProjectBuildNodeData.setCreateBy(SecurityUtils.getUsername());
        }
        surProjectBuildNodeData.setIsDel(0l);
        surProjectBuildNodeData.setCreateTime(DateUtils.getNowDate());
        return surProjectBuildNodeDataMapper.insertSurProjectBuildNodeData(surProjectBuildNodeData);
    }

    /**
     * 修改项目建设节点数据
     * 
     * @param surProjectBuildNodeData 项目建设节点数据
     * @return 结果
     */
    @Override
    public int updateSurProjectBuildNodeData(SurProjectBuildNodeData surProjectBuildNodeData)
    {
        surProjectBuildNodeData.setUpdateBy(SecurityUtils.getUsername());
        surProjectBuildNodeData.setUpdateTime(DateUtils.getNowDate());
        return surProjectBuildNodeDataMapper.updateSurProjectBuildNodeData(surProjectBuildNodeData);
    }

    /**
     * 批量删除项目建设节点数据
     * 
     * @param ids 需要删除的项目建设节点数据主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectBuildNodeDataByIds(Long[] ids)
    {
        return surProjectBuildNodeDataMapper.deleteSurProjectBuildNodeDataByIds(ids);
    }

    /**
     * 删除项目建设节点数据信息
     * 
     * @param id 项目建设节点数据主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectBuildNodeDataById(Long id)
    {
        return surProjectBuildNodeDataMapper.deleteSurProjectBuildNodeDataById(id);
    }

    @Override
    public int addProjectNodes(long projectId) {
        List<BaseBuildNode> list=baseBuildNodeMapper.selectByProject(projectId);
        int n=0;
        for(BaseBuildNode it :list){
            SurProjectBuildNodeData item=new SurProjectBuildNodeData();
            item.setProjectId(projectId);
            item.setNodeId(it.getId());
            item.setIsDel(0l);
            n+=this.insertSurProjectBuildNodeData(item);
        }
        return n;
    }

    @Override
    public int importData(List<SurProjectBuildNodeDataExport> list) {
        if(list.size()==0){
            return 0;
        }
        SurProjectBuildNodeData where=new SurProjectBuildNodeData();
        where.setProjectId(list.get(0).getProjectId());
        List<SurProjectBuildNodeData> items=surProjectBuildNodeDataMapper.selectSurProjectBuildNodeDataList(where);
        if(items.size()==0){
            return 0;
        }
        int cnt=0;
        try {
            for (SurProjectBuildNodeDataExport it : list) {
                List<SurProjectBuildNodeData> finds = items.stream().filter(d -> d.getId().equals(it.getId())).collect(Collectors.toList());
                if (finds.size() == 1) {
                    SurProjectBuildNodeData find = finds.get(0);
                    find.setPlanEndDate(it.getPlanEndDate());
                    find.setPlanStartDate(it.getPlanStartDate());
                    find.setStartDate(it.getStartDate());
                    find.setEndDate(it.getEndDate());
                    cnt++;
                    updateSurProjectBuildNodeData(find);
                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return cnt;
    }

    /**
     * 节点计划预警
     * @param where
     * @return
     */
    @Override
    public List<SurProjectBuildNodeData> selectScheduledAlerts(SurProjectBuildNodeData where) {
        return surProjectBuildNodeDataMapper.selectScheduledAlerts(where);
    }

    /**
     * 获取当前节点及叶子节点
     * @param where
     * @return
     */
    @Override
    public List<SurProjectBuildNodeData> selectCurrent(SurProjectBuildNodeData where) {
        return surProjectBuildNodeDataMapper.selectCurrent(where);
    }

    @Override
    public List<SurProjectBuildNodeData> countCompletionRate(SurProjectBuildNodeData where) {
        return surProjectBuildNodeDataMapper.countCompletionRate(where);
    }

    @Override
    public List<SurProjectBuildNodeData> queryByProjectType(long type) {
        return surProjectBuildNodeDataMapper.queryByProjectType(type);
    }
}
