lizijie
2021-08-18 eccde23ea401394aed432865bfbbbcb89539ba20
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
package com.moral.api.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.moral.api.entity.Organization;
import com.moral.api.entity.SpecialDevice;
import com.moral.api.entity.SysDictData;
import com.moral.api.entity.Version;
import com.moral.api.mapper.SysDictDataMapper;
import com.moral.api.service.OrganizationService;
import com.moral.api.service.SpecialDeviceService;
import com.moral.api.service.VersionService;
import com.moral.constant.Constants;
import com.moral.constant.ResponseCodeEnum;
import com.moral.constant.ResultMessage;
import com.moral.util.WebUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
 
@Slf4j
@Api(tags = {"特殊设备"})
@RestController
@RequestMapping("/specialDevice")
public class SpecialDeviceController {
 
    @Resource
    private SpecialDeviceService specialDeviceService;
 
    @Resource
    private OrganizationService organizationService;
 
    @Resource
    private VersionService versionService;
 
    @Resource
    private SysDictDataMapper sysDictDataMapper;
 
    @RequestMapping(value = "getSpecialDeviceByCondition", method = RequestMethod.GET)
    @ResponseBody
    public ResultMessage getSpecialDeviceByCondition(HttpServletRequest request) {
        Map<String,Object> parameters = WebUtils.getParametersStartingWith(request,null);
        Map<String,Object> resultMap = specialDeviceService.getDataByCondition(parameters);
        if (!resultMap.containsKey("code")){
            return ResultMessage.ok(resultMap);
        }
        return ResultMessage.fail(Integer.parseInt(resultMap.get("code").toString()),resultMap.get("msg").toString());
    }
 
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage insert(@RequestBody SpecialDevice specialDevice){
        Map<String,Object> resultMap = specialDeviceService.insert(specialDevice);
        String msg = resultMap.get("msg").toString();
        int code = Integer.parseInt(resultMap.get("code").toString());
        if (code == 0){
            return ResultMessage.ok(msg);
        }
        return ResultMessage.fail(Integer.parseInt(resultMap.get("code").toString()),resultMap.get("msg").toString());
    }
 
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage update (@RequestBody SpecialDevice specialDevice){
        if (specialDevice.getId() == null) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        if (specialDevice.getMac() != null) {
            //判断mac是否已存在
            QueryWrapper<SpecialDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("mac", specialDevice.getMac());
            queryWrapper.eq("is_delete",Constants.NOT_DELETE);
            if (specialDeviceService.getOne(queryWrapper) != null) {
                return ResultMessage.fail(ResponseCodeEnum.MAC_IS_EXIST.getCode(), ResponseCodeEnum.MAC_IS_EXIST.getMsg());
            }
        }
        if (specialDevice.getOrganizationId() != null) {
            //判断组织是否已存在
            QueryWrapper<Organization> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", specialDevice.getOrganizationId());
            queryWrapper.eq("is_delete",Constants.NOT_DELETE);
            if (ObjectUtils.isEmpty(organizationService.getOne(queryWrapper))) {
                return ResultMessage.fail(ResponseCodeEnum.ORGANIZATION_NOT_EXIST.getCode(), ResponseCodeEnum.ORGANIZATION_NOT_EXIST.getMsg());
            }
        }
        if (specialDevice.getDeviceVersionId() != null) {
            //判断型号是否已存在
            QueryWrapper<Version> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", specialDevice.getDeviceVersionId());
            queryWrapper.eq("is_delete",Constants.NOT_DELETE);
            if (ObjectUtils.isEmpty(versionService.getOne(queryWrapper))) {
                return ResultMessage.fail(ResponseCodeEnum.VERSION_NOT_EXIST.getCode(), ResponseCodeEnum.VERSION_NOT_EXIST.getMsg());
            }
        }
        if (specialDevice.getSpecialType() != null) {
            //判断组织是否已存在
            QueryWrapper<SysDictData> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("dict_type_id", 27);
            queryWrapper.eq("dataKey", specialDevice.getSpecialType());
            queryWrapper.eq("is_delete",Constants.NOT_DELETE);
            if (ObjectUtils.isEmpty(sysDictDataMapper.selectOne(queryWrapper))) {
                return ResultMessage.fail(ResponseCodeEnum.DICTDATA_KEY_NOT_EXIST.getCode(), ResponseCodeEnum.DICTDATA_KEY_NOT_EXIST.getMsg());
            }
        }
        specialDeviceService.update(specialDevice);
        return ResultMessage.ok();
    }
 
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage delete(@RequestBody Map map){
        if (map.get("id") == null){
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        specialDeviceService.delete(Integer.parseInt(map.get("id").toString()));
        return ResultMessage.ok();
    }
}