yangys
2024-04-04 ed4a5236bab800094be4a8378f5098eebe3de6ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package com.qianwen.smartman.modules.system.service.impl;
 
import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.ImmutableMap;
import com.qianwen.core.cache.utils.CacheUtil;
import com.qianwen.core.coderule.annotation.CodeRule;
import com.qianwen.core.log.exception.ServiceException;
import com.qianwen.core.mp.base.BaseServiceImpl;
import com.qianwen.core.tool.utils.BeanUtil;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.smartman.common.cache.DictCache;
import com.qianwen.smartman.common.constant.CommonConstant;
import com.qianwen.smartman.common.constant.ExtCacheConstant;
import com.qianwen.smartman.common.utils.Lambda;
import com.qianwen.smartman.common.utils.MessageUtils;
import com.qianwen.smartman.modules.cps.vo.ConfigVO;
import com.qianwen.smartman.modules.system.convert.BasCoderuleConvert;
import com.qianwen.smartman.modules.system.dto.BasCoderuleDTO;
import com.qianwen.smartman.modules.system.entity.BasCodeRule;
import com.qianwen.smartman.modules.system.entity.BasCodeRuleEntry;
import com.qianwen.smartman.modules.system.entity.MetaObjectType;
import com.qianwen.smartman.modules.system.entity.MetaObjectTypeField;
import com.qianwen.smartman.modules.system.mapper.BasCoderuleMapper;
import com.qianwen.smartman.modules.system.mapper.CodeHistoryMapper;
import com.qianwen.smartman.modules.system.service.IBasCoderuleEntryService;
import com.qianwen.smartman.modules.system.service.IBasCoderuleService;
import com.qianwen.smartman.modules.system.service.IMetaObjectTypeFieldService;
import com.qianwen.smartman.modules.system.service.IMetaObjectTypeService;
 
import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
 
@Service
/* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/modules/system/service/impl/BasCoderuleServiceImpl.class */
public class BasCoderuleServiceImpl extends BaseServiceImpl<BasCoderuleMapper, BasCodeRule> implements IBasCoderuleService {
    private static final Logger log = LoggerFactory.getLogger(BasCoderuleServiceImpl.class);
    private final IBasCoderuleEntryService basCoderuleEntryService;
    private final IMetaObjectTypeService metaObjectTypeService;
    private final IMetaObjectTypeFieldService metaObjectTypeFieldService;
    private final CodeHistoryMapper codeHistoryMapper;
    private final ConfigVO configVO;
 
 
    public BasCoderuleServiceImpl(final IBasCoderuleEntryService basCoderuleEntryService, final IMetaObjectTypeService metaObjectTypeService, final IMetaObjectTypeFieldService metaObjectTypeFieldService, final CodeHistoryMapper codeHistoryMapper, final ConfigVO configVO) {
        this.basCoderuleEntryService = basCoderuleEntryService;
        this.metaObjectTypeService = metaObjectTypeService;
        this.metaObjectTypeFieldService = metaObjectTypeFieldService;
        this.codeHistoryMapper = codeHistoryMapper;
        this.configVO = configVO;
    }
 
    @Override // org.springblade.modules.system.service.IBasCoderuleService
    public BasCoderuleDTO getCoderuleDetails(Long coderuleId) {
        BasCoderuleDTO result = BasCoderuleConvert.INSTANCE.convertToDTO((BasCodeRule) getById(coderuleId));
        if (Func.isNotEmpty(result)) {
            result.setEntryList(this.basCoderuleEntryService.list(Wrappers.<BasCodeRuleEntry>lambdaQuery().eq(BasCodeRuleEntry::getRuleId, coderuleId))); 
            /*
            result.setEntryList(this.basCoderuleEntryService.list((Wrapper) Wrappers.lambdaQuery().eq((v0) -> {
                return v0.getRuleId();
            }, coderuleId)));*/
        }
        return result;
    }
 
