package com.szwl.controller;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.szwl.model.bo.R;
import com.szwl.model.bo.ResponseModel;
import com.szwl.model.entity.TLocationCheck;
import com.szwl.service.TAdminService;
import com.szwl.service.TLocationCheckService;
import com.szwl.util.WhoIsUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.*;
/**
*
* 前端控制器
*
*
* @author wuhs
* @since 2023-12-11
*/
@RestController
@RequestMapping("/tLocationCheck")
public class TLocationCheckController {
TLocationCheckService locationCheckService;
TAdminService adminService;
public TLocationCheckController(TLocationCheckService locationCheckService,
TAdminService adminService) {
this.locationCheckService = locationCheckService;
this.adminService = adminService;
}
@ApiOperation(value = "获取定位信息")
@GetMapping("/getLocInfo")
public ResponseModel> getLocInfo(String clientId) {
if (StringUtils.isEmpty(clientId)) {
return R.fail("设备id不能为空");
}
// 根据设备id查询位置信息
LambdaQueryWrapper lqw = Wrappers.lambdaQuery();
lqw.eq(TLocationCheck::getClientId, clientId);
TLocationCheck locationCheck = locationCheckService.getOne(lqw);
if (Objects.nonNull(locationCheck)) {
HashMap locationMap = new HashMap<>();
String country = locationCheck.getCountry();
String username = locationCheck.getUsername();
String name = locationCheck.getName();
String phone = locationCheck.getPhone();
String longitude = locationCheck.getLongitude();
String latitude = locationCheck.getLatitude();
String addr = locationCheck.getAddr();
String modUsername = locationCheck.getModUsername();
String modName = locationCheck.getModName();
String modPhone = locationCheck.getModPhone();
String message = locationCheck.getMessage();
locationMap.put("country", country); // 录入国家
locationMap.put("username", username); // 账号用户名
locationMap.put("name", name); // 录入人姓名
locationMap.put("phone", phone); // 录入人电话
locationMap.put("longitude", longitude); // 经度
locationMap.put("latitude", latitude); // 纬度
locationMap.put("address", addr); // 反编译的地址
locationMap.put("modUsername", modUsername); // 修改人账号
locationMap.put("modName", modName); // 修改人姓名
locationMap.put("modPhone", modPhone); // 修改人电话
locationMap.put("message", message); // 修改原因
return R.ok(locationMap);
}
return R.fail("地区信息未录入");
}
@ApiOperation(value = "根据ip校验clientId设备定位")
@PostMapping("/ipCheck")
public ResponseModel> ipCheck(@RequestParam String clientId, @RequestParam String ip) {
if (StringUtils.isBlank(clientId) || StringUtils.isBlank(ip)) {
return R.fail("参数不能为空");
}
// 根据 clientId 查设备的 location,country
LambdaQueryWrapper lqw = Wrappers.lambdaQuery();
lqw.eq(TLocationCheck::getClientId, clientId);
TLocationCheck locationCheck = locationCheckService.getOne(lqw);
if (Objects.isNull(locationCheck)) {
return R.fail("生产部未录入改设备的位置信息");
}
locationCheck.setIp(ip);
locationCheck.setModifyTime(new Date());
String addr = WhoIsUtil.getLocByIp(ip);
String result = locationCheckService.locCheckNoMsg(locationCheck, clientId, addr);
if ("fail".equals(result)) {
locationCheck.setErr("不匹配");
}
locationCheckService.updateById(locationCheck);
return R.ok(result);
}
@ApiOperation(value = "修改定位信息")
@PostMapping("/updateLocInfo")
public ResponseModel> updateLocInfo(@RequestBody TLocationCheck locationCheck) {
if (Objects.isNull(locationCheck)) {
return R.fail("参数不能为空");
}
// 售后部同事修改的信息:姓名,电话,username,clientId,国家
String modUsername = locationCheck.getModUsername();
Set allowedUsernames = new HashSet<>(
Arrays.asList("shouhoumi", "sysMgtAcc", "jiang123", "ethan"));
if (!allowedUsernames.contains(modUsername)) {
return R.fail("该账号无权修改");
}
String modName = locationCheck.getModName();
String modPhone = locationCheck.getModPhone();
String clientId = locationCheck.getClientId();
String country = locationCheck.getCountry();
String message = locationCheck.getMessage();
if (Objects.isNull(clientId)) {
return R.fail("设备唯一码不能为空");
}
LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();
wrapper.eq(TLocationCheck::getClientId, clientId);
List checks = locationCheckService.list(wrapper);
TLocationCheck tLocationCheck = checks.get(0);
tLocationCheck.setModifyTime(new Date());
tLocationCheck.setModName(modName);
tLocationCheck.setModPhone(modPhone);
tLocationCheck.setModUsername(modUsername);
tLocationCheck.setClientId(clientId);
tLocationCheck.setCountry(country);
tLocationCheck.setMessage(message);
locationCheckService.updateById(tLocationCheck);
return R.ok("修改成功");
}
@ApiOperation(value = "录入定位信息")
@PostMapping("/inputLocInfo")
public ResponseModel> inputLocInfo(@RequestBody TLocationCheck locationCheck) {
if (Objects.isNull(locationCheck)) {
return R.fail("参数不能为空");
}
String name = locationCheck.getName();
String phone = locationCheck.getPhone();
String username = locationCheck.getUsername();
String clientId = locationCheck.getClientId();
String country = locationCheck.getCountry(); // 国家
if (Objects.isNull(clientId)) {
return R.fail("设备唯一码不能为空");
}
LambdaQueryWrapper wrapper = Wrappers.lambdaQuery();
wrapper.eq(TLocationCheck::getClientId, clientId);
List checks = locationCheckService.list(wrapper);
if (checks.size() > 0) {
return R.fail("此设备信息已录入");
}
Set allowedUsers = new HashSet<>(
Arrays.asList("sysMgtAcc", "jiang123", "shouhou121", "shouhou369", "shouhou397",
"shouhoumi", "zhl123", "ethan", "debugUser"));
if (!allowedUsers.contains(username)) {
return R.fail("该账号无权操作");
}
TLocationCheck tLocationCheck = new TLocationCheck();
String uuid = IdUtil.simpleUUID();
tLocationCheck.setId(uuid);
tLocationCheck.setCreateTime(new Date());
tLocationCheck.setName(name);
tLocationCheck.setPhone(phone);
tLocationCheck.setUsername(username);
tLocationCheck.setClientId(clientId);
tLocationCheck.setCountry(country);
locationCheckService.save(tLocationCheck);
return R.ok("设备出厂信息录入成功");
}
}