提交 1a9663d3 编写于 作者: tony001's avatar tony001

zpc --- 基类增加代码表转换

上级 d4939698
import { Store } from 'vuex'; import { Store } from 'vuex';
import { Util } from '@/utils/util/util'; import { Util } from '@/utils/util/util';
import CodeListService from "@service/app/codelist-service";
/** /**
* 部件服务基类 * 部件服务基类
...@@ -26,6 +27,14 @@ export default class ControlService { ...@@ -26,6 +27,14 @@ export default class ControlService {
*/ */
public model: any | null = null; public model: any | null = null;
/**
* 代码表服务对象
*
* @type {any}
* @memberof ControlService
*/
public codeListService:any;
/** /**
* 是否为从数据模式 * 是否为从数据模式
* *
...@@ -43,6 +52,7 @@ export default class ControlService { ...@@ -43,6 +52,7 @@ export default class ControlService {
constructor(opts: any = {}) { constructor(opts: any = {}) {
this.$store = opts.$store; this.$store = opts.$store;
this.setTempMode(); this.setTempMode();
this.codeListService = new CodeListService({ $store:opts.$store });
} }
/** /**
...@@ -118,17 +128,19 @@ export default class ControlService { ...@@ -118,17 +128,19 @@ export default class ControlService {
* @param {*} response * @param {*} response
* @memberof ControlService * @memberof ControlService
*/ */
public handleResponse(action: string, response: any,isCreate:boolean = false){ public async handleResponse(action: string, response: any,isCreate:boolean = false){
let result = null; let result = null;
if (!response.data) { if (!response.data) {
return return
} else if (response.data instanceof Array) { }
const handleResult:any = (action: string, response: any,isCreate:boolean,codelistArray?:any) =>{
if (response.data instanceof Array) {
result = []; result = [];
response.data.forEach((item:any) =>{ response.data.forEach((item:any) =>{
result.push(this.handleResponseData(action, item, isCreate)); result.push(this.handleResponseData(action, item, isCreate,codelistArray));
}); });
}else{ }else{
result = this.handleResponseData(action, response.data, isCreate); result = this.handleResponseData(action, response.data, isCreate,codelistArray);
} }
// response状态,头文件 // response状态,头文件
if(response.headers){ if(response.headers){
...@@ -144,6 +156,15 @@ export default class ControlService { ...@@ -144,6 +156,15 @@ export default class ControlService {
} }
response.data = result; response.data = result;
} }
let codelistModel:Array<any> = this.handleCodelist();
if(codelistModel.length >0){
this.getAllCodeList(codelistModel).then((res:any) =>{
handleResult(action, response,isCreate,res);
})
}else{
handleResult(action, response,isCreate);
}
}
/** /**
* 处理返回数据 * 处理返回数据
...@@ -152,7 +173,7 @@ export default class ControlService { ...@@ -152,7 +173,7 @@ export default class ControlService {
* @param {*} response * @param {*} response
* @memberof ControlService * @memberof ControlService
*/ */
public handleResponseData(action: string, data: any = {},isCreate?:boolean){ public handleResponseData(action: string, data: any = {},isCreate?:boolean,codelistArray?:any){
let model: any = this.getMode(); let model: any = this.getMode();
if (!model && model.getDataItems instanceof Function) { if (!model && model.getDataItems instanceof Function) {
return data; return data;
...@@ -167,10 +188,13 @@ export default class ControlService { ...@@ -167,10 +188,13 @@ export default class ControlService {
if((isCreate === undefined || isCreate === null ) && Object.is(dataitem.dataType, 'GUID') && Object.is(dataitem.name, 'srfkey') && (val && !Object.is(val, ''))){ if((isCreate === undefined || isCreate === null ) && Object.is(dataitem.dataType, 'GUID') && Object.is(dataitem.name, 'srfkey') && (val && !Object.is(val, ''))){
isCreate = true; isCreate = true;
} }
// if((Object.is(dataitem.dataType,'DATE') || Object.is(dataitem.dataType,'DATETIME')) && !Object.is(Date.parse(val),NaN)){
// val = Util.dateFormat(new Date(val));
// }
item[dataitem.name] = val; item[dataitem.name] = val;
// 转化代码表
if(codelistArray && dataitem.codelist){
if(codelistArray.get(dataitem.codelist.tag) && codelistArray.get(dataitem.codelist.tag).get(val)){
item[dataitem.name] = codelistArray.get(dataitem.codelist.tag).get(val);
}
}
}); });
item.srfuf = data.srfuf ? data.srfuf : (isCreate ? "0" : "1"); item.srfuf = data.srfuf ? data.srfuf : (isCreate ? "0" : "1");
return item; return item;
...@@ -199,4 +223,86 @@ export default class ControlService { ...@@ -199,4 +223,86 @@ export default class ControlService {
return requestData; return requestData;
} }
/**
* 处理代码表
*
* @memberof ControlService
*/
public handleCodelist(){
let model: any = this.getMode();
if (!model) {
return [];
}
let dataItems: any[] = model.getDataItems();
let codelistMap:Map<string,any> = new Map();
if(dataItems && dataItems.length >0){
dataItems.forEach((item:any) =>{
if(item.codelist){
codelistMap.set(item.name,item.codelist);
}
})
}
if(codelistMap.size >0){
return Array.from(codelistMap).map(item => item[1]);
}else{
return [];
}
}
/**
* 获取所有代码表
*
* @param codelistArray 代码表模型数组
* @memberof ControlService
*/
public getAllCodeList(codelistArray:Array<any>):Promise<any>{
return new Promise((resolve:any,reject:any) =>{
let codeListMap:Map<string,any> = new Map();
let promiseArray:Array<any> = [];
codelistArray.forEach((item:any) =>{
if(!codeListMap.get(item.tag)){
promiseArray.push(this.getCodeList(item));
Promise.all(promiseArray).then((result:any) =>{
if(result && result.length >0){
result.forEach((codeList:any) =>{
let tempCodeListMap:Map<number,any> = new Map();
if(codeList.length >0){
codeList.forEach((codeListItem:any) =>{
tempCodeListMap.set(codeListItem.value,codeListItem.text);
})
}
codeListMap.set(item.tag,tempCodeListMap);
})
resolve(codeListMap);
}
})
}
})
})
}
/**
* 获取代码表
*
* @param codeListObject 传入代码表对象
* @memberof ControlService
*/
public getCodeList(codeListObject:any):Promise<any>{
return new Promise((resolve:any,reject:any) =>{
if(codeListObject.tag && Object.is(codeListObject.codelistType,"STATIC")){
const codelist = (this.getStore() as Store<any>).getters.getCodeList(codeListObject.tag);
if (codelist) {
resolve([...JSON.parse(JSON.stringify(codelist.items))]);
} else {
console.log(`<#noparse>----${codeListObject.tag}----代码表不存在</#noparse>`);
}
}else if(codeListObject.tag && Object.is(codeListObject.codelistType,"DYNAMIC")){
this.codeListService.getItems(codeListObject.tag).then((res:any) => {
resolve(res);
}).catch((error:any) => {
console.log(`<#noparse>----${codeListObject.tag}----代码表不存在</#noparse>`);
});
}
})
}
} }
\ No newline at end of file
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册