lizijie
2021-05-12 a5b27c528bb73886ccc6b97ea1262b9d7f403cca
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moral.api.entity.MonitorPoint;
import com.moral.api.entity.Organization;
import com.moral.api.mapper.MonitorPointMapper;
import com.moral.api.mapper.OrganizationMapper;
import com.moral.api.service.MonitorPointService;
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.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-05-12
 */
@Service
@Transactional
public class MonitorPointServiceImpl extends ServiceImpl<MonitorPointMapper, MonitorPoint> implements MonitorPointService {
 
    @Autowired(required = false)
    private MonitorPointMapper monitorPointMapper;
 
    @Autowired(required = false)
    private OrganizationMapper organizationMapper;
 
    @Autowired
    LogUtils logUtils;
 
    @Override
    @Transactional
    public Map<String, Object> insertMonitorPoint(MonitorPoint monitorPoint) {
        Map resultMap = new HashMap();
        if (monitorPoint.getName()==null){
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
            return resultMap;
        }
        QueryWrapper<MonitorPoint> wapper_name = new QueryWrapper<>();
        wapper_name.eq("name",monitorPoint.getName());
        wapper_name.eq("is_delete",Constants.NOT_DELETE);
        if(monitorPointMapper.selectOne(wapper_name)!=null){
            resultMap.put("code",ResponseCodeEnum.MONITOR_POINT_IS_EXIST.getCode());
            resultMap.put("msg",ResponseCodeEnum.MONITOR_POINT_IS_EXIST.getMsg());
            return resultMap;
        }
        if (monitorPoint.getOrganizationId()!=null){
            QueryWrapper<Organization> wapper_org = new QueryWrapper<>();
            wapper_org.eq("id",monitorPoint.getOrganizationId());
            wapper_org.eq("is_delete",Constants.NOT_DELETE);
            if (organizationMapper.selectOne(wapper_org)==null){
                resultMap.put("code",ResponseCodeEnum.ORGANIZATION_NOT_EXIST.getCode());
                resultMap.put("msg",ResponseCodeEnum.ORGANIZATION_NOT_EXIST.getMsg());
                return resultMap;
            }
        }
        monitorPointMapper.insert(monitorPoint);
        //操作插入日志
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String content = "添加了站点:"+monitorPoint.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> getAllMonitorPoint(Map map) {
        Map<String,Object> resultMap = new HashMap<>();
        if (!map.containsKey("current")||!map.containsKey("size")||!map.containsKey("orderType")){
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
            return resultMap;
        }
        int current = Integer.parseInt(map.get("current").toString());
        int size = Integer.parseInt(map.get("size").toString());
        Page<MonitorPoint> page = new Page(current,size);
        QueryWrapper<MonitorPoint> wrapper = new QueryWrapper();
        wrapper.eq("is_delete",0);
        String orderType = map.get("orderType").toString();
        if (orderType.equals(Constants.ORDER_ASC)){
            wrapper.orderByAsc("create_time");
        }else {
            wrapper.orderByDesc("create_time");
        }
        Page resultPage = monitorPointMapper.selectPage(page,wrapper);
        List<MonitorPoint> monitorPoints = resultPage.getRecords();
        SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Map<String,Object>> monitorPointtList = new ArrayList<>();
        for (MonitorPoint monitorPoint:monitorPoints) {
            Map monitorPointMap = JSON.parseObject(JSON.toJSONString(monitorPoint),Map.class);
            String createTime = SDF.format(monitorPoint.getCreateTime());
            String updateTime = SDF.format(monitorPoint.getUpdateTime());
            monitorPointMap.put("createTime",createTime);
            monitorPointMap.put("updateTime",updateTime);
            monitorPointMap.put("key",monitorPoint.getId());
            monitorPointtList.add(monitorPointMap);
        }
        resultMap.put("manageRoles",monitorPointtList);
        int totalNumber = monitorPoints.size();
        resultMap.put("totalNumber",totalNumber);
        resultMap.put("current",current);
        int totalPageNumber = totalNumber/size;
        if(totalNumber%size != 0){
            totalPageNumber += 1;
        }
        resultMap.put("totalPageNumber",totalPageNumber);
        return resultMap;
    }
 
    @Override
    public Map<String, Object> updateMonitorPoint(Map<String,Object> updateMap) {
        Map resultMap = new HashMap();
        if(!updateMap.containsKey("id")){
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
            return resultMap;
        }
        if (updateMap.containsKey("name")){
            if (updateMap.get("name")==null||updateMap.get("name")==""){
                resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
                resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
                return resultMap;
            }
            QueryWrapper<MonitorPoint> wapper_name = new QueryWrapper<>();
            wapper_name.eq("name",updateMap.get("name"));
            wapper_name.eq("is_delete",Constants.NOT_DELETE);
            MonitorPoint monitorPoint_name = monitorPointMapper.selectOne(wapper_name);
            if(monitorPoint_name!=null&&!monitorPoint_name.getId().equals(updateMap.get("id"))){
                resultMap.put("code",ResponseCodeEnum.MONITOR_POINT_IS_EXIST.getCode());
                resultMap.put("msg",ResponseCodeEnum.MONITOR_POINT_IS_EXIST.getMsg());
                return resultMap;
            }
        }
        QueryWrapper<MonitorPoint> wapper_id = new QueryWrapper<>();
        wapper_id.eq("id",updateMap.get("id"));
        wapper_id.eq("is_delete",Constants.NOT_DELETE);
        monitorPointMapper.selectOne(wapper_id);
        return null;
    }
}