xhTrainCourseService.php 2.0 KB

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