package org.springblade.system.service.impl;
import jakarta.annotation.Resource;
import org.springblade.common.core.domain.AjaxResult;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.system.domain.ArchivalCatalogueDto;
import org.springblade.system.domain.DaKyProFilesContentsDto;
import org.springblade.system.domain.ProjectDto;
import org.springblade.system.domain.SelectDto;
import org.springblade.system.mapper.FileManageMapper;
import org.springblade.system.mapper.ProjectMapper;
import org.springblade.system.service.ProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;



/**
 * @Author：liang.chao
 * @Date：2025/9/11 - 10:05
 */
@Service
public class ProjectServiceImpl implements ProjectService {

    @Resource
    private ProjectMapper projectMapper;

    @Resource
    private FileManageMapper fileManageMapper;

    @Override
    public List<ProjectDto> list(ProjectDto dto) {
        List<ProjectDto> list = projectMapper.list(dto);
        for (ProjectDto projectDto : list) {
          Integer num =  fileManageMapper.getFilesNum(projectDto.getId());
          projectDto.setFileUplaudNum(num);
        }
        return list;
    }

    @Override
    public List<SelectDto> getFileCatalogSelect() {
        return projectMapper.getFileCatalogSelect();
    }

    @Override
    @Transactional
    public AjaxResult updateContentsName(ProjectDto projectDto) {
        try {
            Integer num = projectMapper.updateContentsName(projectDto);
            if (num > 0) {
                // 档案管理中新增该工程下所有目录
                List<ArchivalCatalogueDto> list = projectMapper.getfilesContentsById(projectDto);
                for (ArchivalCatalogueDto archivalCatalogueDto : list) {
                    DaKyProFilesContentsDto dakyProFilesContentsDto = new DaKyProFilesContentsDto();
                    BeanUtils.copyProperties(archivalCatalogueDto, dakyProFilesContentsDto);
                    dakyProFilesContentsDto.setId(archivalCatalogueDto.getId().toString());
                    dakyProFilesContentsDto.setParentId(archivalCatalogueDto.getParentId().toString());
                    dakyProFilesContentsDto.setCreateUserId(AuthUtil.getUser().getUserId());
                    dakyProFilesContentsDto.setCreateUserName(AuthUtil.getUser().getUserName());
                    dakyProFilesContentsDto.setProId(projectDto.getProId());
                    dakyProFilesContentsDto.setDataSource("1");
                    dakyProFilesContentsDto.setIsUnique("0");
                    projectMapper.insertDakyProFilesContents(dakyProFilesContentsDto);
                }
                return AjaxResult.success("配置成功");
            } else {
                return AjaxResult.error("配置失败");
            }
        } catch (Exception e) {
            return AjaxResult.error("接口异常");
        }

    }
}
