|  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | 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.exception.CoolException; | 
|---|
|  |  |  | import com.vincent.rsf.server.common.domain.BaseParam; | 
|---|
|  |  |  | 
|---|
|  |  |  | 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.enums.MatnrLocLevel; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.mapper.MatnrMapper; | 
|---|
|  |  |  | import com.vincent.rsf.server.manager.entity.Matnr; | 
|---|
|  |  |  | 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.constant.SerialRuleCode; | 
|---|
|  |  |  | import com.vincent.rsf.server.system.service.FieldsService; | 
|---|
|  |  |  | import com.vincent.rsf.server.system.utils.SerialRuleUtils; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private MatnrGroupService matnrGroupService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private FieldsService fieldsService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | * @throws | 
|---|
|  |  |  | 
|---|
|  |  |  | if (result.getList().isEmpty()) { | 
|---|
|  |  |  | throw new CoolException("物料导入失败!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<Matnr> matnrs = new ArrayList<>(); | 
|---|
|  |  |  | List<Map<String, String>> list = result.getList(); | 
|---|
|  |  |  | list.forEach(template -> { | 
|---|
|  |  |  | Matnr matnr = new Matnr(); | 
|---|
|  |  |  | matnr.setBarcode(template.get("barcode")) | 
|---|
|  |  |  | .setCode(template.get("code")) | 
|---|
|  |  |  | .setDescrible(template.get("describle")) | 
|---|
|  |  |  | .setColor(template.get("color")) | 
|---|
|  |  |  | .setFlagCheck(!Objects.isNull(template.get("flagCheck")) ? Short.parseShort(template.get("flagCheck")) : 0) | 
|---|
|  |  |  | .setWeight(!Objects.isNull(template.get("weight")) ? Double.parseDouble(template.get("weight")) : 0.0) | 
|---|
|  |  |  | .setValidWarn(!Objects.isNull(template.get("validWarn")) ? Integer.parseInt(template.get("validWarn")): 0) | 
|---|
|  |  |  | .setValid(!Objects.isNull(template.get("valid")) ? Integer.parseInt( template.get("valid")) : 0) | 
|---|
|  |  |  | .setUnit(template.get("unit")) | 
|---|
|  |  |  | .setStockUnit(template.get("purUnit")) | 
|---|
|  |  |  | .setSpec(template.get("spec")) | 
|---|
|  |  |  | .setStagn(!Objects.isNull(template.get("stagn")) ? Integer.parseInt(template.get("stagn")) : 0) | 
|---|
|  |  |  | .setModel(template.get("model")) | 
|---|
|  |  |  | .setGroupCode(template.get("groupCode")) | 
|---|
|  |  |  | .setPurUnit(template.get("purUnit")) | 
|---|
|  |  |  | .setStockLevel(!Objects.isNull(template.get("stockLevel")) ? Short.parseShort(template.get("stockLevel")) : 0) | 
|---|
|  |  |  | .setSafeQty(!Objects.isNull(template.get("safeQty")) ? Double.parseDouble(template.get("safeQty")) : 0) | 
|---|
|  |  |  | .setMinQty(!Objects.isNull(template.get("safeQty")) ? Double.parseDouble(template.get("minQty")) : 0); | 
|---|
|  |  |  | if (Objects.isNull(template.get("groupCode")) && Objects.isNull(template.get("groupName"))) { | 
|---|
|  |  |  | MatnrGroup matnrGroups = matnrGroupService.getOne(new LambdaQueryWrapper<MatnrGroup>() | 
|---|
|  |  |  | .eq(!Objects.isNull(template.get("groupCode")), MatnrGroup::getCode, template.get("groupCode")) | 
|---|
|  |  |  | .eq(!Objects.isNull(template.get("groupName")),MatnrGroup::getName, template.get("groupName"))); | 
|---|
|  |  |  | matnr.setGroupId(matnrGroups.getId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //获取动态字段,并保存明细内容 | 
|---|
|  |  |  | if (!FieldsUtils.getFieldsSta().isEmpty()) { | 
|---|
|  |  |  | String uuid = CommonUtil.randomUUID16(); | 
|---|
|  |  |  | matnr.setFieldsIndex(uuid); | 
|---|
|  |  |  | //保存物料扩展属性值 | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | FieldsUtils.saveFields(template, uuid); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | throw new RuntimeException(e); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | matnrs.add(matnr); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<MatnrsTemplate> list = result.getList(); | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取导入物料模板 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | List<Matnr> matnrs = getMatnrTempletes(list); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!this.saveBatch(matnrs)) { | 
|---|
|  |  |  | throw new CoolException("导入失败!!"); | 
|---|
|  |  |  | 
|---|
|  |  |  | return R.ok("导入成功!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取物料模板信息 | 
|---|
|  |  |  | * @param list | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<Matnr> getMatnrTempletes(List<MatnrsTemplate> list) { | 
|---|
|  |  |  | List<Matnr> matnrs = new ArrayList<>(); | 
|---|
|  |  |  | list.forEach(template -> { | 
|---|
|  |  |  | Matnr matnr = new Matnr(); | 
|---|
|  |  |  | matnr.setBarcode(template.getBarcode()) | 
|---|
|  |  |  | .setCode(template.getCode()) | 
|---|
|  |  |  | .setName(template.getName()) | 
|---|
|  |  |  | .setId(null) | 
|---|
|  |  |  | .setDescrible(template.getDescrible()) | 
|---|
|  |  |  | .setColor(template.getColor()) | 
|---|
|  |  |  | .setFlagCheck(!Objects.isNull(template.getFlagCheck()) ? Short.parseShort(template.getFlagCheck()) : 0) | 
|---|
|  |  |  | .setWeight(!Objects.isNull(template.getWeight()) ? Double.parseDouble(template.getWeight()) : 0.0) | 
|---|
|  |  |  | .setValidWarn(!Objects.isNull(template.getValidWarn()) ? Integer.parseInt(template.getValidWarn()): 0) | 
|---|
|  |  |  | .setValid(!Objects.isNull(template.getValid()) ? Integer.parseInt( template.getValid()) : 0) | 
|---|
|  |  |  | .setUnit(template.getUnit()) | 
|---|
|  |  |  | .setStockUnit(template.getPurUnit()) | 
|---|
|  |  |  | .setSize(template.getSize()) | 
|---|
|  |  |  | .setSpec(template.getSpec()) | 
|---|
|  |  |  | .setStagn(!Objects.isNull(template.getStagn()) ? Integer.parseInt(template.getStagn()) : 0) | 
|---|
|  |  |  | .setModel(template.getModel()) | 
|---|
|  |  |  | .setGroupCode(template.getGroupCode()) | 
|---|
|  |  |  | .setPurUnit(template.getPurUnit()) | 
|---|
|  |  |  | .setStockLevel(!Objects.isNull(template.getStockLevel()) ? MatnrLocLevel.getTypeVal(template.getStockLevel()) : null) | 
|---|
|  |  |  | .setSafeQty(!Objects.isNull(template.getSafeQty()) ? Double.parseDouble(template.getSafeQty()) : 0) | 
|---|
|  |  |  | .setMinQty(!Objects.isNull(template.getMinQty()) ? Double.parseDouble(template.getMinQty()) : 0); | 
|---|
|  |  |  | if (!Objects.isNull(template.getGroupCode()) || !Objects.isNull(template.getGroupName())) { | 
|---|
|  |  |  | MatnrGroup matnrGroups = matnrGroupService.getOne(new LambdaQueryWrapper<MatnrGroup>() | 
|---|
|  |  |  | .eq(!Objects.isNull(template.getGroupCode()), MatnrGroup::getCode, template.getGroupCode()) | 
|---|
|  |  |  | .eq(!Objects.isNull(template.getGroupName()), MatnrGroup::getName, template.getGroupName())); | 
|---|
|  |  |  | if (!Objects.isNull(matnrGroups)) { | 
|---|
|  |  |  | matnr.setGroupId(matnrGroups.getId()).setGroupCode(matnrGroups.getCode()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //            List<Fields> fields = fieldsService.list(new LambdaQueryWrapper<Fields>() | 
|---|
|  |  |  | //                    .eq(Fields::getUnique, CommonStatus.COMMONSTATUS_NO.val) | 
|---|
|  |  |  | //                    .eq(Fields::getFlagEnable, CommonStatus.COMMONSTATUS_YES.val)); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //            //获取动态字段,并保存明细内容 | 
|---|
|  |  |  | //            if (!fields.isEmpty()) { | 
|---|
|  |  |  | //                //保存物料扩展属性值 | 
|---|
|  |  |  | //                try { | 
|---|
|  |  |  | //                    Map<String, Object> map = JSONObject.parseObject(JSONObject.toJSONString(template), Map.class); | 
|---|
|  |  |  | //                    String uuid = CommonUtil.randomUUID16(); | 
|---|
|  |  |  | //                    boolean b = FieldsUtils.saveFields(map, uuid); | 
|---|
|  |  |  | //                    if (b) { | 
|---|
|  |  |  | //                        matnr.setFieldsIndex(uuid); | 
|---|
|  |  |  | //                    } | 
|---|
|  |  |  | //                } catch (Exception e) { | 
|---|
|  |  |  | //                    throw new RuntimeException(e); | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | matnrs.add(matnr); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | return matnrs; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public PageParam<Matnr, BaseParam> getMatnrPage(PageParam<Matnr, BaseParam> pageParam, Map<String, Object> params) { | 
|---|
|  |  |  | QueryWrapper<Matnr> queryWrapper = pageParam.buildWrapper(true); | 
|---|
|  |  |  | List<Long> longs = new ArrayList<>(); | 
|---|
|  |  |  | if (params.containsKey("groupId")) { | 
|---|
|  |  |  | Object groupId = params.get("groupId"); | 
|---|
|  |  |  | if (!Objects.isNull(groupId)) { | 
|---|
|  |  |  | List<MatnrGroup> matnrGroups = matnrGroupService.list(new LambdaQueryWrapper<MatnrGroup>().eq(MatnrGroup::getParentId, Long.parseLong(groupId.toString())).select(MatnrGroup::getId)); | 
|---|
|  |  |  | if (!matnrGroups.isEmpty()) { | 
|---|
|  |  |  | List<Long> longs = matnrGroups.stream().map(MatnrGroup::getId).collect(Collectors.toList()); | 
|---|
|  |  |  | queryWrapper.or().in("group_id", longs); | 
|---|
|  |  |  | 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()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //        IPage<Map<String, Object>> reulst = this.baseMapper.selectMatnrs(pageParam, queryWrapper); | 
|---|
|  |  |  | //        /**获取物料分页信息 */ | 
|---|
|  |  |  | //        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()); | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | //            }); | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | QueryWrapper<Matnr> queryWrapper = pageParam.buildWrapper(true); | 
|---|
|  |  |  | queryWrapper.in(!longs.isEmpty(),"group_id", longs); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | FieldsUtils.setFieldsFilters(queryWrapper,pageParam,Matnr.class); | 
|---|
|  |  |  | /**拼接扩展字段*/ | 
|---|
|  |  |  | PageParam<Matnr, BaseParam> page = this.page(pageParam, queryWrapper); | 
|---|
|  |  |  | List<Matnr> records = page.getRecords(); | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | String uuid16 = CommonUtil.randomUUID16(); | 
|---|
|  |  |  | if (!FieldsUtils.getFieldsSta().isEmpty()) { | 
|---|
|  |  |  | Map<String, ?> extendFields = (Map<String, ?>) matnr.get("extendFields"); | 
|---|
|  |  |  | //            Map<String, ?> extendFields = (Map<String, ?>) matnr.get("extendFields"); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | FieldsUtils.saveFields(extendFields, uuid16); | 
|---|
|  |  |  | FieldsUtils.saveFields(matnr, uuid16); | 
|---|
|  |  |  | matnr1.setFieldsIndex(uuid16); | 
|---|
|  |  |  | } catch (Exception ex) { | 
|---|
|  |  |  | log.error(ex.toString()); | 
|---|