提交 86d8798a 编写于 作者: tony001's avatar tony001

调整代码表服务

上级 e47288d3
<#ibiztemplate> <#ibiztemplate>
TARGET=PSAPPCODELIST TARGET=PSAPPCODELIST
</#ibiztemplate> </#ibiztemplate>
import { CodeListBase } from '@/ibiz-core/code-list/code-list-base'; <#if item.getCodeListType() == "DYNAMIC">
import { HttpResponse } from '@/ibiz-core/utils'; <#if item.getPSAppDataEntity?? && item.getPSAppDataEntity()??>
import ${srfclassname('${item.getPSAppDataEntity().codeName}')}Service from '@service/${srffilepath2(item.getPSAppDataEntity().codeName)}/${srffilepath2(item.getPSAppDataEntity().codeName)}-service';
</#if>
/** /**
* 代码表--${item.getName()} * 代码表--${item.getName()}
* *
* @export * @export
* @class ${srfclassname('${item.getCodeName()}')} * @class ${srfclassname('${item.getCodeName()}')}
* @extends {CodeListBase}
*/ */
export class ${srfclassname('${item.getCodeName()}')} extends CodeListBase { export default class ${srfclassname('${item.getCodeName()}')} {
<#if item.getPSAppDataEntity?? && item.getPSAppDataEntity()?? && item.getPSAppDEDataSet?? && item.getPSAppDEDataSet()??>
/** /**
* 代码表项主键标识 * 是否启用缓存
* *
* @protected * @type boolean
* @type {string} * @memberof ${srfclassname('${item.getCodeName()}')}
* @memberof CodeListBase
*/ */
protected idKey: string = '<#if item.getValuePSAppDEField()??>${item.getValuePSAppDEField().getCodeName()?lower_case}<#else>${item.getPSAppDataEntity().getKeyPSAppDEField().codeName?lower_case}</#if>'; public isEnableCache:boolean = ${item.isEnableCache()?c};
/** /**
* 代码表项值项标识 * 过期时间
* *
* @protected * @type any
* @type {string} * @memberof ${srfclassname('${item.getCodeName()}')}
* @memberof CodeListBase
*/ */
protected valueKey: string = '<#if item.getValuePSAppDEField()??>${item.getValuePSAppDEField().getCodeName()?lower_case}<#else>${item.getPSAppDataEntity().getKeyPSAppDEField().codeName?lower_case}</#if>'; public expirationTime:any;
/** /**
* 代码表项文本标识 * 缓存超长时长
* *
* @protected * @type any
* @type {string} * @memberof ${srfclassname('${item.getCodeName()}')}
* @memberof CodeListBase
*/ */
protected textKey: string = '<#if item.getTextPSAppDEField()??>${item.getTextPSAppDEField().getCodeName()?lower_case}<#else>${item.getPSAppDataEntity().getMajorPSAppDEField().codeName?lower_case}</#if>'; public cacheTimeout:any = ${item.getCacheTimeout()};
</#if> /**
* 代码表模型对象
*
* @type any
* @memberof ${srfclassname('${item.getCodeName()}')}
*/
public codelistModel:any = {
codelistid:"${item.getCodeName()}"
};
/** /**
* 自定义参数集合 * 自定义参数集合
...@@ -50,56 +54,196 @@ export class ${srfclassname('${item.getCodeName()}')} extends CodeListBase { ...@@ -50,56 +54,196 @@ export class ${srfclassname('${item.getCodeName()}')} extends CodeListBase {
* @memberof ${srfclassname('${item.getCodeName()}')} * @memberof ${srfclassname('${item.getCodeName()}')}
*/ */
public userParamNames:any ={ public userParamNames:any ={
<#if item.getUserParamNames?? && item.getUserParamNames()??> <#if item.getUserParamNames()??>
<#list item.getUserParamNames() as paramName> <#list item.getUserParamNames() as paramName>
${paramName}:"${item.getUserParam(paramName)}"<#if paramName_has_next>,</#if> ${paramName}:"${item.getUserParam(paramName)}"<#if paramName_has_next>,</#if>
</#list> </#list>
</#if> </#if>
} }
/**
* 查询参数集合
*
* @type any
* @memberof ${srfclassname('${item.getCodeName()}')}
*/
public queryParamNames:any ={
<#if item.getMinorSortDir()?? && item.getMinorSortPSAppDEField()??>
sort: '${item.getMinorSortPSAppDEField().getCodeName()?lower_case},${item.getMinorSortDir()?lower_case}'
</#if>
}
<#if item.getPSAppDataEntity?? && item.getPSAppDataEntity()??>
/**
* ${item.getPSAppDataEntity().getLogicName()}应用实体服务对象
*
* @type {${srfclassname('${item.getPSAppDataEntity().codeName}')}Service}
* @memberof ${srfclassname('${item.getCodeName()}')}
*/
public ${item.getPSAppDataEntity().codeName?lower_case}Service: ${srfclassname('${item.getPSAppDataEntity().codeName}')}Service = new ${srfclassname('${item.getPSAppDataEntity().codeName}')}Service();
</#if>
<#-- 不为子系统代码表 --> <#-- 不为子系统代码表 -->
<#if item.isSubSysCodeList() == false> <#if item.isSubSysCodeList() == false>
<#if item.getPSAppDataEntity?? && item.getPSAppDataEntity()?? && item.getPSAppDEDataSet?? && item.getPSAppDEDataSet()??> <#if item.getPSAppDataEntity?? && item.getPSAppDataEntity()?? && item.getPSAppDEDataSet?? && item.getPSAppDEDataSet()??>
/**
* 处理数据
*
* @public
* @param {any[]} items
* @returns {any[]}
* @memberof ${srfclassname('${item.getCodeName()}')}
*/
public doItems(items: any[]): any[] {
let _items: any[] = [];
items.forEach((item: any) => {
let itemdata:any = {};
Object.assign(itemdata,{id:item.<#if item.getValuePSAppDEField()??>${item.getValuePSAppDEField().getCodeName()?lower_case}<#else>${item.getPSAppDataEntity().getKeyPSAppDEField().codeName?lower_case}</#if>});
Object.assign(itemdata,{value:item.<#if item.getValuePSAppDEField()??>${item.getValuePSAppDEField().getCodeName()?lower_case}<#else>${item.getPSAppDataEntity().getKeyPSAppDEField().codeName?lower_case}</#if>});
Object.assign(itemdata,{text:item.<#if item.getTextPSAppDEField()??>${item.getTextPSAppDEField().getCodeName()?lower_case}<#else>${item.getPSAppDataEntity().getMajorPSAppDEField().codeName?lower_case}</#if>});
<#if item.getPValuePSAppDEField()??>Object.assign(itemdata,{pvalue:item.${item.getPValuePSAppDEField().getCodeName()?lower_case}});</#if>
_items.push(itemdata);
});
return _items;
}
/** /**
* 获取数据项 * 获取数据项
* *
* @param {*} [context={}] 上下文 * @param {*} context
* @param {*} [data={}] 参数 * @param {*} data
* @param {boolean} [isloading]
* @returns {Promise<any>} * @returns {Promise<any>}
* @memberof ${srfclassname('${item.getCodeName()}')} * @memberof ${srfclassname('${item.getCodeName()}')}
*/ */
public async getItems(context: any = {}, data: any = {}): Promise<any> { public getItems(context: any={}, data: any={}, isloading?: boolean): Promise<any> {
const service: any = await this.getService('${item.getPSAppDataEntity().codeName?lower_case}'); return new Promise((resolve, reject) => {
const response: any = await service.${item.getPSAppDEDataSet().codeName}(context, Object.assign(this.userParamNames,data,{page: 0,size:"1000"})); data = this.handleQueryParam(data);
if (response && response.status === 200 && response.data) { const promise: Promise<any> = this.${item.getPSAppDataEntity().codeName?lower_case}Service.${item.getPSAppDEDataSet().codeName}(context, data, isloading);
response.data.sort((value: any,next: any)=>{ return value.ordervalue - next.ordervalue}); promise.then((response: any) => {
return new HttpResponse(200, this.doItems(response.data)); if (response && response.status === 200) {
const data = response.data;
resolve(this.doItems(data));
} else {
resolve([]);
} }
return new HttpResponse(200, []); }).catch((response: any) => {
console.error(response);
reject(response);
});
});
}
<#else>
/**
* 获取数据项
*
* @param {*} data
* @param {boolean} [isloading]
* @returns {Promise<any>}
* @memberof ${srfclassname('${item.getCodeName()}')}
*/
public getItems(data: any={}, isloading?: boolean): Promise<any> {
return Promise.reject([]);
} }
</#if> </#if>
<#else> <#else>
<#if item.getPSAppDataEntity?? && item.getPSAppDataEntity()?? && item.getPSAppDEDataSet?? && item.getPSAppDEDataSet()??> <#if item.getPSAppDataEntity?? && item.getPSAppDataEntity()?? && item.getPSAppDEDataSet?? && item.getPSAppDEDataSet()??>
<#if item.getPSAppDataEntity().codeName == "CodeItem">
/** /**
* 获取数据项 * 获取数据项
* *
* @param {*} [context={}] 上下文 * @param {string} context
* @param {*} [data={}] 参数 * @param {*} data
* @param {boolean} [isloading]
* @returns {Promise<any>} * @returns {Promise<any>}
* @memberof ${srfclassname('${item.getCodeName()}')} * @memberof ${srfclassname('${item.getCodeName()}')}
*/ */
public async getItems(context: any = {}, data: any = {}): Promise<any> { public getItems(context:any = {}, data: any={}, isloading?: boolean): Promise<any> {
const service: any = await this.getService('${item.getPSAppDataEntity().codeName?lower_case}'); return new Promise((resolve, reject) => {
const response: any = await service.${item.getPSAppDEDataSet().codeName}(context, { ...data, codelistid: '${item.id}' ,{page: 0,size:"1000"}}); data = this.handleQueryParam(data);
if (response && response.status === 200 && response.data) { const promise: Promise<any> = this.${item.getPSAppDataEntity().codeName?lower_case}Service.${item.getPSAppDEDataSet().codeName}(context, data, isloading);
response.data.sort((value: any,next: any)=>{ return value.ordervalue - next.ordervalue}); promise.then((response: any) => {
return new HttpResponse(200, this.doItems(response.data)); if (response && response.status === 200) {
const data = response.data;
resolve(this.doItems(data));
} else {
resolve([]);
} }
return new HttpResponse(200, []); }).catch((response: any) => {
console.error(response);
reject(response);
});
});
}
/**
* 处理数据
*
* @public
* @param {any[]} items
* @returns {any[]}
* @memberof ${srfclassname('${item.getCodeName()}')}
*/
public doItems(items: any[]): any[] {
let _items: any[] = [];
items.forEach((item: any) => {
let itemdata:any = {};
Object.assign(itemdata,{id:item.<#if item.getValuePSAppDEField()??>${item.getValuePSAppDEField().getCodeName()?lower_case}<#else>${item.getPSAppDataEntity().getKeyPSAppDEField().codeName?lower_case}</#if>});
Object.assign(itemdata,{value:item.<#if item.getValuePSAppDEField()??>${item.getValuePSAppDEField().getCodeName()?lower_case}<#else>${item.getPSAppDataEntity().getKeyPSAppDEField().codeName?lower_case}</#if>});
Object.assign(itemdata,{text:item.<#if item.getTextPSAppDEField()??>${item.getTextPSAppDEField().getCodeName()?lower_case}<#else>${item.getPSAppDataEntity().getMajorPSAppDEField().codeName?lower_case}</#if>});
<#if item.getPValuePSAppDEField()??>Object.assign(itemdata,{pvalue:item.${item.getPValuePSAppDEField().getCodeName()?lower_case}});</#if>
_items.push(itemdata);
});
return _items;
}
<#else>
/**
* 获取数据项
*
* @param {string} context
* @param {*} data
* @param {boolean} [isloading]
* @returns {Promise<any>}
* @memberof ${srfclassname('${item.getCodeName()}')}
*/
public getItems(context:any = {}, data: any={}, isloading?: boolean): Promise<any> {
return Promise.reject([]);
} }
</#if> </#if>
</#if> </#if>
</#if>
/**
* 处理查询参数
* @param data 传入data
* @memberof ${srfclassname('${item.getCodeName()}')}
*/
public handleQueryParam(data:any){
let tempData:any = data?JSON.parse(JSON.stringify(data)):{};
if(this.userParamNames && Object.keys(this.userParamNames).length >0){
Object.keys(this.userParamNames).forEach((name: string) => {
if (!name) {
return;
}
let value: string | null = this.userParamNames[name];
if (value && value.startsWith('%') && value.endsWith('%')) {
const key = value.substring(1, value.length - 1);
if (this.codelistModel && this.codelistModel.hasOwnProperty(key)) {
value = (this.codelistModel[key] !== null && this.codelistModel[key] !== undefined) ? this.codelistModel[key] : null;
} else {
value = null;
}
}
Object.assign(tempData, { [name]: value });
});
}
Object.assign(tempData,{page: 0, size: 1000});
if(this.queryParamNames && Object.keys(this.queryParamNames).length > 0){
Object.assign(tempData,this.queryParamNames);
}
return tempData;
}
} }
</#if>
\ No newline at end of file
<#ibiztemplate>
TARGET=PSSYSAPP
</#ibiztemplate>
<#if app.getAllPSAppCodeLists()??>
<#list app.getAllPSAppCodeLists() as codelist>
<#if codelist.getCodeListType() == "DYNAMIC">
import ${srfclassname('${codelist.codeName}')} from '@/codelist/${srffilepath2(codelist.codeName)}';
</#if>
</#list>
</#if>
import { Store } from 'vuex';
/**
* 动态代码表服务类
*
* @export
* @class CodeListService
*/
export default class CodeListService {
/**
* Vue 状态管理器
*
* @private
* @type {(any | null)}
* @memberof CodeListService
*/
private $store: Store<any> | null = null;
constructor(opts: any = {}) {
this.$store = opts.$store;
}
/**
* 获取状态管理器
*
* @returns {(any | null)}
* @memberof CodeListService
*/
public getStore(): Store<any> | null {
return this.$store;
}
/**
* 动态代码表缓存(加载中)
*
* @type {Map<string,any>}
* @memberof CodeListService
*/
public static codelistCache:Map<string,any> = new Map();
/**
* 动态代码表缓存(已完成)
*
* @type {Map<string,any>}
* @memberof CodeListService
*/
public static codelistCached:Map<string,any> = new Map();
<#if app.getAllPSAppCodeLists()??>
<#list app.getAllPSAppCodeLists() as codelist>
<#if codelist.getCodeListType() == "DYNAMIC">
/**
* 代码表--${codelist.getName()}
*
* @type {${srfclassname('${codelist.codeName}')}}
* @memberof CodeListService
*/
public ${codelist.codeName}: ${srfclassname('${codelist.codeName}')} = new ${srfclassname('${codelist.codeName}')}();
</#if>
</#list>
</#if>
/**
* 获取动态代码表
*
* @param {string} tag 代码表标识
* @param {string} context
* @returns {Promise<any[]>}
* @memberof CodeListService
*/
<#noparse>public getItems(tag: string,context:any = {}, data?: any, isloading?: boolean,): Promise<any[]> {
let _this: any = this;
if(context && context.srfsessionid){
delete context.srfsessionid;
}
let isEnableCache:boolean = _this[tag].isEnableCache;
let cacheTimeout:any = _this[tag].cacheTimeout;
return new Promise((resolve:any,reject:any) =>{
// 启用缓存
if(isEnableCache){
// 加载完成,从本地缓存获取
if(CodeListService.codelistCached.get(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`)){
let items:any = CodeListService.codelistCached.get(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`);
if(items.length >0){
if(cacheTimeout !== -1){
if(new Date().getTime() > _this[tag].expirationTime){
_this[tag].getItems(context,data,isloading).then((result:any) =>{
CodeListService.codelistCached.set(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`,{items:result});
_this[tag].expirationTime = new Date().getTime() + cacheTimeout;
resolve(result);
}).catch((error:any) =>{
Promise.reject([]);
})
}else{
return resolve(items);
}
}else{
return resolve(items);
}
}
}
if (_this[tag]) {
const callback:Function = (context:any ={},data:any ={},tag:string,promise:Promise<any>) =>{
promise.then((result:any) =>{
console.log()
if(result.length > 0){
CodeListService.codelistCached.set(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`,{items:result});
return resolve(result);
}else{
return resolve([]);
}
}).catch((result:any) =>{
return reject(result);
})
}
// 加载中,UI又需要数据,解决连续加载同一代码表问题
if(CodeListService.codelistCache.get(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`)){
callback(context,data,tag,CodeListService.codelistCache.get(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`));
}else{
let result:Promise<any> = _this[tag].getItems(context,data,isloading);
CodeListService.codelistCache.set(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`,result);
if(cacheTimeout !== -1){
_this[tag].expirationTime = new Date().getTime() + cacheTimeout;
}
callback(context,data,tag,result);
}
}
}else{
if (_this[tag]) {
_this[tag].getItems(context,data,isloading).then((result:any) =>{
resolve(result);
}).catch((error:any) =>{
Promise.reject([]);
})
}else{
return Promise.reject([]);
}
}
})
}</#noparse>
}
\ No newline at end of file
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册