cl
2 天以前 0e85601e2f80b6cb580c0fad9be9518f58c5882c
rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/MatnrServiceImpl.java
@@ -25,17 +25,23 @@
import com.vincent.rsf.server.system.constant.SerialRuleCode;
import com.vincent.rsf.server.system.service.FieldsService;
import com.vincent.rsf.server.system.utils.SerialRuleUtils;
import org.apache.commons.lang3.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
@Slf4j
@Service("matnrService")
public class MatnrServiceImpl extends ServiceImpl<MatnrMapper, Matnr> implements MatnrService {
@@ -44,6 +50,126 @@
    @Autowired
    private FieldsService fieldsService;
    @Override
    public String normalizeMatnrBatchValue(String batch) {
        return StringUtils.isBlank(batch) ? "" : batch.trim();
    }
    @Override
    public Matnr getOneByCodeAndBatch(String code, String batchNullable) {
        if (StringUtils.isBlank(code)) {
            return null;
        }
        String nb = normalizeMatnrBatchValue(batchNullable);
        return this.getOne(new LambdaQueryWrapper<Matnr>()
                .eq(Matnr::getCode, code.trim())
                .eq(Matnr::getBatch, nb), false);
    }
    private void normalizeMatnrBatchOnEntity(Matnr entity) {
        if (entity == null) {
            return;
        }
        entity.setBatch(normalizeMatnrBatchValue(entity.getBatch()));
    }
    private void assertUniqueMatnr(Matnr entity) {
        if (entity == null || StringUtils.isBlank(entity.getCode())) {
            return;
        }
        normalizeMatnrBatchOnEntity(entity);
        LambdaQueryWrapper<Matnr> w = new LambdaQueryWrapper<Matnr>()
                .eq(Matnr::getCode, entity.getCode().trim())
                .eq(Matnr::getBatch, entity.getBatch());
        if (entity.getId() != null) {
            w.ne(Matnr::getId, entity.getId());
        }
        if (this.count(w) > 0) {
            String nb = entity.getBatch();
            throw new CoolException(StringUtils.isBlank(nb)
                    ? "物料编码已存在(无批次):" + entity.getCode().trim()
                    : "物料编码与批次组合已存在:" + entity.getCode().trim() + " / " + nb);
        }
    }
    private void assertNoDuplicateMatnrKeysInList(List<Matnr> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        Set<String> keys = new HashSet<>();
        for (Matnr m : list) {
            if (m == null || StringUtils.isBlank(m.getCode())) {
                continue;
            }
            normalizeMatnrBatchOnEntity(m);
            String key = m.getCode().trim() + "\u0000" + m.getBatch();
            if (!keys.add(key)) {
                throw new CoolException("导入数据中物料编码与批次重复:" + m.getCode().trim()
                        + (StringUtils.isNotBlank(m.getBatch()) ? " / " + m.getBatch() : ""));
            }
        }
    }
    @Override
    public boolean save(Matnr entity) {
        normalizeMatnrBatchOnEntity(entity);
        assertUniqueMatnr(entity);
        return super.save(entity);
    }
    @Override
    public boolean updateById(Matnr entity) {
        normalizeMatnrBatchOnEntity(entity);
        assertUniqueMatnr(entity);
        return super.updateById(entity);
    }
    @Override
    public boolean saveOrUpdate(Matnr entity) {
        normalizeMatnrBatchOnEntity(entity);
        assertUniqueMatnr(entity);
        return super.saveOrUpdate(entity);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(Collection<Matnr> entityList) {
        if (entityList == null || entityList.isEmpty()) {
            return true;
        }
        for (Matnr m : entityList) {
            normalizeMatnrBatchOnEntity(m);
            assertUniqueMatnr(m);
        }
        return super.saveBatch(entityList);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(Collection<Matnr> entityList, int batchSize) {
        if (entityList == null || entityList.isEmpty()) {
            return true;
        }
        for (Matnr m : entityList) {
            normalizeMatnrBatchOnEntity(m);
            assertUniqueMatnr(m);
        }
        return super.saveBatch(entityList, batchSize);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateBatch(Collection<Matnr> entityList, int batchSize) {
        if (entityList == null || entityList.isEmpty()) {
            return true;
        }
        for (Matnr m : entityList) {
            normalizeMatnrBatchOnEntity(m);
            assertUniqueMatnr(m);
        }
        return super.saveOrUpdateBatch(entityList, batchSize);
    }
    /**
     * @return
@@ -66,6 +192,7 @@
         * 获取导入物料模板
         */
        List<Matnr> matnrs = getMatnrTempletes(list);
        assertNoDuplicateMatnrKeysInList(matnrs);
        if (!this.saveBatch(matnrs)) {
            throw new CoolException("导入失败!!");
@@ -85,6 +212,7 @@
            Matnr matnr = new Matnr();
            matnr.setBarcode(template.getBarcode())
                    .setCode(template.getCode())
                    .setBatch(normalizeMatnrBatchValue(template.getBatch()))
                    .setName(template.getName())
                    .setId(null)
                    .setDescrible(template.getDescrible())
@@ -139,24 +267,37 @@
    public PageParam<Matnr, BaseParam> getMatnrPage(PageParam<Matnr, BaseParam> pageParam, Map<String, Object> params) {
        List<Long> longs = new ArrayList<>();
        if (params.containsKey("groupId")) {
            Object groupId = params.get("groupId");
            MatnrGroup group = matnrGroupService.getById(groupId.toString());
            if (null != group){
                pageParam.getWhere().getMap().remove("groupId");
                if(!group.getCode().equals("01")){
                    List<MatnrGroup> matnrGroups = matnrGroupService.list(new LambdaQueryWrapper<MatnrGroup>()
                            .eq(MatnrGroup::getParentId, Long.parseLong(groupId.toString()))
                            .select(MatnrGroup::getId));
                    if (!matnrGroups.isEmpty()) {
                        longs = matnrGroups.stream().map(MatnrGroup::getId).collect(Collectors.toList());
            if (!Objects.isNull(params.get("groupId"))) {
                Object groupId = params.get("groupId");
                MatnrGroup group = matnrGroupService.getById(groupId.toString());
                if (null != group){
                    pageParam.getWhere().getMap().remove("groupId");
                    if(!group.getCode().equals("01")){
                        List<MatnrGroup> matnrGroups = matnrGroupService.list(new LambdaQueryWrapper<MatnrGroup>()
                                .eq(MatnrGroup::getParentId, Long.parseLong(groupId.toString()))
                                .select(MatnrGroup::getId));
                        if (!matnrGroups.isEmpty()) {
                            longs = matnrGroups.stream().map(MatnrGroup::getId).collect(Collectors.toList());
                        }
                        longs.add(group.getId());
                    }
                    longs.add(group.getId());
                }
            }
        }
        // locUseStatus 仅用于下方 EXISTS 子查询,不能作为 man_matnr 表字段参与 buildWrapper
        Object locUseStatus = params.get("locUseStatus");
        if (pageParam.getWhere() != null && pageParam.getWhere().getMap() != null) {
            pageParam.getWhere().getMap().remove("locUseStatus");
        }
        QueryWrapper<Matnr> queryWrapper = pageParam.buildWrapper(true);
        queryWrapper.in(!longs.isEmpty(),"group_id", longs);
        // 出库选物料:按库位状态筛选(仅展示在该库位状态下有有效库存的物料,与 listStockByMatnrIds 口径一致:排除已删明细与已删库位)
        if (locUseStatus != null && StringUtils.isNotBlank(locUseStatus.toString())) {
            String useStatus = locUseStatus.toString().replace("'", "''");
            queryWrapper.apply("EXISTS (SELECT 1 FROM man_loc_item li INNER JOIN man_loc l ON li.loc_id = l.id AND (l.deleted = 0 OR l.deleted IS NULL) WHERE li.matnr_id = man_matnr.id AND li.deleted = 0 AND l.use_status = '" + useStatus + "')");
        }
        FieldsUtils.setFieldsFilters(queryWrapper,pageParam,Matnr.class);
        /**拼接扩展字段*/
@@ -263,6 +404,7 @@
            String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_MATNR_CODE, null);
            matnr1.setCode(ruleCode);
        }
        normalizeMatnrBatchOnEntity(matnr1);
        /**
         * 扩展字段存入库
         */