|
@@ -0,0 +1,231 @@
|
|
|
|
+package com.szwl.controller;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+import cn.com.crbank.ommo.esUtil.BeanUtils;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
+import com.szwl.constant.ResponseCodesEnum;
|
|
|
|
+import com.szwl.model.bo.R;
|
|
|
|
+import com.szwl.model.bo.ResponseModel;
|
|
|
|
+import com.szwl.model.entity.TAdmin;
|
|
|
|
+import com.szwl.model.entity.TEquipment;
|
|
|
|
+import com.szwl.model.entity.TProduct;
|
|
|
|
+import com.szwl.model.entity.TSugarDo;
|
|
|
|
+import com.szwl.model.param.InterfaceParam;
|
|
|
|
+import com.szwl.model.utils.PushUtils;
|
|
|
|
+import com.szwl.model.vo.MachineVo;
|
|
|
|
+import com.szwl.service.TAdminService;
|
|
|
|
+import com.szwl.service.TEquipmentService;
|
|
|
|
+import com.szwl.service.TProductService;
|
|
|
|
+import com.szwl.service.TSugarDoService;
|
|
|
|
+import io.swagger.annotations.Api;
|
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.util.*;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+@Api(value = "/api", tags = {"第三方接口"})
|
|
|
|
+@RestController("ApiInterfaceController")
|
|
|
|
+@RequestMapping("/api")
|
|
|
|
+public class ApiInterfaceController {
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ TAdminService adminService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ TEquipmentService equipmentService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ TProductService productService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ TSugarDoService sugarDoService;
|
|
|
|
+
|
|
|
|
+ @ApiOperation("查询设备信息列表")
|
|
|
|
+ @PostMapping("/getMachineList")
|
|
|
|
+ public ResponseModel<?> getMachineList(@RequestHeader(value = "x-api-key") String apiKey, @RequestBody InterfaceParam interfaceParam) {
|
|
|
|
+ // 校验apiKey是否为空
|
|
|
|
+ if (StringUtils.isEmpty(apiKey)) {
|
|
|
|
+ return R.fail(ResponseCodesEnum.A0001, "apiKey is null");
|
|
|
|
+ }
|
|
|
|
+ // 校验参数是否为空
|
|
|
|
+ String username = interfaceParam.getUsername();
|
|
|
|
+ if (StringUtils.isEmpty(username)) {
|
|
|
|
+ return R.fail(ResponseCodesEnum.A0001, "username is null");
|
|
|
|
+ }
|
|
|
|
+ // 校验apiKey是否正确
|
|
|
|
+ LambdaQueryWrapper<TAdmin> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
+ wrapper.eq(TAdmin::getUsername, username);
|
|
|
|
+ wrapper.eq(TAdmin::getApiKey, apiKey);
|
|
|
|
+ TAdmin admin = adminService.getOne(wrapper);
|
|
|
|
+ if (admin == null) {
|
|
|
|
+ return R.fail(ResponseCodesEnum.A0001, "Invalid apiKey or username is not exist");
|
|
|
|
+ }
|
|
|
|
+ Long size = interfaceParam.getSize();
|
|
|
|
+ Long current = interfaceParam.getCurrent();
|
|
|
|
+ if (size == null || current == null) {
|
|
|
|
+ return R.fail(ResponseCodesEnum.A0001, "size or current is null");
|
|
|
|
+ }
|
|
|
|
+ if (size > 15) {
|
|
|
|
+ return R.fail(ResponseCodesEnum.A0001, "size is too large");
|
|
|
|
+ }
|
|
|
|
+ ArrayList<MachineVo> machineVos = new ArrayList<>();
|
|
|
|
+ LambdaQueryWrapper<TEquipment> query = Wrappers.lambdaQuery();
|
|
|
|
+ query.eq(TEquipment::getAdminId, admin.getId());
|
|
|
|
+ query.orderByDesc(TEquipment::getCreateDate);
|
|
|
|
+ Page<TEquipment> page = new Page<>(interfaceParam.getCurrent(), interfaceParam.getSize(), true);
|
|
|
|
+ IPage<TEquipment> iPage = equipmentService.page(page, query);
|
|
|
|
+ iPage.getRecords().forEach(equipment -> {
|
|
|
|
+ MachineVo machineVo = new MachineVo();
|
|
|
|
+ BeanUtils.copyPropertiesIgnoreNull(equipment, machineVo, true);
|
|
|
|
+ machineVos.add(machineVo);
|
|
|
|
+ });
|
|
|
|
+ return R.ok(machineVos);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @ApiOperation("远程操作")
|
|
|
|
+ @PostMapping("/remoteOperation")
|
|
|
|
+ public ResponseModel<?> remoteOperation(@RequestHeader("x-api-key") String apiKey, @RequestBody InterfaceParam interfaceParam) {
|
|
|
|
+ // 校验apiKey是否为空
|
|
|
|
+ if (StringUtils.isEmpty(apiKey)) {
|
|
|
|
+ return R.fail(ResponseCodesEnum.A0001, "apiKey is null");
|
|
|
|
+ }
|
|
|
|
+ // 校验参数是否为空
|
|
|
|
+ String username = interfaceParam.getUsername();
|
|
|
|
+ String clientId = interfaceParam.getClientId();
|
|
|
|
+ if (StringUtils.isEmpty(username)) {
|
|
|
|
+ return R.fail(ResponseCodesEnum.A0001, "username is null");
|
|
|
|
+ }
|
|
|
|
+ // 校验apiKey是否正确
|
|
|
|
+ LambdaQueryWrapper<TAdmin> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
+ wrapper.eq(TAdmin::getUsername, username);
|
|
|
|
+ wrapper.eq(TAdmin::getApiKey, apiKey);
|
|
|
|
+ TAdmin admin = adminService.getOne(wrapper);
|
|
|
|
+ if (admin == null) {
|
|
|
|
+ return R.fail(ResponseCodesEnum.A0001, "Invalid apiKey or username is not exist");
|
|
|
|
+ }
|
|
|
|
+ // 查询设备是否存在
|
|
|
|
+ if (StringUtils.isEmpty(clientId)) {
|
|
|
|
+ return R.fail(ResponseCodesEnum.A0001, "clientId is null");
|
|
|
|
+ }
|
|
|
|
+ LambdaQueryWrapper<TEquipment> query = Wrappers.lambdaQuery();
|
|
|
|
+ query.eq(TEquipment::getClientId, clientId);
|
|
|
|
+ query.eq(TEquipment::getAdminId, admin.getId());
|
|
|
|
+ TEquipment equipment = equipmentService.getOne(query);
|
|
|
|
+ if (equipment == null) {
|
|
|
|
+ return R.fail(ResponseCodesEnum.A0001, "clientId is not exist");
|
|
|
|
+ }
|
|
|
|
+ String operationField = interfaceParam.getOperationField();
|
|
|
|
+ String operationDesc = interfaceParam.getOperationDesc();
|
|
|
|
+ if (StringUtils.isNotEmpty(operationField) && StringUtils.isNotEmpty(operationDesc)) {
|
|
|
|
+ equipmentService.sentMessage(equipment.getClientId(), PushUtils.buildJson(operationField, operationDesc).toString());
|
|
|
|
+ return R.ok("success");
|
|
|
|
+ }
|
|
|
|
+ return R.fail(ResponseCodesEnum.A0001, "operationField or operationDesc is null");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @ApiOperation("远程制作")
|
|
|
|
+ @PostMapping("/remoteProduction")
|
|
|
|
+ public ResponseModel<?> remoteProduction(@RequestHeader("x-api-key") String apiKey, @RequestBody InterfaceParam interfaceParam) {
|
|
|
|
+ // 校验apiKey是否为空
|
|
|
|
+ if (StringUtils.isEmpty(apiKey)) {
|
|
|
|
+ return R.fail(ResponseCodesEnum.A0001, "apiKey is null");
|
|
|
|
+ }
|
|
|
|
+ // 校验参数是否为空
|
|
|
|
+ String username = interfaceParam.getUsername();
|
|
|
|
+ String clientId = interfaceParam.getClientId();
|
|
|
|
+ String productName = interfaceParam.getProductName();
|
|
|
|
+ if (StringUtils.isEmpty(username)) {
|
|
|
|
+ return R.fail(ResponseCodesEnum.A0001, "username is null");
|
|
|
|
+ }
|
|
|
|
+ if (StringUtils.isEmpty(clientId)) {
|
|
|
|
+ return R.fail(ResponseCodesEnum.A0001, "clientId is null");
|
|
|
|
+ }
|
|
|
|
+ if (StringUtils.isEmpty(productName)) {
|
|
|
|
+ return R.fail(ResponseCodesEnum.A0001, "productName is null");
|
|
|
|
+ }
|
|
|
|
+ // 校验apiKey是否正确
|
|
|
|
+ LambdaQueryWrapper<TAdmin> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
+ wrapper.eq(TAdmin::getUsername, username);
|
|
|
|
+ wrapper.eq(TAdmin::getApiKey, apiKey);
|
|
|
|
+ TAdmin admin = adminService.getOne(wrapper);
|
|
|
|
+ if (admin == null) {
|
|
|
|
+ return R.fail(ResponseCodesEnum.A0001, "Invalid apiKey or username is not exist");
|
|
|
|
+ }
|
|
|
|
+ // 校验clientId是否正确
|
|
|
|
+ LambdaQueryWrapper<TEquipment> equipmentWrapper = new LambdaQueryWrapper<>();
|
|
|
|
+ equipmentWrapper.eq(TEquipment::getClientId, clientId);
|
|
|
|
+ equipmentWrapper.eq(TEquipment::getAdminId, admin.getId());
|
|
|
|
+ TEquipment equipment = equipmentService.getOne(equipmentWrapper);
|
|
|
|
+ if(equipment == null) {
|
|
|
|
+ return R.fail(ResponseCodesEnum.A0001, "clientId is not exist");
|
|
|
|
+ }
|
|
|
|
+ // 校验productName是否正确
|
|
|
|
+ LambdaQueryWrapper<TProduct> productWrapper = new LambdaQueryWrapper<>();
|
|
|
|
+ productWrapper.eq(TProduct::getProductName, productName);
|
|
|
|
+ productWrapper.eq(TProduct::getEquipmentId, equipment.getId());
|
|
|
|
+ TProduct product = productService.getOne(productWrapper);
|
|
|
|
+ if(product == null) {
|
|
|
|
+ return R.fail(ResponseCodesEnum.A0001, "productName is not exist");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Long adminId = equipment.getAdminId();
|
|
|
|
+ // 校验当天是否已经生产超过20个
|
|
|
|
+ List<TSugarDo> sugarDoList = getSugarDos(adminId);
|
|
|
|
+ if (sugarDoList.size() > 20) {
|
|
|
|
+ return R.fail(ResponseCodesEnum.A0001, "There have been over 20 remote productions today, please do them again tomorrow");
|
|
|
|
+ }
|
|
|
|
+ // 远程制作
|
|
|
|
+ remoteResult(productName, equipment, adminId);
|
|
|
|
+ return R.ok();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void remoteResult(String productName, TEquipment equipment, Long adminId) {
|
|
|
|
+ StringBuilder number = new StringBuilder();
|
|
|
|
+ Random random = new Random();
|
|
|
|
+ number.append(adminId).append("-");
|
|
|
|
+ for (int i = 0; i < 6; i++) {
|
|
|
|
+ number.append(random.nextInt(10));
|
|
|
|
+ }
|
|
|
|
+ TSugarDo sugarDo = new TSugarDo();
|
|
|
|
+ sugarDo.setAdminId(adminId);
|
|
|
|
+ sugarDo.setClientId(equipment.getClientId());
|
|
|
|
+ sugarDo.setStatus("0");
|
|
|
|
+ sugarDo.setNo(number.toString());
|
|
|
|
+ sugarDo.setProductName(productName);
|
|
|
|
+ Date date = new Date();
|
|
|
|
+ sugarDo.setCreateDate(date);
|
|
|
|
+ sugarDo.setModifyDate(date);
|
|
|
|
+ sugarDoService.save(sugarDo);
|
|
|
|
+ JSONObject kindData = new JSONObject();
|
|
|
|
+ kindData.put("productName" , productName);
|
|
|
|
+ kindData.put("no" , sugarDo.getNo());
|
|
|
|
+ equipmentService.sentMessage(equipment.getClientId(), PushUtils.buildJson("dosugar", kindData.toString()).toString());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public List<TSugarDo> getSugarDos(Long adminId) {
|
|
|
|
+ LambdaQueryWrapper<TSugarDo> query = Wrappers.lambdaQuery();
|
|
|
|
+ query.eq(TSugarDo::getAdminId, adminId);
|
|
|
|
+ Calendar todayStart = Calendar.getInstance();
|
|
|
|
+ todayStart.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
+ todayStart.set(Calendar.MINUTE, 0);
|
|
|
|
+ todayStart.set(Calendar.SECOND, 0);
|
|
|
|
+ query.gt(TSugarDo::getCreateDate,todayStart.getTime());
|
|
|
|
+ Calendar todayEnd = Calendar.getInstance();
|
|
|
|
+ todayEnd.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
|
+ todayEnd.set(Calendar.MINUTE, 59);
|
|
|
|
+ todayEnd.set(Calendar.SECOND, 59);
|
|
|
|
+ query.le(TSugarDo::getCreateDate,todayEnd.getTime());
|
|
|
|
+ query.eq(TSugarDo::getStatus,"1");
|
|
|
|
+ List<TSugarDo> sugarDoList = sugarDoService.list(query);
|
|
|
|
+ return sugarDoList;
|
|
|
|
+ }
|
|
|
|
+}
|