| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace common\services;
- use Yii;
- use common\components\dict;
- use common\components\stringUtil;
- use common\components\wxUtil;
- use common\models\xhTrainUserCourse;
- class xhTrainUserCourseService {
- public static function getAllByCondition($condition)
- {
- return xhTrainUserCourse::getAllByCondition($condition);
- }
- public static function getById($id)
- {
- $cacheKey = dict::getCacheKey('xhPxUserCourse').$id;
- $course = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
- if(!empty($course)){
- $arr = [];
- $i = 0;
- $x = 0;
- foreach($course as $key => $val){
- $i++;
- if($i%2 == 0){
- $arr[$x]['value'] = $val;
- $x++;
- }else{
- $arr[$x]['key'] = $val;
- }
- }
- $courseArr = [];
- foreach($arr as $key => $val){
- $courseArr[$val['key']] = $val['value'];
- }
- unset($courseArr['info_already_get_by_sql']);
- return $courseArr;
- }
- $course = xhTrainUserCourse::find()->where(['id' => $id])->asArray()->one();
- $params = [$cacheKey];
- $params[] = 'info_already_get_by_sql';
- $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
- if(!empty($course)){
- foreach($course as $key => $val){
- $params[] = $key;
- $params[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET',$params);
- return $course;
- }
- public static function add($data)
- {
- $course = xhTrainUserCourse::add($data);
- $id = $course['id'];
- self::refreshById($id);
- return $course;
- }
- public static function refreshById($id)
- {
- $courseKey = dict::getCacheKey('xhPxUserCourse').$id;
- Yii::$app->redis->executeCommand('DEL', [$courseKey]);
- self::getById($id);
- }
- public static function updateById($id,$data)
- {
- xhTrainUserCourse::updateById($id, $data);
- self::getById($id);//保存有缓存
- $courseKey = dict::getCacheKey('xhPxUserCourse').$id;
- $courseParams = [$courseKey];
- foreach($data as $key => $val){
- $courseParams[] = $key;
- $courseParams[] = $val;
- }
- Yii::$app->redis->executeCommand('HMSET',$courseParams);//更新缓存信息
- }
- public static function getWorksImgList($largeImg)
- {
- $extend = substr($largeImg, strrpos($largeImg, '.')+1);
- $pre = substr($largeImg,0,strrpos($largeImg, '.'));
- return ['small'=>$pre.'_100.'.$extend,'medium'=>$pre.'_300.'.$extend,'large'=>$largeImg];
- }
-
- }
|