import { Http,Util } from '@/utils';
import EntityService from '../entity-service';
import GetSFZXX from '@/service/customer/get-sfzxx-logic';



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

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

    /**
     * 初始化基础数据
     *
     * @memberof CUSTOMERServiceBase
     */
    public initBasicData(){
        this.APPDEKEY = 'customerid';
        this.APPDENAME = 'customers';
        this.APPDETEXT = 'customername';
    }

// 实体接口
            /**
     * 根据身份证号获取出生日期
     *
     * @param {IdNO} [string]
     * @memberof CUSTOMERServiceBase
     */
    public getBirthdayByIdNO (IdNO:string){
        let birthday = "";
        if (IdNO.length==18) {
          birthday = IdNO.substr(6,8);
          return birthday.replace(/(.{4})(.{2})/,"$1-$2-");
        }else if(IdNO.length==15){
          birthday = "19"+IdNO.substr(6,6);
          return birthday.replace(/(.{4})(.{2})/,"$1-$2-");
        }else{
          return ""; 
        }
    }

    /**
     * 根据身份证号获取性别
     *
     * @param {IdNO} [any]
     * @memberof CUSTOMERServiceBase
     */
    public getSexByIdNO(IdNO:any){
        if (IdNO.length==18) {
          return IdNO.charAt(16)%2==0?"B":"A";
        }else if(IdNO.length==15){
          return IdNO.charAt(14)%2==0?"B":"A";
        }else{
          return "";
        }
    }

    /**
     * 根据身份证号获取年龄
     *
     * @param {IdNO} [any]
     * @memberof CUSTOMERServiceBase
     */
    public  getAge(IdNO:any) {
        let len = (IdNO + "").length;
        if (len == 0) {
            return 0;
        } else {
            if ((len != 15) && (len != 18))//身份证号码只能为15位或18位其它不合法
            {
                return 0;
            }
        }
        let strBirthday = "";
        if (len == 18){
            strBirthday = IdNO.substr(6, 4) + "/" + IdNO.substr(10, 2) + "/" + IdNO.substr(12, 2);
        }
        if (len == 15) {
            strBirthday = "19" + IdNO.substr(6, 2) + "/" + IdNO.substr(8, 2) + "/" + IdNO.substr(10, 2);
        }
        //时间字符串里，必须是“/”
        var birthDate = new Date(strBirthday);
        var nowDateTime = new Date();
        var age = nowDateTime.getFullYear() - birthDate.getFullYear();
        //再考虑月、天的因素;.getMonth()获取的是从0开始的，这里进行比较，不需要加1
        if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate())) {
            age--;
        }
        return age;
    }

    /**
     * GetSFZXX接口方法
     *
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof CUSTOMERServiceBase
     */
    public async GetSFZXX(context:any ={},data: any = {}, isloading?: boolean): Promise<any> {
        if(Object.is(data.zjlx,'A1')){
            data.csny = this.getBirthdayByIdNO(data.zjhm);
            data.xb = this.getSexByIdNO(data.zjhm);
            data.nl = this.getAge(data.zjhm);	
        }
        return Promise.resolve({"status":200,"data":data});
    }

    /**
     * FetchDefault接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof CUSTOMERServiceBase
     */
    public async FetchDefault(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        // FetchDefault ---FETCH
        if(context.srfsessionkey && !Object.is(this.tempStorage.getItem(context.srfsessionkey+'_customers'),'undefined')){
            let result:any = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_customers') as any);
            if(result){
                return {"status":200,"data":result};
            }else{
                return {"status":200,"data":[]};
            } 
        }else{
            return {"status":200,"data":[]};
        }
    }

    /**
     * FetchTempDefault接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof CUSTOMERServiceBase
     */
    public async FetchTempDefault(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        // FetchTempDefault ---FETCHTEMP
        if(context.srfsessionkey && !Object.is(this.tempStorage.getItem(context.srfsessionkey+'_customers'),'undefined')){
            let result:any = JSON.parse(this.tempStorage.getItem(context.srfsessionkey+'_customers') as any);
            if(result){
                return {"status":200,"data":result};
            }else{
                return {"status":200,"data":[]};
            } 
        }else{
            return {"status":200,"data":[]};
        }
    }
}