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

import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.sys.utils.UserUtils;
import com.jeeplus.test.program.configuration.fileDict.domain.ProgramFileDict;
import com.jeeplus.test.program.configuration.fileDict.mapper.ProgramFileDictMapper;
import com.jeeplus.test.program.configuration.projectDict.domain.ProgramProjectDict;
import com.jeeplus.test.program.configuration.projectDict.domain.vo.FileDictVo;
import com.jeeplus.test.program.configuration.projectDict.mapper.ProjectDictMapper;
import com.jeeplus.test.program.configuration.projectDict.service.dto.ProjectDictDto;
import org.flowable.editor.language.json.converter.util.CollectionUtils;
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 = UserUtils.getCurrentUserDTO();
        String id = UUID.randomUUID().toString().replace("-", "");
        ProgramProjectDict dict = new ProgramProjectDict();
        BeanUtils.copyProperties(projectDictDto, dict);
        dict.setId(id);
        dict.setCreateBy(userDTO.getId());
        dict.setCreateDate(new Date());
        dict.setUpdateBy(userDTO.getId());
        dict.setUpdateDate(new Date());
        dict.setDelFlag(0);
        projectDictMapper.insert(dict);
        return "操作成功";
    }

    /**
     * 修改
     * @param projectDictDto
     * @return
     */
    public String update(ProjectDictDto projectDictDto) {
        UserDTO userDTO = UserUtils.getCurrentUserDTO();
        ProgramProjectDict dict = new ProgramProjectDict();
        BeanUtils.copyProperties(projectDictDto, dict);
        dict.setUpdateBy(userDTO.getId());
        dict.setUpdateDate(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 (CollectionUtils.isNotEmpty(list)) {
            List<String> fileIds = list.stream().map(i -> i.getFileId()).collect(Collectors.toList());
            // 根据fileId查询所属层级
            String parentId = programFileDictMapper.selectById(projectDictDto.getFileId()).getParentId();
            if (CollectionUtils.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);
    }
}
