| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace common\services;
- use Yii;
- use common\components\dict;
- use common\components\stringUtil;
- use linslin\yii2\curl;
- use common\components\wxUtil;
- use common\models\xhTrainClass;
- class xhTrainClassService {
- public static function getAllByCondition($condition)
- {
- return xhTrainClass::getAllByCondition($condition);
- }
- public static function getById($id)
- {
- $cacheKey = dict::getCacheKey('xhPxClass').$id;
- $class = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
- if(!empty($class)){
- $arr = [];
- $i = 0;
- $x = 0;
- foreach($class as $key => $val){
- $i++;
- if($i%2 == 0){
- $arr[$x]['value'] = $val;
- $x++;
- }else{
- $arr[$x]['key'] = $val;
- }
- }
- $classArr = [];
- foreach($arr as $key => $val){
- $classArr[$val['key']] = $val['value'];
- }
- unset($classArr['info_already_get_by_sql']);
- return $classArr;
- }
- $class = xhTrainClass::find()->where(['id' => $id])->asArray()->one();
- $params = [$cacheKey];
- $params[] = 'info_already_get_by_sql';
- $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
- if(!empty($class)){
- foreach($class as $key => $val){
- $params[] = $key;
- $params[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET',$params);
- return $class;
- }
- public static function add($data)
- {
- $class = xhTrainClass::add($data);
- $id = $class['id'];
- self::refreshById($id);
- return $class;
- }
- public static function refreshById($id)
- {
- $classKey = dict::getCacheKey('xhPxClass').$id;
- Yii::$app->redis->executeCommand('DEL', [$classKey]);
- self::getById($id);
- }
- public static function updateById($id,$data)
- {
- xhTrainClass::updateById($id, $data);
- $classKey = dict::getCacheKey('xhPxClass').$id;
- $classParams = [$classKey];
- foreach($data as $key => $val){
- $classParams[] = $key;
- $classParams[] = $val;
- }
- Yii::$app->redis->executeCommand('HMSET',$classParams);//更新缓存信息
- }
- public static function getAllClass($sjId)
- {
- $data = xhTrainClass::find()->where(['sjId'=>$sjId])->orderBy('sort asc')->asArray()->all();
- return $data;
- }
- }
|