package com.fzzy.igds.app.v1.manager; import com.alibaba.fastjson.JSONObject; import com.fzzy.common.constant.RespCodeEnum; import com.fzzy.igds.app.v1.data.AuthUser; import com.fzzy.igds.app.v1.data.PhoneRequest; import com.fzzy.igds.app.v1.data.PhoneResponse; import com.fzzy.igds.app.v1.dto.PUploadDto; import com.fzzy.igds.app.v1.service.PhoneService; import com.fzzy.igds.app.v1.util.PhoneRespUtil; import com.fzzy.igds.app.v1.util.PhoneUtil; import com.fzzy.igds.data.PageResponse; import com.fzzy.igds.service.FileService; import com.fzzy.igds.utils.ContextUtil; import com.fzzy.igds.utils.DateUtil; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.StringUtils; import com.sun.jna.platform.mac.MacFileUtils; import org.apache.commons.lang3.time.DateFormatUtils; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * @Author: YYC * @Description: * @DateTime: 2026-1-14 11:47 **/ @Component(PhoneManager.BEAN_ID) public class PhoneManager implements ApplicationContextAware { public static final String BEAN_ID = "phone.phoneManager"; private static Map serviceMap; @Autowired private RedisCache redisUtil; @Resource private FileService fileService; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { Map map = applicationContext .getBeansOfType(PhoneService.class); serviceMap = new HashMap<>(); for (String key : map.keySet()) { serviceMap.put(map.get(key).getInterfaceId(), map.get(key)); } } /** * 业务执行入口 * * @param req * @param * @return * @throws Exception */ @SuppressWarnings("unchecked") public PhoneResponse execute(PhoneRequest req, AuthUser authUser) throws Exception { PhoneService service = serviceMap.get(req.getInterfaceId()); if (null == service) { return PhoneRespUtil.error(RespCodeEnum.CODE_1111, "当前接口没有找到实现类,无法执行处理"); } return service.execute(req, authUser); } /** * 根据tokenAuth获取缓存中的tokenUser * * @param req * @return */ public AuthUser checkTokenAuth(PhoneRequest req) { String tokenAuth = req.getTokenAuth(); return redisUtil.getCacheObject(PhoneUtil.createKey(tokenAuth)); } /** * 值仓图片上传 * * @param file * @param req * @return * @throws IOException */ @SuppressWarnings("unchecked") public PhoneResponse phoneUploadImg(MultipartFile file, HttpServletRequest req) throws IOException { String plateNum = req.getParameter("plateNum"); PageResponse result = upLoadInoutImg(file, plateNum); if (result.getCode().equals(RespCodeEnum.CODE_0000.getCode())) { //上传成功 PUploadDto dtoUpload = new PUploadDto(); dtoUpload.setStatus("success"); dtoUpload.setMsg("上传成功!"); dtoUpload.setFileName(result.getMsg()); return PhoneRespUtil.success(dtoUpload); } else { //上传失败 return PhoneRespUtil.error(RespCodeEnum.CODE_1111, "上传失败"); } } /** * 值仓图片上传 * * @param file * @param req * @return * @throws IOException */ @SuppressWarnings("unchecked") public PhoneResponse phonePatrol(MultipartFile file, HttpServletRequest req) throws IOException { String deptId = req.getParameter("deptId"); PageResponse result = upLoadPatrolImg(file, deptId); if (result.getCode().equals(RespCodeEnum.CODE_0000.getCode())) { //上传成功 PUploadDto dtoUpload = new PUploadDto(); dtoUpload.setStatus("success"); dtoUpload.setMsg("上传成功!"); dtoUpload.setFileName(result.getMsg()); return PhoneRespUtil.success(dtoUpload); } else { //上传失败 return PhoneRespUtil.error(RespCodeEnum.CODE_1111, "上传失败"); } } /** * 出入库的附件上次,参数是车牌号 * * @param file * @return * @throws IOException */ public PageResponse upLoadInoutImg(MultipartFile file, String plateNum) throws IOException { // 获取默认编码的字节数组 byte[] fileByte = file.getBytes(); // 获取文件的源文件名称 String oldFileName = file.getOriginalFilename(); // 获取文件保存路径 String filePath = fileService.getInoutFilePath(); // 获取新的ID String newFileName = DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSS"); // 文件后缀名 String suffixName = oldFileName.substring(oldFileName.lastIndexOf(".")); newFileName = newFileName + suffixName; // 文件上传 boolean flag = uploadFile(fileByte, filePath, newFileName); // 判断文件上传是否成功,成功返回文件名 if (flag) { return new PageResponse(RespCodeEnum.CODE_0000.getCode(), newFileName); } else { return new PageResponse(RespCodeEnum.CODE_1111.getCode(), "上传失败"); } } /** * 打卡的附件上次,参数是车牌号 * * @param file * @return * @throws IOException */ public PageResponse upLoadPatrolImg(MultipartFile file, String deptId) throws IOException { // 获取默认编码的字节数组 byte[] fileByte = file.getBytes(); // 获取文件的源文件名称 String oldFileName = file.getOriginalFilename(); // 获取文件保存路径 String filePath = fileService.getPatrolFilePath(); // 获取新的ID String newFileName = DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSS"); // 文件后缀名 String suffixName = oldFileName.substring(oldFileName.lastIndexOf(".")); // 合成新的文件名 if (StringUtils.isEmpty(deptId)) { newFileName = newFileName + suffixName; } else { newFileName = deptId + "_" + newFileName + suffixName; } // 文件上传 boolean flag = uploadFile(fileByte, filePath, newFileName); // 判断文件上传是否成功,成功返回文件名 if (flag) { return new PageResponse(RespCodeEnum.CODE_0000.getCode(), newFileName); } else { return new PageResponse(RespCodeEnum.CODE_1111.getCode(), "上传失败"); } } /** * 文件流上传文件 * * @param file * @param filePath * @param fileName * @return */ public static boolean uploadFile(byte[] file, String filePath, String fileName) throws IOException { // 默认文件上传成功 boolean flag = true; // new一个文件对象实例 File targetFile = new File(filePath); // 如果当前文件目录不存在就自动创建该文件或者目录 if (!targetFile.exists()) { targetFile.mkdirs(); } // 创建文件流 FileOutputStream fileOutputStream = new FileOutputStream(filePath + fileName); try { fileOutputStream.write(file); } catch (FileNotFoundException e) { flag = false; } catch (IOException ioException) { flag = false; } finally { fileOutputStream.flush(); fileOutputStream.close(); } return flag; } }