package com.whyc.service;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.whyc.dto.Response;
|
import com.whyc.dto.paramter.UserWorkAlarmParam;
|
import com.whyc.mapper.UserWorkMapper;
|
import com.whyc.mapper.WorkAlarmMapper;
|
import com.whyc.pojo.UserWork;
|
import com.whyc.util.ActionUtil;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.boot.system.ApplicationHome;
|
import org.springframework.core.env.Environment;
|
import org.springframework.stereotype.Service;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.annotation.Resource;
|
import java.io.*;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
|
@Service
|
public class UserWorkService {
|
@Resource
|
private UserWorkMapper mapper;
|
@Resource
|
private WorkAlarmMapper workAlarmMapper;
|
@Autowired
|
private Environment environment;
|
|
public Response addOrUpdate(UserWork userWork){
|
if (userWork.getId()==null || userWork.getId()==0){
|
mapper.insert(userWork);
|
}else {
|
mapper.updateById(userWork);
|
}
|
//当状态为-1时,驳回操作,需要重新创建已派单订单;如果是3则为完成 同时更改wolkAlarm表的status
|
if ("-1".equals(userWork.getCheckStatus())||userWork.getCheckStatus()==-1){
|
UserWork uw = new UserWork();
|
uw.setWorkId(userWork.getWorkId());
|
uw.setUserId(userWork.getUserId());
|
uw.setCreateTime(new Date());
|
uw.setEndTime(new Date());
|
uw.setCheckStatus(1);
|
if(mapper.insert(uw)>0){
|
return new Response().set(1,true,"操作成功");
|
}else {
|
return new Response().set(1,false,"操作失败");
|
}
|
}
|
if ("3".equals(userWork.getCheckStatus())|| userWork.getCheckStatus()==3){
|
int bool = workAlarmMapper.updateStatus(userWork.getWorkId(),3);
|
if (bool>0){
|
return new Response().set(1,true,"操作成功");
|
}else {
|
return new Response().set(1,false,"操作失败");
|
}
|
}
|
return new Response().set(1,true,"操作成功");
|
}
|
|
public Response searchByCondition(UserWork userWork){
|
QueryWrapper<UserWork> queryWrapper = new QueryWrapper<>();
|
queryWrapper.eq(userWork.getUserId() != null && userWork.getUserId() != 0, "userId", userWork.getUserId());
|
queryWrapper.eq(userWork.getManagerId() != null && userWork.getManagerId() != 0, "managerId", userWork.getManagerId());
|
queryWrapper.eq(userWork.getWorkId() != null && userWork.getWorkId() != 0, "workId", userWork.getWorkId());
|
List<UserWork> list = mapper.selectList(queryWrapper);
|
return new Response().set(1, list, "查询成功");
|
}
|
|
|
|
public Response uploadAlarmFile(List<MultipartFile> file, UserWorkAlarmParam param) {
|
String fileDirName = "";
|
int configType = Integer.parseInt(environment.getProperty("configFile.type"));
|
ApplicationHome applicationHome = new ApplicationHome(getClass());
|
File jarFile = applicationHome.getDir();
|
//测试版
|
if(configType==1){
|
fileDirName = jarFile.getParentFile().toString();
|
}else{
|
//打包版
|
fileDirName = jarFile.toString();
|
}
|
String root=fileDirName+"/fg_photo/stationsrc/alarm/"+ param.getStationId() + "/" + param.getAfterOrBefore() + "/";
|
List<String> filePathList = new ArrayList<>();
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
|
boolean isSuccess = false;
|
try {
|
for (int i = 0; i < file.size() && param != null; i++) {
|
String fileFileName = file.get(i).getOriginalFilename();
|
fileFileName= fileFileName.replace(".","_"+sdf.format(new Date())+".");
|
String filePath = root + fileFileName;
|
createFilefolderIFNotExist(filePath);
|
file.get(i).transferTo(new File(filePath));
|
isSuccess = true;
|
filePathList.add(filePath);
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
if (isSuccess) {
|
return new Response().set(1,filePathList,"上传成功");
|
} else {
|
return new Response().set(0,false,"上传失败");
|
}
|
|
}
|
//将文件复制到指定的目录
|
public void copyFile(File oldFile, String newPath){
|
File file = new File(newPath);
|
FileInputStream in = null;
|
FileOutputStream out = null;
|
try {
|
in = new FileInputStream(oldFile);
|
out = new FileOutputStream(file);;
|
|
byte[] buffer=new byte[2097152];
|
int readByte = 0;
|
while((readByte = in.read(buffer)) != -1){
|
out.write(buffer, 0, readByte);
|
}
|
} catch (FileNotFoundException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
if (in != null) {
|
try {
|
in.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
if (out != null) {
|
try {
|
out.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|
|
public void createFilefolderIFNotExist(String filePath) {
|
File f = new File(filePath);
|
if (!f.exists()) {
|
if (!f.getParentFile().exists()) {
|
f.getParentFile().mkdirs();
|
}
|
}
|
}
|
|
|
public Response deleteAlarmFile(String fileNames, int stationId, String afterOrBefore) {
|
//过滤特殊字符,避免路径遍历攻击
|
afterOrBefore = ActionUtil.filterFileName(afterOrBefore);
|
String names[] = fileNames.split(",");
|
String fileDirName = "";
|
int configType = Integer.parseInt(environment.getProperty("configFile.type"));
|
ApplicationHome applicationHome = new ApplicationHome(getClass());
|
File jarFile = applicationHome.getDir();
|
//测试版
|
if (configType == 1) {
|
fileDirName = jarFile.getParentFile().toString();
|
} else {
|
//打包版
|
fileDirName = jarFile.toString();
|
}
|
String root = fileDirName + "/fg_photo/stationsrc/alarm/" + stationId + File.separator + afterOrBefore + File.separator;
|
for (String name : names) {
|
String targetFilePath = root + name;
|
File file = new File(targetFilePath);
|
file.delete();
|
}
|
return new Response().set(1, "删除成功");
|
}
|
|
}
|