修改岗位发布和修改是上传附件
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.ruoyi.cms.controller.app;
|
||||
|
||||
import com.ruoyi.cms.util.IdGenerator;
|
||||
import com.ruoyi.common.core.domain.entity.File;
|
||||
import com.ruoyi.cms.service.IFileService;
|
||||
import com.ruoyi.common.core.controller.BaseController;
|
||||
@@ -17,10 +18,13 @@ import java.util.List;
|
||||
public class AppFileController extends BaseController {
|
||||
@Autowired
|
||||
private IFileService fileService;
|
||||
@Autowired
|
||||
private IdGenerator idGenerator;
|
||||
|
||||
@ApiOperation("上传文件")
|
||||
@PostMapping("/upload")
|
||||
public AjaxResult uploadFile(@RequestParam("file") MultipartFile file, @RequestParam(value = "bussinessid",required = false) Long bussinessId) {
|
||||
return fileService.uploadFile(file,bussinessId);
|
||||
public AjaxResult upload(@RequestParam("file") MultipartFile file, @RequestParam(value = "bussinessid",required = false) Long bussinessId) {
|
||||
return fileService.upload(file,bussinessId);
|
||||
}
|
||||
|
||||
@ApiOperation("获取附件列表")
|
||||
@@ -30,4 +34,20 @@ public class AppFileController extends BaseController {
|
||||
List<File> results = fileService.selectFileList(file);
|
||||
return getDataTable(results);
|
||||
}
|
||||
|
||||
@ApiOperation("删除附件")
|
||||
@DeleteMapping("/{id}")
|
||||
public AjaxResult remove(@PathVariable Long id)
|
||||
{
|
||||
return toAjax(fileService.deleteFileByIds(new Long[]{id}));
|
||||
}
|
||||
|
||||
@ApiOperation("上传文件")
|
||||
@PostMapping("/uploadFile")
|
||||
public AjaxResult uploadFile(@RequestParam("file") MultipartFile file, @RequestParam(value = "bussinessid",required = false) Long bussinessId) {
|
||||
if(bussinessId==null){
|
||||
bussinessId=idGenerator.generateId();
|
||||
}
|
||||
return fileService.uploadFile(file,bussinessId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.ruoyi.cms.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.ruoyi.common.core.domain.entity.File;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -19,4 +20,6 @@ public interface FileMapper extends BaseMapper<File>
|
||||
* @return 文件集合
|
||||
*/
|
||||
public List<File> selectFileList(File file);
|
||||
|
||||
public int updateBussinessids(@Param("longs") List<Long> longs,@Param("newBussinessid") Long bussinessid);
|
||||
}
|
||||
|
||||
@@ -54,5 +54,7 @@ public interface IFileService
|
||||
*/
|
||||
public int deleteFileByIds(Long[] ids);
|
||||
|
||||
AjaxResult upload(MultipartFile file, Long bussinessid);
|
||||
|
||||
AjaxResult uploadFile(MultipartFile file, Long bussinessid);
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements IF
|
||||
}
|
||||
|
||||
@Override
|
||||
public AjaxResult uploadFile(MultipartFile file, Long bussinessid) {
|
||||
public AjaxResult upload(MultipartFile file, Long bussinessid) {
|
||||
if (file.isEmpty()) {
|
||||
return AjaxResult.error("文件为空,请选择文件上传");
|
||||
}
|
||||
@@ -119,6 +119,45 @@ public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements IF
|
||||
return AjaxResult.error("文件上传失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加返回bussinessid的接口,新增时使用
|
||||
* @param file
|
||||
* @param bussinessid
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public AjaxResult uploadFile(MultipartFile file, Long bussinessid) {
|
||||
if (file.isEmpty()) {
|
||||
return AjaxResult.error("文件为空,请选择文件上传");
|
||||
}
|
||||
|
||||
try {
|
||||
// 创建上传目录
|
||||
java.io.File dir = new java.io.File(uploadDir);
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
|
||||
// 生成唯一的文件名
|
||||
String fileName = UUID.randomUUID().toString() + "_" + file.getOriginalFilename();
|
||||
Path filePath = Paths.get(uploadDir, fileName);
|
||||
|
||||
// 保存文件到服务器
|
||||
Files.copy(file.getInputStream(), filePath);
|
||||
|
||||
// 保存文件信息到数据库
|
||||
saveFileInfo(fileName, bussinessid);
|
||||
AjaxResult ajaxResult=AjaxResult.success();
|
||||
ajaxResult.put("filePath","http://39.98.44.136/file/"+fileName);
|
||||
ajaxResult.put("bussinessid",bussinessid);
|
||||
return ajaxResult;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return AjaxResult.error("文件上传失败");
|
||||
}
|
||||
}
|
||||
|
||||
private void saveFileInfo(String fileName, Long bussinessid) {
|
||||
// 这里假设你已经有了一个FileService来处理数据库操作
|
||||
File file = new File();
|
||||
|
||||
@@ -4,6 +4,7 @@ import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
@@ -275,12 +276,20 @@ public class JobServiceImpl extends ServiceImpl<JobMapper,Job> implements IJobSe
|
||||
});
|
||||
thread.start();
|
||||
if(insert>0){
|
||||
job.getJobContactList().forEach(x->{
|
||||
//添加联系人
|
||||
List<JobContact> jobContactList = job.getJobContactList() != null ? job.getJobContactList() : Collections.emptyList();
|
||||
jobContactList.forEach(x->{
|
||||
JobContact jobContact=new JobContact();
|
||||
BeanUtils.copyProperties(x,jobContact);
|
||||
jobContact.setJobId(job.getJobId());
|
||||
jobContactMapper.insert(jobContact);
|
||||
});
|
||||
//添加附件
|
||||
List<File> filesList = job.getFilesList() != null ? job.getFilesList() : Collections.emptyList();
|
||||
List<Long> longs = filesList.stream().filter(Objects::nonNull).map(File::getBussinessid).collect(Collectors.toList());
|
||||
if(!longs.isEmpty()){
|
||||
fileMapper.updateBussinessids(longs,job.getJobId());
|
||||
}
|
||||
}
|
||||
return insert;
|
||||
}
|
||||
@@ -323,15 +332,28 @@ public class JobServiceImpl extends ServiceImpl<JobMapper,Job> implements IJobSe
|
||||
int i=jobMapper.updateById(job);
|
||||
//修改岗位联系人列表
|
||||
if(i>0){
|
||||
jobContactMapper.update(null,Wrappers.<JobContact>lambdaUpdate()
|
||||
LambdaUpdateWrapper<JobContact> updateWrapper = Wrappers.<JobContact>lambdaUpdate()
|
||||
.eq(JobContact::getJobId, job.getJobId())
|
||||
.set(JobContact::getDelFlag, Constants.Del_FLAG_DELETE));
|
||||
if(Objects.isNull(job.getJobContactList())){return i;}
|
||||
job.getJobContactList().forEach(x -> {
|
||||
.set(JobContact::getDelFlag, Constants.Del_FLAG_DELETE);
|
||||
|
||||
JobContact emptyEntity = new JobContact();
|
||||
jobContactMapper.update(emptyEntity, updateWrapper);
|
||||
List<JobContact> jobContactList = job.getJobContactList() != null ? job.getJobContactList() : Collections.emptyList();
|
||||
List<JobContact> insertList = jobContactList.stream()
|
||||
.filter(Objects::nonNull).map(x -> {
|
||||
JobContact jobContact = new JobContact();
|
||||
jobContact.setJobId(job.getJobId());
|
||||
jobContactMapper.insert(jobContact);
|
||||
});
|
||||
return jobContact;
|
||||
}).collect(Collectors.toList());
|
||||
if (!insertList.isEmpty()) {
|
||||
jobContactMapper.batchInsert(insertList);
|
||||
}
|
||||
//添加附件
|
||||
List<File> filesList = job.getFilesList() != null ? job.getFilesList() : Collections.emptyList();
|
||||
List<Long> longs = filesList.stream().filter(Objects::nonNull).filter(file -> Objects.isNull(file.getId())).map(File::getBussinessid).collect(Collectors.toList());
|
||||
if(!longs.isEmpty()){
|
||||
fileMapper.updateBussinessids(longs,job.getJobId());
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.ruoyi.cms.util;
|
||||
|
||||
import cn.hutool.core.lang.Snowflake;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import org.springframework.stereotype.Component;
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
/**
|
||||
* 分布式唯一 ID 生成工具类(适配 Hutool 5.7.22)
|
||||
* 生成 Long 型 ID,适配数据库 bigint 字段,无高版本方法依赖
|
||||
*/
|
||||
@Component
|
||||
public class IdGenerator {
|
||||
|
||||
// 雪花算法实例(全局单例)
|
||||
private Snowflake snowflake;
|
||||
|
||||
/**
|
||||
* 初始化雪花算法(Spring 启动时执行,兼容 Hutool 5.7.22)
|
||||
* 核心:用 IP 哈希 + 随机数生成唯一机器码,避免高版本方法依赖
|
||||
*/
|
||||
@PostConstruct
|
||||
public void initSnowflake() {
|
||||
long workerId = generateWorkerId();
|
||||
long dataCenterId = generateDataCenterId();
|
||||
snowflake = IdUtil.createSnowflake(workerId, dataCenterId);
|
||||
System.out.printf("雪花算法初始化成功:workerId=%d,dataCenterId=%d%n", workerId, dataCenterId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 workerId(0-31):基于本地 IP 哈希,避免重复
|
||||
*/
|
||||
private long generateWorkerId() {
|
||||
try {
|
||||
// 获取本地 IP 地址(兼容本地开发、服务器环境)
|
||||
InetAddress localHost = InetAddress.getLocalHost();
|
||||
String ip = localHost.getHostAddress();
|
||||
// IP 哈希后取模 32,确保在 0-31 范围内
|
||||
return Math.abs(ip.hashCode()) % 32;
|
||||
} catch (UnknownHostException e) {
|
||||
// 异常降级:IP 获取失败时,用随机数生成(0-31)
|
||||
return (long) (Math.random() * 32);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 dataCenterId(0-31):基于系统信息哈希,与 workerId 组合确保唯一性
|
||||
*/
|
||||
private long generateDataCenterId() {
|
||||
// 取系统主机名哈希,避免与 workerId 重复
|
||||
try {
|
||||
String hostName = InetAddress.getLocalHost().getHostName();
|
||||
return Math.abs(hostName.hashCode()) % 32;
|
||||
} catch (UnknownHostException e) {
|
||||
// 异常降级:主机名获取失败时,用随机数生成(0-31)
|
||||
return (long) (Math.random() * 32);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成唯一 Long 型 ID(适配数据库 bigint)
|
||||
*/
|
||||
public Long generateId() {
|
||||
if (snowflake == null) {
|
||||
throw new RuntimeException("雪花算法未初始化,无法生成 ID");
|
||||
}
|
||||
return snowflake.nextId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 ID 字符串(前端避免精度丢失时使用)
|
||||
*/
|
||||
public String generateIdStr() {
|
||||
return generateId().toString();
|
||||
}
|
||||
}
|
||||
@@ -26,4 +26,27 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
<if test="bussinessid != null "> and bussinessid = #{bussinessid}</if>
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<update id="updateBussinessids">
|
||||
update file
|
||||
<if test="newBussinessid != null">
|
||||
set bussinessid = #{newBussinessid}
|
||||
</if>
|
||||
<if test="newBussinessid == null">
|
||||
set bussinessid = bussinessid
|
||||
where 1 = 2
|
||||
</if>
|
||||
where
|
||||
<if test="longs != null and longs.size() > 0">
|
||||
bussinessid in (
|
||||
<foreach collection="longs" item="oldId" open="(" close=")" separator=",">
|
||||
#{oldId}
|
||||
</foreach>
|
||||
)
|
||||
</if>
|
||||
<if test="longs == null or longs.size() == 0">
|
||||
1 = 2
|
||||
</if>
|
||||
</update>
|
||||
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user