RoleController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace app\admin\controller\member;
  3. use app\admin\validate\member\RoleValidate;
  4. use app\controller\Curd;
  5. use app\model\MemberRole;
  6. use app\model\RuleAdded;
  7. use app\model\RulePricing;
  8. use app\model\SysSerial;
  9. use support\exception\BusinessException;
  10. use support\Request;
  11. use support\Response;
  12. class RoleController extends Curd
  13. {
  14. public function __construct()
  15. {
  16. $this->model = new MemberRole();
  17. $this->validate = true;
  18. $this->validateClass = new RoleValidate();
  19. }
  20. public function select(Request $request): Response
  21. {
  22. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  23. $order = $request->get('order', 'desc');
  24. $field = $field ?? 'member_role_addtimes';
  25. $query = $this->doSelect($where, $field, $order);
  26. return $this->doFormat($query, $format, $limit);
  27. }
  28. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  29. {
  30. $model = $this->model->with([
  31. 'category',
  32. 'rulePricing'
  33. ]);
  34. foreach ($where as $column => $value) {
  35. if (is_array($value)) {
  36. if ($value[0] === 'like' || $value[0] === 'not like') {
  37. $model = $model->where($column, $value[0], "%$value[1]%");
  38. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  39. $model = $model->where($column, $value[0], $value[1]);
  40. } elseif ($value[0] == 'in' && !empty($value[1])) {
  41. $valArr = $value[1];
  42. if (is_string($value[1])) {
  43. $valArr = explode(",", trim($value[1]));
  44. }
  45. $model = $model->whereIn($column, $valArr);
  46. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  47. $valArr = $value[1];
  48. if (is_string($value[1])) {
  49. $valArr = explode(",", trim($value[1]));
  50. }
  51. $model = $model->whereNotIn($column, $valArr);
  52. } elseif ($value[0] == 'null') {
  53. $model = $model->whereNull($column);
  54. } elseif ($value[0] == 'not null') {
  55. $model = $model->whereNotNull($column);
  56. } elseif ($value[0] !== '' || $value[1] !== '') {
  57. $model = $model->whereBetween($column, $value);
  58. }
  59. } else {
  60. $model = $model->where($column, $value);
  61. }
  62. }
  63. if ($field) {
  64. $model = $model->orderBy($field, $order);
  65. }
  66. return $model;
  67. }
  68. protected function afterQuery($items)
  69. {
  70. foreach ($items as &$item) {
  71. if (!empty($item->member_role_rule_json)) {
  72. $ruleJson = json_decode($item->member_role_rule_json, true);
  73. if (!empty($ruleJson['pricing'])) {
  74. $item->join_member_role_rule_pricing_id = $ruleJson['pricing'];
  75. $item->pricingName = RulePricing::where('rule_pricing_id', $ruleJson['pricing'])->value('rule_pricing_name');
  76. }
  77. if (!empty($ruleJson['added'])) {
  78. $item->join_member_role_added_id = $ruleJson['added'];
  79. $item->addedName = RuleAdded::where('rule_added_id', $ruleJson['added'])->value('rule_added_name');
  80. }
  81. }
  82. }
  83. return $items;
  84. }
  85. protected function insertInput(Request $request): array
  86. {
  87. $data = $this->inputFilter($request->post());
  88. $data['member_role_id'] = "MR" . date('ymdHi') . random_string(4, 'up');
  89. $data['member_role_rule_json'] = json_encode([
  90. 'pricing' => $request->post('join_member_role_rule_pricing_id',''),
  91. 'added' => $request->post('join_member_role_added_id',''),
  92. ]);
  93. return $data;
  94. }
  95. protected function updateInput(Request $request): array
  96. {
  97. $params = $request->post();
  98. $primary_key = $this->model->getKeyName();
  99. $id = $request->post($primary_key);
  100. $data = $this->inputFilter($params);
  101. $model = $this->model->find($id);
  102. if (!$model) {
  103. throw new BusinessException('记录不存在', 2);
  104. }
  105. if (!empty($model->member_role_rule_json)) {
  106. $ruleJson = json_decode($model->member_role_rule_json, true);
  107. if (!empty($params['join_member_role_rule_pricing_id'])) {
  108. $ruleJson['pricing'] = $params['join_member_role_rule_pricing_id'];
  109. }else{
  110. $ruleJson['pricing'] = '';
  111. }
  112. if (!empty($params['join_member_role_added_id'])) {
  113. $ruleJson['added'] = $params['join_member_role_added_id'];
  114. }else{
  115. $ruleJson['added'] = '';
  116. }
  117. $data['member_role_rule_json'] = json_encode($ruleJson);
  118. } else {
  119. $data['member_role_rule_json'] = json_encode([
  120. 'pricing' => $params['join_member_role_rule_pricing_id'] ?? '',
  121. 'added' => $params['join_member_role_added_id'] ?? ''
  122. ]);
  123. }
  124. unset($data[$primary_key]);
  125. return [$id, $data];
  126. }
  127. public function selectList()
  128. {
  129. $roles = MemberRole::select('member_role_id','member_role_name')
  130. ->orderBy('member_role_sort','desc')
  131. ->get();
  132. return json_success('',$roles);
  133. }
  134. }