ZhuDongming
2020-06-30 67a7a24b47d71dd843dd0b17c5cdf9f519e29d5d
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package com.moral.controller;
 
import static com.moral.common.util.WebUtils.getParametersStartingWith;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
import org.apache.commons.lang3.StringUtils;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import com.moral.common.bean.AppData;
import com.moral.common.bean.ResultBean;
import com.moral.common.util.Crypto;
import com.moral.common.util.WebUtils;
import com.moral.entity.Account;
import com.moral.entity.Device;
import com.moral.entity.MonitorPoint;
import com.moral.entity.OperateUser;
import com.moral.entity.Organization;
import com.moral.entity.Profession;
import com.moral.entity.adapter.EquDeviceAdapter;
import com.moral.service.AccountService;
import com.moral.service.DeviceService;
import com.moral.service.HistoryService;
import com.moral.service.MonitorPointService;
import com.moral.service.OperateUserService;
import com.moral.service.OrganizationService;
import com.moral.service.ProfessionService;
import com.moral.util.MessageUtils;
import com.taobao.api.ApiException;
 
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
 
/**
 * The Class MobileController.
 */
@RestController
@RequestMapping("mobile")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(tags = "Mobile", description = "APP接口") 
public class MobileController {
 
    /** The operate user service. */
    @Resource
    private OperateUserService operateUserService;
 
    /** The device service. */
    @Resource
    private DeviceService deviceService;
 
    /** The organization service. */
    @Resource
    private OrganizationService organizationService;
 
    /** The monitor point service. */
    @Resource
    private MonitorPointService monitorPointService;
 
    @Resource
    private ProfessionService professionService;
 
    @Resource
    private AccountService accountService;
 
    @Resource
    private HistoryService historyService;
 
    /**
     * Mobile login.登录
     *
     * @param username the username
     * @param password the password
     * @return the result bean
     */
    @RequestMapping("accountlogin")
    public AppData<OperateUser> mobileLogin(String username, String password) {
        OperateUser operateUser = operateUserService.mobileLogin(username,password);
        return new AppData<OperateUser>(operateUser); 
    }
    
    /**
     * Update password.修改密码
     *
     * @param uid the uid
     * @param password the password
     * @param newpassword the newpassword
     * @return the result bean
     */
    @RequestMapping("upPassword")
    public AppData<OperateUser> updatePassword(Integer uid, String password, String newpassword) {
        OperateUser operateUser = operateUserService.updatePassword(uid,password,newpassword);
        return new AppData<OperateUser>(operateUser); 
    }
    /**
     * 根据 设备id 来获取设备信息
     */
    @RequestMapping("deviceInfo")
    public AppData<Device> deviceInfo(Integer id){
        Device device = deviceService.queryById(id);
        return  new AppData<Device>(device);
    }
    /**
     *
     * @param equDeviceAdapter
     * @return
     */
    @RequestMapping("reportDevice")
    public AppData<String> installDevice(EquDeviceAdapter equDeviceAdapter) {
        //deviceService.addOrModify(equDeviceAdapter.toDevice());
        deviceService.saveOrUpdate4Mobile(equDeviceAdapter.toDevice(), equDeviceAdapter.getDeviceProperty());
        return new AppData<String>("");
    }
 
    /**
     * Gets the install devices by operate user.终端采集
     *
     * @param uid the uid
     * @param pageIndex the page index
     * @param pageSize the page size
     * @return the install devices by operate user
     */
    @RequestMapping("myRelease")
    public AppData<List<Device>> getInstallDevicesByOperateUser(@RequestParam(value="uid", required=true)Integer uid, 
            @RequestParam(defaultValue="0")Integer pageIndex, @RequestParam(defaultValue="0")Integer pageSize) {
        List<Device> devices = deviceService.getInstallDevicesByOperateUser(uid, pageIndex, pageSize);
        return new AppData<List<Device>>(devices);
    }
 
