RuleAddedController.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace app\admin\controller\member;
  3. use app\admin\validate\member\RuleAddedValidate;
  4. use app\admin\validate\member\RulePricingValidate;
  5. use app\controller\Curd;
  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 RuleAddedController extends Curd
  13. {
  14. public function __construct()
  15. {
  16. $this->model = new RuleAdded();
  17. $this->validate = true;
  18. $this->validateClass = new RuleAddedValidate();
  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 ?? 'rule_added_addtimes';
  25. $query = $this->doSelect($where, $field, $order);
  26. return $this->doFormat($query, $format, $limit);
  27. }
  28. public function selectList(Request $request)
  29. {
  30. $data = $this->model->where('rule_added_status', 'ACTIVED')
  31. ->select('rule_added_id', 'rule_added_name')
  32. ->get()
  33. ->toArray();
  34. return json_success('', $data);
  35. }
  36. protected function insertInput(Request $request): array
  37. {
  38. $data = $this->inputFilter($request->post());
  39. $data['rule_added_id'] = "RA" . str_pad(SysSerial::getSerial(), 16, '0', STR_PAD_LEFT) . random_string(6, 'up');
  40. return $data;
  41. }
  42. protected function updateInput(Request $request): array
  43. {
  44. $primary_key = $this->model->getKeyName();
  45. $id = $request->post($primary_key);
  46. $data = $this->inputFilter($request->post());
  47. if (!empty($data['rule_pricing_goods_json'])) {
  48. $data['rule_pricing_goods_json'] = json_encode(explode(',', $data['rule_pricing_goods_json']));
  49. } else {
  50. $data['rule_pricing_goods_json'] = '[]';
  51. }
  52. $model = $this->model->find($id);
  53. if (!$model) {
  54. throw new BusinessException('记录不存在', 2);
  55. }
  56. unset($data[$primary_key]);
  57. return [$id, $data];
  58. }
  59. }