| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- namespace app\common\model\user;
- use app\common\model\BaseModel;
- use app\common\repositories\user\AwardIntegralPriceRepository;
- use app\common\repositories\user\UserBillRepository;
- use app\common\repositories\user\UserRepository;
- 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_lake';
- }
- /**
- * 奖池累加
- * @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;
- }
- //30秒执行一次
- 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']);
- }
- return true;
- }
- public function handSend()
- {
- // $time = strtotime(date('Y-m-d 01:00:00'));
- // if (time() < $time) {
- // return true;
- // }
- $time= time();
- $lakes = self::where('send_time', '<', $time)->select();
- // return $lakes;
- foreach ($lakes as $lake) {
- $this->sendAward($lake['id']);
- }
- return '发送成功';
- }
- public function sendAward($lake_id)
- {
- try{
- $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';
- $name = '节能油';
- $title = '节能油积分分红';
- break;
- case 2:
- $model = new GiftLevel();
- $field = 'gift_level';
- $name = '礼包';
- $title = '礼包积分分红';
- break;
- default :
- $model = new OilLevel();
- $field = 'oil_level';
- $name = '节能油';
- $title = '节能油积分分红';
- }
- $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();
- if (count($users) <= 0){
- return true;
- }
- @file_put_contents('quanju.txt',json_encode($users)."-参加分红的人员\r\n",8);
- @file_put_contents('quanju.txt',$lake_id."-分红奖池id\r\n",8);
- $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(); //分红积分价格
- @file_put_contents('quanju.txt',json_encode($price)."-分红积分价格\r\n",8);
- @file_put_contents('quanju.txt',$every."-每个用户分得的金额\r\n",8);
- foreach ($users as $user) {
- @file_put_contents('quanju.txt',$user['uid']."-参与分红的uid\r\n",8);
- //todo 添加用户积分 用户分的奖励除以积分价格就是积分数量
- $integral = bcdiv($every, $price['price'], 2);
- $award_integral=User::where('uid', $user['uid'])->value('award_integral');
- // if ($award_integral>0){
- $after = bcadd($award_integral, $integral, 2);
- // 创建用户账单仓库实例
- $make = app()->make(UserBillRepository::class);
- if ($integral>0){
- $make->incBill($user['uid'], 'award_integral', 'oil_integral', [
- 'number' => $integral,
- 'title' => $title,
- 'balance' => $after,
- 'status' => 1,
- 'link_id' => 0,
- 'mark' => $user['nickname'] . '获得' .$name.'分红积分'.$integral ,
- ]);
- }
- User::where('uid', $user['uid'])->update(['award_integral' => $after]);
- // }
- //todo end
- $real_send = bcadd($real_send, $every, 2);
- $UserRepository = app()->make(UserRepository::class);
- $UserRepository->checkAward($user['uid']);
- }
- AwardLakeLog::expend($lake['id'], 'send', $real_send, 0, '奖池分红');
- self::where('id', $lake['id'])->update(['send_time' => time()]);
- }catch (Exception $e){
- @file_put_contents('quanju.txt',$e->getMessage()."-错误信息\r\n",8);
- @file_put_contents('quanju.txt',$e->getLine()."-错误位置\r\n",8);
- }
- }
- }
|