package com.jeeplus.assess.program.configuration.projectDict.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.jeeplus.assess.program.configuration.fileDict.domain.ProgramFileDict;
import com.jeeplus.assess.program.configuration.fileDict.mapper.ProgramFileDictMapper;
import com.jeeplus.assess.program.configuration.projectDict.domain.ProgramProjectDict;
import com.jeeplus.assess.program.configuration.projectDict.domain.vo.FileDictVo;
import com.jeeplus.assess.program.configuration.projectDict.mapper.ProjectDictMapper;
import com.jeeplus.assess.program.configuration.projectDict.service.dto.ProjectDictDto;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
//import com.jeeplus.sys.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class ProjectDictService {

    @Resource
    private ProjectDictMapper projectDictMapper;

    @Resource
    private ProgramFileDictMapper programFileDictMapper;

    /**
     * 查询
     * @param projectDictDto
     * @return
     */
    public List<FileDictVo> list(ProjectDictDto projectDictDto) {
        List<FileDictVo> list = new ArrayList<>();
        if (StringUtils.isNotEmpty(projectDictDto.getAttachmentProjectType()) /*&& StringUtils.isNotEmpty(projectDictDto.getAttachmentProjectSort())*/) {
            //查询父级
            List<FileDictVo> parentList = projectDictMapper.parentList();
            list.addAll(parentList);
            //查询子级
            List<FileDictVo> dictVos = projectDictMapper.list(projectDictDto.getAttachmentProjectType()/*, projectDictDto.getAttachmentProjectSort()*/);
            list.addAll(dictVos);
        }
        return list;
    }

    /**
     * 保存
     * @param projectDictDto
     * @return
     */
    public String save(ProjectDictDto projectDictDto) {
        if (StringUtils.isNotEmpty(projectDictDto.getId())) {
            return update(projectDictDto);
        } else {
            return add(projectDictDto);
        }
    }

    /**
     * 新增
     * @param projectDictDto
     * @return
     */
    public String add(ProjectDictDto projectDictDto) {
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        String id = UUID.randomUUID().toString().replace("-", "");
        ProgramProjectDict dict = new ProgramProjectDict();
        BeanUtils.copyProperties(projectDictDto, dict);
        dict.setId(id);
        dict.setCreateById(userDTO.getId());
        dict.setCreateTime(new Date());
        dict.setUpdateById(userDTO.getId());
        dict.setUpdateTime(new Date());
        dict.setDelFlag(0);
        projectDictMapper.insert(dict);
        return "操作成功";
    }

    /**
     * 修改
     * @param projectDictDto
     * @return
     */
    public String update(ProjectDictDto projectDictDto) {
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        ProgramProjectDict dict = new ProgramProjectDict();
        BeanUtils.copyProperties(projectDictDto, dict);
        dict.setUpdateById(userDTO.getId());
        dict.setUpdateTime(new Date());
        dict.setDelFlag(0);
        projectDictMapper.updateById(dict);
        return "操作成功";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public String deleteById(String id) {
        projectDictMapper.deleteById(id);
        return "操作成功";
    }

    /**
     * 新增附件结构信息附件名称查询
     * @param projectDictDto
     * @return
     */
    public List<ProgramFileDict> getFileList(ProjectDictDto projectDictDto) {
        List<FileDictVo> list = list(projectDictDto);
        if (CollectionUtil.isNotEmpty(list)) {
            List<String> fileIds = list.stream().map(i -> i.getFileId()).collect(Collectors.toList());
            // 根据fileId查询所属层级
            String parentId = programFileDictMapper.selectById(projectDictDto.getFileId()).getParentId();
            if (CollectionUtil.isNotEmpty(fileIds) && StringUtils.isNotEmpty(projectDictDto.getFileId()) && !"0".equals(parentId)) {
                fileIds.remove(projectDictDto.getFileId());
                return projectDictMapper.getFileList(projectDictDto.getFileId(), fileIds);
            } else {
                return projectDictMapper.getFileParentList(projectDictDto.getFileId(), fileIds);
            }
        }
        return new ArrayList<>();
    }

    /**
     * 根据fileId查询
     * @param id
     * @return
     */
    public ProgramFileDict findByFileId(String id) {
        return projectDictMapper.findById(id);
    }
}
