package com.ruoyi.project.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.enums.FileBuzType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.business.domain.TbFile;
import com.ruoyi.project.business.domain.vo.quotation.DescFileVO;
import com.ruoyi.project.business.enums.DelFlagEnum;
import com.ruoyi.project.business.mapper.TbFileMapper;
import com.ruoyi.project.business.service.ITbFileService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 附件Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-19
 */
@Service
public class TbFileServiceImpl extends ServiceImpl<TbFileMapper, TbFile> implements ITbFileService {

    @Resource
    private TbFileMapper tbFileMapper;

    /**
     * 查询附件
     *
     * @param id 附件主键
     * @return 附件
     */
    @Override
    public TbFile selectTbFileById(Long id) {
        return tbFileMapper.selectTbFileById(id);
    }

    /**
     * 查询附件列表
     *
     * @param tbFile 附件
     * @return 附件
     */
    @Override
    public List<TbFile> selectTbFileList(TbFile tbFile) {
        return tbFileMapper.selectTbFileList(tbFile);
    }

    /**
     * 新增附件
     *
     * @param tbFile 附件
     * @return 结果
     */
    @Override
    public int insertTbFile(TbFile tbFile) {
        tbFile.setCreateTime(DateUtils.getNowDate());
        return tbFileMapper.insertTbFile(tbFile);
    }

    /**
     * 修改附件
     *
     * @param tbFile 附件
     * @return 结果
     */
    @Override
    public int updateTbFile(TbFile tbFile) {
        tbFile.setUpdateTime(DateUtils.getNowDate());
        return tbFileMapper.updateTbFile(tbFile);
    }

    /**
     * 批量删除附件
     *
     * @param ids 需要删除的附件主键
     * @return 结果
     */
    @Override
    public int deleteTbFileByIds(Long[] ids) {
        return tbFileMapper.deleteTbFileByIds(ids);
    }

    /**
     * 删除附件信息
     *
     * @param id 附件主键
     * @return 结果
     */
    @Override
    public int deleteTbFileById(Long id) {
        return tbFileMapper.deleteTbFileById(id);
    }

    @Override
    public List<TbFile> selectTbFileListByBuzIdAndBuzType(Long leadId, String type) {
        return tbFileMapper.selectTbFileListByBuzIdAndBuzType(leadId, type);
    }

    @Override
    public Map<Long, List<DescFileVO>> getDescFileVOMap(List<Long> nonStandardDescIdList) {
        Map<Long, List<DescFileVO>> descFileMap = new HashMap<>();
        if (CollUtil.isNotEmpty(nonStandardDescIdList)) {
            List<TbFile> list = this.list(Wrappers.<TbFile>lambdaQuery()
                .eq(TbFile::getBusinessType, FileBuzType.NON_STANDARD_DESC.getType())
                .eq(TbFile::getDelFlag, DelFlagEnum.NORMAL.getValue())
                .in(TbFile::getBusinessId, nonStandardDescIdList));
            descFileMap = list.stream().map(item -> {
                DescFileVO descFileVO = new DescFileVO();
                descFileVO.setNonStandardDescId(item.getBusinessId());
                descFileVO.setFilePath(item.getFilePath());
                descFileVO.setName(item.getRealFileName());
                descFileVO.setRealFileName(item.getRealFileName());
                descFileVO.setServerFileName(item.getServerFileName());
                descFileVO.setUid(IdUtil.simpleUUID());
                return descFileVO;
            }).collect(Collectors.groupingBy(DescFileVO::getNonStandardDescId));
        }
        return descFileMap;
    }
}
