xhTrainUserCourseService.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use common\components\configDict;
  5. use common\components\stringUtil;
  6. use common\components\wxUtil;
  7. use common\models\xhTrainUserCourse;
  8. class xhTrainUserCourseService {
  9. public static function getAllByCondition($condition)
  10. {
  11. return xhTrainUserCourse::getAllByCondition($condition);
  12. }
  13. public static function getById($id)
  14. {
  15. $cacheKey = configDict::getCacheKey('xhPxUserCourse').$id;
  16. $course = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
  17. if(!empty($course)){
  18. $arr = [];
  19. $i = 0;
  20. $x = 0;
  21. foreach($course as $key => $val){
  22. $i++;
  23. if($i%2 == 0){
  24. $arr[$x]['value'] = $val;
  25. $x++;
  26. }else{
  27. $arr[$x]['key'] = $val;
  28. }
  29. }
  30. $courseArr = [];
  31. foreach($arr as $key => $val){
  32. $courseArr[$val['key']] = $val['value'];
  33. }
  34. unset($courseArr['info_already_get_by_sql']);
  35. return $courseArr;
  36. }
  37. $course = xhTrainUserCourse::find()->where(['id' => $id])->asArray()->one();
  38. $params = [$cacheKey];
  39. $params[] = 'info_already_get_by_sql';
  40. $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
  41. if(!empty($course)){
  42. foreach($course as $key => $val){
  43. $params[] = $key;
  44. $params[] = $val;
  45. }
  46. }
  47. Yii::$app->redis->executeCommand('HMSET',$params);
  48. return $course;
  49. }
  50. public static function add($data)
  51. {
  52. $course = xhTrainUserCourse::add($data);
  53. $id = $course['id'];
  54. self::refreshById($id);
  55. return $course;
  56. }
  57. public static function refreshById($id)
  58. {
  59. $courseKey = configDict::getCacheKey('xhPxUserCourse').$id;
  60. Yii::$app->redis->executeCommand('DEL', [$courseKey]);
  61. self::getById($id);
  62. }
  63. public static function updateById($id,$data)
  64. {
  65. xhTrainUserCourse::updateById($id, $data);
  66. self::getById($id);//保存有缓存
  67. $courseKey = configDict::getCacheKey('xhPxUserCourse').$id;
  68. $courseParams = [$courseKey];
  69. foreach($data as $key => $val){
  70. $courseParams[] = $key;
  71. $courseParams[] = $val;
  72. }
  73. Yii::$app->redis->executeCommand('HMSET',$courseParams);//更新缓存信息
  74. }
  75. public static function getWorksImgList($largeImg)
  76. {
  77. $extend = substr($largeImg, strrpos($largeImg, '.')+1);
  78. $pre = substr($largeImg,0,strrpos($largeImg, '.'));
  79. return ['small'=>$pre.'_100.'.$extend,'medium'=>$pre.'_300.'.$extend,'large'=>$largeImg];
  80. }
  81. }