22
33import cn .cordys .common .constants .BusinessModuleField ;
44import cn .cordys .common .constants .FormKey ;
5+ import cn .cordys .common .domain .BaseModuleFieldValue ;
56import cn .cordys .common .dto .BaseTreeNode ;
7+ import cn .cordys .common .dto .OptionDTO ;
68import cn .cordys .common .exception .GenericException ;
79import cn .cordys .common .mapper .CommonMapper ;
10+ import cn .cordys .common .util .CommonBeanFactory ;
811import cn .cordys .common .util .JSON ;
912import cn .cordys .common .util .Translator ;
13+ import cn .cordys .crm .product .service .ProductPriceService ;
14+ import cn .cordys .crm .product .service .ProductService ;
15+ import cn .cordys .crm .system .constants .FieldSourceType ;
1016import cn .cordys .crm .system .constants .FieldType ;
1117import cn .cordys .crm .system .domain .ModuleField ;
1218import cn .cordys .crm .system .domain .ModuleFieldBlob ;
19+ import cn .cordys .crm .system .dto .DatasourceRefDTO ;
1320import cn .cordys .crm .system .dto .field .DatasourceField ;
1421import cn .cordys .crm .system .dto .field .DateTimeField ;
22+ import cn .cordys .crm .system .dto .request .DatasourceRefQueryRequest ;
1523import cn .cordys .crm .system .dto .request .FieldRepeatCheckRequest ;
1624import cn .cordys .crm .system .dto .response .FieldRepeatCheckResponse ;
1725import cn .cordys .mybatis .BaseMapper ;
1826import cn .cordys .mybatis .lambda .LambdaQueryWrapper ;
1927import jakarta .annotation .Resource ;
28+ import lombok .extern .slf4j .Slf4j ;
2029import org .apache .commons .collections4 .CollectionUtils ;
2130import org .apache .commons .lang3 .StringUtils ;
2231import org .apache .commons .lang3 .Strings ;
2332import org .springframework .stereotype .Service ;
2433
34+ import java .lang .reflect .Field ;
35+ import java .lang .reflect .Method ;
36+ import java .util .ArrayList ;
2537import java .util .HashMap ;
2638import java .util .List ;
2739import java .util .Map ;
40+ import java .util .stream .Stream ;
2841
2942/**
3043 * @author song-cc-rock
3144 */
3245@ Service
46+ @ Slf4j
3347public class ModuleFieldService {
3448
3549 /**
3650 * 表单表格映射
3751 */
3852 private static final Map <String , String > FORM_TABLE = new HashMap <>(8 );
3953
54+ private static final Map <String , Class <?>> SOURCE_REF_CLASS = new HashMap <>();
55+
4056 static {
4157 FORM_TABLE .put (FormKey .CLUE .getKey (), "clue" );
4258 FORM_TABLE .put (FormKey .CUSTOMER .getKey (), "customer" );
@@ -51,6 +67,9 @@ public class ModuleFieldService {
5167 FORM_TABLE .put (FormKey .CONTRACT_PAYMENT_PLAN .getKey (), "contract_payment_plan" );
5268 FORM_TABLE .put (FormKey .CONTRACT_PAYMENT_RECORD .getKey (), "contract_payment_record" );
5369 FORM_TABLE .put (FormKey .INVOICE .getKey (), "contract_invoice" );
70+
71+ SOURCE_REF_CLASS .put (FieldSourceType .PRODUCT .name (), ProductService .class );
72+ SOURCE_REF_CLASS .put (FieldSourceType .PRICE .name (), ProductPriceService .class );
5473 }
5574
5675 @ Resource
@@ -125,6 +144,39 @@ public FieldRepeatCheckResponse checkRepeat(FieldRepeatCheckRequest request, Str
125144 return FieldRepeatCheckResponse .builder ().name (repeatName ).repeat (StringUtils .isNotBlank (repeatName )).build ();
126145 }
127146
147+ @ SuppressWarnings ("unchecked" )
148+ public List <DatasourceRefDTO > getSourceRefDetail (DatasourceRefQueryRequest request ) {
149+ List <String > sourceIds = request .getSourceIds ().stream ().distinct ().toList ();
150+ Class <?> sourceClass = SOURCE_REF_CLASS .get (request .getDataSourceType ());
151+ if (sourceClass == null ) {
152+ return new ArrayList <>();
153+ }
154+
155+ Object service = CommonBeanFactory .getBean (sourceClass );
156+ List <DatasourceRefDTO > result = new ArrayList <>();
157+ try {
158+ Method executeMethod = sourceClass .getMethod ("get" , String .class );
159+ for (String id : sourceIds ) {
160+ Object res = executeMethod .invoke (service , id );
161+ if (res == null ) {
162+ continue ;
163+ }
164+ DatasourceRefDTO dto = new DatasourceRefDTO ();
165+ dto .setId (id );
166+ dto .setModuleFields ((List <BaseModuleFieldValue >) getField (res , "moduleFields" , List .class ));
167+ dto .setProducts ((List <Map <String , Object >>) getField (res , "products" , List .class ));
168+ dto .setOptionMap ((Map <String , List <OptionDTO >>) getField (res , "optionMap" , Map .class ));
169+
170+ result .add (dto );
171+ }
172+ } catch (Exception e ) {
173+ log .error ("获取数据源引用详细失败, {}" , e .getMessage ());
174+ return new ArrayList <>();
175+ }
176+
177+ return result ;
178+ }
179+
128180 public void modifyInvoiceShowFields () {
129181 LambdaQueryWrapper <ModuleField > queryWrapper = new LambdaQueryWrapper <>();
130182 queryWrapper .eq (ModuleField ::getInternalKey , BusinessModuleField .INVOICE_CONTRACT_ID .getKey ());
@@ -165,4 +217,29 @@ public ModuleField selectFieldsByInternalKey(String internalKey) {
165217 field .setInternalKey (internalKey );
166218 return moduleFieldMapper .selectOne (field );
167219 }
220+
221+ @ SuppressWarnings ("unchecked" )
222+ private <T > T getField (Object obj , String fieldName , Class <T > type ) {
223+ Class <?> clazz = obj .getClass ();
224+
225+ while (clazz != null ) {
226+ try {
227+ Field field = clazz .getDeclaredField (fieldName );
228+ field .setAccessible (true );
229+
230+ Object value = field .get (obj );
231+ if (type .isInstance (value )) {
232+ return (T ) value ;
233+ }
234+
235+ return null ;
236+ } catch (NoSuchFieldException e ) {
237+ clazz = clazz .getSuperclass ();
238+ } catch (IllegalAccessException e ) {
239+ return null ;
240+ }
241+ }
242+
243+ return null ;
244+ }
168245}
0 commit comments