app-mob-calendar-service.ts 12.0 KB
Newer Older
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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
import { ControlServiceBase, Util } from 'ibiz-core';
import { AppMobCalendarModel, Errorlog } from 'ibiz-vue';
import { DataServiceHelp } from 'ibiz-core';
import { IPSAppDEDataSet, IPSDECalendar, IPSSysCalendar, IPSSysCalendarItem, IPSAppDataEntity } from '@ibiz/dynamic-model-api';

export class AppMobCalendarService extends ControlServiceBase {

    /**
    * 日历实例对象
    *
    * @memberof AppMobCalendarService
    */
    public declare controlInstance: IPSDECalendar;

    /**
     * 数据服务对象
     *
     * @type {any}
     * @memberof AppMobCalendarService
     */
    public appEntityService!: any;

    /**
     * 日历项实体服务集合
     *
     * @private
     * @type {Map<string, any>}
     * @memberof AppMobCalendarService
     */
    private $itemEntityServiceMap: Map<string, any> = new Map();


    /**
     * 初始化服务参数
     *
     * @type {boolean}
     * @memberof AppMobCalendarService
     */
    public async initServiceParam(opts: any) {
        this.controlInstance = opts;
        this.appEntityService = await DataServiceHelp.getInstance().getService(this.controlInstance?.getPSAppDataEntity(), { context: this.context });
    }

    /**
     * Creates an instance of AppMobFormService.
     * 
     * @param {*} [opts={}]
     * @memberof AppMobCalendarService
     */
    constructor(opts: any = {}, context?: any, args?: any) {
        super(opts, context, args);
        this.model = new AppMobCalendarModel(opts);
        this.initServiceParam(opts);
        this.initEventsConfig();
    }


    /**
     * 事件配置集合
     *
     * @public
     * @type {any[]}
     * @memberof AppMobCalendarService
     */
    public eventsConfig: any[] = [];

    /**
     * 初始化事件配置集合
     * 
     * @memberof AppMobCalendarService
     */
    public initEventsConfig() {
        let tempArr: any[] = [];
        const calendarItems = (this.controlInstance as IPSSysCalendar).getPSSysCalendarItems() || [];
        if (calendarItems?.length > 0) {
            calendarItems.forEach((item: IPSSysCalendarItem) => {
                tempArr.push({
                    itemName: item.name,
                    itemType: item.itemType,
                    color: item.bKColor,
                    textColor: item.color
                });
            })
        }
        this.eventsConfig = [...tempArr];
    }

    /**
     * 初始化日历项实体服务
     *
     * @memberof AppMobCalendarService
     */
    public async initItemEntityService() {
        const calendarItems = (this.controlInstance as IPSSysCalendar).getPSSysCalendarItems() || [];
        if (calendarItems?.length > 0) {
            for (const item of calendarItems) {
                const codeName = item.getPSAppDataEntity()?.codeName;
                if (codeName) {
                    let service: any = await DataServiceHelp.getInstance().getService(this.controlInstance?.getPSAppDataEntity(), { context: this.context });
                    this.$itemEntityServiceMap.set(codeName, service);
                }
            }
        }
    }

    /**
     * 查询数据
     *
     * @param {string} action
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof AppMobCalendarService
     */
    @Errorlog
    public async search(action: string, context: any = {}, data: any = {}, isloading?: boolean): Promise<any> {
        let _this = this;
        const calendarItems = (_this.controlInstance as IPSSysCalendar).getPSSysCalendarItems() || [];
        if (calendarItems.length != _this.$itemEntityServiceMap.size) {
            await this.initItemEntityService();
        }
        return new Promise((resolve: any, reject: any) => {
            let item: any = {};
            let promises: any[] = [];
            const items = (_this.controlInstance as IPSSysCalendar).getPSSysCalendarItems() || [];
            const appde = this.controlInstance.getPSAppDataEntity();
            if (items?.length > 0) {
                for (const item of calendarItems) {
                    const codeName = (item.getPSAppDataEntity() as IPSAppDataEntity)?.codeName || '' as string;
                    let service: any = _this.$itemEntityServiceMap.get(codeName);
                    let tempRequest: any = _this.handleRequestData(action, context, data, true, item.itemStyle || '' as string);
                    Object.assign(tempRequest.data, { start: data.start, end: data.end });
                    const appDeDataSet: IPSAppDEDataSet = item.getPSAppDEDataSet() as IPSAppDEDataSet;
                    if (appDeDataSet.codeName && service) {
                        promises.push(service.execute(appDeDataSet.codeName, tempRequest.context, tempRequest.data));
                    }
                }
            }
            Promise.all(promises).then((resArray: any) => {
                let _data: any = [];
                resArray.forEach((response: any, resIndex: number) => {
                    if (!response || response.status !== 200) {
                        return;
                    }
                    let _response: any = JSON.parse(JSON.stringify(response));
                    _response.data.forEach((item: any, index: number) => {
                        _response.data[index].color = _this.eventsConfig[resIndex].color;
                        _response.data[index].textColor = _this.eventsConfig[resIndex].textColor;
                        _response.data[index].itemType = _this.eventsConfig[resIndex].itemType;
                    });
                    _this.handleResponse(action, _response, false, _this.eventsConfig[resIndex]?.itemType);
                    let itemType = items[resIndex].itemType.toLowerCase();
                    Object.assign(item, { [itemType]: _response.data });
                });
                // 排序
                // _data.sort((a:any, b:any)=>{
                //     let dateA = new Date(Date.parse(a.start.replace(/-/g, "/")));
                //     let dateB = new Date(Date.parse(b.start.replace(/-/g, "/")));
                //     return dateA > dateB ? 1 : -1 ;
                // });
                let result = { status: 200, data: item };
                resolve(result);
            }).catch((response: any) => {
                reject(response);
            });
        });
    }

