123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace app\models\system;
- use app\admin\model\system\SystemUserLevel;
- use app\models\store\StoreCart;
- use app\models\store\StoreOrder;
- use app\models\user\User;
- use app\models\user\UserBill;
- use crmeb\basic\BaseModel;
- use crmeb\traits\ModelTrait;
- /**
- * TODO 会员等级Model
- * Class UserLevel
- * @package app\models\user
- */
- class SystemAwardLevel extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'system_award_level';
- use ModelTrait;
- /**
- * 获取查询条件
- * @param $where
- * @param string $alert
- * @param null $model
- * @return SystemUserLevel|null
- */
- public static function setWhere($where, $alert = '', $model = null)
- {
- $model = $model === null ? new self() : $model;
- if ($alert) $model = $model->alias($alert);
- $alert = $alert ? $alert . '.' : '';
- $model = $model->where("{$alert}is_del", 0);
- if (isset($where['is_show']) && $where['is_show'] !== '') $model = $model->where("{$alert}is_show", $where['is_show']);
- if (isset($where['title']) && $where['title']) $model = $model->where("{$alert}name", 'LIKE', "%$where[title]%");
- return $model;
- }
- public static function getSystemList($where)
- {
- $data = self::setWhere($where)->order('id asc')->page((int)$where['page'], (int)$where['limit'])->select();
- $data = count($data) ? $data->toArray() : [];
- $count = self::setWhere($where)->count();
- return compact('data', 'count');
- }
- public static function checkLevel($uid)
- {
- $spread = User::where('uid', $uid)->find();
- // $spread = User::where('uid', $user['spread_uid'])->find();
- while ($spread) {
- //推荐全员
- // $uids = User::where('spread_uid', $spread['uid'])->column('uid');
- // $orders = StoreOrder::where('paid', 1)
- // ->where('status', 'in', [2, 3])
- // ->where('uid', 'in', $uids)
- // ->where('refund_status', 0)
- // ->where('is_del', 0)
- // ->where('is_system_del', 0)->select();
- // $num = 0;
- // foreach ($orders as $v) {
- // $num += StoreCart::where('id', 'in', $v['cart_id'])->where('is_suit', 1)->sum('cart_num');
- // }
- $level_info = self::where('id', $spread['award_level'])->find();
- $level = self::where('level_num', '>', $level_info['level_num'] ?? 0)->order('level_num', 'desc')->select();
- foreach ($level as $v) {
- if ($spread['recommend_suit_num'] >= $v['level_num']) {
- User::where('uid', $spread['uid'])->update(['award_level' => $v['id']]);
- self::sendBackOrderBrokerage($spread['uid'], $spread['group_level'] > $v['id'] ? $v['id'] : $spread['group_level']);
- break;
- }
- }
- $spread = User::where('uid', $spread['spread_uid'])->find();
- }
- return true;
- }
- public static function sendBackOrderBrokerage($uid, $level)
- {
- $list = UserBill::where('award_level', '<=', $level)->where(['type' => 'brokerage', 'category' => 'now_money', 'pm' => 1, 'status' => 0, 'uid' => $uid, 'title' => '津贴绿积分|极差奖'])->where('title','in',['津贴绿积分','极差奖'])->select();
- if ($list) {
- self::beginTrans();
- try {
- foreach ($list as $v) {
- User::bcInc($v['uid'], 'brokerage_price', $v['number'], 'uid');
- $balance = User::getUserInfo($v['uid'])['brokerage_price'];
- UserBill::where('id', $v['id'])->update(['add_time' => date('Y-m-d H:i:s'), 'status' => 1, 'balance' => $balance]);
- }
- self::commitTrans();
- return true;
- } catch (\Exception $e) {
- self::rollbackTrans();
- return false;
- }
- }
- return true;
- }
- }
|