import { Injectable } from '@nestjs/common' ; import { InjectConnection , InjectRepository } from '@nestjs/typeorm' ; import * as _ from 'lodash' ; import { Connection , Repository } from 'typeorm' ; import { CacheService } from '../../cache/cache.service' ; import * as CACHE_KEY from '../../common/constants/cache.constant' ; import { ID } from '../../common/shared-types' ; import { Option } from '../../entity' ; @ Injectable ( ) export class OptionService { constructor ( @ InjectConnection ( ) private readonly connection , private readonly cacheService ) { } /**
     * 初始化系统配置到缓存服务中
     */ async initOptions ( ) { await this . load ( true ) ; } param flag async load ( flag = false ) { if ( flag ) { await this . cacheService . set ( CACHE_KEY . OPTIONS , null ) ; } let cacheOptions = await this . cacheService . get ( CACHE_KEY . OPTIONS ) ; if ( _ . isEmpty ( cacheOptions ) ) { const data = await this . connection . getRepository ( Option ) . find ( ) ; const result = { } ; data . forEach ( item => { result [ item . key ] = item . value ; } ) ; await this . cacheService . set ( CACHE_KEY . OPTIONS , result ) ; cacheOptions = await this . cacheService . get ( CACHE_KEY . OPTIONS ) ; } return cacheOptions ; } param optionKey param optionValue async updateOptions ( optionKey , optionValue ) { const data = _ . isObject ( optionKey ) ? _ . extend ( { } , optionKey ) : { [ optionKey ] : optionValue } ; let cacheOptions = await this . cacheService . get ( CACHE_KEY . OPTIONS ) ; if ( _ . isEmpty ( cacheOptions ) ) { cacheOptions = await this . load ( ) ; } const changedOptions = { } ; for ( const tempKey in data ) { if ( data [ tempKey ] !== cacheOptions [ tempKey ] ) { changedOptions [ tempKey ] = data [ tempKey ] ; } } const jsonSQL = `JSON_SET(value,'$.${ optionValue . key }', '${ optionValue . value }') WHERE \`key\` = '${ optionKey }'` ; if ( _ . isEmpty ( changedOptions ) ) { return ; } const updateResult = await this . connection . createQueryBuilder ( ) . update ( Option ) . set ( { value : jsonSQL } ) . where ( 'key = :key' , { key : optionKey } ) . execute ( ) ; if ( updateResult ) { return await this . load ( true ) ; } return await this . load ( ) ; } param key param itemId async addSticky ( key , itemId ) { const options = await this . load ( ) ; const stickys = options . stickys ; for ( const stickysKey in stickys ) { if ( stickysKey === key ) { if ( ! stickys [ key ] . includes ( itemId ) ) { stickys [ key ] . unshift ( itemId ) ; return await this . saveSticky ( stickys ) ; } } } } /**
     * 删除置顶数据项
     */ async removeSticky ( key , itemId ) { const options = await this . load ( ) ; const stickys = options . stickys ; for ( const stickysKey in stickys ) { if ( stickysKey === key ) { if ( stickys [ key ] . includes ( itemId ) ) { for ( let i = 0 ; i < stickys [ key ] . length ; i ++ ) { if ( stickys [ key ] [ i ] === itemId ) { stickys [ key ] . splice ( i , 1 ) ; } } } } } await this . saveSticky ( stickys ) ; } param stickys async saveSticky ( stickys ) { const updateResult = await this . connection . createQueryBuilder ( ) . update ( Option ) . set ( { value : stickys } ) . where ( 'key = :key' , { key : 'stickys' } ) . execute ( ) ; return updateResult ; } }