    @Override // org.springblade.modules.system.service.IBasCoderuleService
    @Transactional(rollbackFor = {Exception.class})
    public BasCodeRule submitCoderule(BasCoderuleDTO basCoderuleDTO) {
        CacheUtil.clear(ExtCacheConstant.CODE_RULE_CACHE, Boolean.FALSE);
        if (Func.isNotEmpty(basCoderuleDTO)) {
            BasCodeRule basCoderule = (BasCodeRule) BeanUtil.copy(basCoderuleDTO, BasCodeRule.class);
            if (basCoderuleDTO.getSystemDefault().booleanValue() && basCoderule.getId() != null) {
                int existCount = getDefaultCount(basCoderule);
                if (existCount != 0) {
                    throw new ServiceException("已经存在一个默认的规则,请先取消该规则");
                }
            }
            saveOrUpdate(basCoderule);
            List<BasCodeRuleEntry> entryList = basCoderuleDTO.getEntryList();
            this.basCoderuleEntryService.remove(Wrappers.<BasCodeRuleEntry>lambdaQuery().eq(BasCodeRuleEntry::getRuleId, basCoderule.getId()));
            /*
            this.basCoderuleEntryService.remove((Wrapper) Wrappers.lambdaQuery().eq((v0) -> {
                return v0.getRuleId();
            }, basCoderule.getId()));*/
            if (Func.isNotEmpty(entryList)) {
                entryList.forEach(x -> {
                    x.setRuleId(basCoderule.getId());
                });
                this.basCoderuleEntryService.saveOrUpdateBatch(entryList);
            }
            return basCoderule;
        }
        return null;
    }
 
    @Override // org.springblade.modules.system.service.IBasCoderuleService
    @Transactional(rollbackFor = {Exception.class})
    public Boolean deleteCoderule(List<Long> ids) {
        deleteLogic(ids);
        this.basCoderuleEntryService.remove(Wrappers.<BasCodeRuleEntry>lambdaQuery().in(BasCodeRuleEntry::getRuleId, ids));
        /*
        this.basCoderuleEntryService.remove((Wrapper) Wrappers.lambdaQuery().in((v0) -> {
            return v0.getRuleId();
        }, ids));*/
        return true;
    }
 
    @Override // org.springblade.modules.system.service.IBasCoderuleService
    public boolean isExistCodeRule(String billFormId) {
        return (count(Lambda.eq(BasCodeRule::getBillFormId, billFormId).eq(BasCodeRule::getSystemDefault, Boolean.valueOf(true))) != 0L);
        /*
        return count((Wrapper) Lambda.eq((v0) -> {
            return v0.getBillFormId();
        }, billFormId).eq((v0) -> {
            return v0.getSystemDefault();
        }, true)) != 0;*/
    }
 
    public String getAnnotationFieldValue(Object data, String dbField) {
        String fieldName = StrUtil.toCamelCase(dbField);
        Object fieldValue = ReflectUtil.getFieldValue(data, fieldName);
        if (fieldValue == null) {
            Field[] fields = data.getClass().getDeclaredFields();
            int i = 0;
            while (true) {
                if (i >= fields.length) {
                    break;
                }
                String code = (String) AnnotationUtil.getAnnotationValue(fields[i], CodeRule.class, "code");
                if (!dbField.equals(code)) {
                    i++;
                } else {
                    fields[i].setAccessible(true);
                    try {
                        fieldValue = fields[i].get(data);
                    } catch (IllegalArgumentException | IllegalAccessException e) {
                        log.error("设置字段数据错误",e);
                    } 
                }
            }
        }
        if (fieldValue == null) {
            return null;
        }
        return fieldValue.toString();
    }
 
