kaiyu
2021-05-11 39c6e9b5e6006053d42c0c8d4dc777add9257ae6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.moral.api.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.Sensor;
import com.moral.api.mapper.SensorMapper;
import com.moral.api.service.SensorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.util.LogUtils;
import com.moral.constant.Constants;
import com.moral.constant.ResponseCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-05-08
 */
@Service
@Transactional
public class SensorServiceImpl extends ServiceImpl<SensorMapper, Sensor> implements SensorService {
 
    @Autowired(required = false)
    private SensorMapper sensorMapper;
 
    @Autowired
    LogUtils logUtils;
 
    @Override
    @Transactional
    public Map<String, Object> insertOne(Sensor sensor) {
        Map<String,Object> resultMap = new HashMap<>();
        if (sensor.getName()==null || sensor.getCode()==null){
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
            return resultMap;
        }
        QueryWrapper<Sensor> wrapper_name = new QueryWrapper<>();
        wrapper_name.eq("name",sensor.getName());
        wrapper_name.eq("is_delete","0");
        if (sensorMapper.selectCount(wrapper_name)!=0){
            resultMap.put("code",ResponseCodeEnum.SENSOR_IS_EXIST.getCode());
            resultMap.put("msg",ResponseCodeEnum.SENSOR_IS_EXIST.getMsg());
            return resultMap;
        }
        QueryWrapper<Sensor> wrapper_code = new QueryWrapper<>();
        wrapper_code.eq("code",sensor.getCode());
        wrapper_code.eq("is_delete","0");
        if (sensorMapper.selectCount(wrapper_code)!=0){
            resultMap.put("code",ResponseCodeEnum.SENSOR_KEY_IS_USED.getCode());
            resultMap.put("msg",ResponseCodeEnum.SENSOR_KEY_IS_USED.getMsg());
            return resultMap;
        }
        sensorMapper.insertOne(sensor);
        //操作插入日志
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String content = "添加因子:"+sensor.getName()+";";
        logUtils.saveOperationForManage(request,content,Constants.INSERT_OPERATE_TYPE);
        resultMap.put("code",ResponseCodeEnum.SUCCESS.getCode());
        resultMap.put("msg",ResponseCodeEnum.SUCCESS.getMsg());
        return resultMap;
    }
 
    @Override
    public Map<String, Object> updateSensor(Map<String,Object> updateSensorMap) {
        Map<String,Object> resultMap = new HashMap<>();
        if(ObjectUtils.isEmpty(updateSensorMap.get("id"))){
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
            return resultMap;
        }
        if (updateSensorMap.containsKey("name")){
            if (updateSensorMap.get("name")==null||updateSensorMap.get("name")==""){
                resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
                resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
                return resultMap;
            }
        }
        if (updateSensorMap.containsKey("code")){
            if(ObjectUtils.isEmpty(updateSensorMap.get("code"))){
                resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
                resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
                return resultMap;
            }
        }
        Sensor oldSensor = sensorMapper.getSensorById(Integer.parseInt(updateSensorMap.get("id").toString()));
        if (ObjectUtils.isEmpty(oldSensor)){
            resultMap.put("code",ResponseCodeEnum.SENSOR_IS_NOT_EXIST.getCode());
            resultMap.put("msg",ResponseCodeEnum.SENSOR_IS_NOT_EXIST.getMsg());
            return resultMap;
        }
        QueryWrapper<Sensor> wrapper_code = new QueryWrapper<>();
        wrapper_code.eq("code",updateSensorMap.get("code"));
        wrapper_code.eq("is_delete","0");
        List<Sensor> sensorList = sensorMapper.selectList(wrapper_code);
        System.out.println(sensorList.get(0));
        if (sensorList.size()!=0&&!sensorList.get(0).getId().toString().equals(updateSensorMap.get("id").toString())){
            resultMap.put("code",ResponseCodeEnum.SENSOR_KEY_IS_USED.getCode());
            resultMap.put("msg",ResponseCodeEnum.SENSOR_KEY_IS_USED.getMsg());
            return resultMap;
        }
        sensorMapper.updateSensor(updateSensorMap);
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String content = "修改因子:"+oldSensor.getName()+";";
        for (Object key:updateSensorMap.keySet()) {
            if (key.toString().equals("name")){
                content = content+"角色名:"+oldSensor.getName()+"->"+updateSensorMap.get(key)+";";
            }
            if (key.toString().equals("code")){
                content = content+"编号:"+oldSensor.getCode()+"->"+updateSensorMap.get(key)+";";
            }
            if (key.toString().equals("desc")){
                content = content+"备注:"+oldSensor.getDesc()+"->"+updateSensorMap.get(key)+";";
            }
        }
        System.out.println(content);
        logUtils.saveOperationForManage(request,content,Constants.UPDATE_OPERATE_TYPE);
        resultMap.put("code",ResponseCodeEnum.SUCCESS.getCode());
        resultMap.put("msg",ResponseCodeEnum.SUCCESS.getMsg());
        return resultMap;
    }
}