| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace bizHd\stat\classes;
- use bizHd\stat\models\StatIncome;
- use common\components\dateUtil;
- use common\components\noticeUtil;
- use Yii;
- use bizHd\base\classes\BaseClass;
- class StatIncomeClass extends BaseClass
- {
- public static $baseFile = '\bizHd\stat\models\StatIncome';
- //更新当前商家的数据 lqh 2021.4.15
- // 收入更新,若无则新增
- public static function updateOrInsert($main, $shop, $amount)
- {
- $time = date('Ymd');
- $mainId = $shop->mainId ?? 0;
- $where = ['mainId' => $mainId, 'time' => $time];
- $model = self::getByCondition($where, true);
- if (empty($model)) {
- $data = ['mainId' => $mainId, 'time' => $time, 'amount' => 0, 'totalAmount' => $main->totalIncome, 'createTime' => date('Y-m-d H:i:s')];
- $model = self::add($data, true);
- }
- $id = $model->id;
- //查id行级锁
- $stat = StatIncomeClass::getLockById($id);
- $stat->amount = bcadd($amount, $stat->amount, 2);
- $stat->totalAmount = $main->totalIncome;
- $stat->save();
- //每月
- StatIncomeMonthClass::updateOrInsert($main, $shop, $amount);
- }
- //查询当天收入金额
- public static function getAmountToday($shopId)
- {
- $time = date('Ymd');
- $res = self::getDetail($shopId, $time);
- return $res['amount'] ?? 0;
- }
- //yesterday
- public static function getAmountYesterday($shopId)
- {
- $time = date('Ymd', strtotime("-1 days"));
- $res = self::getDetail($shopId, $time);
- return $res['amount'] ?? 0;
- }
- //近七天收入金额
- public static function getAmountWeek($shopId)
- {
- $end = date('Ymd');
- $start = date('Ymd', strtotime("-7 days"));
- return self::getAmountByTime($shopId, $start, $end);
- }
- //30天输入金额
- public static function getAmountThirty($shopId)
- {
- $end = date('Ymd');
- $start = date('Ymd', strtotime("-30 days"));
- return self::getAmountByTime($shopId, $start, $end);
- }
- //查询某一天的信息
- public static function getDetail($shopId, $time)
- {
- $where = [
- 'shopId' => $shopId,
- 'time' => $time,
- ];
- $model = self::getByCondition($where);
- return $model;
- }
- public static function getAmountByTime($shopId, $start, $end)
- {
- $where = [];
- $where['shopId'] = $shopId;
- $where['time'] = ['between', [$start, $end]];
- $res = self::getAllByCondition($where, null, "amount");
- $amount = 0;
- if ($res) {
- foreach ($res as $v) {
- $amount = bcadd($amount, $v['amount'], 2);
- }
- }
- return $amount;
- }
- }
|