    @Override // org.springblade.modules.system.service.IBasCoderuleService
    public BasCodeRule getRuleByObjectType(Object data, String typeId) {
        BasCodeRule basCodeRule;
        MetaObjectType metaObjectType = (MetaObjectType) this.metaObjectTypeService.getOne(Lambda.eq((v0) -> {
            return v0.getFId();
        }, typeId));
        if (metaObjectType.getRuleField() != null) {
            String fieldValue = getAnnotationFieldValue(data, metaObjectType.getRuleField());
            if (Func.isEmpty(fieldValue)) {
                throw new ServiceException(String.format(MessageUtils.message("system.coderule.filed.empty.err", new Object[0]), metaObjectType.getName(), metaObjectType.getRuleField()));
            }
            basCodeRule = getDictRuleByObjectType(typeId, fieldValue);
            if (basCodeRule == null) {
                String dictValue = DictCache.getValue(metaObjectType.getRuleDictCode(), fieldValue);
                if (dictValue == null) {
                    throw new ServiceException(String.format(MessageUtils.message("system.coderule.dict.search.err", new Object[0]), metaObjectType.getRuleDictCode(), fieldValue.toString()));
                }
                throw new ServiceException(String.format(MessageUtils.message("system.coderule.get.rule.err2", new Object[0]), metaObjectType.getName(), dictValue));
            }
        } else {
            basCodeRule = getDefaultRuleByObjectType(typeId);
            if (basCodeRule == null) {
                throw new ServiceException(String.format(MessageUtils.message("system.coderule.get.rule.err1", new Object[0]), metaObjectType.getName()));
            }
        }
        return basCodeRule;
    }
 
    private BasCodeRule getDefaultRuleByObjectType(String objectTypeId) {
        BasCodeRule basCodeRule = getOne(
                Lambda.eq(BasCodeRule::getBillFormId, objectTypeId)
                .eq(BasCodeRule::getSystemDefault, Boolean.TRUE)
                .isNull(BasCodeRule::getRuleFieldDictKey), false);
        /*
        BasCodeRule basCodeRule = (BasCodeRule) getOne((Wrapper) ((ExtraLambdaQueryWrapper) Lambda.eq((v0) -> {
            return v0.getBillFormId();
        }, objectTypeId).eq((v0) -> {
            return v0.getSystemDefault();
        }, Boolean.TRUE)).isNull((v0) -> {
            return v0.getRuleFieldDictKey();
        }), false);*/
        return basCodeRule;
    }
 
    private BasCodeRule getDictRuleByObjectType(String objectTypeId, String dictKey) {
        if (dictKey == null) {
            return getDefaultRuleByObjectType(objectTypeId);
        }
        
        BasCodeRule basCodeRule = (BasCodeRule)getOne(
                Lambda.eq(BasCodeRule::getBillFormId, objectTypeId)
                .eq(BasCodeRule::getSystemDefault, Boolean.valueOf(true))
                .eq(BasCodeRule::getRuleFieldDictKey, dictKey));
        /*
        BasCodeRule basCodeRule = (BasCodeRule) getOne((Wrapper) ((ExtraLambdaQueryWrapper) Lambda.eq((v0) -> {
            return v0.getBillFormId();
        }, objectTypeId).eq((v0) -> {
            return v0.getSystemDefault();
        }, true)).eq((v0) -> {
            return v0.getRuleFieldDictKey();
        }, dictKey));*/
        if (basCodeRule == null) {
            return getDefaultRuleByObjectType(objectTypeId);
        }
        return basCodeRule;
    }
 
    private int getDefaultCount(BasCodeRule basCodeRule) {
        long existCount = count(
                Lambda.eq(BasCodeRule::getBillFormId, basCodeRule.getBillFormId())
                .eq(Func.isNotEmpty(basCodeRule.getRuleFieldDictKey()), BasCodeRule::getRuleFieldDictKey, basCodeRule.getRuleFieldDictKey())
                .isNull(Func.isNull(basCodeRule.getRuleFieldDictKey()), BasCodeRule::getRuleFieldDictKey)
                .ne(BasCodeRule::getId, basCodeRule.getId())
                .eq(BasCodeRule::getSystemDefault, true));
        /*
        long existCount = count((Wrapper) ((ExtraLambdaQueryWrapper) Lambda.eq((v0) -> {
            return v0.getBillFormId();
        }, basCodeRule.getBillFormId()).eq(Func.isNotEmpty(basCodeRule.getRuleFieldDictKey()), (v0) -> {
            return v0.getRuleFieldDictKey();
        }, basCodeRule.getRuleFieldDictKey()).isNull(Func.isNull(basCodeRule.getRuleFieldDictKey()), (v0) -> {
            return v0.getRuleFieldDictKey();
        }).ne((v0) -> {
            return v0.getId();
        }, basCodeRule.getId())).eq((v0) -> {
            return v0.getSystemDefault();
        }, true));*/
        return Func.toInt(Long.valueOf(existCount));
    }
 
