skyouc
2025-03-25 f967831b87cda525db4bf6f99c5caf3f2cbdef43
rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/MatnrServiceImpl.java
@@ -2,13 +2,20 @@
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.framework.common.SpringUtils;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.common.domain.BaseParam;
import com.vincent.rsf.server.common.domain.PageParam;
import com.vincent.rsf.server.common.utils.CommonUtil;
import com.vincent.rsf.server.common.utils.ExcelUtil;
import com.vincent.rsf.server.common.utils.FieldsUtils;
import com.vincent.rsf.server.manager.controller.params.MatnrToGroupParams;
import com.vincent.rsf.server.manager.entity.MatnrGroup;
import com.vincent.rsf.server.manager.entity.excel.MatnrsTemplate;
import com.vincent.rsf.server.manager.mapper.MatnrMapper;
@@ -16,8 +23,8 @@
import com.vincent.rsf.server.manager.service.MatnrGroupService;
import com.vincent.rsf.server.manager.service.MatnrService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vincent.rsf.server.system.entity.Fields;
import com.vincent.rsf.server.system.service.FieldsService;
import com.vincent.rsf.server.system.constant.SerialRuleCode;
import com.vincent.rsf.server.system.utils.SerialRuleUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -92,4 +99,95 @@
        }
        return R.ok("导入成功!!");
    }
    @Override
    public PageParam<Matnr, BaseParam> getMatnrPage(PageParam<Matnr, BaseParam> pageParam) {
        IPage<Map<String, Object>> reulst = this.baseMapper.selectMatnrs(pageParam, pageParam.buildWrapper(true));
        /**获取物料分页信息 */
        List<Map<String, Object>> mapList = reulst.getRecords();
        if (!mapList.isEmpty()) {
            mapList.forEach(map -> {
                if (!Objects.isNull(map.get("fieldsIndex"))) {
                   FieldsUtils.mergeFields(map, map.get("fieldsIndex").toString());
                }
            });
        }
        PageParam<Matnr, BaseParam> page = this.page(pageParam, pageParam.buildWrapper(true));
        List<Matnr> records = page.getRecords();
        for (Matnr record : records) {
            if (!Objects.isNull(record.getFieldsIndex())) {
                Map<String, String> fields = FieldsUtils.getFields(record.getFieldsIndex());
                record.setExtendFields(fields);
            }
        }
        page.setRecords(records);
        return page;
    }
    @Override
    public Matnr selectMatnrById(Long id) {
        Matnr matnr = this.baseMapper.selectById(id);
        if (Objects.isNull(matnr)) {
            throw new CoolException("当前物料不存在!!");
        }
        if (!Objects.isNull(matnr.getFieldsIndex())) {
            Map<String, String> fields = FieldsUtils.getFields(matnr.getFieldsIndex());
            matnr.setExtendFields(fields);
        }
        return matnr;
    }
    /**
     * 物料绑定
     * @param params
     * @return
     */
    @Override
    public boolean bindMatnrs(MatnrToGroupParams params) {
        if (Objects.isNull(params) && params.getIds().isEmpty()) {
            throw new CoolException("物料不能为空!!");
        }
        if (Objects.isNull(params.getGroupId())) {
            throw new CoolException("分组不能为空!!");
        }
        MatnrGroup groups = matnrGroupService.getOne(new LambdaQueryWrapper<MatnrGroup>().eq(MatnrGroup::getId, params.getGroupId()));
        if (Objects.isNull(groups)) {
            throw new CoolException("物料分组不存在!!");
        }
        if (!this.update(new LambdaUpdateWrapper<Matnr>()
                .in(Matnr::getId, params.getIds())
                .set(Matnr::getGroupId, groups.getId())
                .set(Matnr::getGroupCode, groups.getCode()))) {
            throw new CoolException("绑定失败!!");
        }
        return true;
    }
    /**
     * @desc 更新扩展物料扩展字段值
     * @param matnr
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveMatnrs(Map<String, Object> matnr) {
        Matnr matnr1 = JSONObject.parseObject(JSONObject.toJSONString(matnr), Matnr.class);
        if (Objects.isNull(matnr1.getCode())) {
            String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_MATNR_CODE, null);
            matnr1.setCode(ruleCode);
        }
        /**
         * 扩展字段存入库
         */
        String uuid16 = CommonUtil.randomUUID16();
        if (!FieldsUtils.getFieldsSta().isEmpty()) {
            FieldsUtils.saveFields(matnr, uuid16);
            matnr1.setFieldsIndex(uuid16);
        }
        if (!this.saveOrUpdate(matnr1)) {
            return R.error("Save Fail");
        }
        return R.ok(matnr1);
    }
}