StatOrderClass.php 3.0 KB

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