package com.zy.acs.manager.manager.controller;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.zy.acs.common.constant.CommonConstant;
|
import com.zy.acs.common.utils.Utils;
|
import com.zy.acs.framework.common.Cools;
|
import com.zy.acs.framework.common.R;
|
import com.zy.acs.framework.exception.CoolException;
|
import com.zy.acs.manager.common.annotation.OperationLog;
|
import com.zy.acs.manager.common.domain.BaseParam;
|
import com.zy.acs.manager.common.domain.KeyValVo;
|
import com.zy.acs.manager.common.domain.PageParam;
|
import com.zy.acs.manager.common.utils.ExcelUtil;
|
import com.zy.acs.manager.manager.controller.param.LocInitParam;
|
import com.zy.acs.manager.manager.entity.Code;
|
import com.zy.acs.manager.manager.entity.Loc;
|
import com.zy.acs.manager.manager.entity.LocSts;
|
import com.zy.acs.manager.manager.entity.LocType;
|
import com.zy.acs.manager.manager.entity.Zone;
|
import com.zy.acs.manager.manager.service.CodeService;
|
import com.zy.acs.manager.manager.service.LocService;
|
import com.zy.acs.manager.manager.service.LocStsService;
|
import com.zy.acs.manager.manager.service.LocTypeService;
|
import com.zy.acs.manager.manager.service.ZoneService;
|
import com.zy.acs.manager.system.controller.BaseController;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.servlet.http.HttpServletResponse;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
@RestController
|
@RequestMapping("/api")
|
public class LocController extends BaseController {
|
|
@Autowired
|
private LocService locService;
|
@Autowired
|
private ZoneService zoneService;
|
@Autowired
|
private CodeService codeService;
|
@Autowired
|
private LocStsService locStsService;
|
@Autowired
|
private LocTypeService locTypeService;
|
|
@PreAuthorize("hasAuthority('manager:loc:list')")
|
@PostMapping("/loc/page")
|
public R page(@RequestBody Map<String, Object> map) {
|
BaseParam baseParam = buildParam(map, BaseParam.class);
|
PageParam<Loc, BaseParam> pageParam = new PageParam<>(baseParam, Loc.class);
|
return R.ok().add(locService.page(pageParam, pageParam.buildWrapper(false)));
|
}
|
|
@PreAuthorize("hasAuthority('manager:loc:list')")
|
@PostMapping("/loc/list")
|
public R list(@RequestBody Map<String, Object> map) {
|
return R.ok().add(locService.list());
|
}
|
|
@PreAuthorize("hasAuthority('manager:loc:list')")
|
@PostMapping({"/loc/many/{ids}", "/locs/many/{ids}"})
|
public R many(@PathVariable Long[] ids) {
|
return R.ok().add(locService.listByIds(Arrays.asList(ids)));
|
}
|
|
@PreAuthorize("hasAuthority('manager:loc:list')")
|
@GetMapping("/loc/{id}")
|
public R get(@PathVariable("id") Long id) {
|
return R.ok().add(locService.getById(id));
|
}
|
|
@PreAuthorize("hasAuthority('manager:loc:save')")
|
@OperationLog("Create Loc")
|
@PostMapping("/loc/save")
|
public R save(@RequestBody Loc loc) {
|
loc.setCreateBy(getLoginUserId());
|
loc.setCreateTime(new Date());
|
loc.setUpdateBy(getLoginUserId());
|
loc.setUpdateTime(new Date());
|
if (!locService.save(loc)) {
|
return R.error("Save Fail");
|
}
|
return R.ok("Save Success").add(loc);
|
}
|
|
@PreAuthorize("hasAuthority('manager:loc:update')")
|
@OperationLog("Update Loc")
|
@PostMapping("/loc/update")
|
public R update(@RequestBody Loc loc) {
|
loc.setUpdateBy(getLoginUserId());
|
loc.setUpdateTime(new Date());
|
if (!locService.updateById(loc)) {
|
return R.error("Update Fail");
|
}
|
return R.ok("Update Success").add(loc);
|
}
|
|
@PreAuthorize("hasAuthority('manager:loc:update')")
|
@OperationLog("Update Loc")
|
@PostMapping("/loc/update/many")
|
public R updateMany(@RequestBody List<Loc> locList) {
|
if (!Cools.isEmpty(locList)) {
|
for (Loc loc : locList) {
|
loc.setUpdateBy(getLoginUserId());
|
loc.setUpdateTime(new Date());
|
if (!locService.updateById(loc)) {
|
return R.error("Update Fail");
|
}
|
}
|
}
|
return R.ok("Update Success").add(locList.stream().map(Loc::getId).collect(Collectors.toList()));
|
}
|
|
@PreAuthorize("hasAuthority('manager:loc:remove')")
|
@OperationLog("Delete Loc")
|
@PostMapping("/loc/remove/{ids}")
|
public R remove(@PathVariable Long[] ids) {
|
if (!locService.removeByIds(Arrays.asList(ids))) {
|
return R.error("Delete Fail");
|
}
|
return R.ok("Delete Success").add(ids);
|
}
|
|
@PreAuthorize("hasAuthority('manager:loc:list')")
|
@PostMapping("/loc/query")
|
public R query(@RequestParam(required = false) String condition) {
|
List<KeyValVo> vos = new ArrayList<>();
|
LambdaQueryWrapper<Loc> wrapper = new LambdaQueryWrapper<>();
|
if (!Cools.isEmpty(condition)) {
|
wrapper.like(Loc::getLocNo, condition);
|
}
|
locService.page(new Page<>(1, 30), wrapper).getRecords().forEach(
|
item -> vos.add(new KeyValVo(item.getId(), item.getLocNo()))
|
);
|
return R.ok().add(vos);
|
}
|
|
@PreAuthorize("hasAuthority('manager:loc:list')")
|
@PostMapping("/loc/export")
|
public void export(@RequestBody Map<String, Object> map, HttpServletResponse response) throws Exception {
|
ExcelUtil.build(ExcelUtil.create(locService.list(), Loc.class), response);
|
}
|
|
@PreAuthorize("hasAuthority('manager:loc:save')")
|
@PostMapping("/loc/import")
|
public R importBatch(@RequestBody List<Map<String, Object>> rows) {
|
if (Cools.isEmpty(rows)) {
|
return R.ok();
|
}
|
Date now = new Date();
|
Long userId = getLoginUserId();
|
for (Map<String, Object> row : rows) {
|
Zone zone = resolveZone(readCell(row, "zone"));
|
Integer rowNo = parseInteger(row.get("row"), "row", true);
|
Integer bayNo = parseInteger(row.get("bay"), "bay", true);
|
Integer levNo = parseInteger(row.get("lev"), "lev", true);
|
String locNo = buildLocNo(zone, rowNo, bayNo, levNo);
|
|
Long locStsId = resolveLocStsId(readCell(row, "loc_sts"));
|
Long locTypeId = resolveLocTypeId(readCell(row, "loc_type"));
|
Long codeId = resolveCodeId(readCell(row, "code"));
|
Integer compDirect = parseInteger(row.get("comp_direct"), "comp_direct", false);
|
Double offset = parseDouble(row.get("offset"), "offset");
|
|
Loc loc = locService.selectByLocNo(locNo);
|
boolean exists = loc != null;
|
if (!exists) {
|
loc = new Loc();
|
loc.setUuid(locNo);
|
loc.setLocNo(locNo);
|
loc.setName(locNo);
|
loc.setStatus(1);
|
loc.setDeleted(0);
|
loc.setCreateBy(userId);
|
loc.setCreateTime(now);
|
}
|
loc.setZoneId(zone.getId());
|
loc.setRow(rowNo);
|
loc.setBay(bayNo);
|
loc.setLev(levNo);
|
loc.setLocSts(locStsId);
|
loc.setLocType(locTypeId);
|
loc.setCode(codeId);
|
loc.setCompDirect(compDirect);
|
loc.setOffset(offset);
|
loc.setUpdateBy(userId);
|
loc.setUpdateTime(now);
|
|
if (!exists) {
|
if (!locService.save(loc)) {
|
throw new CoolException(locNo + " save fail !");
|
}
|
} else {
|
if (!locService.updateById(loc)) {
|
throw new CoolException(locNo + " update fail !");
|
}
|
}
|
}
|
return R.ok();
|
}
|
|
@PreAuthorize("hasAuthority('manager:loc:save')")
|
@OperationLog
|
@PostMapping("/loc/init")
|
public R init(@RequestBody LocInitParam param) {
|
if (param.getStartRow() > param.getEndRow()) {
|
return R.error("the start row cannot be greater than the end row !");
|
}
|
if (param.getStartBay() > param.getEndBay()) {
|
return R.error("the start bay cannot be greater than the end bay !");
|
}
|
if (param.getStartLev() > param.getEndLev()) {
|
return R.error("the start lev cannot be greater than the end lev !");
|
}
|
Zone zone = zoneService.getById(param.getZoneId());
|
for (int r = param.getStartRow(); r <= param.getEndRow(); r++) {
|
for (int b = param.getStartBay(); b <= param.getEndBay(); b++) {
|
for (int l = param.getStartLev(); l <= param.getEndLev(); l++) {
|
String locNo = Utils.zeroFill(zone.getUuid(), 2) + String.format("%03d", r) + String.format("%03d", b) + String.format("%02d", l);
|
double offset = param.getBottom() + ((l-1) * param.getLevOffset());
|
Loc loc = new Loc(
|
locNo, // 编号
|
zone.getId(), // 库区
|
locNo, // 库位编号
|
null, // 名称
|
null, // 条码
|
param.getLocSts(), // 库位状态
|
offset, // 偏移量
|
r, // 排
|
b, // 列
|
l, // 层
|
null, // 托盘码
|
param.getLocType(), // 库位类型
|
null, // 状态[非空]
|
null, // 是否删除[非空]
|
null, // 租户
|
getLoginUserId(), // 添加人员
|
null, // 添加时间[非空]
|
getLoginUserId(), // 修改人员
|
null, // 修改时间
|
null // 备注
|
);
|
loc.setCompDirect(param.getCompDirect());
|
if (locService.count(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, locNo)) > 0) {
|
throw new CoolException(locNo + " location has exist !");
|
}
|
if (!locService.save(loc)) {
|
throw new CoolException(locNo + "location save fail !");
|
}
|
}
|
}
|
}
|
return R.ok("initialize success");
|
}
|
|
private Zone resolveZone(String identifier) {
|
if (Cools.isEmpty(identifier)) {
|
throw new CoolException("zone is required");
|
}
|
Zone zone = zoneService.getOne(new LambdaQueryWrapper<Zone>().eq(Zone::getUuid, identifier), false);
|
if (zone == null) {
|
zone = zoneService.getOne(new LambdaQueryWrapper<Zone>().eq(Zone::getName, identifier), false);
|
}
|
if (zone == null) {
|
throw new CoolException("zone " + identifier + " not found");
|
}
|
return zone;
|
}
|
|
private Long resolveLocStsId(String identifier) {
|
if (Cools.isEmpty(identifier)) {
|
throw new CoolException("loc_sts is required");
|
}
|
LocSts locSts = locStsService.getOne(new LambdaQueryWrapper<LocSts>().eq(LocSts::getUuid, identifier), false);
|
if (locSts == null) {
|
locSts = locStsService.getOne(new LambdaQueryWrapper<LocSts>().eq(LocSts::getName, identifier), false);
|
}
|
if (locSts == null) {
|
throw new CoolException("loc_sts " + identifier + " not found");
|
}
|
return locSts.getId();
|
}
|
|
private Long resolveLocTypeId(String identifier) {
|
if (Cools.isEmpty(identifier)) {
|
return null;
|
}
|
LocType locType = locTypeService.getOne(new LambdaQueryWrapper<LocType>().eq(LocType::getUuid, identifier), false);
|
if (locType == null) {
|
locType = locTypeService.getOne(new LambdaQueryWrapper<LocType>().eq(LocType::getName, identifier), false);
|
}
|
if (locType == null) {
|
throw new CoolException("loc_type " + identifier + " not found");
|
}
|
return locType.getId();
|
}
|
|
private Long resolveCodeId(String data) {
|
if (Cools.isEmpty(data)) {
|
return null;
|
}
|
String codeData = Utils.zeroFill(data, CommonConstant.QR_CODE_LEN);
|
Code code = codeService.getCacheByData(codeData);
|
if (code == null) {
|
throw new CoolException("code " + data + " not exist");
|
}
|
return code.getId();
|
}
|
|
private Integer parseInteger(Object value, String field, boolean required) {
|
if (value instanceof Number) {
|
return ((Number) value).intValue();
|
}
|
String text = value == null ? null : String.valueOf(value).trim();
|
if (Cools.isEmpty(text)) {
|
if (required) {
|
throw new CoolException(field + " is required");
|
}
|
return null;
|
}
|
try {
|
return Double.valueOf(text).intValue();
|
} catch (NumberFormatException ex) {
|
throw new CoolException(field + " format error: " + text);
|
}
|
}
|
|
private Double parseDouble(Object value, String field) {
|
if (value instanceof Number) {
|
return ((Number) value).doubleValue();
|
}
|
String text = value == null ? null : String.valueOf(value).trim();
|
if (Cools.isEmpty(text)) {
|
return null;
|
}
|
try {
|
return Double.valueOf(text);
|
} catch (NumberFormatException ex) {
|
throw new CoolException(field + " format error: " + text);
|
}
|
}
|
|
private String readCell(Map<String, Object> row, String key) {
|
if (row == null) {
|
return null;
|
}
|
Object value = row.get(key);
|
if (value == null) {
|
return null;
|
}
|
String text = String.valueOf(value).trim();
|
return Cools.isEmpty(text) ? null : text;
|
}
|
|
private String buildLocNo(Zone zone, Integer row, Integer bay, Integer lev) {
|
if (zone == null || row == null || bay == null || lev == null) {
|
throw new CoolException("zone/row/bay/lev cannot be null");
|
}
|
if (Cools.isEmpty(zone.getUuid())) {
|
throw new CoolException("zone uuid is empty");
|
}
|
return Utils.zeroFill(zone.getUuid(), 2)
|
+ String.format("%03d", row)
|
+ String.format("%03d", bay)
|
+ String.format("%02d", lev);
|
}
|
|
}
|