package com.moral.api.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.moral.api.entity.Supervision;
|
import com.moral.api.mapper.SupervisionMapper;
|
import com.moral.api.service.SupervisionService;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.moral.constant.Constants;
|
import com.moral.constant.ResponseCodeEnum;
|
import com.moral.util.FileUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.boot.system.ApplicationHome;
|
import org.springframework.stereotype.Service;
|
import org.springframework.util.ObjectUtils;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.LinkedHashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.UUID;
|
import java.util.stream.Collectors;
|
|
|
/**
|
* <p>
|
* 督办单 服务实现类
|
* </p>
|
*
|
* @author moral
|
* @since 2022-01-12
|
*/
|
@Service
|
public class SupervisionServiceImpl extends ServiceImpl<SupervisionMapper, Supervision> implements SupervisionService {
|
|
@Autowired
|
private SupervisionMapper supervisionMapper;
|
|
@Override
|
public Map<String, Object> add(MultipartFile[] files, Supervision supervision) {
|
|
|
ApplicationHome applicationHome = new ApplicationHome(getClass());
|
String path = applicationHome.getSource().getParentFile().getPath()+"/static/img/";
|
|
Map<String, Object> result = new HashMap<>();
|
|
List<String> images = new ArrayList<>();
|
|
for (MultipartFile file : files) {
|
//判断上传文件格式
|
String fileType = file.getContentType();
|
if ("image/jpg".equals(fileType) || "image/png".equals(fileType) || "image/jpeg".equals(fileType)) {
|
//获取文件名
|
String fileName = file.getOriginalFilename();
|
//获取文件后缀名
|
String suffixName = fileName.substring(fileName.lastIndexOf("."));
|
//重新生成文件名
|
fileName = UUID.randomUUID() + suffixName;
|
//图片上传
|
if (FileUtils.upload(file, path, fileName)) {
|
images.add(fileName);
|
}
|
} else {
|
result.put("code", ResponseCodeEnum.IMG_UPLOAD_FAIl.getCode());
|
result.put("msg", ResponseCodeEnum.IMG_UPLOAD_FAIl.getMsg());
|
return result;
|
}
|
}
|
|
if (!ObjectUtils.isEmpty(images)) {
|
String image = images.stream()
|
.map(String::valueOf)
|
.collect(Collectors.joining(","));
|
supervision.setImages(image);
|
}
|
supervisionMapper.insert(supervision);
|
return result;
|
}
|
|
@Override
|
public Map<String, Object> selectSupervisions(Map<String, Object> params) {
|
//获取查询参数
|
int cityCode = Integer.parseInt(params.get("cityCode").toString());
|
int page = Integer.parseInt(params.get("page").toString());
|
int size = Integer.parseInt(params.get("size").toString());
|
Object start = params.get("start");
|
Object end = params.get("end");
|
|
QueryWrapper<Supervision> queryWrapper = new QueryWrapper<>();
|
queryWrapper.eq("city_code", cityCode)
|
.eq("is_delete", Constants.NOT_DELETE);
|
if (start != null) {
|
queryWrapper.ge("time", start);
|
}
|
if (start != null) {
|
queryWrapper.le("time", end);
|
}
|
|
//按time倒序
|
queryWrapper.orderByDesc("time");
|
|
//分页
|
Page<Supervision> supervisionPage = new Page<>(page, size);
|
supervisionMapper.selectPage(supervisionPage, queryWrapper);
|
List<Supervision> supervisions = supervisionPage.getRecords();
|
|
|
Map<String, Object> result = new LinkedHashMap<>();
|
result.put("total", supervisionPage.getTotal());
|
result.put("totalPage", supervisionPage.getPages());
|
result.put("current", supervisionPage.getCurrent());
|
result.put("pageSize", supervisionPage.getSize());
|
result.put("item", supervisions);
|
|
return result;
|
}
|
|
@Override
|
public Map<String, Object> updateSupervision(MultipartFile[] files, Supervision supervision) {
|
|
String path = this.getClass().getClassLoader()
|
.getResource("").getFile() + "static/img/";
|
|
Map<String, Object> result = new HashMap<>();
|
|
List<String> images = new ArrayList<>();
|
|
|
for (MultipartFile file : files) {
|
//判断上传文件格式
|
String fileType = file.getContentType();
|
if ("image/jpg".equals(fileType) || "image/png".equals(fileType) || "image/jpeg".equals(fileType)) {
|
//获取文件名
|
String fileName = file.getOriginalFilename();
|
//获取文件后缀名
|
String suffixName = fileName.substring(fileName.lastIndexOf("."));
|
//重新生成文件名
|
fileName = UUID.randomUUID() + suffixName;
|
//图片上传
|
if (FileUtils.upload(file, path, fileName)) {
|
images.add(fileName);
|
}
|
} else {
|
result.put("code", ResponseCodeEnum.IMG_UPLOAD_FAIl.getCode());
|
result.put("msg", ResponseCodeEnum.IMG_UPLOAD_FAIl.getMsg());
|
return result;
|
}
|
}
|
if (!ObjectUtils.isEmpty(images)) {
|
String image = images.stream()
|
.map(String::valueOf)
|
.collect(Collectors.joining(","));
|
supervision.setImages(image);
|
}
|
supervisionMapper.updateById(supervision);
|
return result;
|
}
|
}
|