    /**
     * Gets the device by mac.
     *
     * @param mac the mac
     * @return the device by mac
     */
    @RequestMapping("getEquInfoByMac")
    public AppData<Map> getDeviceByMac(@RequestParam(value="mac", required=true)String mac) {
        Device device = deviceService.getDeviceByMac(mac,false);//不从缓存取
        if(device!=null){
            Map<String,Object> deviceMap = new HashMap<>(BeanMap.create(device));
            deviceMap.put("monitorpointDetail",deviceMap.get("monitorPoint"));
            deviceMap.remove("monitorPoint");
            return new AppData<Map>(deviceMap);
        }else{
            return  new AppData("mac地址不存在",AppData.FAIL);
        }
    }
 
    /**
     * Gets the monitor points by area name.
     *
     * @param request the area name
     * @return the monitor points by area name
     */
    @RequestMapping("getMpointsByAreaName")
    public AppData<List<MonitorPoint>> getMonitorPointsByAreaName(HttpServletRequest request) {
        Map<String, Object> parameters = getParametersStartingWith(request, null);
        List<MonitorPoint> monitorPoints = monitorPointService.getMonitorPointsByAreaName(parameters);
        return new AppData<List<MonitorPoint>>(monitorPoints);
    }
 
    /**
     * Gets the organizations by area name.
     *
     * @param request the area name
     * @return the organizations by area name
     */
    @RequestMapping("getOrgsByAreaName")
    public AppData<List<Organization>> getOrganizationsByAreaName(HttpServletRequest request) {
        Map<String, Object> parameters = getParametersStartingWith(request, null);
        List<Organization> organizations = organizationService.getOrganizationsByAreaName(parameters);
        return new AppData<List<Organization>>(organizations);
    }
 
    /**
     * Send SMS
     *
     * @param mobile
     * @return
     */
    @RequestMapping("sendSms")
    public AppData<String> sendSms(String mobile) {
        AppData apiData = new AppData();
 
        if (StringUtils.isBlank(mobile)){
            apiData.setErrno(1);
            apiData.setMessage("没有输入手机号码");
            apiData.setData("{}");
            return  apiData;
        }
        OperateUser user = operateUserService.getOperateUserByMobile(mobile);
        if (user==null){
            apiData.setErrno(1);
            apiData.setMessage("手机号没有关联用户");
            apiData.setData("{}");
            return  apiData;
        }
        //生成验证码
        int code = (int)((Math.random() * 9 + 1) * 100000);
        user.setCode(String.valueOf(code));
        operateUserService.updateOperateUser(user);
 
        //发送到手机
        try {
            int state = MessageUtils.sendMsg(mobile, String.valueOf(code));
            if(state==0){
                apiData.setErrno(0);
                apiData.setMessage("发送验证码成功");
                apiData.setData("{}");
                return  apiData;
            }
            if(state==1){
                apiData.setErrno(1);
                apiData.setMessage("发送验证码失败");
                apiData.setData("{}");
                return  apiData;
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return new AppData<String>("");
    }
 
    /**
     *
     * @param username
     * @param password
     * @param code
     * @return
     */
    @RequestMapping("password")
    public AppData<String> password(String username, String password, String code){
        AppData apiData = new AppData();
 
        if (StringUtils.isBlank(code)){
            apiData.setErrno(1);
            apiData.setMessage("重置密码失败,没有输出验证码");
            apiData.setData("{}");
            return  apiData;
        }
        OperateUser user = operateUserService.getOperateUserByMobile(username);
        if (user==null){
            apiData.setErrno(1);
            apiData.setMessage("手机号没有关联用户");
            apiData.setData("{}");
            return  apiData;
        }
        if (user.getCode().equals(code)){
            user.setPassword(Crypto.md5(password));
            int new_code = (int)((Math.random() * 9 + 1) * 100000);
            user.setCode(String.valueOf(new_code));
            operateUserService.updateOperateUser(user);
 
            apiData.setErrno(0);
            apiData.setMessage("重置密码成功");
            apiData.setData("{}");
            return  apiData;
        } else {
            apiData.setErrno(1);
            apiData.setMessage("输入验证码不正确");
            apiData.setData("{}");
            return  apiData;
        }
    }
 
    @RequestMapping("profession")
    public AppData<List<Profession>> profession(){
 
        List<Profession> professions = professionService.getProfessiontList();
        return new AppData<List<Profession>>(professions);
    }
 
    @GetMapping("login")
    @ApiOperation(value = "公司用户登录", notes = "公司用户登录")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "account", value = "账号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "query", dataType = "String")
    })
    public ResultBean<Account> companyLogin(HttpServletRequest request){
        Map<String, Object> parameters = getParametersStartingWith(request, null);
        Account account = accountService.companyLogin(parameters);
        return new ResultBean<Account>(account);
    }
 
