xhSetMealService.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use common\components\dict;
  5. use common\components\stringUtil;
  6. use linslin\yii2\curl;
  7. use common\components\wxUtil;
  8. use common\models\xhSetMeal;
  9. class xhSetMealService {
  10. /**
  11. * 随机取一个套餐
  12. */
  13. public static function getRandOne()
  14. {
  15. $set = xhSetMeal::find()->asArray()->one();
  16. return $set;
  17. }
  18. public static function getById($id)
  19. {
  20. $cacheKey = dict::getCacheKey('xhSet').$id;
  21. $set = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
  22. if(!empty($set)){
  23. $arr = [];
  24. $i = 0;
  25. $x = 0;
  26. foreach($set as $key => $val){
  27. $i++;
  28. if($i%2 == 0){
  29. $arr[$x]['value'] = $val;
  30. $x++;
  31. }else{
  32. $arr[$x]['key'] = $val;
  33. }
  34. }
  35. $setArr = [];
  36. foreach($arr as $key => $val){
  37. $setArr[$val['key']] = $val['value'];
  38. }
  39. unset($setArr['info_already_get_by_sql']);
  40. return $setArr;
  41. }
  42. $set = xhSetMeal::find()->where(['id' => $id])->asArray()->one();
  43. $params = [$cacheKey];
  44. $params[] = 'info_already_get_by_sql';
  45. $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
  46. if(!empty($set)){
  47. foreach($set as $key => $val){
  48. $params[] = $key;
  49. $params[] = $val;
  50. }
  51. }
  52. Yii::$app->redis->executeCommand('HMSET',$params);
  53. return $set;
  54. }
  55. public static function add($data)
  56. {
  57. $return = xhSetMeal::add($data);
  58. $id = $return['id'];
  59. $cacheKey = dict::getCacheKey('xhSet').$id;
  60. $params = [$cacheKey];
  61. if(!empty($return)){
  62. foreach($return as $key => $val){
  63. $params[] = $key;
  64. $params[] = $val;
  65. }
  66. }
  67. Yii::$app->redis->executeCommand('HMSET',$params);
  68. return $return;
  69. }
  70. }