Agent.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 代理管理
  7. */
  8. namespace app\fastshop\model;
  9. use think\Model;
  10. class Agent extends Model{
  11. protected $pk = 'id';
  12. protected $table = 'ai_fastshop_agent';
  13. /**
  14. * 代理列表
  15. */
  16. public function lists($condition){
  17. return self::view('fastshop_agent','user_id,rebate,id as agent_id')
  18. ->view('system_user','*','fastshop_agent.user_id = system_user.id')->order('fastshop_agent.id desc')
  19. ->where($condition)
  20. ->paginate(20);
  21. }
  22. /**
  23. * 选择代理用户类别
  24. */
  25. public function selects($condition){
  26. $user = self::where(['member_miniapp_id' => $condition['member_miniapp_id']])->field('user_id')->select()->toArray();
  27. $user_id = [];
  28. if(!empty($user)){
  29. $user_id = array_column($user,'user_id');
  30. }
  31. return model('SystemUser')->where($condition)->whereNotIn('id',$user_id)->order('id desc')->paginate(10,false);
  32. }
  33. /**
  34. * 读取代理用户ID(API使用)
  35. */
  36. public function agentUid(array $condition,$miniapp_id){
  37. $level = model('SystemUserLevel')->where($condition)->field('parent_id,level,user_id')->select()->toArray();
  38. if(empty($level)){
  39. return;
  40. }
  41. $uid = array_column($level,'parent_id');
  42. $agent = model('Agent')->where(['member_miniapp_id' => $miniapp_id])->whereIn('user_id',$uid)->select()->toArray();
  43. if(empty($agent)){
  44. return;
  45. }
  46. $agent_uid = [];
  47. foreach ($agent as $key => $value) {
  48. $agent_uid[$key]['user_id'] = $value['user_id'];
  49. $agent_uid[$key]['rebate'] = $value['rebate'];
  50. }
  51. rsort($agent_uid);
  52. return empty($agent_uid[0]) ? false : $agent_uid[0];
  53. }
  54. /**
  55. * 添加编辑
  56. * @param array $param 数组
  57. */
  58. public function add(int $miniapp_id,array $ids){
  59. $data = [];
  60. $user = self::where(['member_miniapp_id' => $miniapp_id,'user_id' => $ids])->field('user_id')->select()->toArray();
  61. $user_id = [];
  62. if(!empty($user)){
  63. $user_id = array_column($user,'user_id');
  64. }
  65. foreach ($ids as $key => $value) {
  66. if(!in_array($value,$user_id)){
  67. $data[$key]['member_miniapp_id'] = $miniapp_id;
  68. $data[$key]['user_id'] = $value;
  69. $data[$key]['rebate'] = 0;
  70. }
  71. }
  72. if(empty($data)){
  73. return;
  74. }
  75. return self::insertAll($data);
  76. }
  77. }