RulePricingController.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. protected function afterQuery($items)
  27. {
  28. foreach ($items as &$item) {
  29. if (!empty($item->rule_pricing_goods_json)){
  30. $goodsJson = json_decode($item->rule_pricing_goods_json,true);
  31. $item->rule_pricing_goods_json = $goodsJson;
  32. }
  33. }
  34. return $items;
  35. }
  36. protected function insertInput(Request $request): array
  37. {
  38. $data = $this->inputFilter($request->post());
  39. $data['rule_pricing_id'] = "RP" . str_pad(SysSerial::getSerial(), 16, '0', STR_PAD_LEFT) . random_string(6, 'up');
  40. if (!empty($data['rule_pricing_goods_json'])) {
  41. $data['rule_pricing_goods_json'] = json_encode(explode(',', $data['rule_pricing_goods_json']));
  42. }else{
  43. $data['rule_pricing_goods_json'] = '[]';
  44. }
  45. return $data;
  46. }
  47. protected function updateInput(Request $request): array
  48. {
  49. $primary_key = $this->model->getKeyName();
  50. $id = $request->post($primary_key);
  51. $data = $this->inputFilter($request->post());
  52. if (!empty($data['rule_pricing_goods_json'])) {
  53. $data['rule_pricing_goods_json'] = json_encode(explode(',', $data['rule_pricing_goods_json']));
  54. }else{
  55. $data['rule_pricing_goods_json'] = '[]';
  56. }
  57. $model = $this->model->find($id);
  58. if (!$model) {
  59. throw new BusinessException('记录不存在', 2);
  60. }
  61. unset($data[$primary_key]);
  62. return [$id, $data];
  63. }
  64. }