MotorAgent.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace app\admin\model\users;
  3. use app\agent\model\auth\Access;
  4. use app\agent\model\auth\Permission;
  5. use app\agent\model\auth\PermissionRole;
  6. use app\agent\model\auth\Role;
  7. use app\common\model\TaxiOrder;
  8. use think\Model;
  9. class MotorAgent extends Model
  10. {
  11. protected $name = 'users_motor_agent';
  12. protected $append = [
  13. 'status_text'
  14. ];
  15. public function getStatusTextAttr($value, $data)
  16. {
  17. if (isset($data['status']) && $data) {
  18. $status = [10=>'待审核',20=>'已拒绝',30=>'已通过',40=>'已缴费',50=>'已锁定'];
  19. return $status[$data['status']];
  20. }
  21. return '待审核';
  22. }
  23. public function user(): \think\model\relation\HasOne
  24. {
  25. return $this->hasOne('\app\common\model\Users', 'id', 'user_id')
  26. ->field('id,nickname,partnership');
  27. }
  28. /**
  29. * @desc 摩的代理统计数据
  30. * @return array
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. * @throws \think\exception\DbException
  34. */
  35. public function stats()
  36. {
  37. $user = $this->where('status', '>', 20)
  38. ->select();
  39. $agent_num = count($user);
  40. $total_num = 0;
  41. $total_amount = 0;
  42. if ($user) {
  43. $area_ids = [];
  44. foreach ($user as $item) {
  45. $area_ids[] = $item->area_id;
  46. }
  47. $TaxiOrder = new TaxiOrder();
  48. $taxi_order = $TaxiOrder->whereIn('area_id', $area_ids)->whereNotIn('status', [1])->column('price', 'id');
  49. $total_num += count($taxi_order);
  50. $total_amount += array_sum($taxi_order);
  51. }
  52. return [
  53. 'agent_num' => $agent_num,
  54. 'total_num' => $total_num,
  55. 'total_amount' => $total_amount,
  56. ];
  57. }
  58. public function authority($RoleId, $roleName)
  59. {
  60. $Role = new Role();
  61. $Access = new Access();
  62. $Permission = new Permission();
  63. $PermissionRole = new PermissionRole();
  64. // 获取角色信息
  65. $role = $Role::get($RoleId);
  66. if ($role){
  67. $admin = $PermissionRole::where('role_id', 2)->find();
  68. $PermissionRole::create([
  69. 'role_id' => $RoleId,
  70. 'permission_idx' => $admin['permission_idx']
  71. ]);
  72. $access = $Access->where('v0', 'motor_agent_173')
  73. ->select();
  74. foreach ($access as $item) {
  75. $item->v0 = $roleName;
  76. $item->hidden = ['id'];
  77. }
  78. $Access->allowField(true)->saveAll($access->toArray());
  79. }
  80. }
  81. }