|  |  | 
 |  |  | package com.vincent.rsf.server.common.utils; | 
 |  |  |  | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
 |  |  | import com.baomidou.mybatisplus.core.metadata.TableInfo; | 
 |  |  | import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; | 
 |  |  | import com.vincent.rsf.framework.common.Cools; | 
 |  |  | 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.manager.enums.CommonStatus; | 
 |  |  | import com.vincent.rsf.server.system.entity.Fields; | 
 |  |  | import com.vincent.rsf.server.system.entity.FieldsItem; | 
 |  |  | import com.vincent.rsf.server.system.service.FieldsItemService; | 
 |  |  | import com.vincent.rsf.server.system.service.FieldsService; | 
 |  |  | import lombok.Synchronized; | 
 |  |  | import org.apache.commons.lang3.StringUtils; | 
 |  |  | import org.springframework.transaction.annotation.Transactional; | 
 |  |  |  | 
 |  |  | import java.util.*; | 
 |  |  |  | 
 |  |  | 
 |  |  | public class FieldsUtils { | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * @param | 
 |  |  |      * @return 扩展字段对象 | 
 |  |  |      * @author Ryan | 
 |  |  |      * @description 通过字段唯一标识获取动态字段对象key-value | 
 |  |  |      * @param | 
 |  |  |      * @return | 
 |  |  |      * @time 2025/3/12 12:50 | 
 |  |  |      */ | 
 |  |  |     public static Map<String, String> getFields(String uuid) { | 
 |  |  | 
 |  |  |         } | 
 |  |  |         FieldsItemService fieldsItemService = SpringUtils.getBean(FieldsItemService.class); | 
 |  |  |         List<FieldsItem> fieldsItems = fieldsItemService.list(new LambdaQueryWrapper<FieldsItem>().eq(FieldsItem::getUuid, uuid)); | 
 |  |  |         for (Fields field : fields ) { | 
 |  |  |         for (Fields field : fields) { | 
 |  |  |             if (fieldsItems.isEmpty()) { | 
 |  |  |                 fieldsMap.put(field.getFields(), null); | 
 |  |  |                 continue; | 
 |  |  | 
 |  |  |             }); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         return  fieldsMap; | 
 |  |  |         return fieldsMap; | 
 |  |  |     } | 
 |  |  |      | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * @param | 
 |  |  |      * @return 包含扩展字段的集合对象 | 
 |  |  |      * @author Ryan | 
 |  |  |      * @description 获取集合扩展字段key-value值 | 
 |  |  |      * @time 2025/3/15 15:05 | 
 |  |  |      */ | 
 |  |  |     public static List<Map<String, Object>> getExtendFields(List<Map<String, Object>> params) { | 
 |  |  |         FieldsService fieldsService = SpringUtils.getBean(FieldsService.class); | 
 |  |  |         List<Fields> fields = fieldsService.list(new LambdaQueryWrapper<Fields>() | 
 |  |  |                 .eq(Fields::getStatus, 1) | 
 |  |  |                 .eq(Fields::getFlagEnable, 1)); | 
 |  |  |         FieldsItemService fieldsItemService = SpringUtils.getBean(FieldsItemService.class); | 
 |  |  |         List<Map<String, Object>> result = new ArrayList<>(); | 
 |  |  |         for (Map<String, Object> param : params) { | 
 |  |  |             result.add(param); | 
 |  |  |             if (Objects.isNull(param.get("fieldsIndex"))) { | 
 |  |  |                 continue; | 
 |  |  |             } | 
 |  |  |             List<FieldsItem> itemList = fieldsItemService | 
 |  |  |                     .list(new LambdaQueryWrapper<FieldsItem>() | 
 |  |  |                             .eq(FieldsItem::getUuid, param.get("fieldsIndex"))); | 
 |  |  |             if (itemList.isEmpty()) { | 
 |  |  |                 continue; | 
 |  |  |             } | 
 |  |  |             fields.forEach(fds -> { | 
 |  |  |                 for (FieldsItem fieldsItem : itemList) { | 
 |  |  |                     if (!Objects.isNull(fieldsItem.getFieldsId()) && fieldsItem.getFieldsId().equals(fds.getId())) { | 
 |  |  |                         param.put(fds.getFields(), fieldsItem.getValue()); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             }); | 
 |  |  |         } | 
 |  |  |         return result; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * @param template | 
 |  |  |      * @return | 
 |  |  | 
 |  |  |      * @description 动态字段value保存 | 
 |  |  |      * @time 2025/3/18 15:00 | 
 |  |  |      */ | 
 |  |  |     public static void saveFields(Map<String, String> template, String uuid) { | 
 |  |  |     @Transactional(rollbackFor = Exception.class) | 
 |  |  |     public static boolean saveFields(Map<String, ?> template, String uuid) { | 
 |  |  |         List<Fields> fields = getFieldsSta(); | 
 |  |  |         FieldsItemService fieldsItemService = SpringUtils.getBean(FieldsItemService.class); | 
 |  |  |         if (fields.isEmpty()) { | 
 |  |  |             throw new CoolException("扩展字段不存在!!"); | 
 |  |  |         } | 
 |  |  |         List<FieldsItem> fieldsItems = new ArrayList<>(); | 
 |  |  |         fields.forEach(fields1 -> { | 
 |  |  |             if (!Objects.isNull(template.get(fields1.getFields()))) { | 
 |  |  |                 FieldsItem item = new FieldsItem(); | 
 |  |  |                 item.setFieldsId(fields1.getId()) | 
 |  |  |                         .setUuid(uuid) | 
 |  |  |                         .setValue(template.get(fields1.getFields())); | 
 |  |  |                 fieldsItems.add(item); | 
 |  |  |         if (!fields.isEmpty()) { | 
 |  |  |             for (Fields obj : fields) { | 
 |  |  |                 if (!Objects.isNull(template.get(obj.getFields())) && StringUtils.isNotBlank(template.get(obj.getFields()).toString())) { | 
 |  |  |                     FieldsItem item = new FieldsItem(); | 
 |  |  |                     item.setUuid(uuid) | 
 |  |  |                             .setValue(template.get(obj.getFields()).toString()) | 
 |  |  |                             .setMatnrId(!Objects.isNull(template.get("matnrId")) ? Long.parseLong(template.get("matnrId").toString()) : null) | 
 |  |  |                             .setFieldsId(obj.getId()); | 
 |  |  |                     fieldsItems.add(item); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         }); | 
 |  |  |        if (!fieldsItemService.saveBatch(fieldsItems)) { | 
 |  |  |            throw new CoolException("动态字段值保存失败!!"); | 
 |  |  |        } | 
 |  |  |             if (fieldsItems.isEmpty()) { | 
 |  |  |                 return false; | 
 |  |  |             } | 
 |  |  |             if (!fieldsItemService.saveBatch(fieldsItems)) { | 
 |  |  |                 throw new CoolException("扩展字段保存失败!!"); | 
 |  |  |             } | 
 |  |  |             return true; | 
 |  |  |         } | 
 |  |  |         return false; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 获取所有开启动态扩展字段 | 
 |  |  |      * | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public static List<Fields> getFieldsSta() { | 
 |  |  |         FieldsService fieldsService = SpringUtils.getBean(FieldsService.class); | 
 |  |  |         return fieldsService.list(new LambdaQueryWrapper<Fields>().eq(Fields::getStatus, 1).eq(Fields::getFlagEnable, 1)); | 
 |  |  |         return fieldsService.list(new LambdaQueryWrapper<Fields>().eq(Fields::getStatus, CommonStatus.COMMONSTATUS_YES.val) | 
 |  |  |                 .eq(Fields::getUnique, CommonStatus.COMMONSTATUS_YES.val) | 
 |  |  |                 .eq(Fields::getFlagEnable, CommonStatus.COMMONSTATUS_YES.val).orderByAsc(Fields::getId)); | 
 |  |  |     } | 
 |  |  | } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * @param | 
 |  |  |      * @return | 
 |  |  |      * @author Ryan | 
 |  |  |      * @description 动态字段修改 | 
 |  |  |      * @time 2025/4/7 15:28 | 
 |  |  |      */ | 
 |  |  |     @Synchronized | 
 |  |  |     @Transactional(rollbackFor = Exception.class) | 
 |  |  |     public static void updateFieldsValue(Map<String, Object> params) throws Exception { | 
 |  |  |         List<Fields> fields = getFieldsSta(); | 
 |  |  |         if (fields.isEmpty()) { | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  |         Object fieldsIndex = params.get("fieldsIndex"); | 
 |  |  |         if (!Objects.isNull(fieldsIndex) && StringUtils.isNotBlank(fieldsIndex.toString())) { | 
 |  |  |             String index = fieldsIndex.toString(); | 
 |  |  |             FieldsItemService fieldsItemService = SpringUtils.getBean(FieldsItemService.class); | 
 |  |  |             for (Fields field : fields) { | 
 |  |  |                 if (!Objects.isNull(params.get(field.getFields()))) { | 
 |  |  |                     FieldsItem indexItem = fieldsItemService.getOne(new LambdaQueryWrapper<FieldsItem>() | 
 |  |  |                             .eq(FieldsItem::getUuid, index) | 
 |  |  |                             .eq(FieldsItem::getFieldsId, field.getId())); | 
 |  |  |                     //如果子表为空,执行插入操作,否则就执行修改操作 | 
 |  |  |                     if (Objects.isNull(indexItem)) { | 
 |  |  |                         FieldsItem item = new FieldsItem(); | 
 |  |  |                         item.setUuid(index) | 
 |  |  |                                 .setFieldsId(field.getId()) | 
 |  |  |                                 .setMatnrId(!Objects.isNull(params.get("matnrId")) ? Long.parseLong(params.get("matnrId").toString()) : null) | 
 |  |  |                                 .setValue(params.get(field.getFields()).toString()); | 
 |  |  |                         if (!fieldsItemService.save(item)) { | 
 |  |  |                             throw new CoolException("扩展字段修改失败!!"); | 
 |  |  |                         } | 
 |  |  |                     } else { | 
 |  |  |                         indexItem.setValue(params.get(field.getFields()).toString()); | 
 |  |  |                         if (!fieldsItemService.updateById(indexItem)) { | 
 |  |  |                             throw new CoolException("扩展字段修改失败!!"); | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } else { | 
 |  |  |             saveFields(params, params.get("index").toString()); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static <T> QueryWrapper<T> setFieldsFilters(QueryWrapper<T> queryWrapper, PageParam<T, BaseParam> pageParam, Class<T> entityClass) { | 
 |  |  |         // 动态获取表名 | 
 |  |  |         TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass); | 
 |  |  |         String tableName = tableInfo.getTableName(); | 
 |  |  |  | 
 |  |  |         FieldsService fieldsService = SpringUtils.getBean(FieldsService.class); | 
 |  |  |         Map<String, Object> map = pageParam.getWhere().getFields(); | 
 |  |  |  | 
 |  |  |         for (String key : map.keySet()) { | 
 |  |  |             Object val = map.get(key); | 
 |  |  |             Fields fields = fieldsService.getOne(new LambdaQueryWrapper<Fields>().eq(Fields::getFields, key)); | 
 |  |  |             if (!Cools.isEmpty(fields)) { | 
 |  |  |                 String applySql = String.format( | 
 |  |  |                         "EXISTS (SELECT 1 FROM sys_fields_item fie " + | 
 |  |  |                                 "WHERE %s.fields_index IS NOT NULL " + | 
 |  |  |                                 "AND fie.uuid = %s.fields_index " + | 
 |  |  |                                 "AND fie.fields_id = '%s' " + | 
 |  |  |                                 "AND fie.value = '%s')", | 
 |  |  |                         tableName, tableName, fields.getId(), val | 
 |  |  |                 ); | 
 |  |  |                 queryWrapper.apply(applySql); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return queryWrapper; | 
 |  |  |     } | 
 |  |  | } |