package com.yfd.platform.modules.specialDocument.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfd.platform.modules.specialDocument.domain.Nodes;
import com.yfd.platform.modules.specialDocument.domain.Project;
import com.yfd.platform.modules.specialDocument.mapper.ProjectMapper;
import com.yfd.platform.modules.specialDocument.service.INodesService;
import com.yfd.platform.modules.specialDocument.service.IProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfd.platform.system.domain.SysDictionaryItems;
import com.yfd.platform.system.mapper.SysDictionaryItemsMapper;
import com.yfd.platform.utils.StringUtils;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 专项项目表 服务实现类
 * </p>
 *
 * @author LiMengNan
 * @since 2025-01-20
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ChannelInboundHandlerAdapter.class);


    //专项项目表Mapper
    @Resource
    private ProjectMapper projectMapper;


    //专项文档节点服务类
    @Resource
    private INodesService nodesService;

    //字典表Mapper
    @Resource
    private SysDictionaryItemsMapper sysDictionaryItemsMapper;

    /**********************************
     * 用途说明: 分页查询专项文档管理-项目管理
     * 参数说明
     * projectCode   项目编号
     * projectType   项目类型
     *projectName    项目名称
     * pageNum 当前页
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回分页查询结果
     ***********************************/
    @Override
    public Page<Project> getSdProjectPage(String projectCode, String projectType, String projectName, Page<Project> page) {

        LambdaQueryWrapper<Project> queryWrapper = new LambdaQueryWrapper<>();
        //如果项目编号 projectCode 不为空
        if (StringUtils.isNotEmpty(projectCode)) {
            queryWrapper.like(Project::getProjectCode, projectCode);
        }
        //如果项目类型 projectType 不为空
        if (StringUtils.isNotEmpty(projectType)) {
            queryWrapper.like(Project::getProjectType, projectType);
//            //查询字典表获取项目类型对应数据字典
//            QueryWrapper<SysDictionaryItems> queryWrapperSysDictionary = new QueryWrapper<>();
//            queryWrapperSysDictionary.eq("parentcode", "zxxmlx");
//            queryWrapperSysDictionary.eq("itemcode", projectType);
//            queryWrapperSysDictionary.orderByAsc("orderno");
//            SysDictionaryItems sysDictionaryItems = sysDictionaryItemsMapper.selectOne(queryWrapperSysDictionary);
//            if(sysDictionaryItems != null){
//                queryWrapper.like(Project::getProjectType, sysDictionaryItems.getDictName());
//            }else {
//              queryWrapper.like(Project::getProjectType, projectType);
//            }
        }
        //如果项目名称 projectName 不为空
        if (StringUtils.isNotEmpty(projectName)) {
            queryWrapper.like(Project::getProjectName, projectName);
        }
        //根据创建时间排序
        queryWrapper.orderByAsc(Project::getCreateTime);
        //分页查询
        Page<Project> sdProjectPage = projectMapper.selectPage(page, queryWrapper);
        return sdProjectPage;
    }

    /***********************************
     * 用途说明：新增专项文档管理-项目管理
     * 参数说明
     * Project     项目管理
     * 返回值说明:  com.yfd.platform.config.ResponseResult 返回新增成功或者失败
     ***********************************/
    @Override
    public Boolean addSdproject(Project project) {
//        //查询字典表获取项目类型对应数据字典
//        QueryWrapper<SysDictionaryItems> queryWrapperSysDictionary = new QueryWrapper<>();
//        queryWrapperSysDictionary.eq("parentcode", "compressType");
//        queryWrapperSysDictionary.orderByAsc("orderno");
//        SysDictionaryItems sysDictionaryItems = sysDictionaryItemsMapper.selectOne(queryWrapperSysDictionary);
//        if(sysDictionaryItems != null){
//            project.setProjectType(sysDictionaryItems.getDictName());
//        }


        //TODO 01.21沟通以后说是先不用管重复校验问题
//        //通过项目名称 项目编号查询 查看是否存在
//        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
//        if(project.getProjectCode() != null){
//            queryWrapper.eq("project_code",project.getProjectCode());
//        }
//        if(project.getProjectName() != null){
//            queryWrapper.eq("project_name",project.getProjectName());
//        }
//        List<Project> projects = projectMapper.selectList(queryWrapper);
//        if(){
//
//        }
        // 设置当前时间
        LocalDateTime now = LocalDateTime.now();
        // 转换为 Timestamp
        Timestamp currentTime = Timestamp.valueOf(now);
        project.setCreateTime(currentTime);
        int valueAdded = projectMapper.insert(project);
        if (valueAdded == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**********************************
     * 用途说明: 修改专项文档管理-项目管理
     * 参数说明
     * Project     项目管理
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回修改成功或者失败
     ***********************************/
    @Override
    public boolean updateSdproject(Project project) {
        //查询字典表获取项目类型对应数据字典
//        QueryWrapper<SysDictionaryItems> queryWrapperSysDictionary = new QueryWrapper<>();
//        queryWrapperSysDictionary.eq("parentcode", "zxxmlx");
//        queryWrapperSysDictionary.eq("itemcode", project.getProjectType());
//        queryWrapperSysDictionary.orderByAsc("orderno");
//        SysDictionaryItems sysDictionaryItems = sysDictionaryItemsMapper.selectOne(queryWrapperSysDictionary);
//        if(sysDictionaryItems != null){
//            project.setProjectType(sysDictionaryItems.getDictName());
//        }


        int valueUpdate = projectMapper.updateById(project);
        if (valueUpdate == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**********************************
     * 用途说明: 批量删除专项文档管理-项目管理
     * 参数说明  ids 项目管理id数组
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回批量删除成功或失败
     ***********************************/
    @Override
    public boolean deleteProjectByIds(List<String> dataset) {
        Boolean value = false;

        //循环所有的ID
        for (String id : dataset) {
            QueryWrapper<Nodes> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("project_id", id);
            queryWrapper.eq("parent_id", "00");
            List<Nodes> nodesList = nodesService.list(queryWrapper);
            //如果为空 跳过当前项目 接着下一个项目
            if (nodesList == null || nodesList.isEmpty()) {
                return true;
            }
            for (Nodes nodes : nodesList) {
                //调用删除节点 根据任务ID
                Boolean deleteTsnodes = nodesService.deleteNodesById(nodes.getParentId());
                //如果删除成功 接着删除节点表数据
                if (deleteTsnodes) {
                    LOGGER.info("nodes表结删除改成功");
                    value = true;
                } else {
                    LOGGER.error("nodes表结构删除失败");
                    value = false;
                }
            }
            // 删除当前项目
            int deleteCount = projectMapper.deleteById(id);
            if (deleteCount == 1) {
                LOGGER.info("project表结删除改成功");
                value = true;
            } else {
                LOGGER.error("project表结构删除失败");
                value = false;
            }
        }
        return value;
    }
}
