package com.fdkankan.manage.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.manage.common.PageInfo;
import com.fdkankan.manage.common.ResultCode;
import com.fdkankan.manage.constant.FileTypeEnum;
import com.fdkankan.manage.entity.CommonUpload;
import com.fdkankan.manage.entity.DictFile;
import com.fdkankan.manage.exception.BusinessException;
import com.fdkankan.manage.mapper.IDictFileMapper;
import com.fdkankan.manage.service.ICommonUploadService;
import com.fdkankan.manage.service.IDictFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.manage.vo.request.DictFileParam;
import com.fdkankan.manage.vo.response.DictFileVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
/**
*
* 服务实现类
*
*
* @author
* @since 2024-12-02
*/
@Service
public class DictFileServiceImpl extends ServiceImpl implements IDictFileService {
@Autowired
ICommonUploadService commonUploadService;
@Override
public Object pageList(DictFileParam param) {
if(StringUtils.isBlank(param.getTypeKey())){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
if(StringUtils.isNotBlank(param.getName())){
wrapper.like(DictFile::getName,param.getName());
}
if(param.getDictId() != null){
wrapper.eq(DictFile::getDictId,param.getDictId());
}
wrapper.eq(DictFile::getSysUserId, StpUtil.getLoginId());
wrapper.orderByDesc(DictFile::getId);
Page page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
List uploadIds = page.getRecords().stream().map(DictFile::getUploadId).collect(Collectors.toList());
HashMap map = new HashMap<>();
if(!uploadIds.isEmpty()){
List commonUploads = commonUploadService.listByIds(uploadIds);
commonUploads.forEach(e -> map.put(e.getId(),e));
}
List voList = new ArrayList<>();
for (DictFile record : page.getRecords()) {
DictFileVo vo = new DictFileVo();
BeanUtils.copyProperties(record,vo);
CommonUpload upload = map.get(record.getUploadId());
if(upload != null){
BeanUtils.copyProperties(upload,vo);
}
voList.add(vo);
}
Page voPage = new Page<>(param.getPageNum(),param.getPageSize());
voPage.setTotal(page.getTotal());
voPage.setRecords(voList);
return PageInfo.PageInfo(voPage);
}
@Override
public void addOrUpdate(DictFile dictFile) {
if(StringUtils.isBlank(dictFile.getName()) || StringUtils.isBlank(dictFile.getTypeKey())
|| dictFile.getDictId() == null ){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
this.saveOrUpdate(dictFile);
}
@Override
public void del(DictFile dictFile) {
if(dictFile.getId() == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
this.removeById(dictFile.getId());
}
}