xhOrderDayNumService.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use common\models\xhOrderDayNum;
  5. use common\components\configDict;
  6. class xhOrderDayNumService {
  7. /**
  8. * 增加一条订单数
  9. */
  10. public static function addOne($time,$merchantId)
  11. {
  12. $order = self::getByIds($time, $merchantId);
  13. if(empty($order)){
  14. $data = ['time'=>$time,'riseNum'=>1,'totalNum'=>1,'merchantId'=>$merchantId];
  15. self::add($data);
  16. }else{
  17. $newRiseNum = $order['riseNum'] + 1;
  18. $newTotalNum = $order['totalNum'] + 1;
  19. $data = ['riseNum'=>$newRiseNum,'totalNum'=>$newTotalNum];
  20. self::updateByIds($time, $merchantId, $data);
  21. }
  22. return $data;
  23. }
  24. public static function updateByIds($time,$merchantId,$data)
  25. {
  26. xhOrderDayNum::updateByCondition(['time'=>$time,'merchantId'=>$merchantId], $data);
  27. self::getByIds($time, $merchantId);
  28. $cacheKey = configDict::getCacheKey('xhOrderDayNum').'_'.$time.'_'.$merchantId;
  29. $params = [$cacheKey];
  30. if(!empty($data)){
  31. foreach($data as $key => $val){
  32. $params[] = $key;
  33. $params[] = $val;
  34. }
  35. }
  36. Yii::$app->redis->executeCommand('HMSET',$params);
  37. }
  38. public static function add($data)
  39. {
  40. $return = xhOrderDayNum::add($data);
  41. $merchantId = $return['merchantId'];
  42. $time = $return['time'];
  43. $cacheKey = configDict::getCacheKey('xhOrderDayNum').'_'.$time.'_'.$merchantId;
  44. $params = [$cacheKey];
  45. if(!empty($return)){
  46. foreach($return as $key => $val){
  47. $params[] = $key;
  48. $params[] = $val;
  49. }
  50. }
  51. Yii::$app->redis->executeCommand('HMSET',$params);
  52. return $return;
  53. }
  54. public static function getByIds($time,$merchantId)
  55. {
  56. $cacheKey = configDict::getCacheKey('xhOrderDayNum').'_'.$time.'_'.$merchantId;
  57. $order = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
  58. if(!empty($order)){
  59. $arr = [];
  60. $i = 0;
  61. $x = 0;
  62. foreach($order as $key => $val){
  63. $i++;
  64. if($i%2 == 0){
  65. $arr[$x]['value'] = $val;
  66. $x++;
  67. }else{
  68. $arr[$x]['key'] = $val;
  69. }
  70. }
  71. $orderArr = [];
  72. foreach($arr as $key => $val){
  73. $orderArr[$val['key']] = $val['value'];
  74. }
  75. unset($orderArr['info_already_get_by_sql']);
  76. return $orderArr;
  77. }
  78. $order = xhOrderDayNum::find()->where(['time'=>$time,'merchantId'=>$merchantId])->asArray()->one();
  79. $params = [$cacheKey];
  80. $params[] = 'info_already_get_by_sql';
  81. $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
  82. if(!empty($order)){
  83. foreach($order as $key => $val){
  84. $params[] = $key;
  85. $params[] = $val;
  86. }
  87. }
  88. Yii::$app->redis->executeCommand('HMSET',$params);
  89. return $order;
  90. }
  91. }