wangmengzhao
2023-12-08 0f550cb81c4d663849a54e7f27b26c737dc47ec7
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package com.ruoyi.iot.service.impl;
 
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.iot.domain.Device;
import com.ruoyi.iot.domain.DeviceOrder;
import com.ruoyi.iot.mapper.DeviceOrderMapper;
import com.ruoyi.iot.service.IDeviceOrderService;
import com.ruoyi.iot.service.IDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
 
 
/**
 * @author wmz
 * @version v1.0
 * @ClassName DeviceOrderServiceImpl
 * @description 设备安装单/维修单业务层处理
 * @createTime 2023/11/13 0:25
 */
 
@Service
public class DeviceOrderServiceImpl extends BaseController implements IDeviceOrderService {
 
    @Resource
    private DeviceOrderMapper deviceOrderMapper;
    @Autowired
    private IDeviceService deviceService;
 
    /**
     * 根据条件分页查询设备安装信息
     *
     * @param deviceOrder
     * @return
     * @throws Exception
     */
    @Override
    public List<DeviceOrder> selectDeviceOrderList(DeviceOrder deviceOrder) throws Exception {
        return deviceOrderMapper.selectDeviceOrderList(deviceOrder);
    }
 
    /**
     * 根据登录的用户来查询自己未派单的安装单/维修单数量
     *
     * @param deviceOrder
     * @return
     * @throws Exception
     */
    @Override
    public AjaxResult selectDeviceOrderCountByUndeliveredorders(DeviceOrder deviceOrder) throws Exception {
        //根据登录的用户来查询自己未派单的安装单/维修单数量
        int count = deviceOrderMapper.selectDeviceOrderCountByUndeliveredorders(deviceOrder);
        //根据登录的用户来查询自己已派单的安装单/维修单数量
        int total = deviceOrderMapper.selectDeviceOrderCountByDispatchedorders(deviceOrder);
        //根据登录的用户来查询自己已接单的安装单/维修单数量
        int number = deviceOrderMapper.selectDeviceOrderCountByReceivedorders(deviceOrder);
        //根据登录的用户来查询自己已完成的安装单/维修单数量
        int amount = deviceOrderMapper.selectDeviceOrderCountByCompleted(deviceOrder);
        AjaxResult ajax = new AjaxResult();
        //未派单的安装单/维修单的数量
        ajax.put("deviceOrderUndeliveredorders", count);
        //已派单的安装单/维修单的数量
        ajax.put("deviceOrderDispatchedorders", total);
        //已接单的安装单/维修单的数量
        ajax.put("deviceOrderReceivedorders", number);
        //已完成的安装单/维修单的数量
        ajax.put("deviceOrderCompleted", amount);
        return AjaxResult.success("查询成功", ajax);
    }
 
    /**
     * 根据登录的用户来查询自己已派单的安装单/维修单数量
     *
     * @param deviceOrder
     * @return
     * @throws Exception
     */
    @Override
    public int selectDeviceOrderCountByDispatchedorders(DeviceOrder deviceOrder) throws Exception {
        return deviceOrderMapper.selectDeviceOrderCountByDispatchedorders(deviceOrder);
    }
 
    /**
     * 根据登录的用户来查询自己已接单的安装单/维修单数量
     *
     * @param deviceOrder
     * @return
     * @throws Exception
     */
    @Override
    public int selectDeviceOrderCountByReceivedorders(DeviceOrder deviceOrder) throws Exception {
        return deviceOrderMapper.selectDeviceOrderCountByReceivedorders(deviceOrder);
    }
 
    /**
     * 根据登录的用户来查询自己已完成的安装单/维修单数量
     *
     * @param deviceOrder
     * @return
     * @throws Exception
     */
    @Override
    public int selectDeviceOrderCountByCompleted(DeviceOrder deviceOrder) throws Exception {
        return deviceOrderMapper.selectDeviceOrderCountByCompleted(deviceOrder);
    }
 
