Ver código fonte

feat(admin): 添加分红积分基准价格设置并进行有效性验证

- 在系统配置中添加"award_integral_price"字段,用于设置分红积分基准价格
- 添加对分红积分基准价格的验证,确保其值大于 0
- 优化了代码格式,调整了数组元素的缩进
kirin 5 meses atrás
pai
commit
5ed3daaa74

+ 45 - 0
app/common/dao/user/AwardIntegralPriceDao.php

@@ -0,0 +1,45 @@
+<?php
+
+// +----------------------------------------------------------------------
+// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
+// +----------------------------------------------------------------------
+// | Author: CRMEB Team <admin@crmeb.com>
+// +----------------------------------------------------------------------
+
+
+namespace app\common\dao\user;
+
+
+use app\common\dao\BaseDao;
+use app\common\model\BaseModel;
+use app\common\model\user\AwardIntegralPrice;
+
+/**
+ * Class UserLabelDao
+ * @package app\common\dao\user
+ * @author xaboy
+ * @day 2020-05-07
+ */
+class AwardIntegralPriceDao extends BaseDao
+{
+
+    /**
+     * @return BaseModel
+     * @author xaboy
+     * @day 2020-03-30
+     */
+    protected function getModel(): string
+    {
+        return AwardIntegralPrice::class;
+    }
+
+    public function search($where)
+    {
+        return $this->getModel()->where($where);
+    }
+
+}

+ 18 - 0
app/common/model/user/AwardIntegralPrice.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace app\common\model\user;
+
+use app\common\model\BaseModel;
+
+class AwardIntegralPrice extends BaseModel
+{
+    public static function tablePk(): ?string
+    {
+        return 'id';
+    }
+
+    public static function tableName(): string
+    {
+        return 'award_integral_price';
+    }
+}

+ 71 - 0
app/common/repositories/user/AwardIntegralPriceRepository.php

@@ -0,0 +1,71 @@
+<?php
+
+// +----------------------------------------------------------------------
+// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
+// +----------------------------------------------------------------------
+// | Author: CRMEB Team <admin@crmeb.com>
+// +----------------------------------------------------------------------
+
+
+namespace app\common\repositories\user;
+
+
+use app\common\dao\user\AwardIntegralPriceDao;
+use app\common\repositories\BaseRepository;
+
+
+class AwardIntegralPriceRepository extends BaseRepository
+{
+    /**
+     * @var AwardIntegralPriceDao
+     */
+    protected $dao;
+
+    /**
+     * UserSignRepository constructor.
+     * @param AwardIntegralPriceDao $dao
+     */
+    public function __construct(AwardIntegralPriceDao $dao)
+    {
+        $this->dao = $dao;
+    }
+
+
+    public function awardIntegralPrice($day = '')
+    {
+        if (!$day) {
+            $day = date('Y-m-d');
+        }
+        if ($info = $this->dao->search(['day' => date('Y-m-d')])->find()) {
+            return $info->toArray();
+        }
+        $yesterday_price = $this->dao->search(['day' => date('Y-m-d', strtotime('-1 day', strtotime($day)))])->find();
+        if ($yesterday_price) {
+            $basePrice = $yesterday_price->price;
+        } else {
+            $basePrice = systemConfig('award_integral_price', 0.1);
+        }
+        //todo 业绩
+        $achievement = 0;
+        /** @var UserExtractRepository $extractRepositories */
+        $extractRepositories = app()->make(UserExtractRepository::class);
+        $commission = $extractRepositories->search(['status' => 1])->whereTime('check_time', $day)->sum('commission');
+        $userService = app()->make(UserRepository::class);
+        $num = $userService->search(['status' => 1])->sum('award_integral');
+        $sum_achievement = $commission + $achievement;
+
+        $rise = bcdiv($sum_achievement, $num, 3);
+        $price = bcadd($rise, $basePrice, 3);
+        return compact('basePrice', 'price', 'rise', 'achievement', 'commission', 'num');
+    }
+
+    public function setPrice($day, $price, $commission, $achievement, $num)
+    {
+        $add_time = time();
+        return $this->dao->create(compact('day', 'price', 'commission', 'achievement', 'num', 'add_time'));
+    }
+}

+ 1 - 12
app/common/repositories/user/UserRepository.php

@@ -2673,17 +2673,6 @@ class UserRepository extends BaseRepository
 
     public function awardIntegralPrice()
     {
-        $basePrice = systemConfig('award_integral_price', 0.1);
-        //业绩
-        $achievement = 0;
-        /** @var UserExtractRepository $extractRepositories */
-        $extractRepositories = app()->make(UserExtractRepository::class);
-        $extractCommission = $extractRepositories->search(['status' => 1])->whereTime('check_time', 'today')->sum('commission');
-        $sumAwardIntegral = $this->dao->search(['status' => 1])->sum('award_integral');
-        $achievement = $extractCommission + $achievement;
-
-        $rise = bcdiv($achievement, $sumAwardIntegral, 3);
-        $price = bcadd($rise, $basePrice, 3);
-        return compact('basePrice', 'price', 'rise');
+        return app()->make(AwardIntegralPriceRepository::class)->awardIntegralPrice();
     }
 }

+ 8 - 1
crmeb/listens/AutoSavePriceListen.php

@@ -13,6 +13,7 @@
 
 namespace crmeb\listens;
 
+use app\common\repositories\user\AwardIntegralPriceRepository;
 use crmeb\interfaces\ListenerInterface;
 use crmeb\services\TimerService;
 
@@ -25,7 +26,13 @@ class AutoSavePriceListen extends TimerService implements ListenerInterface
         //TODO 自动清除积分
         $this->tick(1000 * 60 * 20, function () {
             //如果没有昨天的价格,就保存
-
+            $day = date('Y-m-d', strtotime('-1 day'));
+            $service = app()->make(AwardIntegralPriceRepository::class);
+            $yesterday = $service->awardIntegralPrice($day);
+            if (!isset($yesterday['basePrice'])) {
+                return;
+            }
+            $service->setPrice($day, $yesterday['price'], $yesterday['commission'], $yesterday['achievement'], $yesterday['num']);
         });
     }
 }