RoleController.php 5.5 KB

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