StatIncomeClass.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace bizHd\stat\classes;
  3. use bizHd\stat\models\StatIncome;
  4. use common\components\dateUtil;
  5. use common\components\noticeUtil;
  6. use Yii;
  7. use bizHd\base\classes\BaseClass;
  8. class StatIncomeClass extends BaseClass
  9. {
  10. public static $baseFile = '\bizHd\stat\models\StatIncome';
  11. //更新当前商家的数据 lqh 2021.4.15
  12. // 收入更新,若无则新增
  13. public static function updateOrInsert($main, $shop, $amount)
  14. {
  15. $time = date('Ymd');
  16. $mainId = $shop->mainId ?? 0;
  17. $where = ['mainId' => $mainId, 'time' => $time];
  18. $model = self::getByCondition($where, true);
  19. if (empty($model)) {
  20. $data = ['mainId' => $mainId, 'time' => $time, 'amount' => 0, 'totalAmount' => $main->totalIncome, 'createTime' => date('Y-m-d H:i:s')];
  21. $model = self::add($data, true);
  22. }
  23. $id = $model->id;
  24. //查id行级锁
  25. $stat = StatIncomeClass::getLockById($id);
  26. $stat->amount = bcadd($amount, $stat->amount, 2);
  27. $stat->totalAmount = $main->totalIncome;
  28. $stat->save();
  29. //每月
  30. StatIncomeMonthClass::updateOrInsert($main, $shop, $amount);
  31. }
  32. //查询当天收入金额
  33. public static function getAmountToday($shopId)
  34. {
  35. $time = date('Ymd');
  36. $res = self::getDetail($shopId, $time);
  37. return $res['amount'] ?? 0;
  38. }
  39. //yesterday
  40. public static function getAmountYesterday($shopId)
  41. {
  42. $time = date('Ymd', strtotime("-1 days"));
  43. $res = self::getDetail($shopId, $time);
  44. return $res['amount'] ?? 0;
  45. }
  46. //近七天收入金额
  47. public static function getAmountWeek($shopId)
  48. {
  49. $end = date('Ymd');
  50. $start = date('Ymd', strtotime("-7 days"));
  51. return self::getAmountByTime($shopId, $start, $end);
  52. }
  53. //30天输入金额
  54. public static function getAmountThirty($shopId)
  55. {
  56. $end = date('Ymd');
  57. $start = date('Ymd', strtotime("-30 days"));
  58. return self::getAmountByTime($shopId, $start, $end);
  59. }
  60. //查询某一天的信息
  61. public static function getDetail($shopId, $time)
  62. {
  63. $where = [
  64. 'shopId' => $shopId,
  65. 'time' => $time,
  66. ];
  67. $model = self::getByCondition($where);
  68. return $model;
  69. }
  70. public static function getAmountByTime($shopId, $start, $end)
  71. {
  72. $where = [];
  73. $where['shopId'] = $shopId;
  74. $where['time'] = ['between', [$start, $end]];
  75. $res = self::getAllByCondition($where, null, "amount");
  76. $amount = 0;
  77. if ($res) {
  78. foreach ($res as $v) {
  79. $amount = bcadd($amount, $v['amount'], 2);
  80. }
  81. }
  82. return $amount;
  83. }
  84. }