StatOrderClass.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace bizHd\stat\classes;
  3. use bizHd\stat\models\StatOrder;
  4. use bizHd\base\classes\BaseClass;
  5. class StatOrderClass extends BaseClass
  6. {
  7. public static $baseFile = '\bizHd\stat\models\StatOrder';
  8. //今日订单+1
  9. public static function updateOrInsert($main, $shop)
  10. {
  11. $time = date('Ymd');
  12. $mainId = $shop->mainId ?? 0;
  13. $where = ['mainId' => $mainId, 'time' => $time];
  14. $model = self::getByCondition($where, true);
  15. if (empty($model)) {
  16. $model = self::add(['mainId' => $mainId, 'time' => $time, 'riseNum' => 0, 'totalNum' => $main->totalOrderNum], true);
  17. }
  18. $id = $model->id;
  19. //查id行级锁
  20. $stat = StatOrderClass::getLockById($id);
  21. $stat->riseNum += 1;
  22. $stat->totalNum += 1;
  23. $stat->save();
  24. //本月订单+1
  25. StatOrderMonthClass::updateOrInsert($main, $shop);
  26. }
  27. public static function getRiseNumToday($shopId)
  28. {
  29. $time = date('Ymd');
  30. $res = self::getDetail($shopId, $time);
  31. return $res['riseNum'] ?? 0;
  32. }
  33. //yesterday
  34. public static function getRiseNumYesterday($shopId)
  35. {
  36. $time = date('Ymd', strtotime("-1 days"));
  37. $res = self::getDetail($shopId, $time);
  38. return $res['riseNum'] ?? 0;
  39. }
  40. public static function getDetail($shopId, $time)
  41. {
  42. $where = [
  43. 'shopId' => $shopId,
  44. 'time' => $time,
  45. ];
  46. $model = self::getByCondition($where);
  47. return $model;
  48. }
  49. //近7天
  50. public static function getRiseNumWeek($shopId)
  51. {
  52. $end = date('Ymd');
  53. $start = date('Ymd', strtotime("-6 days"));
  54. return self::getRiseNumByTime($shopId, $start, $end);
  55. }
  56. //近30天
  57. public static function getRiseNumThirty($shopId)
  58. {
  59. $end = date('Ymd');
  60. $start = date('Ymd', strtotime("-29 days"));
  61. return self::getRiseNumByTime($shopId, $start, $end);
  62. }
  63. public static function getRiseNumByTime($shopId, $start, $end)
  64. {
  65. $where = [];
  66. $where['shopId'] = $shopId;
  67. $where['time'] = ['between', [$start, $end]];
  68. $res = self::getAllByCondition($where, null, "riseNum");
  69. $riseNum = 0;
  70. if ($res) {
  71. foreach ($res as $v) {
  72. $riseNum = bcadd($riseNum, $v['riseNum'], 0);
  73. }
  74. }
  75. return $riseNum;
  76. }
  77. }