xhOrderMonthNumService.php 2.7 KB

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