    /**
     * 查询设备订单表中已派单/已接单所有的数据
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<DeviceOrder> selectDeviceOrderAll() throws Exception {
        return deviceOrderMapper.selectDeviceOrderAll();
    }
 
    /**
     * 根据id查询安装订单信息
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public DeviceOrder selectDeviceOrderById(Long id) throws Exception {
        return deviceOrderMapper.selectDeviceOrderById(id);
    }
 
 
    /**
     * 添加安装设备订单信息
     *
     * @param deviceOrder
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public AjaxResult insertDeviceOrder(DeviceOrder deviceOrder) throws Exception {
        SysUser sysUser = getLoginUser().getUser();
        Device device = new Device();
        //更新订单的初始化状态
        if ("未指派".equals(deviceOrder.getErectoName())) {
            deviceOrder.setState(0);
        } else {
            deviceOrder.setState(1);
            deviceOrder.setAssginTime(new Date());
        }
 
        //普通用户创建维修单的时候userid=他本身登录的userid,create_userid=联营商的id
        //更新订单的用户信息
        //判断userId是否大于0,大于0就执行SysUser sysUser = getLoginUser().getUser();deviceOrder.setCreateUserId(sysUser.getUserId());
        if (deviceOrder.getUserId() != null) {
            //用户在小程序创建维修单的情况,userId=2,createUserId=140
            deviceOrder.setUserId(deviceOrder.getUserId());
            deviceOrder.setCreateUserId(deviceOrder.getCreateUserId());
            //用户创建CustomFlag=1
            deviceOrder.setCustomFlag(1);
        } else {
            //联营商创建安装单/维修单的情况,userId=0,createUserId=14
            deviceOrder.setCreateUserId(sysUser.getUserId());
            if ("2".equals(deviceOrder.getOrderType())) {
                //根据deviceId反查userId填入
                Device devices = deviceService.selectDeviceByDeviceId(deviceOrder.getDeviceId());
                deviceOrder.setUserId(devices.getUserId());
                //联营商创建CustomFlag=0
                deviceOrder.setCustomFlag(0);
            }
        }
 
        //判断传过来的orderType的值是否为2-维修
        if ("2".equals(deviceOrder.getOrderType())) {
            //更新订单的用户信息
            //插入维修单
            //deviceOrder.setAssginTime(new Date());
            int total = deviceOrderMapper.insertDeviceOrder(deviceOrder);
            if (total > 0) {
                Device deviceOrders = deviceService.selectDeviceByDeviceId(deviceOrder.getDeviceId());
                //更新当前设备的是否维修信息-未维修
                device.setDeviceId(deviceOrder.getDeviceId());
                device.setRepairFlag(1);
                device.setStatus(deviceOrders.getStatus());
                deviceService.updateDevice(device);
                return toAjax(total);
            }
        } else {
            //插入安装订单
            int rows = deviceOrderMapper.insertDeviceOrder(deviceOrder);
            if (rows > 0) {
                //更新当前设备状态信息--安装中
                device.setDeviceId(deviceOrder.getDeviceId());
                device.setStatus(5);
                deviceService.updateDevice(device);
                return toAjax(rows);
            }
        }
        return null;
    }
 
    /**
     * 修改保存设备订单信息
     *
     * @param deviceOrder
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public AjaxResult updateDeviceOrder(DeviceOrder deviceOrder) throws Exception {
        SysUser sysUser = getLoginUser().getUser();
        Device device = new Device();
        DeviceOrder deviceOrderA = deviceOrderMapper.selectDeviceOrderByIdAnderectoName(deviceOrder.getId(), deviceOrder.getErectoName());
        if (deviceOrderA == null) {
            deviceOrder.setAssginTime(new Date());
        }
        //判断传过来的orderType的值是否为1-安装,并且安装单的状态为已完成,修改设备的状态为离线状态
        if ("1".equals(deviceOrder.getOrderType()) && deviceOrder.getState() == 3) {
            //获取修改订单的用户信息
            //拿到修改安装单的用户id
            deviceOrder.setUpdateUserId(sysUser.getUserId());
            deviceOrder.setFinishTime(new Date());
            int rows = deviceOrderMapper.updateDeviceOrder(deviceOrder);
            if (rows > 0) {
                device.setDeviceId(deviceOrder.getDeviceId());
                //修改设备的状态为离线
                device.setStatus(4);
                //修改设备的是否安装标识为1-已安装
                device.setInstallFlag(1);
                deviceService.updateDevice(device);
            }
            return toAjax(rows);
        } else {
            //判断传过来的orderType的值是否为2-维修,并且维修单的状态为为已完成,修改设备的是否维修为1-已维修
            if ("2".equals(deviceOrder.getOrderType()) && deviceOrder.getState() == 3) {
                //获取修改订单的用户信息
                //拿到修维修单的用户id
                deviceOrder.setUpdateUserId(sysUser.getUserId());
                deviceOrder.setFinishTime(new Date());
                int rows = deviceOrderMapper.updateDeviceOrder(deviceOrder);
                if (rows > 0) {
                    Device deviceRepairOrder = deviceService.selectDeviceByDeviceId(deviceOrder.getDeviceId());
                    device.setDeviceId(deviceOrder.getDeviceId());
                    device.setRepairFlag(0);
                    device.setStatus(deviceRepairOrder.getStatus());
                    deviceService.updateDevice(device);
                }
                return toAjax(rows);
            }
        }
        //拿到修改安装单的用户id
        deviceOrder.setUpdateUserId(sysUser.getUserId());
        return toAjax(deviceOrderMapper.updateDeviceOrder(deviceOrder));
    }
 
    /**
     * 用户对完成的安装单/维修单打分和评价
     *
     * @param deviceOrder
     * @return
     * @throws Exception
     */
    @Override
    public AjaxResult updateDeviceOrderScoreAndComment(DeviceOrder deviceOrder) throws Exception {
        if (deviceOrder != null) {
            DeviceOrder order = deviceOrderMapper.selectDeviceOrderById(deviceOrder.getId());
            if (order != null) {
                deviceOrder.setUserId(deviceOrder.getUserId());
                deviceOrder.setIsRate(1);
                int rows = deviceOrderMapper.updateDeviceOrderScoreAndComment(deviceOrder);
                if (rows > 0) {
                    return AjaxResult.success("操作成功");
                }
            } else {
                return AjaxResult.error("暂无此单");
            }
        } else {
            return AjaxResult.error();
        }
        return null;
    }
 
