|
@@ -2,7 +2,11 @@
|
|
|
|
|
|
|
|
namespace app\api\service;
|
|
namespace app\api\service;
|
|
|
|
|
|
|
|
|
|
+use app\api\model\user\Grade;
|
|
|
|
|
+use app\api\model\user\GradeLog;
|
|
|
|
|
+use app\common\model\dealer\Setting;
|
|
|
use think\Cache;
|
|
use think\Cache;
|
|
|
|
|
+use think\Db;
|
|
|
use think\Exception;
|
|
use think\Exception;
|
|
|
use app\api\model\User as UserModel;
|
|
use app\api\model\User as UserModel;
|
|
|
class User
|
|
class User
|
|
@@ -58,4 +62,154 @@ class User
|
|
|
return UserModel::where('user_id', $userId)->update(['pay_password'=>$payPassword,'update_time'=>time()]);
|
|
return UserModel::where('user_id', $userId)->update(['pay_password'=>$payPassword,'update_time'=>time()]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 用户升级
|
|
|
|
|
+ * @param $user_id
|
|
|
|
|
+ * @param int $wxapp_id
|
|
|
|
|
+ * @return bool
|
|
|
|
|
+ * @throws Exception
|
|
|
|
|
+ * @throws \think\db\exception\DataNotFoundException
|
|
|
|
|
+ * @throws \think\db\exception\ModelNotFoundException
|
|
|
|
|
+ * @throws \think\exception\DbException
|
|
|
|
|
+ */
|
|
|
|
|
+ public static function upgrade($user_id, $wxapp_id=0)
|
|
|
|
|
+ {
|
|
|
|
|
+ $info = UserModel::alias('a')
|
|
|
|
|
+ ->leftJoin('user_grade ug', 'ug.grade_id=a.grade_id')
|
|
|
|
|
+ ->where('user_id', $user_id)
|
|
|
|
|
+ ->field('a.user_id,a.grade_id,ug.name,a.expend_upgrade_money')
|
|
|
|
|
+ ->find();
|
|
|
|
|
+ if(empty($info)){
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $gradeId = isset($info['grade_id'])? intval($info['grade_id']) : 0;
|
|
|
|
|
+ $upgradeMoney = isset($info['expend_upgrade_money'])? floatval($info['expend_upgrade_money']) : 0;
|
|
|
|
|
+ if($upgradeMoney>0){
|
|
|
|
|
+ $gradeList = Grade::where(['status'=> 1, 'is_delete'=> 0,'wxapp_id'=>$wxapp_id])
|
|
|
|
|
+ ->where('grade_id','>', $gradeId)
|
|
|
|
|
+ ->field('grade_id,name,upgrade')
|
|
|
|
|
+ ->order('weight asc')
|
|
|
|
|
+ ->select();
|
|
|
|
|
+ $gradeList = $gradeList? $gradeList->toArray() : [];
|
|
|
|
|
+ if($gradeList){
|
|
|
|
|
+ $log = [];
|
|
|
|
|
+ $newGradeId = 0;
|
|
|
|
|
+ foreach ($gradeList as $v){
|
|
|
|
|
+ $id = isset($v['grade_id'])? $v['grade_id'] : 0;
|
|
|
|
|
+ $upgrade = isset($v['upgrade']) && $v['upgrade']? json_decode($v['upgrade'], true) : [];
|
|
|
|
|
+ $upgradeGradeMoney = isset($upgrade['expend_money'])? floatval($upgrade['expend_money']) : 0;
|
|
|
|
|
+ if($id>$gradeId && $upgradeGradeMoney<=$upgradeMoney){
|
|
|
|
|
+ $newGradeId = $id;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 处理
|
|
|
|
|
+ if($newGradeId>0){
|
|
|
|
|
+ Db::startTrans();
|
|
|
|
|
+ if(!UserModel::where(['user_id'=>$user_id])->update(['grade_id'=> $newGradeId,'update_time'=> time()])){
|
|
|
|
|
+ Db::rollback();
|
|
|
|
|
+ throw new Exception('升级处理失败');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $log = [
|
|
|
|
|
+ 'user_id'=> $user_id,
|
|
|
|
|
+ 'old_grade_id'=> $gradeId,
|
|
|
|
|
+ 'new_grade_id'=> $newGradeId,
|
|
|
|
|
+ 'change_type'=> 20,
|
|
|
|
|
+ 'wxapp_id'=> $wxapp_id,
|
|
|
|
|
+ 'remark'=> "升级成功",
|
|
|
|
|
+ 'create_time'=> time()
|
|
|
|
|
+ ];
|
|
|
|
|
+ if(!GradeLog::insertgetId($log)){
|
|
|
|
|
+ Db::rollback();
|
|
|
|
|
+ throw new Exception('升级处理失败');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // 推荐奖金
|
|
|
|
|
+ self::settleAward($user_id, $wxapp_id);
|
|
|
|
|
+
|
|
|
|
|
+ Db::commit();
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取用户信息
|
|
|
|
|
+ * @param $userId
|
|
|
|
|
+ * @param $wxapp_id
|
|
|
|
|
+ * @return mixed
|
|
|
|
|
+ */
|
|
|
|
|
+ public static function getUserInfo($userId, $wxapp_id)
|
|
|
|
|
+ {
|
|
|
|
|
+ if(empty($userId)){
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $model = new \app\common\model\dealer\User();
|
|
|
|
|
+ return $dealerUserInfo = $model::alias('a')
|
|
|
|
|
+ ->leftJoin('user u','u.user_id=a.user_id')
|
|
|
|
|
+ ->leftJoin('user_grade ug','ug.grade_id=u.grade_id')
|
|
|
|
|
+ ->where(['a.user_id'=>$userId,'a.wxapp_id'=>$wxapp_id,'a.is_delete'=>0])
|
|
|
|
|
+ ->field('a.user_id,a.money,a.referee_id,u.grade_id,ug.weight as level,ug.name as grade_name')
|
|
|
|
|
+ ->find();
|
|
|
|
|
+ }
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 结算推荐佣金
|
|
|
|
|
+ */
|
|
|
|
|
+ public static function settleAward($userId, $wxapp_id)
|
|
|
|
|
+ {
|
|
|
|
|
+ // 奖金配置参数
|
|
|
|
|
+ $config = Setting::getItem('commission', $wxapp_id);
|
|
|
|
|
+ if(empty($config)){
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $dealerUserInfo = self::getUserInfo($userId, $wxapp_id);
|
|
|
|
|
+ $userLevel = isset($dealerUserInfo['level'])? $dealerUserInfo['level'] : 0;
|
|
|
|
|
+ $firstId = isset($dealerUserInfo['referee_id'])? $dealerUserInfo['referee_id'] : 0;
|
|
|
|
|
+ if(empty($dealerUserInfo) || $firstId<=0){
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $firstInfo = self::getUserInfo($firstId, $wxapp_id);
|
|
|
|
|
+ $firstLevel = isset($firstInfo['level'])? $firstInfo['level'] : 0;
|
|
|
|
|
+ $firstLevelName = isset($firstInfo['grade_name'])? $firstInfo['grade_name'] : '';
|
|
|
|
|
+ $secondId = isset($firstInfo['referee_id'])? $firstInfo['referee_id'] : 0;
|
|
|
|
|
+
|
|
|
|
|
+ $secondInfo = self::getUserInfo($secondId, $wxapp_id);
|
|
|
|
|
+ $secondLevel = isset($secondInfo['level'])? $secondInfo['level'] : 0;
|
|
|
|
|
+ $secondLevelName = isset($secondInfo['grade_name'])? $secondInfo['grade_name'] : '';
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // 平级奖 (vip以及以上等级)
|
|
|
|
|
+ $model = new \app\common\model\dealer\User();
|
|
|
|
|
+ if($firstInfo && $userLevel>=1){
|
|
|
|
|
+ // 直推平级奖
|
|
|
|
|
+ $equalScore1 = isset($config['equal_score_1'])? floatval($config['equal_score_1']) : 0;
|
|
|
|
|
+ if($userLevel==$firstLevel && $equalScore1>0){
|
|
|
|
|
+ $model->grantMoney($firstId, $equalScore1, 2, "来自用户[{$userId}]直推[{$firstLevelName}]平级奖");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 间推平级奖
|
|
|
|
|
+ $equalScore2 = isset($config['equal_score_2'])? floatval($config['equal_score_2']) : 0;
|
|
|
|
|
+ if($secondInfo && $userLevel==$secondLevel && $equalScore2>0){
|
|
|
|
|
+ $model->grantMoney($secondId, $equalScore2, 2, "来自用户[{$userId}]间推[{$secondLevelName}]平级奖");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // 反推奖(上级的级别大于当前用户)
|
|
|
|
|
+ $reverseScore = isset($config['reverse_score_'.$firstLevel])? floatval($config['reverse_score_'.$firstLevel]) : 0;
|
|
|
|
|
+ if($firstLevel>$userLevel && $reverseScore>0){
|
|
|
|
|
+ $model->grantMoney($firstId, $reverseScore, 2, "来自用户[{$userId}]反推[{$firstLevelName}]奖");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|