mongodb文件上传

This commit is contained in:
cwchen 2024-07-16 18:16:31 +08:00
parent e1be7c7523
commit b3afa3cfd7
19 changed files with 943 additions and 17 deletions

View File

@ -40,7 +40,7 @@ public interface RemoteFileService
* @author cwchen
* @date 2024/3/21 15:24
*/
@PostMapping(value = "/file/getImgBase64")
@PostMapping(value = "/mongoFile/getImgBase64")
public R<SysFile> getImgBase64(@RequestBody String fileId, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
@ -54,7 +54,7 @@ public interface RemoteFileService
* @author cwchen
* @date 2024/3/21 15:24
*/
@PostMapping(value = "/file/getFileBast64")
@PostMapping(value = "/mongoFile/getFileBast64")
public R<SysFile> getFileBast64(@RequestBody String fileId, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
/**
@ -64,7 +64,7 @@ public interface RemoteFileService
* @param source
* @return
*/
@PostMapping(value = "/file/getFileByte")
@PostMapping(value = "/mongoFile/getFileByte")
public R<byte[]> getFileByte(@RequestBody String fileId, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
@ -78,7 +78,7 @@ public interface RemoteFileService
* @author cwchen
* @date 2024/3/21 17:25
*/
@PostMapping(value = "/file/singleUploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@PostMapping(value = "/mongoFile/singleUploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public R singleUploadFile(@RequestPart(value = "file") MultipartFile file, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
/**
@ -91,7 +91,7 @@ public interface RemoteFileService
* @author cwchen
* @date 2024/3/21 17:25
*/
@PostMapping(value = "/file/mostUploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@PostMapping(value = "/mongoFile/mostUploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public R mostUploadFile(@RequestPart(value = "files") MultipartFile[] files, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
/**
@ -104,7 +104,7 @@ public interface RemoteFileService
* @author cwchen
* @date 2024/3/21 17:27
*/
@PostMapping(value = "/file/delFile")
@PostMapping(value = "/mongoFile/delFile")
public R delFile(@RequestBody String fileId, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
/**
@ -115,6 +115,6 @@ public interface RemoteFileService
* @author cwchen
* @date 2024/6/11 10:22
*/
@PostMapping(value = "/file/uploadFileByBase64", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@PostMapping(value = "/mongoFile/uploadFileByBase64", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public R uploadFileByBase64(@RequestPart(value = "base64") String base64, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
}

View File

@ -126,6 +126,12 @@
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.18.Final</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,17 @@
package com.bonus.common.core.constant;
/**
* @className:CommonConstants
* @author:cwchen
* @date:2024-07-16-13:32
* @version:1.0
* @description:业务常量
*/
public class BusinessConstants {
public static final int TYPE = 1;
public static final int TYPE2 = 2;
/** 资源类型 1-人员*/
public static final String RESOURCE_TYPE_USER = "1";
}

View File

@ -0,0 +1,100 @@
package com.bonus.common.core.utils;
import org.springframework.web.multipart.MultipartFile;
import java.util.Arrays;
import java.util.Locale;
/**
* 文件上传校验的公共方法
*
* @authorcwchen
* @date2024-03-11-10:11
* @version1.0
* @description文件上传校验的公共方法
*/
public class UploadCheckUtils {
/**
* 文件大小 10MB(可用于图片和视频区分)
*/
public static final long IMG_FILE_SIZE = 10 * 1024 * 1024;
/**
* 只支持图片格式
*/
public static final String[] YES_IMAGE_SUPPORT = {".jpg", ".jpeg", ".png"};
/**
* 只支持视频格式
*/
public static final String[] YES_VIDEO_SUPPORT = {".mp4", ".avi", ".mp3"};
/**
* 只支持音频格式
*/
public static final String[] YES_AUDIO_SUPPORT = {".mp3"};
/**
* 只支持文件格式
*/
public static final String[] YES_FILE_SUPPORT = {".xlsx", ".xls", ".doc", ".docx", ".txt", ".csv"};
/**
* 全部文件(普通文件,图片, 视频,音频)后缀 支持的类型
*/
public static final String[] FILE_SUFFIX_SUPPORT = {".xlsx", ".xls", ".doc", ".docx", ".txt", ".csv",
".jpg", ".jpeg", ".png", ".mp4", ".avi", ".mp3"};
/**
* 文件名字 需要排除的字符
* 废弃: "(", ")","",".", "——", "_","-"
*/
public static final String[] FILE_NAME_EXCLUDE = {
"`", "!", "@", "#", "$", "%", "^", "&", "*", "=", "+",
"~", "·", "", "", "……", "", "",
"?", ",", "<", ">", ":", ";", "[", "]", "{", "}", "/", "\\", "|",
"", "", "", "", "", "", "", "", "", ""
};
public static final String SUFFIX = ".";
/**
* 图片上传文件校验大小名字后缀
*
* @param multipartFile multipartFile
*/
public static String uploadImgVerify(MultipartFile multipartFile) {
// 校验文件是否为空
if (multipartFile == null) {
return "上传图片不能为空";
}
// 校验文件名字
String originalFilename = multipartFile.getOriginalFilename();
if (originalFilename == null) {
return "上传图片名字不能为空";
}
for (String realKey : FILE_NAME_EXCLUDE) {
if (originalFilename.contains(realKey)) {
return "上传图片名称不允许出现"+realKey+"关键字";
}
}
// 校验文件后缀
if (!originalFilename.contains(SUFFIX)) {
return "图片不能没有后缀";
}
String suffix = originalFilename.substring(originalFilename.lastIndexOf('.'));
/*校验: 文件格式是否符合要求*/
if (!Arrays.asList(YES_IMAGE_SUPPORT).contains(suffix.toLowerCase(Locale.ROOT))) {
return "图片格式仅支持" + Arrays.asList(YES_IMAGE_SUPPORT).toString();
}
Long fileSize = multipartFile.getSize();
if (fileSize > IMG_FILE_SIZE) {
return "图片大小仅支持10MB以内";
}
return null;
}
}

View File

@ -11,6 +11,11 @@ import lombok.Data;
*/
@Data
public class BraceletParamsDto {
/**
* id
*/
private Long id;
/**
* 班组名称
*/
@ -20,6 +25,16 @@ public class BraceletParamsDto {
*/
private String teamLeader;
/**
* 姓名
*/
private String name;
/**
* 性别
*/
private Integer sex;
/**
* 角色编码
*/
@ -33,5 +48,10 @@ public class BraceletParamsDto {
/**
* 班组ID
*/
private Integer teamId;
private Long teamId;
/**
* 资源类型
*/
private String sourceType;
}

View File

@ -2,7 +2,12 @@ package com.bonus.common.entity.bracelet.vo;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.Date;
/**
@ -22,37 +27,49 @@ public class PersonVo {
/**
* 姓名
*/
@NotBlank(message = "姓名不能为空", groups = {Query.class})
@Length(max = 32, message = "姓名字符长度不能超过32", groups = {Query.class})
private String name;
/**
* 性别 0 1 2 其他
*/
@NotNull(message = "性别不能为空", groups = {Query.class})
@Range(min = 0, max = 2, message = "性别: 0 女 1 男 2 其他", groups = {Query.class})
private Integer sex;
/**
* 身份证号码
*/
@NotBlank(message = "身份证号码不能为空", groups = {Query.class})
@Length(max = 20, message = "身份证号码字符长度不能超过20", groups = {Query.class})
@Pattern(regexp = "[1-9]\\d{5}(19|20)\\d{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2]\\d|3[0-1])|(04|06|09|11)(0[1-9]|[1-2]\\d|30)|02(0[1-9]|[1-2]\\d))\\d{3}[\\dXx]", message = "身份证号码格式不正确", groups = {Query.class})
private String idCard;
/**
* 手机号
* 电话
*/
@NotBlank(message = "电话不能为空", groups = {Query.class})
@Length(max = 20, message = "电话字符长度不能超过20", groups = {Query.class})
@Pattern(regexp = "(0|86|17951)?(13[0-9]|19[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}", message = "电话格式不正确", groups = {Query.class})
private String phone;
/**
* 安全帽编号
*/
@Length(max = 10, message = "安全帽编号字符长度不能超过20", groups = {Query.class})
private String aqmCode;
/**
* 马甲编号
*/
@Length(max = 10, message = "马甲编号字符长度不能超过20", groups = {Query.class})
private String mjCode;
/**
* 创建时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
private Date createTime = new Date();
/**
* 修改时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
private Date updateTime = new Date();
/**
* 创建人
*/
@ -65,4 +82,30 @@ public class PersonVo {
* 所属班组ID
*/
private Long teamId;
/**
* 删除的文件ID
*/
private String delFiles;
/**
* 删除的资源文件ID
*/
private Long fileId;
/**
* 文件ID
*/
private String filePath;
/**
* 文件BASE64地址
*/
private String base64Url;
/**
* 查询条件限制
*/
public interface Query {
}
}

View File

@ -0,0 +1,49 @@
package com.bonus.common.entity.file;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
/**
* @authorcwchen
* @date2024-03-12-9:29
* @version1.0
* @description文件资源-vo
*/
@Data
public class ResourceFileVo {
@ApiModelProperty(value = "文件名称")
private String fileName;
@ApiModelProperty(value = "文件后缀")
private String fileSuffix;
@ApiModelProperty(value = "文件id/路径")
private String filePath;
@ApiModelProperty(value = "文件类型 0 文件 1图片")
private Integer fileType;
@ApiModelProperty(value = "业务id")
private String sourceId;
@ApiModelProperty(value = "业务类型")
private String sourceType;
@ApiModelProperty(value = "创建日期")
private Date createTime = new Date();
@ApiModelProperty(value = "创建人")
private Long createUser;
@ApiModelProperty(value = "修改日期")
private Date updateTime = new Date();
@ApiModelProperty(value = "修改人")
private Long updateUser;
@ApiModelProperty(value = "是否删除")
private Integer delFlag = 0;
}

View File

@ -0,0 +1,100 @@
package com.bonus.common.security.utils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.List;
import java.util.Set;
/**
* @description: <br/>
* 通用Server validation方法
* <p>
* <br/>
* @author: Qz1997
* @create 2021/2/9 14:41
*/
@SuppressWarnings("unused")
@Component(value = "ValidatorsUtils")
public final class ValidatorsUtils {
@Resource
private Validator validator;
/**
* 验证实体
*
* @param obj 实体
* @param <T> 实体类类型
* @return 结果
*/
public <T> String valid(T obj) {
return this.valid(obj, new Class<?>[]{});
}
/**
* 验证实体
*
* @param obj 实体
* @param group 实体组
* @param <T> 实体类类型
* @return 结果
*/
public <T> String valid(T obj, Class<?>... group) {
Set<ConstraintViolation<T>> violations;
if (ArrayUtils.isEmpty(group)) {
violations = validator.validate(obj);
} else {
violations = validator.validate(obj, group);
}
if (CollectionUtils.isNotEmpty(violations)) {
for (ConstraintViolation<T> constraintViolation : violations) {
return constraintViolation.getMessage();
}
}
return null;
}
/**
* 校验list
*
* @param objList list
* @param <T> 实体类类型
* @return 结果
*/
public <T> String validList(List<T> objList) {
return this.validList(objList, new Class<?>[]{});
}
/**
* 校验list
*
* @param objList list
* @param group
* @param <T> 实体类类型
* @return 结果
*/
public <T> String validList(List<T> objList, Class<?>... group) {
if (CollectionUtils.isEmpty(objList)) {
return "对象空";
}
String result;
for (T t : objList) {
if (ArrayUtils.isEmpty(group)) {
result = this.valid(t);
} else {
result = this.valid(t, group);
}
if (!StringUtils.isBlank(result)) {
return result;
}
}
return null;
}
}

View File

@ -4,3 +4,4 @@ com.bonus.common.security.service.TokenService
com.bonus.common.security.aspect.PreAuthorizeAspect
com.bonus.common.security.aspect.InnerAuthAspect
com.bonus.common.security.handler.GlobalExceptionHandler
com.bonus.common.security.utils.ValidatorsUtils

View File

@ -88,6 +88,12 @@
<version>24.6.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.bonus</groupId>
<artifactId>bonus-common-security</artifactId>
<version>24.6.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>

View File

@ -5,6 +5,7 @@ import com.bonus.common.security.annotation.EnableRyFeignClients;
import com.bonus.common.swagger.annotation.EnableCustomSwagger2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
/**
* 手环模块
@ -14,7 +15,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableCustomConfig
@EnableCustomSwagger2
@EnableRyFeignClients
@SpringBootApplication
@SpringBootApplication(exclude = MongoAutoConfiguration.class )
public class BonusBraceletApplication {
public static void main(String[] args) {
SpringApplication.run(BonusBraceletApplication.class, args);

View File

@ -1,11 +1,21 @@
package com.bonus.bracelet.controller;
import com.bonus.bracelet.service.IPersonMgeService;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.entity.bracelet.BraceletParamsDto;
import com.bonus.common.entity.bracelet.vo.PersonVo;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.common.security.annotation.RequiresPermissions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* @className:PersonMgeController
@ -17,8 +27,43 @@ import javax.annotation.Resource;
@RestController
@RequestMapping("/person/")
@Slf4j
public class PersonMgeController {
public class PersonMgeController extends BaseController {
@Resource(name = "IPersonMgeService")
private IPersonMgeService service;
@RequiresPermissions("basic:person:list")
@GetMapping("list")
@SysLog(title = "人员管理", businessType = OperaType.QUERY,logType = 0,module = "基础管理->人员管理",details ="查询人员列表")
public TableDataInfo list(BraceletParamsDto dto) {
startPage();
List<PersonVo> list = service.getPersonLists(dto);
return getDataTable(list);
}
@RequiresPermissions("basic:person:add")
@PostMapping("addPerson")
@SysLog(title = "人员管理", businessType = OperaType.INSERT,logType = 0,module = "基础管理->人员管理",details ="新增人员" )
public AjaxResult add(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file, String params) {
return service.addPerson(file, params);
}
@RequiresPermissions("basic:person:edit")
@PostMapping("editPerson")
@SysLog(title = "人员管理", businessType = OperaType.INSERT,logType = 0,module = "基础管理->人员管理",details ="修改人员" )
public AjaxResult editPerson(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file, String params) {
return service.editPerson(file, params);
}
@PostMapping("getPersonInfo")
public AjaxResult getPersonInfo(@RequestBody BraceletParamsDto dto) {
return service.getPersonInfo(dto);
}
@RequiresPermissions("basic:person:del")
@PostMapping("delPerson")
@SysLog(title = "人员管理", businessType = OperaType.INSERT,logType = 0,module = "基础管理->人员管理",details ="删除人员" )
public AjaxResult delPerson(@RequestBody BraceletParamsDto dto) {
return service.delPerson(dto);
}
}

View File

@ -1,7 +1,13 @@
package com.bonus.bracelet.mapper;
import com.bonus.common.entity.bracelet.BraceletParamsDto;
import com.bonus.common.entity.bracelet.vo.PersonVo;
import org.apache.ibatis.annotations.MapKey;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
/**
* @className:PersonMgeMapper
* @author:cwchen
@ -11,4 +17,63 @@ import org.springframework.stereotype.Repository;
*/
@Repository(value = "PersonMgeMapper")
public interface PersonMgeMapper {
/**
* 人员身份证号是否存在
*
* @param vo
* @return List<Map < String, String>>
* @author cwchen
* @date 2024/7/16 13:15
*/
@MapKey("id")
List<Map<String, String>> personIsExist(PersonVo vo);
/**
* 新增人员
*
* @param vo
* @return void
* @author cwchen
* @date 2024/7/16 14:26
*/
void addPerson(PersonVo vo);
/**
* 修改人员
*
* @param vo
* @return void
* @author cwchen
* @date 2024/7/16 14:35
*/
void editPerson(PersonVo vo);
/**
* 人员详情
*
* @param dto
* @return PersonVo
* @author cwchen
* @date 2024/7/16 15:15
*/
PersonVo getPersonInfo(BraceletParamsDto dto);
/**
* 删除人员
*
* @param dto
* @return void
* @author cwchen
* @date 2024/7/16 15:38
*/
void delPerson(BraceletParamsDto dto);
/**
* 查询人员列表
* @param dto
* @return List<PersonVo>
* @author cwchen
* @date 2024/7/16 15:45
*/
List<PersonVo> getPersonLists(BraceletParamsDto dto);
}

View File

@ -0,0 +1,34 @@
package com.bonus.bracelet.mapper;
import com.bonus.common.entity.bracelet.vo.PersonVo;
import com.bonus.common.entity.file.ResourceFileVo;
import org.springframework.stereotype.Repository;
/**
* @className:ResourceFileMapper
* @author:cwchen
* @date:2024-07-16-13:57
* @version:1.0
* @description:文件上传-mapper
*/
@Repository(value = "ResourceFileMapper")
public interface ResourceFileMapper {
/**
* 添加文件资源
*
* @param fileVo
* @return void
* @author cwchen
* @date 2024/7/16 14:01
*/
void addFile(ResourceFileVo fileVo);
/**
* 删除资源文件
* @param vo
* @return void
* @author cwchen
* @date 2024/7/16 15:07
*/
void delResourceFile(PersonVo vo);
}

View File

@ -1,5 +1,12 @@
package com.bonus.bracelet.service;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.entity.bracelet.BraceletParamsDto;
import com.bonus.common.entity.bracelet.vo.PersonVo;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
/**
* @className:PersonMgeService
* @author:cwchen
@ -7,5 +14,47 @@ package com.bonus.bracelet.service;
* @version:1.0
* @description:人员-service
*/
public interface IPersonMgeService{
public interface IPersonMgeService {
/**
* 新增人员
*
* @param file
* @param params
* @return AjaxResult
* @author cwchen
* @date 2024/7/16 12:57
*/
AjaxResult addPerson(MultipartFile file, String params);
/**
* 修改人员
*
* @param file
* @param params
* @return AjaxResult
* @author cwchen
* @date 2024/7/16 14:28
*/
AjaxResult editPerson(MultipartFile file, String params);
/**
* 人员详情
*
* @param dto
* @return AjaxResult
* @author cwchen
* @date 2024/7/16 15:10
*/
AjaxResult getPersonInfo(BraceletParamsDto dto);
/**
* 删除人员
* @param dto
* @return AjaxResult
* @author cwchen
* @date 2024/7/16 15:34
*/
AjaxResult delPerson(BraceletParamsDto dto);
List<PersonVo> getPersonLists(BraceletParamsDto dto);
}

View File

@ -1,11 +1,37 @@
package com.bonus.bracelet.service.impl;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.bonus.bracelet.mapper.PersonMgeMapper;
import com.bonus.bracelet.mapper.ResourceFileMapper;
import com.bonus.bracelet.service.IPersonMgeService;
import com.bonus.common.core.constant.BusinessConstants;
import com.bonus.common.core.constant.HttpStatus;
import com.bonus.common.core.constant.SecurityConstants;
import com.bonus.common.core.domain.R;
import com.bonus.common.core.utils.UploadCheckUtils;
import com.bonus.common.core.utils.encryption.Sm4Utils;
import com.bonus.common.core.utils.uuid.IdUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.entity.bracelet.BraceletParamsDto;
import com.bonus.common.entity.bracelet.vo.PersonVo;
import com.bonus.common.entity.file.ResourceFileVo;
import com.bonus.common.security.utils.ValidatorsUtils;
import com.bonus.system.api.RemoteFileService;
import com.bonus.system.api.domain.SysFile;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* @className:PersonMgeServiceImpl
@ -15,11 +41,241 @@ import javax.annotation.Resource;
* @description:人员-serviceImpl
*/
@Service(value = "IPersonMgeService")
@Slf4j
public class PersonMgeServiceImpl implements IPersonMgeService {
@Resource(name = "PersonMgeMapper")
private PersonMgeMapper mapper;
@Resource(name = "ResourceFileMapper")
private ResourceFileMapper resourceFileMapper;
@Resource(name = "ValidatorsUtils")
private ValidatorsUtils validatorsUtils;
@Resource
private RemoteFileService remoteFileService;
@Override
public List<PersonVo> getPersonLists(BraceletParamsDto dto) {
dto.setSourceType(BusinessConstants.RESOURCE_TYPE_USER);
List<PersonVo> list = new ArrayList<>();
try {
list = mapper.getPersonLists(dto);
for (PersonVo vo : list) {
vo = handleData(vo);
}
} catch (Exception e) {
log.error("查询人员列表",e);
}
return list;
}
@Override
@Transactional(rollbackFor = Exception.class)
public AjaxResult addPerson(MultipartFile file, String params) {
// 文件ID
String delFileId = null;
try {
PersonVo vo = JSON.parseObject(params, PersonVo.class);
String validResult = validatorsUtils.valid(vo, PersonVo.Query.class);
if (StringUtils.isNotBlank(validResult)) {
return AjaxResult.error(validResult);
}
// 验证身份证是否重复手机号
List<Map<String, String>> list = mapper.personIsExist(vo);
if (idCardIsExist(list, vo, 1)) {
return AjaxResult.error("身份证号码" + vo.getIdCard() + "已存在");
}
if (idCardIsExist(list, vo, 2)) {
return AjaxResult.error("电话" + vo.getPhone() + "已存在");
}
// 校验文件上传文件
if (file == null) {
return AjaxResult.error("人员照片未上传");
} else {
String isVerify = UploadCheckUtils.uploadImgVerify(file);
if (StringUtils.isNotBlank(isVerify)) {
return AjaxResult.error(isVerify);
}
}
// 对身份证号手机号 进行 sm4加密 保存人员数据保存文件资源
vo.setIdCard(Sm4Utils.encode(vo.getIdCard()));
vo.setPhone(Sm4Utils.encode(vo.getPhone()));
mapper.addPerson(vo);
delFileId = uploadFile(file, vo);
} catch (Exception e) {
log.error("新增人员", e);
// 添加失败-删除文件
if (StringUtils.isNotEmpty(delFileId)) {
remoteFileService.delFile(delFileId, SecurityConstants.INNER);
}
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return AjaxResult.error();
}
return AjaxResult.success();
}
@Override
@Transactional(rollbackFor = Exception.class)
public AjaxResult editPerson(MultipartFile file, String params) {
// 文件ID
String delFileId = null;
try {
PersonVo vo = JSON.parseObject(params, PersonVo.class);
String validResult = validatorsUtils.valid(vo, PersonVo.Query.class);
if (StringUtils.isNotBlank(validResult)) {
return AjaxResult.error(validResult);
}
// 验证身份证是否重复手机号
List<Map<String, String>> list = mapper.personIsExist(vo);
if (idCardIsExist(list, vo, 1)) {
return AjaxResult.error("身份证号码" + vo.getIdCard() + "已存在");
}
if (idCardIsExist(list, vo, 2)) {
return AjaxResult.error("电话" + vo.getPhone() + "已存在");
}
// 校验文件上传文件
if (file == null && StringUtils.isNotBlank(vo.getDelFiles())) {
return AjaxResult.error("人员照片未上传");
} else if (file != null) {
String isVerify = UploadCheckUtils.uploadImgVerify(file);
if (StringUtils.isNotBlank(isVerify)) {
return AjaxResult.error(isVerify);
}
}
// 对身份证号手机号 进行 sm4加密 保存人员数据 保存文件资源 删除文件
vo.setIdCard(Sm4Utils.encode(vo.getIdCard()));
vo.setPhone(Sm4Utils.encode(vo.getPhone()));
mapper.editPerson(vo);
delFileId = uploadFile(file, vo);
if (StringUtils.isNotEmpty(vo.getDelFiles())) {
String[] delFiles = vo.getDelFiles().split(",");
for (String fileId : delFiles) {
resourceFileMapper.delResourceFile(vo);
remoteFileService.delFile(fileId, SecurityConstants.INNER);
}
}
} catch (Exception e) {
log.error("修改人员", e);
// 添加失败-删除文件
if (StringUtils.isNotEmpty(delFileId)) {
remoteFileService.delFile(delFileId, SecurityConstants.INNER);
}
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return AjaxResult.error();
}
return AjaxResult.success();
}
@Override
public AjaxResult getPersonInfo(BraceletParamsDto dto) {
try {
dto.setSourceType(BusinessConstants.RESOURCE_TYPE_USER);
PersonVo vo = mapper.getPersonInfo(dto);
PersonVo handleData = handleData(vo);
return AjaxResult.success(handleData);
} catch (Exception e) {
log.error("人员详情", e);
return AjaxResult.error();
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public AjaxResult delPerson(BraceletParamsDto dto) {
try {
if (dto.getTeamId() != null) {
return AjaxResult.error("该人员已分配班组,请先从班组中移除该人员");
}
mapper.delPerson(dto);
return AjaxResult.success();
} catch (Exception e) {
log.error("删除人员", e);
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return AjaxResult.error();
}
}
/**
* 验证身份证号是否存在
*
* @param list
* @return boolean
* @author cwchen
* @date 2024/7/16 13:21
*/
public boolean idCardIsExist(List<Map<String, String>> list, PersonVo vo, int type) {
if (CollectionUtils.isEmpty(list)) {
return false;
} else {
String encodeValue = Sm4Utils.encode(type == BusinessConstants.TYPE ? vo.getIdCard() : vo.getPhone());
for (Map<String, String> map : list) {
String value = type == BusinessConstants.TYPE ? map.get("value") : map.get("phone");
if (Objects.equals(encodeValue, map.get("value"))) {
return true;
}
}
return false;
}
}
/**
* 文件上传
*
* @param file
* @param vo
* @return void
* @author cwchen
* @date 2024/7/16 14:46
*/
public String uploadFile(MultipartFile file, PersonVo vo) {
R result = remoteFileService.singleUploadFile(file, SecurityConstants.INNER);
if (result != null && result.getCode() == HttpStatus.ERROR) {
log.error("人员照片上传失败");
return null;
} else if (result != null && result.getCode() == HttpStatus.SUCCESS && result.getData() != null) {
String jsonString = JSON.toJSONString(result.getData());
JSONObject item = JSON.parseObject(jsonString);
if (item != null) {
ResourceFileVo fileVo = setResourceFileData(item, vo);
resourceFileMapper.addFile(fileVo);
}
return item.getString("fileId");
}
return null;
}
public PersonVo handleData(PersonVo vo){
vo.setIdCard(Sm4Utils.decode(vo.getIdCard()));
vo.setPhone(Sm4Utils.decode(vo.getPhone()));
R<SysFile> result = remoteFileService.getImgBase64(vo.getFilePath(), SecurityConstants.INNER);
if (result != null && result.getCode() == HttpStatus.SUCCESS && result.getData() != null) {
String jsonString = JSON.toJSONString(result.getData());
JSONObject item = JSON.parseObject(jsonString);
String base64 = item.getString("url");
vo.setBase64Url(base64);
}
return vo;
}
/**
* 资源文件赋值
*
* @param item
* @return ResourceFileVo
* @author cwchen
* @date 2024/7/16 13:49
*/
public ResourceFileVo setResourceFileData(JSONObject item, PersonVo vo) {
ResourceFileVo fileVo = new ResourceFileVo();
String resourceId = IdUtils.simpleUUID();
fileVo.setFileType(1);
fileVo.setFilePath(item.getString("fileId"));
fileVo.setFileSuffix(item.getString("suffix"));
fileVo.setFileName(item.getString("fileName"));
fileVo.setSourceId(vo.getId() + "");
fileVo.setSourceType("1");
return fileVo;
}
}

View File

@ -3,5 +3,98 @@
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bonus.bracelet.mapper.PersonMgeMapper">
<!--新增人员-->
<insert id="addPerson" useGeneratedKeys="true" keyProperty="id">
INSERT INTO tb_people
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name != null and name != ''">name,</if>
<if test="sex != null">sex,</if>
<if test="idCard != null and idCard != ''">id_card,</if>
<if test="phone != null and phone!=''">phone,</if>
<if test="aqmCode != null and aqmCode!=''">aqm_code,</if>
<if test="mjCode != null and mjCode!=''">mj_code,</if>
create_time,
create_user,
update_time,
update_user,
del_flag,
id
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="name != null and name != ''">#{name},</if>
<if test="sex != null">#{sex},</if>
<if test="idCard != null and idCard != ''">#{idCard},</if>
<if test="phone != null and phone!=''">#{phone},</if>
<if test="aqmCode != null and aqmCode!=''">#{aqmCode},</if>
<if test="mjCode != null and mjCode!=''">#{mjCode},</if>
#{createTime},
#{createUser},
#{updateTime},
#{updateUser},
0,
null
</trim>
</insert>
<!--修改人员-->
<update id="editPerson">
UPDATE tb_people SET name = #{name},sex = #{sex},id_card = #{idCard},phone = #{phone},aqm_code = #{aqmCode},mj_code = #{mjCode},update_time = #{updateTime},update_user = #{updateUser} WHERE id = #{id}
</update>
<!--删除人员-->
<update id="delPerson">
UPDATE tb_people SET del_flag = 1 WHERE id = #{id}
</update>
<!--人员身份证号是否存在-->
<select id="personIsExist" resultType="java.util.Map">
<if test="id == null">
SELECT id,id_card AS value,phone FROM tb_people WHERE del_flag = 0
</if>
<if test="id != null">
SELECT id,id_card AS value,phone FROM tb_people WHERE id != #{id} AND del_flag = 0
</if>
</select>
<!--人员详情-->
<select id="getPersonInfo" resultType="com.bonus.common.entity.bracelet.vo.PersonVo">
SELECT tp.id,
tp.name,
tp.sex,
tp.id_card AS idCard,
tp.phone,
tp.aqm_code AS aqmCode,
tp.mj_code AS mjCode,
sfs.file_path AS filePath,
sfs.id AS fileId
FROM tb_people tp
LEFT JOIN sys_file_source sfs ON tp.id = sfs.source_id AND sfs.source_type = #{sourceType} AND sfs.del_flag = 0
WHERE tp.id = #{id} AND tp.del_flag = 0
</select>
<!--查询人员列表-->
<select id="getPersonLists" resultType="com.bonus.common.entity.bracelet.vo.PersonVo">
SELECT tp.id,
tp.id_card AS idCard,
tp.phone,
tp.aqm_code AS aqmCode,
tp.mj_code AS mjCode,
sfs.file_path AS filePath,
sfs.id AS fileId
FROM tb_people tp
LEFT JOIN sys_file_source sfs ON tp.id = sfs.source_id AND sfs.source_type = #{sourceType} AND sfs.del_flag = 0
WHERE tp.del_flag = 0
<if test="name != null and name!=''">
AND INSTR(name,#{name}) > 0
</if>
<if test="sex != null">
AND tp.sex = #{sex}
</if>
<if test="roleCode == 'administrators'">
</if>
<if test="roleCode == 'depart'">
</if>
<if test="roleCode == 'team'">
</if>
ORDER BY create_time
</select>
</mapper>

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bonus.bracelet.mapper.ResourceFileMapper">
<!--添加文件资源-->
<insert id="addFile">
INSERT INTO sys_file_source
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="fileName != null and fileName!=''">file_name,</if>
<if test="fileSuffix != null and fileSuffix!=''">file_suffix,</if>
<if test="filePath != null and filePath!=''">file_path,</if>
<if test="fileType != null and fileType!=''">file_type,</if>
<if test="sourceId != null and sourceId!=''">source_id,</if>
<if test="sourceType != null and sourceType!=''">source_type,</if>
create_time,
create_user,
update_time,
update_user,
del_flag
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="fileName != null and fileName!=''">#{fileName},</if>
<if test="fileSuffix != null and fileSuffix!=''">#{fileSuffix},</if>
<if test="filePath != null and filePath!=''">#{filePath},</if>
<if test="fileType != null and fileType!=''">#{filePath},</if>
<if test="sourceId != null and sourceId!=''">#{sourceId},</if>
<if test="sourceType != null and sourceType!=''">#{sourceType},</if>
#{createTime},
#{createUser},
#{updateTime},
#{updateUser},
#{delFlag}
</trim>
</insert>
<!--删除资源文件-->
<update id="delResourceFile">
UPDATE sys_file_source SET del_flag = 1 WHERE id = #{id}
</update>
</mapper>

View File

@ -1,6 +1,6 @@
package com.bonus.file.mongodb.util;
import com.securitycontrol.entity.file.FileExportVo;
import com.bonus.common.entity.file.FileExportVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Base64Utils;