RulePricingController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. $item->rulr_priceing_nbr = $item->rulr_priceing_nbr * 100;
  42. }
  43. return $items;
  44. }
  45. protected function insertInput(Request $request): array
  46. {
  47. $data = $this->inputFilter($request->post());
  48. $data['rule_pricing_id'] = "RP" . date("ymdHi") . random_string(4, 'up');
  49. $data['rulr_priceing_nbr'] = $data['rulr_priceing_nbr'] / 100;
  50. // if (!empty($data['rule_pricing_goods_json'])) {
  51. // $data['rule_pricing_goods_json'] = json_encode(explode(',', $data['rule_pricing_goods_json']));
  52. // } else {
  53. // $data['rule_pricing_goods_json'] = '[]';
  54. // }
  55. return $data;
  56. }
  57. protected function updateInput(Request $request): array
  58. {
  59. $primary_key = $this->model->getKeyName();
  60. $id = $request->post($primary_key);
  61. $data = $this->inputFilter($request->post());
  62. $data['rulr_priceing_nbr'] = $data['rulr_priceing_nbr'] / 100;
  63. $model = $this->model->find($id);
  64. if (!$model) {
  65. throw new BusinessException('记录不存在', 2);
  66. }
  67. unset($data[$primary_key]);
  68. return [$id, $data];
  69. }
  70. }