Subordinate.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace App\Models;
  3. use App\Models\AccountsInfo;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\DB;
  6. use Exception;
  7. use Log;
  8. use Illuminate\Support\Carbon;
  9. class Subordinate extends Model
  10. {
  11. const TABLE = 'QPPlatformDB.dbo.Subordinate';
  12. protected $table = self::TABLE;
  13. protected $primaryKey = 'UserID';
  14. public $timestamps = false;
  15. protected static function setCommission($params)
  16. {
  17. $user_id = $params['user_id'];//被设置用户
  18. $spreader_id = $params['spreader_id'];//上级用户
  19. $commission = (int)$params['commission'];//设置的点位数
  20. if($commission > 100){
  21. return ['status'=>false,'msg'=>'设置失败,比例不能大于100'];
  22. }
  23. $user_info = AccountsInfo::select('UserID','SpreaderID')->find($user_id);//被设置用户信息
  24. if(empty($user_info)){
  25. return ['status'=>false,'msg'=>'当前用户不存在'];
  26. }
  27. if($spreader_id != 0){
  28. $spreader_info = self::where('UserID',$spreader_id)->first();//上级代理点位数
  29. if($commission > $spreader_info->Commission){
  30. return ['status'=>false,'msg'=>'设置失败,比例不能大于上级'];
  31. }
  32. }
  33. $subordinate = self::where('SpreaderID',$user_id)->value('Commission');//下级代理点数
  34. if($subordinate > $commission){
  35. return ['status'=>false,'msg'=>'设置失败,比例不能小于下级'];
  36. }
  37. $user_commission = self::where('UserID',$user_id)->value('Commission');
  38. //判断是否设置过点位
  39. if(!empty($user_commission)){
  40. try{
  41. $result = self::where('UserID',$user_id)->update(['Commission'=>$commission]);
  42. return ['status'=>true,'msg'=>'设置成功'];
  43. }catch(Exception $e){
  44. Log::channel('setCommission')->info($e->getMessage());
  45. return ['status'=>false,'msg'=>'设置失败'];
  46. }
  47. }
  48. $insert['UserID'] = $user_id;
  49. $insert['SpreaderID'] = $spreader_id;
  50. $insert['Commission'] = $commission;
  51. $insert['CreateAt'] = now();
  52. $insert['Level'] = $spreader_info->Level+1;
  53. try{
  54. $result = self::insert($insert);
  55. return ['status'=>true,'msg'=>'设置成功'];
  56. }catch(Exception $e){
  57. Log::channel('setCommission')->info($e->getMessage());
  58. return ['status'=>false,'msg'=>'设置失败'];
  59. }
  60. }
  61. }