    /**
     * 修改数据
     *
     * @param {string} action
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof AppMobCalendarService
     */
    @Errorlog
    public update(itemType: string, context: any = {}, data: any = {}, isloading?: boolean): Promise<any> {
        return new Promise((resolve: any, reject: any) => {
            let result: any;
            let tempRequest: any;
            const calendarItems = (this.controlInstance as IPSSysCalendar).getPSSysCalendarItems() || [];
            const item = calendarItems.find((_item: IPSSysCalendarItem) => { return _item.itemType == itemType });
            if (item) {
                tempRequest = this.handleRequestData("", context, data, false, itemType);
                let codeName: any = item.getPSAppDataEntity()?.codeName;
                if (codeName && this.$itemEntityServiceMap.get(codeName)) {
                    let service: any = this.$itemEntityServiceMap.get(codeName);
                    const action = item.getUpdatePSAppDEAction?.()?.codeName || 'Update';
                    if (service && action) {
                        result = service.execute(action, tempRequest.context, tempRequest.data);
                    }
                }
            }
            if (result) {
                result.then((response: any) => {
                    this.handleResponse("", response);
                    resolve(response);
                }).catch((response: any) => {
                    reject(response);
                });
            } else {
                reject("没有匹配的实体服务");
            }
        });
    }

    /**
     * 删除数据
     *
     * @param {string} action
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isLoading]
     * @returns {Promise<any>}
     * @memberof AppMobCalendarService
     */
    public async delete(itemType: string, context: any = {}, data: any = {}, isloading?: boolean): Promise<any> {
        return new Promise((resolve: any, reject: any) => {
            let result: any;
            let tempRequest: any;
            const calendarItems = (this.controlInstance as IPSSysCalendar).getPSSysCalendarItems() || [];
            const item = calendarItems.find((_item: IPSSysCalendarItem) => { return _item.itemType == itemType });
            if (item) {
                tempRequest = this.handleRequestData("", context, data, false, itemType);
                let codeName: any = item.getPSAppDataEntity()?.codeName;
                if (codeName && this.$itemEntityServiceMap.get(codeName)) {
                    let service: any = this.$itemEntityServiceMap.get(codeName);
                    result = service.Remove(tempRequest.context, tempRequest.data, isloading);
                }
            }
            if (result) {
                result.then((response: any) => {
                    this.handleResponse("", response);
                    resolve(response);
                }).catch((response: any) => {
                    reject(response);
                });
            } else {
                reject("没有匹配的实体服务");
            }
        });
    }

    /**
     * 处理request请求数据
     * 
     * @param action 行为 
     * @param data 数据
     * @memberof ControlService
     */
    public handleRequestData(action: string, context: any = {}, data: any = {}, isMerge: boolean = false, itemType: string = "") {
        let model: any = this.getMode();
        model.itemType = itemType;
        if (!model && model.getDataItems instanceof Function) {
            return data;
        }
        let dataItems: any[] = model.getDataItems();
        let requestData: any = {};
        if (isMerge && (data && data.viewparams)) {
            Object.assign(requestData, data.viewparams);
        }
        dataItems.forEach((item: any) => {
            if (item && item.dataType && Object.is(item.dataType, 'FRONTKEY')) {
                if (item && item.prop && item.name) {
                    requestData[item.prop] = context[item.name];
                }
            } else {
                if (item && item.prop) {
                    requestData[item.prop] = data[item.name];
                }
            }
        });
        let tempContext: any = JSON.parse(JSON.stringify(context));
        if (tempContext && tempContext.srfsessionid) {
            tempContext.srfsessionkey = tempContext.srfsessionid;
            delete tempContext.srfsessionid;
        }
        return { context: tempContext, data: requestData };
    }

    /**
     * 处理response返回数据
     *
     * @param {string} action
     * @param {*} response
     * @memberof ControlService
     */
    public async handleResponse(action: string, response: any, isCreate: boolean = false, itemType: string = "") {
        let model: AppMobCalendarModel = this.getMode();
        model.itemType = itemType;
        super.handleResponse(action, response, isCreate);
    }


    /**
     * 处理返回数据
     *
     * @param {string} action
     * @param {*} response
     * @memberof ControlService
     */
    public handleResponseData(action: string, data: any = {}, isCreate?: boolean, codelistArray?: any) {
        let model: any = this.getMode();
        if (!model && model.getDataItems instanceof Function) {
            return data;
        }
        const tempData: any = data;
        let dataItems: any[] = model.getDataItems();
        dataItems.forEach(dataitem => {
            let val = tempData.hasOwnProperty(dataitem.prop) ? tempData[dataitem.prop] : null;
            if (!val) {
                val = tempData.hasOwnProperty(dataitem.name) ? tempData[dataitem.name] : null;
            }
            tempData[dataitem.name] = val;
        });
        return tempData;
    }
}