    /**
     * 删除设备安装单/维修单信息
     *
     * @param id
     * @param deviceId
     * @return
     * @throws Exception
     */
    @Override
    public AjaxResult deleteDeviceOrderById(Long id, Long deviceId) throws Exception {
        Device device = new Device();
        //根据要删除安装单的id查询出安装单信息
        DeviceOrder deviceOrders = deviceOrderMapper.selectDeviceOrderById(id);
        //根据设备id查询设备信息
        Device deviceA = deviceService.selectDeviceByDeviceId(deviceId);
        SysUser user = getLoginUser().getUser();
        //如果根据查询出来的安装单对象不为空的话,并且状态等于未派单才可以删除
        if (deviceOrders != null && deviceOrders.getState() == 0 && user.getUserId() != 1 && "2".equals(deviceOrders.getOrderType())) {
            device.setDeviceId(deviceA.getDeviceId());
            device.setRepairFlag(0);
            device.setStatus(deviceA.getStatus());
            deviceService.updateDevice(device);
            return toAjax(deviceOrderMapper.deleteDeviceOrderById(id));
        } else {
            if (deviceOrders != null && deviceOrders.getState() == 0 && user.getUserId() != 1) {
                //未派单的安装单删除后设备的状态更新为-未激活
                device.setDeviceId(deviceOrders.getDeviceId());
                device.setStatus(1);
                deviceService.updateDevice(device);
                int i = deviceOrderMapper.deleteDeviceOrderById(id);
                return toAjax(i);
            }
            return null;
        }
    }
 
    /**
     * 批量删除设备订单安装信息
     *
     * @param ids
     * @return
     * @throws Exception
     */
    @Override
    public int deleteDeviceOrder(Integer[] ids) throws Exception {
        return deviceOrderMapper.deleteDeviceOrder(ids);
    }
}