    @Override // org.springblade.modules.system.service.IBasCoderuleService
    public boolean changeDefault(String id) {
        BasCodeRule basCodeRule = (BasCodeRule) getById(id);
        if (basCodeRule != null) {
            int existCount = getDefaultCount(basCodeRule);
            if (existCount != 0) {
                throw new ServiceException("已经存在一个默认的规则,请先取消该规则");
            }
            basCodeRule.setSystemDefault(Boolean.valueOf(!basCodeRule.getSystemDefault().booleanValue()));
            return updateById(basCodeRule);
        }
        return false;
    }
 
    @Override // org.springblade.modules.system.service.IBasCoderuleService
    public List<String> patchCodeList(final String billFormId, final Map<String, Object> data) {
        BasCodeRule rule;
        MetaObjectType metaObjectType = (MetaObjectType) this.metaObjectTypeService.getById(billFormId);
        if (metaObjectType == null) {
            throw new ServiceException(MessageUtils.message("system.metaobjet.not.found", new Object[0]) + billFormId);
        }
        if (Func.isNotEmpty(metaObjectType.getRuleField())) {
             MetaObjectTypeField field = this.metaObjectTypeFieldService.getOne(Lambda.eq(MetaObjectTypeField::getEnumType, metaObjectType.getRuleDictCode())
                      .eq(MetaObjectTypeField::getObjectId, billFormId).limit(1));
             /*
            MetaObjectTypeField field = (MetaObjectTypeField) this.metaObjectTypeFieldService.getOne(((ExtraLambdaQueryWrapper) Lambda.eq((v0) -> {
                return v0.getEnumType();
            }, metaObjectType.getRuleDictCode()).eq((v0) -> {
                return v0.getObjectId();
            }, billFormId)).limit(1));*/
            rule = (BasCodeRule) Optional.ofNullable(data.get(field.getField())).map(item -> {
                return (BasCodeRule) Optional.ofNullable(getDictRuleByObjectType(billFormId, item.toString())).orElseThrow(() -> {
                    return new IllegalArgumentException(JSONObject.toJSONString(ImmutableMap.of(CommonConstant.ALARM_MSG, String.format(MessageUtils.message("system.coderule.rule.not.found", new Object[0]), field.getFName(), data.get(field.getField())), "data", field.getField())));
                });
            }).orElseThrow(() -> {
                return new IllegalArgumentException(JSONObject.toJSONString(ImmutableMap.of(CommonConstant.ALARM_MSG, MessageUtils.message("system.coderuel.select.rule", new Object[0]), "data", field.getField())));
            });
        } else {
            rule = getDefaultRuleByObjectType(billFormId);
        }
        Date endTime = new Date();
        DateTime offset = DateUtil.offset(endTime, DateField.HOUR_OF_DAY, -720);
        if (((Boolean) Optional.ofNullable(rule).map((v0) -> {
            return v0.getRepairBillno();
        }).orElseThrow(() -> {
            return new ServiceException(MessageUtils.message("system.coderule.not.found", new Object[0]));
        })).booleanValue()) {
            return Optional.ofNullable(metaObjectType.getCode()).map(item2 -> {
                return this.codeHistoryMapper.getPatchCodeList(billFormId, metaObjectType.getTableName(), item2, offset, endTime);
            }).orElseThrow(() -> {
                return new ServiceException(MessageUtils.message("system.coderule.code.not.set", new Object[0]));
            });
        }
        throw new ServiceException(MessageUtils.message("system.coderule.patch.not.open", new Object[0]));
    }
}