| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- namespace app\common\model\user;
- use app\common\model\BaseModel;
- use app\common\repositories\user\AwardIntegralPriceRepository;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\Exception;
- class Awardlake extends BaseModel
- {
- public static function tablePk(): ?string
- {
- return 'id';
- }
- public static function tableName(): string
- {
- return 'award_integral_price';
- }
- /**
- * @param int $lake_type 奖池类型 1:节能油,2:礼包
- * @param float $order_price 订单价格
- * @param int $order_id 订单ID
- * @return true
- * @throws Exception
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function addOrderLakes($lake_type, $order_price, $order_id)
- {
- switch ($lake_type) {
- case 1:
- $model = new OilLevel();
- break;
- case 2:
- $model = new GiftLevel();
- break;
- default :
- throw new Exception('奖池类型错误');
- }
- $levels = $model->select();
- foreach ($levels as $level) {
- $lake = self::where('level', $level['id'])->where('type', $lake_type)
- ->find();
- if (!$lake) $lake = self::create([
- 'level' => $level['id'],
- 'type' => $lake_type,
- 'award_name' => $level['name'] . '分红',
- 'num' => 0,
- 'send_time' => time()
- ]);
- $award = bcdiv(bcmul((string)$order_price, (string)$level['award_ratio']), '100', 2);
- AwardLakeLog::income($lake['id'], 'order_add', $award, $order_id, '用户购买商品添加分红池');
- }
- return true;
- }
- public function autoSend()
- {
- $time = strtotime(date('Y-m-d 01:00:00'));
- if (time() < $time) {
- return true;
- }
- $lakes = self::where('send_time', '<', $time)->select();
- foreach ($lakes as $lake) {
- $this->sendAward($lake['id']);
- }
- }
- public function sendAward($lake_id)
- {
- $lake = self::where('id', $lake_id)->find();
- if (!$lake) return true;
- if ($lake['num'] <= 0) {
- self::where('id', $lake['id'])->update(['send_time' => time()]);
- return true;
- }
- switch ($lake['type']) {
- case 1:
- $model = new OilLevel();
- $field = 'oil_level';
- break;
- case 2:
- $model = new GiftLevel();
- $field = 'gift_level';
- break;
- default :
- $model = new OilLevel();
- $field = 'oil_level';
- }
- $level = $model->where('id', $lake['level'])->find();
- $levels = $model->where('grade', '>=', $level['grade'])->column('id');
- $users = User::where($field, 'in', $levels)->where('status', 1)->select();
- $every = bcdiv($lake['num'], count($users), 2);
- if ($every <= 0) {
- self::where('id', $lake['id'])->update(['send_time' => time()]);
- return true;
- }
- $real_send = 0;
- $price = app()->make(AwardIntegralPriceRepository::class)->awardIntegralPrice();
- foreach ($users as $user) {
- //todo 添加用户积分
- //todo end
- $real_send = bcadd($real_send, $every, 2);
- }
- AwardLakeLog::expend($lake['id'], 'send', $real_send, 0, '奖池分红');
- self::where('id', $lake['id'])->update(['send_time' => time()]);
- }
- }
|