import { Http,Util } from '@/utils';
import EntityService from '../entity-service';



/**
 * 用户服务对象基类
 *
 * @export
 * @class UserServiceBase
 * @extends {EntityServie}
 */
export default class UserServiceBase extends EntityService {

    /**
     * Creates an instance of  UserServiceBase.
     * 
     * @param {*} [opts={}]
     * @memberof  UserServiceBase
     */
    constructor(opts: any = {}) {
        super(opts);
    }

    /**
     * 初始化基础数据
     *
     * @memberof UserServiceBase
     */
    public initBasicData(){
        this.APPLYDEKEY ='user';
        this.APPDEKEY = 'userid';
        this.APPDENAME = 'users';
        this.APPDETEXT = 'username';
    }

// 实体接口

    /**
     * Remove接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof UserServiceBase
     */
    public async Remove(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
            return Http.getInstance().delete(`/users/${context.user}`,isloading);

    }

    /**
     * Create接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof UserServiceBase
     */
    public async Create(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let masterData:any = {};
        let loginaccountsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_loginaccounts'),'undefined')){
            loginaccountsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_loginaccounts') as any);
        }
        masterData.loginaccounts = loginaccountsData;
        Object.assign(data,masterData);
        data[this.APPDEKEY] = null;
        let tempContext:any = JSON.parse(JSON.stringify(context));
        let res:any = await Http.getInstance().post(`/users`,data,isloading);
        this.tempStorage.setItem(tempContext.srfsessionkey+'_loginaccounts',JSON.stringify(res.data.loginaccounts));
        return res;
    }

    /**
     * SyncToUser接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof UserServiceBase
     */
    public async SyncToUser(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
            return Http.getInstance().post(`/users/${context.user}/synctouser`,data,isloading);
    }

    /**
     * CheckKey接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof UserServiceBase
     */
    public async CheckKey(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
            return Http.getInstance().post(`/users/${context.user}/checkkey`,data,isloading);
    }

    /**
     * Get接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof UserServiceBase
     */
    public async Get(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
            let res:any = await Http.getInstance().get(`/users/${context.user}`,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_loginaccounts',JSON.stringify(res.data.loginaccounts));
            return res;

    }

    /**
     * Save接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof UserServiceBase
     */
    public async Save(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let masterData:any = {};
        let loginaccountsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_loginaccounts'),'undefined')){
            loginaccountsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_loginaccounts') as any);
        }
        masterData.loginaccounts = loginaccountsData;
        Object.assign(data,masterData);
            let res:any = await  Http.getInstance().post(`/users/${context.user}/save`,data,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_loginaccounts',JSON.stringify(res.data.loginaccounts));
            return res;
    }

    /**
     * GetDraft接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof UserServiceBase
     */
    public async GetDraft(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let res:any = await  Http.getInstance().get(`/users/getdraft`,isloading);
        res.data.user = data.user;
            this.tempStorage.setItem(context.srfsessionkey+'_loginaccounts',JSON.stringify(res.data.loginaccounts));
        return res;
    }

    /**
     * Update接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof UserServiceBase
     */
    public async Update(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let masterData:any = {};
        let loginaccountsData:any = [];
        if(!Object.is(this.tempStorage.getItem(context.srfsessionkey+'_loginaccounts'),'undefined')){
            loginaccountsData = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_loginaccounts') as any);
        }
        masterData.loginaccounts = loginaccountsData;
        Object.assign(data,masterData);
            let res:any = await  Http.getInstance().put(`/users/${context.user}`,data,isloading);
            this.tempStorage.setItem(context.srfsessionkey+'_loginaccounts',JSON.stringify(res.data.loginaccounts));
            return res;
    }

    /**
     * FetchDefault接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof UserServiceBase
     */
    public async FetchDefault(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let tempData:any = JSON.parse(JSON.stringify(data));
        return Http.getInstance().get(`/users/fetchdefault`,tempData,isloading);
    }
}