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

import java.util.List;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.DateUtils;
import com.yanzhu.jh.project.domain.BaseBuildNode;
import com.yanzhu.jh.project.domain.SurProjectBuildNodeData;
import com.yanzhu.jh.project.mapper.BaseBuildNodeMapper;
import com.yanzhu.jh.project.service.ISurProjectBuildNodeDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanzhu.jh.project.mapper.SurProjectBuildDesignDataMapper;
import com.yanzhu.jh.project.domain.SurProjectBuildDesignData;
import com.yanzhu.jh.project.service.ISurProjectBuildDesignDataService;
import com.ruoyi.common.utils.SecurityUtils;
/**
 * 设计管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-16
 */
@Service
public class SurProjectBuildDesignDataServiceImpl implements ISurProjectBuildDesignDataService 
{
    @Autowired
    private SurProjectBuildDesignDataMapper surProjectBuildDesignDataMapper;

    @Autowired
    private BaseBuildNodeMapper baseBuildNodeMapper;

    @Autowired
    private ISurProjectBuildNodeDataService surProjectBuildNodeDataService;
    /**
     * 查询设计管理
     * 
     * @param id 设计管理主键
     * @return 设计管理
     */
    @Override
    public SurProjectBuildDesignData selectSurProjectBuildDesignDataById(Long id)
    {
        return surProjectBuildDesignDataMapper.selectSurProjectBuildDesignDataById(id);
    }

    /**
     * 查询设计管理列表
     * 
     * @param surProjectBuildDesignData 设计管理
     * @return 设计管理
     */
    @Override
    public List<SurProjectBuildDesignData> selectSurProjectBuildDesignDataList(SurProjectBuildDesignData surProjectBuildDesignData)
    {
        return surProjectBuildDesignDataMapper.selectSurProjectBuildDesignDataList(surProjectBuildDesignData);
    }

    /**
     * 新增设计管理
     * 
     * @param surProjectBuildDesignData 设计管理
     * @return 结果
     */
    @Override
    public int insertSurProjectBuildDesignData(SurProjectBuildDesignData surProjectBuildDesignData)
    {
        surProjectBuildDesignData.setCreateBy(SecurityUtils.getUsername());
        surProjectBuildDesignData.setCreateTime(DateUtils.getNowDate());
        return surProjectBuildDesignDataMapper.insertSurProjectBuildDesignData(surProjectBuildDesignData);
    }

    /**
     * 修改设计管理
     * 
     * @param surProjectBuildDesignData 设计管理
     * @return 结果
     */
    @Override
    public int updateSurProjectBuildDesignData(SurProjectBuildDesignData surProjectBuildDesignData)
    {
        surProjectBuildDesignData.setUpdateBy(SecurityUtils.getUsername());
        surProjectBuildDesignData.setUpdateTime(DateUtils.getNowDate());
        return surProjectBuildDesignDataMapper.updateSurProjectBuildDesignData(surProjectBuildDesignData);
    }

    /**
     * 批量删除设计管理
     * 
     * @param ids 需要删除的设计管理主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectBuildDesignDataByIds(Long[] ids)
    {
        return surProjectBuildDesignDataMapper.deleteSurProjectBuildDesignDataByIds(ids);
    }

    /**
     * 删除设计管理信息
     * 
     * @param id 设计管理主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectBuildDesignDataById(Long id)
    {
        return surProjectBuildDesignDataMapper.deleteSurProjectBuildDesignDataById(id);
    }
    /**
     * 按周查询设计计划
     * @param projectId,startDate,endDate
     * @return
     */
    @Override
    public List<SurProjectBuildDesignData> selectWeekData(SurProjectBuildDesignData where) {
        return surProjectBuildDesignDataMapper.selectWeekData(where);
    }

    @Override
    public int addWeekData(SurProjectBuildDesignData where) {
        int n=0;
        //查询上周数据
        SurProjectBuildDesignData prvWhere=new SurProjectBuildDesignData();
        DateTime dt= DateUtil.offsetDay(DateUtil.date(),-7);
        prvWhere.setProjectId(where.getProjectId());
        prvWhere.setStartDate(DateUtil.beginOfWeek(dt));
        prvWhere.setEndDate(DateUtil.endOfWeek(dt));
        List<SurProjectBuildDesignData> prvList=selectWeekData(prvWhere);
        DateTime dataTime=DateTime.now();
        if(prvList.size()>0){ //上周有数据,使用上周的数据，如果没有就创建新数据
            for(SurProjectBuildDesignData node :prvList){
                SurProjectBuildDesignData it = getSurProjectBuildDesignData(node, dataTime);
                insertSurProjectBuildDesignData(it);
                n++;
            }
            return n;
        }
        List<BaseBuildNode> list=baseBuildNodeMapper.selectByProject(where.getProjectId());

        for(BaseBuildNode node:list){
            if(node.getNodeLvl().startsWith("03") && node.getNodeLvl().length()>2){
                SurProjectBuildDesignData it=new SurProjectBuildDesignData();
                it.setNodeId(node.getId());
                it.setProjectId(where.getProjectId());
                it.setDataDate(dataTime);
                it.setIsDel(0l);
                insertSurProjectBuildDesignData(it);
                n++;
            }
        }
        return n;
    }

    /**
     * 批量修改
     * @param list
     * @return
     */
    @Override
    public int updateList(List<SurProjectBuildDesignData> list) {
        if(list.size()==0){
            return 0;
        }
        long projectId= list.get(0).getProjectId();
        SurProjectBuildNodeData where=new SurProjectBuildNodeData();
        where.setProjectId(projectId);
        List<SurProjectBuildNodeData> nodeDataList=surProjectBuildNodeDataService.selectSurProjectBuildNodeDataListAndAdd(where);
        int cnt=0;
        for(SurProjectBuildDesignData item :list){
            updateSurProjectBuildDesignData(item);
            SurProjectBuildNodeData it=findNodeDataInList(nodeDataList,item);
            if(it!=null){
                it.setStartDate(item.getStartDate());
                it.setEndDate(item.getEndDate());
                it.setPlanStartDate(item.getPlanStartDate());
                it.setPlanEndDate(item.getPlanEndDate());
                surProjectBuildNodeDataService.updateSurProjectBuildNodeData(it);
            }
        }
        return cnt;
    }

    private SurProjectBuildNodeData findNodeDataInList(List<SurProjectBuildNodeData> nodeDataList, SurProjectBuildDesignData item) {
        for(SurProjectBuildNodeData it:nodeDataList){
            if(it.getNodeId()==item.getNodeId()){
                return it;
            }
        }
        return null;
    }

    private static SurProjectBuildDesignData getSurProjectBuildDesignData(SurProjectBuildDesignData node, DateTime dataTime) {
        SurProjectBuildDesignData it=new SurProjectBuildDesignData();
        it.setOwnerPhone(node.getOwnerPhone());
        it.setOwnerName(node.getOwnerName());
        it.setStartDate(node.getStartDate());
        it.setDataDate(dataTime);
        it.setProjectId(node.getProjectId());
        it.setEndDate(node.getEndDate());
        it.setIsDel(0l);
        it.setNodeId(node.getNodeId());
        it.setDuration(node.getDuration());
        it.setNextWeekWork(node.getNextWeekWork());
        it.setPlanEndDate(node.getPlanEndDate());
        it.setPlanEndDate(node.getPlanEndDate());
        it.setProblem(node.getProblem());
        it.setWork(node.getWork());
        it.setRemainingDays(node.getRemainingDays());
        it.setStatus(node.getStatus());
        return it;
    }
}