    @GetMapping("devices")
    @ApiOperation(value = "获取设备信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "organizationId", value = "组织Id", required = true, paramType = "query", dataType = "String"),
    })
    public ResultBean<List<Device>> getDevicesByOrganizationId(HttpServletRequest request){
        Map<String, Object> parameters = getParametersStartingWith(request, null);
        List<Device> devices = deviceService.getDevicesByOrganizationId(parameters);
        return new ResultBean<List<Device>>(devices);
    }
 
    @GetMapping("sensors")
    @ApiOperation(value = "获取设备传感器信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "deviceVersionId", value = "设备版本Id", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "mac", value = "设备mac", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", required = true, paramType = "query", dataType = "int"),
    })
    public ResultBean<List<Map<String, Object>>> getSensorsDataByDevice(HttpServletRequest request){
        Map<String, Object> parameters = getParametersStartingWith(request, null);
        List<Map<String, Object>> sensors = historyService.getSensorsDataByDevice(parameters);
        return new ResultBean<List<Map<String, Object>>>(sensors);
    }
 
    @GetMapping("sensor")
    @ApiOperation(value = "获取单一传感器数据")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "mac", value = "设备mac", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sensorKey", value = "传感器key", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "size", value = "获取数据条数", required = true, paramType = "query", dataType = "int"),
    })
    public ResultBean<List<Map<String, Object>>> getSensorDataBySensorKey(HttpServletRequest request){
        Map<String, Object> parameters = getParametersStartingWith(request, null);
        List<Map<String, Object>> sensors = historyService.getSensorDataBySensorKey(parameters);
        return new ResultBean<List<Map<String, Object>>>(sensors);
    }
    
    @GetMapping("profession-provincecode")
    @ApiOperation(value = "根据省code返回行业数据", notes = "根据省code返回行业数据")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "provinceCode", value = "省code", required = true, paramType = "query", dataType = "String")
    })
    public ResultBean<List<Profession>> getProfessionByProvinceCode(HttpServletRequest request){
        Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
        List<Profession> professions = professionService.getProfessiontList(parameters);
        return new ResultBean<List<Profession>>(professions);
    }
 
    @PostMapping("update-maintenance")
    @ApiOperation(value = "设备状态从离线更新为维保", notes = "设备状态从离线更新为维保")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "设备ID", required = true, paramType = "query", dataType = "int") ,
            @ApiImplicitParam(name = "old_state", value = "旧的设备状态", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "new_state", value = "新的设备状态", required = true, paramType = "query", dataType = "String") 
            
    })
    public ResultBean offLinToMaintenance(HttpServletRequest request) {
        Map<String, Object> parameters = getParametersStartingWith(request, null);
        deviceService.offLinToMaintenance(parameters);
        ResultBean resultBean = new ResultBean(parameters);
        return resultBean;
    }
    
    @GetMapping("mointorPointRank")
    @ApiOperation(value = "获取站点排名", notes = "获取站点排名")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "accountId", value = "账号id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sensor", value = "因子", required = true, paramType = "query", dataType = "String")
    })
    public ResultBean<List<Map<String, Object>>> getMointorPointRank(HttpServletRequest request){
        Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
        String id = parameters.get("accountId").toString();
        String sensor = parameters.get("sensor").toString();
        Map<String, Object> account = accountService.getOrganizationIdByAccountId(id);
        //通过组织id来获取站点和设备信息
        account.put("sensor", sensor);
        List<Map<String, Object>> returnList = historyService.getMointorPointRankByOrganizationId(account);
        return new ResultBean<List<Map<String, Object>>>(returnList);
    }
 
}