import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs/Observable' ; import { HttpResult } from 'app/base/shared/model/http-result' ; import { Page } from 'app/base/shared/model/page' ; export class UserFilter { codeEq ? : string ; nameLk ? : string ; mobileLk ? : string ; pageNumber = 0 ; pageSize = 10 ; } export class QUser { code ? : string ; name ? : string ; mobile ? : string ; checked ? = false ; } export class User { code ? : string ; login ? : string ; name ? : string ; mobile ? : string ; hasOperateView ? = false ; hasPropertyView ? = false ; hasBrandView ? = false ; remark ? : string ; } export class UserRole { name ? : string ; checked ? = false ; remark ? : string ; } @ Injectable ( ) export class UserService { constructor ( ) { } public getList ( filter ) { const roles = userData . filter ( r => { if ( filter . codeEq != null && r . code !== filter . codeEq ) { return false ; } if ( filter . nameLk != null && ! r . name . includes ( filter . nameLk ) ) { return false ; } return true ; } ) ; return new Observable ( observer => { observer . next ( new HttpResult ( 0 , 'OK' , this . getPageRole ( roles , filter . pageNumber , filter . pageSize ) ) ) ; } ) ; } public save ( user ) { if ( user . code ) { const u = userData . find ( us => us . code === user . code ) ; if ( u ) { u . name = user . name ; u . login = user . login ; u . mobile = user . mobile ; u . hasOperateView = user . hasOperateView ; u . hasPropertyView = user . hasPropertyView ; u . hasBrandView = user . hasBrandView ; u . remark = user . remark ; } return new Observable ( observer => { observer . next ( new HttpResult ( 0 , 'OK' , user . code ) ) ; } ) ; } else { user . code = ( parseInt ( userData [ userData . length - 1 ] . code , 10 ) + 1 ) . toString ( ) ; userData . push ( user ) ; return new Observable ( observer => { observer . next ( new HttpResult ( 0 , 'OK' , user . code ) ) ; } ) ; } } public delete ( userCode ) { const index = userData . findIndex ( u => u . code === userCode ) ; if ( index >= 0 ) { userData . splice ( index , 1 ) ; } return new Observable ( observer => { observer . next ( new HttpResult ( 0 , 'OK' , true ) ) ; } ) ; } public getUser ( code ) { const user = userData . find ( u => u . code === code ) ; return new Observable ( observer => { observer . next ( new HttpResult ( 0 , 'OK' , user ) ) ; } ) ; } public addRoles ( code , roles ) { return new Observable ( observer => { observer . next ( new HttpResult ( 0 , 'OK' , true ) ) ; } ) ; } public removeRole ( userCode , roleName ) { const index = userRoleData . findIndex ( r => r . name === roleName ) ; if ( index >= 0 ) { userRoleData . splice ( index , 1 ) ; } return new Observable ( observer => { observer . next ( new HttpResult ( 0 , 'OK' , true ) ) ; } ) ; } public getUserRoles ( code ) { return new Observable ( observer => { observer . next ( new HttpResult ( 0 , 'OK' , userRoleData ) ) ; } ) ; } public resetPassword ( code , password ) { return new Observable ( observer => { observer . next ( new HttpResult ( 0 , 'OK' , true ) ) ; } ) ; } private getPageRole ( roles ? , pageNumber ? , pageSize ? ) { pageNumber = pageNumber ? pageNumber : 0 ; pageSize = pageSize ? pageSize : 10 ; roles = roles ? roles : [ ] ; const page = new Page < QUser > ( ) ; page . totalElements = roles . length ; page . totalPages = Math . floor ( roles . length / pageSize ) ; page . pageNumber = pageNumber ; page . pageSize = pageSize ; const startIndex = pageNumber * pageSize ; const endIndex = Math . min ( startIndex + pageSize , roles . length ) ; page . content = roles . slice ( startIndex , endIndex ) ; page . hasContent = page . content . length > 0 ; page . hasNext = endIndex < roles . length ; return page ; } } const userData = [ { code : '001' , name : '员工001' , login : 'login001' } , { code : '002' , name : '员工002' , login : 'login002' } , { code : '003' , name : '员工003' , login : 'login003' } , { code : '004' , name : '员工004' , login : 'login004' } , { code : '005' , name : '员工005' , login : 'login005' } , { code : '006' , name : '员工006' , login : 'login006' } , { code : '007' , name : '员工007' , login : 'login007' } , { code : '008' , name : '员工008' , login : 'login008' } , { code : '009' , name : '员工009' , login : 'login009' } , { code : '010' , name : '员工010' , login : 'login010' } , { code : '011' , name : '员工011' , login : 'login011' } , { code : '012' , name : '员工012' , login : 'login012' } , { code : '013' , name : '员工013' , login : 'login013' } , { code : '014' , name : '员工014' , login : 'login014' } , { code : '015' , name : '员工015' , login : 'login015' } , ] ; const userRoleData = [ { name : '管理员' } , { name : '采购员' } , { name : '补货员' } , { name : '哈哈哈' } , ] ;