xhActiveOrderService.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use common\components\dict;
  5. use common\models\xhActiveOrder;
  6. class xhActiveOrderService {
  7. public static function add($data)
  8. {
  9. $return = xhActiveOrder::add($data);
  10. return $return;
  11. }
  12. public static function updateById($id,$data)
  13. {
  14. return xhActiveOrder::updateById($id, $data);
  15. }
  16. public static function getByCondition($condition)
  17. {
  18. return $order = xhActiveOrder::getByCondition($condition);
  19. }
  20. public static function getById($id)
  21. {
  22. $cacheKey = dict::getCacheKey('xhActiveOrder').$id;
  23. $active = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
  24. if(!empty($active)){
  25. $arr = [];
  26. $i = 0;
  27. $x = 0;
  28. foreach($active as $key => $val){
  29. $i++;
  30. if($i%2 == 0){
  31. $arr[$x]['value'] = $val;
  32. $x++;
  33. }else{
  34. $arr[$x]['key'] = $val;
  35. }
  36. }
  37. $activeArr = [];
  38. foreach($arr as $key => $val){
  39. $activeArr[$val['key']] = $val['value'];
  40. }
  41. unset($activeArr['info_already_get_by_sql']);
  42. return $activeArr;
  43. }
  44. $active = xhActiveOrder::find()->where(['id' => $id])->asArray()->one();
  45. $params = [$cacheKey];
  46. $params[] = 'info_already_get_by_sql';
  47. $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
  48. if(!empty($active)){
  49. foreach($active as $key => $val){
  50. $params[] = $key;
  51. $params[] = $val;
  52. }
  53. }
  54. Yii::$app->redis->executeCommand('HMSET',$params);
  55. return $active;
  56. }
  57. }