#
vincentlu
23 小时以前 dcc2fc4faf3150480c83c1c818d6a945eb6ed007
zy-acs-manager/src/main/java/com/zy/acs/manager/manager/controller/LocController.java
@@ -2,22 +2,21 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.acs.common.utils.GsonUtils;
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.utils.ExcelUtil;
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.Loc;
import com.zy.acs.manager.manager.entity.Zone;
import com.zy.acs.manager.manager.entity.*;
import com.zy.acs.manager.manager.enums.LocStsType;
import com.zy.acs.manager.manager.service.LocService;
import com.zy.acs.manager.manager.service.ZoneService;
import com.zy.acs.manager.manager.enums.StatusType;
import com.zy.acs.manager.manager.service.*;
import com.zy.acs.manager.system.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
@@ -35,13 +34,19 @@
    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(true)));
        return R.ok().add(locService.page(pageParam, pageParam.buildWrapper(false)));
    }
    @PreAuthorize("hasAuthority('manager:loc:list')")
@@ -128,12 +133,6 @@
        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')")
    @OperationLog
    @PostMapping("/loc/init")
@@ -188,4 +187,160 @@
        return R.ok("initialize success");
    }
    @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);
            if (locService.selectByLocNo(locNo) != null) {
                continue;
            }
            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 = new Loc();
            loc.setZoneId(zone.getId());
            loc.setUuid(locNo);
            loc.setLocNo(locNo);
            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.setStatus(StatusType.ENABLE.val);
            loc.setCreateBy(userId);
            loc.setCreateTime(now);
            loc.setUpdateBy(userId);
            loc.setUpdateTime(now);
            if (!locService.save(loc)) {
                throw new CoolException(locNo + " save fail !");
            }
        }
        return R.ok();
    }
    private Zone resolveZone(String identifier) {
        if (Cools.isEmpty(identifier)) {
            throw new CoolException("zone is required");
        }
        return zoneService.selectByUuid(identifier);
    }
    private Long resolveLocStsId(String identifier) {
        if (Cools.isEmpty(identifier)) {
            throw new CoolException("loc_sts is required");
        }
        LocStsType locStsType = LocStsType.valueOf(identifier);
        if (locStsType == null) {
            throw new CoolException("loc_sts " + identifier + " not found");
        }
        return locStsType.val();
    }
    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) {
            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);
    }
}