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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.ruoyi.iot.controller;
 
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.iot.domain.Device;
import com.ruoyi.iot.domain.DeviceOrder;
import com.ruoyi.iot.service.IDeviceOrderService;
import com.ruoyi.iot.service.IDeviceService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
 
/**
 * @author wmz
 * @version v1.0
 * @ClassName DeviceOrderController
 * @description 设备安装controller
 * @createTime 2023/11/13 0:27
 */
@Api(tags = "设备安装信息")
@RestController
@RequestMapping("/iot/deviceOrder")
public class DeviceOrderController extends BaseController {
 
    private static final Logger log = LoggerFactory.getLogger(DeviceOrderController.class);
 
    @Resource
    private IDeviceOrderService iDeviceOrderService;
 
    @Autowired
    private IDeviceService deviceService;
 
    @Autowired
    private ISysUserService userService;
 
    @Autowired
    private TokenService tokenService;
 
    /**
     * 查询全部设备安装单订单信息
     *
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasAnyPermi('iot:deviceOrder:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备订单分页列表")
    public TableDataInfo list(DeviceOrder deviceOrder) throws Exception {
        startPage();
        List<DeviceOrder> list = iDeviceOrderService.selectDeviceOrderList(deviceOrder);
        return getDataTable(list);
    }
 
    /**
     * 添加安装设备订单信息
     *
     * @param deviceOrder
     * @return
     * @throws Exception
     */
    @ApiOperation("添加安装单")
    @PreAuthorize("@ss.hasPermi('iot:device:add')")
    @Log(title = "添加安装单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody DeviceOrder deviceOrder) throws Exception {
        //更新订单的初始化状态
        if ("未指派".equals(deviceOrder.getErectoName())) {
            deviceOrder.setState(0);
        } else {
            deviceOrder.setState(1);
        }
        //判断传过来的orderType的值是否为2-维修
        if ("2".equals(deviceOrder.getOrderType())) {
            log.info("orderType{}", deviceOrder.getOrderType());
            System.out.println(deviceOrder.getOrderType());
            //更新订单的用户信息
            SysUser sysUser = getLoginUser().getUser();
            deviceOrder.setCreateUserId(sysUser.getUserId());
            //插入维修单
            int rows = iDeviceOrderService.insertDeviceOrder(deviceOrder);
            if (rows > 0) {
                Device device1 = deviceService.selectDeviceByDeviceId(deviceOrder.getDeviceId());
                //更新当前设备的是否维修信息-未维修
                Device device = new Device();
                device.setDeviceId(deviceOrder.getDeviceId());
                device.setRepairFlag(1);
                device.setStatus(device1.getStatus());
                deviceService.updateDevice(device);
            }
            return toAjax(rows);
        }
        //更新订单的用户信息
        SysUser sysUser = getLoginUser().getUser();
        deviceOrder.setCreateUserId(sysUser.getUserId());
        //插入安装订单
        int rows = iDeviceOrderService.insertDeviceOrder(deviceOrder);
        if (rows > 0) {
            //更新当前设备状态信息--安装中
            Device device = new Device();
            device.setDeviceId(deviceOrder.getDeviceId());
            device.setStatus(5);
            deviceService.updateDevice(device);
        }
        return toAjax(rows);
    }
 
    /**
     * 修改设备订单信息
     *
     * @param deviceOrder
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "修改设备订单信息")
    @PreAuthorize("@ss.hasPermi('iot:device:edit')")
    @Log(title = "修改设备订单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceOrder deviceOrder) throws Exception {
        //判断传过来的orderType的值是否为1-安装,并且安装单的状态为为已完成,修改设备的状态为离线状态
        if ("1".equals(deviceOrder.getOrderType()) && deviceOrder.getState() == 3) {
            Device device = new Device();
            device.setDeviceId(deviceOrder.getDeviceId());
            //修改设备的状态为离线
            device.setStatus(4);
            deviceService.updateDevice(device);
        }
        //判断传过来的orderType的值是否为2-维修,并且维修单的状态为为已完成,修改设备的是否为修为0-已维修
        if ("2".equals(deviceOrder.getOrderType()) && deviceOrder.getState() == 3) {
            Device device1 = deviceService.selectDeviceByDeviceId(deviceOrder.getDeviceId());
            Device device = new Device();
            device.setDeviceId(deviceOrder.getDeviceId());
            device.setRepairFlag(0);
            device.setStatus(device1.getStatus());
            deviceService.updateDevice(device);
        }
        //获取修改订单的用户信息
        SysUser sysUser = getLoginUser().getUser();
        //拿到修改安装单的用户id
        deviceOrder.setUpdateUserId(sysUser.getUserId());
        return toAjax(iDeviceOrderService.updateDeviceOrder(deviceOrder));
    }
 
    /**
     * 删除设备订单信息
     *
     * @param deviceId
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('iot:device:remove')")
    @Log(title = "删除设备订单信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deviceId}")
    @ApiOperation("删除订单信息")
    public AjaxResult remove(@PathVariable("deviceId") Integer deviceId) throws Exception {
        //根据要删除安装单的id查询出安装单信息
        DeviceOrder deviceOrder = iDeviceOrderService.selectDeviceOrderById(deviceId);
        SysUser user = getLoginUser().getUser();
        //如果根据查询出来的安装单对象不为空的话,并且状态等于未派单才可以删除
        if (deviceOrder != null && deviceOrder.getState() == 0 && user.getUserId() != 1) {
            //未派单的安装单删除后设备的状态更新为-未激活
            Device device = new Device();
            device.setDeviceId(deviceOrder.getDeviceId());
            device.setStatus(1);
            deviceService.updateDevice(device);
            int i = iDeviceOrderService.deleteDeviceOrderById(deviceOrder.getId());
            return toAjax(i);
        } else {
            return AjaxResult.error();
        }
    }
 
    /**
     * 安装单维修单图片上传
     */
 
    @Log(title = "安装单图片上传", businessType = BusinessType.INSERT)
    @PostMapping("/profile/avatar")
    @ApiOperation("安装单图片上传")
    public AjaxResult imgurl(@RequestParam("avatarfile") MultipartFile file) throws Exception {
        // 判断头像文件不为空
        if (!file.isEmpty()) {
            // 通过Security获取到登录的用户信息
            LoginUser loginUser = getLoginUser();
            String avatar = FileUploadUtils.upload(RuoYiConfig.getDeviceOrderPath(), file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", avatar);
            return ajax;
        }
        return AjaxResult.error("图片上传失败");
    }
}