|  |  | 
 |  |  |  | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
 |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
 |  |  | import com.moral.api.entity.Organization; | 
 |  |  | import com.moral.api.entity.Supervision; | 
 |  |  | import com.moral.api.mapper.SupervisionMapper; | 
 |  |  | import com.moral.api.service.OrganizationService; | 
 |  |  | import com.moral.api.service.SupervisionService; | 
 |  |  |  | 
 |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
 |  |  | 
 |  |  | import com.moral.constant.ResponseCodeEnum; | 
 |  |  | import com.moral.util.FileUtils; | 
 |  |  |  | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.boot.system.ApplicationHome; | 
 |  |  | import org.springframework.stereotype.Service; | 
 |  |  | import org.springframework.transaction.annotation.Transactional; | 
 |  |  | import org.springframework.util.ObjectUtils; | 
 |  |  | import org.springframework.util.ResourceUtils; | 
 |  |  | import org.springframework.web.multipart.MultipartFile; | 
 |  |  |  | 
 |  |  | import java.io.FileNotFoundException; | 
 |  |  | import java.io.File; | 
 |  |  | import java.util.ArrayList; | 
 |  |  | import java.util.HashMap; | 
 |  |  | import java.util.LinkedHashMap; | 
 |  |  | 
 |  |  |  * @since 2022-01-12 | 
 |  |  |  */ | 
 |  |  | @Service | 
 |  |  | @Slf4j | 
 |  |  | public class SupervisionServiceImpl extends ServiceImpl<SupervisionMapper, Supervision> implements SupervisionService { | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private SupervisionMapper supervisionMapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private OrganizationService organizationService; | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public Map<String, Object> add(MultipartFile[] files, Supervision supervision) { | 
 |  |  |  | 
 |  |  |         String path = null; | 
 |  |  |         try { | 
 |  |  |             path = ResourceUtils.getURL("classpath:").getPath() + "static/img/"; | 
 |  |  |         } catch (FileNotFoundException e) { | 
 |  |  |             e.printStackTrace(); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |     @Transactional | 
 |  |  |     public Map<String, Object> add(List<MultipartFile> files, Supervision supervision) { | 
 |  |  |         //获取jar包所在目录 | 
 |  |  |         ApplicationHome applicationHome = new ApplicationHome(getClass()); | 
 |  |  |         //在jar包所在目录下生成一个upload文件夹用来存储上传的图片 | 
 |  |  |         String path = applicationHome.getSource().getParentFile().toString() + "/static/img"; | 
 |  |  |  | 
 |  |  |         Map<String, Object> result = new HashMap<>(); | 
 |  |  |  | 
 |  |  |         //督办单编号唯一 | 
 |  |  |         QueryWrapper<Supervision> queryWrapper = new QueryWrapper<>(); | 
 |  |  |         queryWrapper.eq("number", supervision.getNumber()).eq("is_delete", Constants.NOT_DELETE); | 
 |  |  |         if (supervisionMapper.selectOne(queryWrapper) != null) { | 
 |  |  |             result.put("code", ResponseCodeEnum.SUPERVISION_NUMBER_EXIT.getCode()); | 
 |  |  |             result.put("msg", ResponseCodeEnum.SUPERVISION_NUMBER_EXIT.getMsg()); | 
 |  |  |             return result; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         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); | 
 |  |  |         if (files.size() > 0) { | 
 |  |  |             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; | 
 |  |  |                 } | 
 |  |  |             } else { | 
 |  |  |                 result.put("code", ResponseCodeEnum.IMG_UPLOAD_FAIl.getCode()); | 
 |  |  |                 result.put("msg", ResponseCodeEnum.IMG_UPLOAD_FAIl.getMsg()); | 
 |  |  |                 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"); | 
 |  |  |         Integer orgId = Integer.parseInt(params.get("organizationId").toString()); | 
 |  |  |  | 
 |  |  |         QueryWrapper<Supervision> queryWrapper = new QueryWrapper<>(); | 
 |  |  |         queryWrapper.eq("city_code", cityCode) | 
 |  |  |                 .eq("is_delete", Constants.NOT_DELETE); | 
 |  |  |         //获取组织下所有子组织 | 
 |  |  |         List<Organization> organizations = organizationService.getChildrenOrganizationsById(orgId); | 
 |  |  |         List<Integer> orgIds = organizations.stream().map(Organization::getId).collect(Collectors.toList()); | 
 |  |  |         orgIds.add(orgId); | 
 |  |  |  | 
 |  |  |         QueryWrapper<Supervision> supervisionQueryWrapper = new QueryWrapper<>(); | 
 |  |  |         supervisionQueryWrapper.eq("is_delete", Constants.NOT_DELETE).in("organization_id", orgIds); | 
 |  |  |         //如果指定了地区,就获取该组织及其子组织下该地区的。否则获取该组织及其子组织下所有的 | 
 |  |  |         if (params.get("cityCode") != null) { | 
 |  |  |             String cityCode = params.get("cityCode").toString(); | 
 |  |  |             //如果是市级,就查询该市下所有县市区的,否则只查询该县市区的 | 
 |  |  |             if (cityCode.endsWith("00")) { | 
 |  |  |                 supervisionQueryWrapper.eq("CONCAT(LEFT(city_code,4),'00')", cityCode); | 
 |  |  |             } else { | 
 |  |  |                 supervisionQueryWrapper.eq("city_code", cityCode); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         if (start != null) { | 
 |  |  |             queryWrapper.ge("time", start); | 
 |  |  |             supervisionQueryWrapper.ge("time", start); | 
 |  |  |         } | 
 |  |  |         if (start != null) { | 
 |  |  |             queryWrapper.le("time", end); | 
 |  |  |             supervisionQueryWrapper.le("time", end); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         //按time倒序 | 
 |  |  |         queryWrapper.orderByDesc("time"); | 
 |  |  |         supervisionQueryWrapper.orderByDesc("time"); | 
 |  |  |  | 
 |  |  |         //分页 | 
 |  |  |         Page<Supervision> supervisionPage = new Page<>(page, size); | 
 |  |  |         supervisionMapper.selectPage(supervisionPage, queryWrapper); | 
 |  |  |         supervisionMapper.selectPage(supervisionPage, supervisionQueryWrapper); | 
 |  |  |         List<Supervision> supervisions = supervisionPage.getRecords(); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         Map<String, Object> result = new LinkedHashMap<>(); | 
 |  |  |         result.put("total", supervisionPage.getTotal()); | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public Map<String, Object> updateSupervision(MultipartFile[] files, Supervision supervision) { | 
 |  |  |  | 
 |  |  |         String path = this.getClass().getClassLoader() | 
 |  |  |                 .getResource("").getFile() + "static/img/"; | 
 |  |  |     @Transactional | 
 |  |  |     public Map<String, Object> updateSupervision(List<MultipartFile> files, Supervision supervision) { | 
 |  |  |         //获取jar包所在目录 | 
 |  |  |         ApplicationHome applicationHome = new ApplicationHome(getClass()); | 
 |  |  |         //在jar包所在目录下生成一个upload文件夹用来存储上传的图片 | 
 |  |  |         String path = applicationHome.getSource().getParentFile().toString() + "/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); | 
 |  |  |         //如果上传了图片,就覆盖之前图片,删除服务器中图片 | 
 |  |  |         if (files.size() > 0) { | 
 |  |  |             //删除服务器中的原来督办单中图片 | 
 |  |  |             String[] oldImages = supervisionMapper.selectById(supervision.getId()).getImages().split(","); | 
 |  |  |             for (String image : oldImages) { | 
 |  |  |                 String realPath = path + File.separator + image; | 
 |  |  |                 File file = new File(realPath); | 
 |  |  |                 if (file.exists() && file.isFile()) { | 
 |  |  |                     file.delete(); | 
 |  |  |                 } | 
 |  |  |             } else { | 
 |  |  |                 result.put("code", ResponseCodeEnum.IMG_UPLOAD_FAIl.getCode()); | 
 |  |  |                 result.put("msg", ResponseCodeEnum.IMG_UPLOAD_FAIl.getMsg()); | 
 |  |  |                 return result; | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |             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(",")); | 
 |  |  | 
 |  |  |         supervisionMapper.updateById(supervision); | 
 |  |  |         return result; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     @Transactional | 
 |  |  |     public void deleteSupervision(Integer supervisionId) { | 
 |  |  |         QueryWrapper<Supervision> queryWrapper = new QueryWrapper<>(); | 
 |  |  |         queryWrapper.select("id", "images").eq("id", supervisionId); | 
 |  |  |         Supervision supervision = supervisionMapper.selectOne(queryWrapper); | 
 |  |  |         String[] images = supervision.getImages().split(","); | 
 |  |  |  | 
 |  |  |         //逻辑删除 | 
 |  |  |         supervision.setIsDelete(Constants.DELETE); | 
 |  |  |         supervision.setImages(null); | 
 |  |  |         supervisionMapper.updateById(supervision); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         //删除服务器中的督办单中图片 | 
 |  |  |         ApplicationHome applicationHome = new ApplicationHome(getClass()); | 
 |  |  |         String path = applicationHome.getSource().getParentFile().toString() + "/static/img"; | 
 |  |  |         for (String image : images) { | 
 |  |  |             String realPath = path + File.separator + image; | 
 |  |  |             File file = new File(realPath); | 
 |  |  |             if (file.exists() && file.isFile()) { | 
 |  |  |                 file.delete(); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  | } |