RulePricingController.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace app\admin\controller\member;
  3. use app\admin\validate\member\RulePricingValidate;
  4. use app\controller\Curd;
  5. use app\model\RulePricing;
  6. use app\model\SysSerial;
  7. use support\exception\BusinessException;
  8. use support\Request;
  9. use support\Response;
  10. class RulePricingController extends Curd
  11. {
  12. public function __construct()
  13. {
  14. $this->model = new RulePricing();
  15. $this->validate = true;
  16. $this->validateClass = new RulePricingValidate();
  17. }
  18. public function select(Request $request): Response
  19. {
  20. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  21. $order = $request->get('order', 'desc');
  22. $field = $field ?? 'rule_pricing_addtimes';
  23. $query = $this->doSelect($where, $field, $order);
  24. return $this->doFormat($query, $format, $limit);
  25. }
  26. public function selectList(Request $request)
  27. {
  28. $data = $this->model->where('rule_pricing_status', 'ACTIVED')
  29. ->select('rule_pricing_id', 'rule_pricing_name')
  30. ->get()
  31. ->toArray();
  32. return json_success('', $data);
  33. }
  34. protected function afterQuery($items)
  35. {
  36. foreach ($items as &$item) {
  37. if (!empty($item->rule_pricing_goods_json)) {
  38. $goodsJson = json_decode($item->rule_pricing_goods_json, true);
  39. $item->rule_pricing_goods_json = $goodsJson;
  40. }
  41. }
  42. return $items;
  43. }
  44. protected function insertInput(Request $request): array
  45. {
  46. $data = $this->inputFilter($request->post());
  47. $data['rule_pricing_id'] = "RP" . str_pad(SysSerial::getSerial(), 16, '0', STR_PAD_LEFT) . random_string(6, 'up');
  48. if (!empty($data['rule_pricing_goods_json'])) {
  49. $data['rule_pricing_goods_json'] = json_encode(explode(',', $data['rule_pricing_goods_json']));
  50. } else {
  51. $data['rule_pricing_goods_json'] = '[]';
  52. }
  53. return $data;
  54. }
  55. protected function updateInput(Request $request): array
  56. {
  57. $primary_key = $this->model->getKeyName();
  58. $id = $request->post($primary_key);
  59. $data = $this->inputFilter($request->post());
  60. if (!empty($data['rule_pricing_goods_json'])) {
  61. $data['rule_pricing_goods_json'] = json_encode(explode(',', $data['rule_pricing_goods_json']));
  62. } else {
  63. $data['rule_pricing_goods_json'] = '[]';
  64. }
  65. $model = $this->model->find($id);
  66. if (!$model) {
  67. throw new BusinessException('记录不存在', 2);
  68. }
  69. unset($data[$primary_key]);
  70. return [$id, $data];
  71. }
  72. }