| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace bizHd\stat\classes;
- use bizHd\stat\models\StatOrder;
- use Yii;
- use bizHd\base\classes\BaseClass;
- class StatOrderClass extends BaseClass
- {
- public static $baseFile = '\bizHd\stat\models\StatOrder';
- public static function addOrder($merchantId, $asset, $date = null)
- {
- $date = isset($date) ? $date : date("Ymd");
- $stat = self::getByCondition(['merchantId' => $merchantId, 'time' => $date], true);
- if (empty($stat)) {
- $stat = self::add(['merchantId' => $merchantId, 'time' => $date], true);
- }
- $stat->riseNum += 1;
- $stat->save();
- //月订单数增加
- StatOrderMonthClass::addOrder($merchantId, $asset);
- }
- //今日订单+1
- public static function updateOrInsert($shop)
- {
- $time = date('Ymd');
- $shopId = $shop->id;
- $sjId = $shop->merchantId;
- $where = [
- 'shopId' => $shopId,
- 'time' => $time,
- ];
- $model = self::getByCondition($where, true);
- if (empty($model)) {
- $model = self::add(['merchantId' => $sjId, 'shopId'=>$shopId, 'time' => $time, 'riseNum' => 0, 'totalNum' => $shop->totalOrderNum], true);
- }
- $id = $model->id;
- //查id行级锁
- $stat = StatOrder::findBySql('select * from ' . StatOrder::tableName() . ' where id = :id for update', ['id' => $id])->one();
- $stat->riseNum += 1;
- $stat->totalNum += 1;
- $stat->save();
- //本月订单+1
- StatOrderMonthClass::updateOrInsert($shop);
- }
- public static function getRiseNumToday($shopId)
- {
- $time = date('Ymd');
- $res = self::getDetail($shopId,$time);
- return $res['riseNum']??0;
- }
- //yesterday
- public static function getRiseNumYesterday($shopId)
- {
- $time = date('Ymd',strtotime("-1 days"));
- $res = self::getDetail($shopId,$time);
- return $res['riseNum']??0;
- }
- public static function getDetail($shopId,$time)
- {
- $where = [
- 'shopId' => $shopId,
- 'time' => $time,
- ];
- $model = self::getByCondition($where);
- return $model;
- }
- //近7天
- public static function getRiseNumWeek($shopId)
- {
- $end = date('Ymd');
- $start = date('Ymd',strtotime("-6 days"));
- return self::getRiseNumByTime($shopId,$start,$end);
- }
- //近30天
- public static function getRiseNumThirty($shopId)
- {
- $end = date('Ymd');
- $start = date('Ymd',strtotime("-29 days"));
- return self::getRiseNumByTime($shopId,$start,$end);
- }
- public static function getRiseNumByTime($shopId,$start,$end)
- {
- $where = [];
- $where['shopId'] = $shopId;
- $where['time'] = ['between', [$start, $end]];
- $res = self::getAllByCondition($where, null, "riseNum");
- $riseNum = 0;
- if ($res) {
- foreach ($res as $v) {
- $riseNum = bcadd($riseNum, $v['riseNum'], 2);
- }
- }
- return $riseNum;
- }
- }
|