lxw
2023-08-15 160e150009b51a39fa95d9462c3798ba28d51a09
src/main/java/com/whyc/service/UserWorkService.java
@@ -1,8 +1,11 @@
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.core.env.Environment;
@@ -20,12 +23,54 @@
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(MultipartFile[] file, UserWorkAlarmParam param) {
    public Response uploadAlarmFile(List<MultipartFile> file, UserWorkAlarmParam param) {
        String fileDirName = "";
        int configType = Integer.parseInt(environment.getProperty("configFile.type"));
        ApplicationHome applicationHome = new ApplicationHome(getClass());
@@ -37,17 +82,17 @@
            //打包版
            fileDirName = jarFile.toString();
        }
        String root=fileDirName+"/stationsrc/alarm"+ param.getStationId() + "/" + param.getAfterOrBefore() + "/";
        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.length && param != null; i++) {
                String fileFileName = file[i].getOriginalFilename();
            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[i].transferTo(new File(filePath));
                file.get(i).transferTo(new File(filePath));
                isSuccess = true;
                filePathList.add(filePath);
            }
@@ -80,14 +125,14 @@
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(in != null){
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
@@ -96,14 +141,37 @@
            }
        }
    }
    public void createFilefolderIFNotExist(String filePath){
    public void createFilefolderIFNotExist(String filePath) {
        File f = new File(filePath);
        if(!f.exists()){
            if(!f.getParentFile().exists()){
        if (!f.exists()) {
            if (!f.getParentFile().exists()) {
                f.getParentFile().mkdirs();
            }
        }
    }
    public Response deleteAlarmFile(String fileNames, int stationId, String 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, "删除成功");
    }
}