package com.wms.controller; import com.alibaba.fastjson2.JSON; import com.wms.constants.enums.LocationStatus; import com.wms.constants.enums.ResponseCode; import com.wms.constants.enums.VehicleStatus; import com.wms.entity.app.ResponseEntity; import com.wms.entity.table.Location; import com.wms.entity.table.User; import com.wms.entity.table.Vehicle; import com.wms.entity.test.ExcelTest; import com.wms.service.LocationService; import com.wms.service.VehicleService; import com.wms.utils.MyPassword; import com.wms.utils.excel.ExcelUtils; import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.interceptor.TransactionAspectSupport; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.util.ArrayList; import java.util.List; /** * 测试控制类 */ @Controller @CrossOrigin @RequestMapping(value = "/wms/test") @RequiredArgsConstructor(onConstructor = @__(@Autowired)) public class TestController extends BaseController { /** * 库位服务 */ private final LocationService locationService; private final VehicleService vehicleService; @PostMapping("/genLocations") @ResponseBody public String genLocations() { // 巷道1 一共2排45列12层 for (int i = 1; i <= 2; i++) {// 排 for (int j = 1; j <= 45; j++) {// 列 for (int k = 1; k <= 12; k++) {// 层 Location tempLocation = new Location(); tempLocation.setLocationId(StringUtils.leftPad(String.valueOf(i), 2, "0") + StringUtils.leftPad(String.valueOf(j), 2, "0") + StringUtils.leftPad(String.valueOf(k), 2, "0")); tempLocation.setAreaId(1); tempLocation.setTunnelId(1); tempLocation.setEquipmentId(1); tempLocation.setLocationType(0); tempLocation.setQueue(i); tempLocation.setLine(j); tempLocation.setLayer(k); tempLocation.setDepth(1); tempLocation.setIsLock(0); tempLocation.setLocationStatus(0); if (locationService.selLocations(tempLocation).size() == 0) { locationService.addLocation(tempLocation); } } } } // // 巷道2 // for (int i = 3; i <= 4; i++) {// 排 // for (int j = 3; j <= 34; j++) {// 列 // Location tempLocation = new Location(); // tempLocation.setLocationId(StringUtils.leftPad(String.valueOf(i), 2, "0") + StringUtils.leftPad(String.valueOf(j), 2, "0") + StringUtils.leftPad(String.valueOf(13), 2, "0")); // tempLocation.setAreaId(1); // tempLocation.setTunnelId(2); // tempLocation.setEquipmentId(2); // tempLocation.setLocationType(0); // tempLocation.setQueue(i); // tempLocation.setLine(j); // tempLocation.setLayer(13); // tempLocation.setDepth(1); // tempLocation.setIsLock(0); // tempLocation.setLocationStatus(0); // locationService.addLocation(tempLocation); // } // } return "success"; } @PostMapping("/genETagLocations") @ResponseBody public String genETagLocations() { // for (int i = 1; i <= 40; i++) { // ETagLocation eTagLocation = new ETagLocation(); // eTagLocation.setLocationId("A" + StringUtils.leftPad(String.valueOf(i), 2, "0")); // eTagLocation.setAreaId("1"); // eTagLocation.setStandId("2001"); // eTagLocation.setSequenceNo(i); // eTagLocationService.addETagLocation(eTagLocation); // } // for (int i = 1; i <= 70; i++) { // ETagLocation eTagLocation = new ETagLocation(); // eTagLocation.setLocationId("B" + StringUtils.leftPad(String.valueOf(i), 2, "0")); // eTagLocation.setAreaId("2"); // eTagLocation.setStandId("2002"); // eTagLocation.setSequenceNo(i); // eTagLocationService.addETagLocation(eTagLocation); // } // for (int i = 1; i <= 70; i++) { // ETagLocation eTagLocation = new ETagLocation(); // eTagLocation.setLocationId("C" + StringUtils.leftPad(String.valueOf(i), 2, "0")); // eTagLocation.setAreaId("3"); // eTagLocation.setStandId("2003"); // eTagLocation.setSequenceNo(i); // eTagLocationService.addETagLocation(eTagLocation); // } // for (int i = 1; i <= 70; i++) { // ETagLocation eTagLocation = new ETagLocation(); // eTagLocation.setLocationId("D" + StringUtils.leftPad(String.valueOf(i), 2, "0")); // eTagLocation.setAreaId("4"); // eTagLocation.setStandId("2004"); // eTagLocation.setSequenceNo(i); // eTagLocationService.addETagLocation(eTagLocation); // } // for (int i = 1; i <= 70; i++) { // ETagLocation eTagLocation = new ETagLocation(); // eTagLocation.setLocationId("E" + StringUtils.leftPad(String.valueOf(i), 2, "0")); // eTagLocation.setAreaId("5"); // eTagLocation.setStandId("2005"); // eTagLocation.setSequenceNo(i); // eTagLocationService.addETagLocation(eTagLocation); // } // for (int i = 1; i <= 70; i++) { // ETagLocation eTagLocation = new ETagLocation(); // eTagLocation.setLocationId("F" + StringUtils.leftPad(String.valueOf(i), 2, "0")); // eTagLocation.setAreaId("6"); // eTagLocation.setStandId("2006"); // eTagLocation.setSequenceNo(i); // eTagLocationService.addETagLocation(eTagLocation); // } // for (int i = 1; i <= 70; i++) { // ETagLocation eTagLocation = new ETagLocation(); // eTagLocation.setLocationId("G" + StringUtils.leftPad(String.valueOf(i), 2, "0")); // eTagLocation.setAreaId("7"); // eTagLocation.setStandId("2007"); // eTagLocation.setSequenceNo(i); // eTagLocationService.addETagLocation(eTagLocation); // } // for (int i = 1; i <= 40; i++) { // ETagLocation eTagLocation = new ETagLocation(); // eTagLocation.setLocationId("H" + StringUtils.leftPad(String.valueOf(i), 2, "0")); // eTagLocation.setAreaId("8"); // eTagLocation.setStandId("2008"); // eTagLocation.setSequenceNo(i); // eTagLocationService.addETagLocation(eTagLocation); // } return "success"; } /** * 测试导入 * * @param file 文件 * @return 导入结果 */ @PostMapping("/testExcelImport") @ResponseBody @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) public String testExcelImport(@RequestPart("file") MultipartFile file) { ResponseEntity response = new ResponseEntity(); try { List tests = ExcelUtils.readMultipartFile(file, ExcelTest.class); for (ExcelTest test : tests) { // 更新库位 Location locationQuery = new Location(); locationQuery.setLocationId(test.getLocationId()); List locations = locationService.selLocations(locationQuery); if (locations.size() > 0) { Location currentLocation = locations.get(0); currentLocation.setLocationStatus(LocationStatus.OCCUPY.getCode()); currentLocation.setVehicleId("XZBK" + StringUtils.leftPad(test.getVehicleId().trim(), 4, "0")); locationService.modifyLocation(currentLocation); // 添加载具 Vehicle vehicle = new Vehicle(); vehicle.setVehicleId("XZBK" + StringUtils.leftPad(test.getVehicleId().trim(), 4, "0")); vehicle.setCurrentLocation(test.getLocationId()); vehicle.setVehicleStatus(VehicleStatus.ON.getCode()); vehicle.setIsEmpty(1); if (vehicleService.selVehicleById(test.getVehicleId()) == null) { vehicleService.addVehicle(vehicle); } else { vehicleService.modifyVehicle(vehicle); } } } response.setCode(ResponseCode.OK.getCode()); response.setMessage("导入excel成功"); response.setReturnData(tests); } catch (Exception e) { // 回滚事务 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); response.setCode(ResponseCode.ERROR.getCode()); response.setMessage("导入excel发生错误"); } logger.info(JSON.toJSONString(response)); return JSON.toJSONString(response); } /** * 测试导出 * * @param response 请求 */ @GetMapping("/testExcelExport") @ResponseBody public void testExcelExport(HttpServletResponse response) { List tests = new ArrayList<>(); ExcelTest temp1 = new ExcelTest("010101", "XZBK0001"); ExcelTest temp2 = new ExcelTest("020101", "XZBK0002"); tests.add(temp1); tests.add(temp2); ExcelUtils.export(response, "测试导出", tests, ExcelTest.class); } @PostMapping("/genPassword") @ResponseBody @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) public String genPassword(@RequestBody User user) { ResponseEntity response = new ResponseEntity(); response.setCode(ResponseCode.OK.getCode()); response.setMessage("创建密码成功"); response.setReturnData(MyPassword.encrypt(user.getLoginPassword())); return JSON.toJSONString(response); } }