CouponController.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\admin\controller\coupon;
  3. use app\admin\validate\coupon\CouponValidate;
  4. use app\admin\validate\device\DeviceValidate;
  5. use app\controller\Curd;
  6. use app\model\Coupon;
  7. use app\model\Device;
  8. use app\model\SysSerial;
  9. use support\exception\BusinessException;
  10. use support\Request;
  11. use support\Response;
  12. class CouponController extends Curd
  13. {
  14. public function __construct()
  15. {
  16. $this->model = new Coupon();
  17. $this->validate = true;
  18. $this->validateClass = new CouponValidate();
  19. }
  20. /**
  21. * @Desc 列表
  22. * @Author Gorden
  23. * @Date 2024/3/28 15:01
  24. *
  25. * @param Request $request
  26. * @return Response
  27. * @throws \support\exception\BusinessException
  28. */
  29. public function select(Request $request): Response
  30. {
  31. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  32. $order = $request->get('order', 'desc');
  33. $field = $field ?? 'coupon_addtimes';
  34. $query = $this->doSelect($where, $field, $order);
  35. return $this->doFormat($query, $format, $limit);
  36. }
  37. public function selectList()
  38. {
  39. $coupons = Coupon::where('coupon_status','ACTIVED')
  40. ->select('coupon_id','coupon_name')
  41. ->get()
  42. ->toArray();
  43. return json_success('',$coupons);
  44. }
  45. // public function afterQuery($items)
  46. // {
  47. // foreach ($items as &$item) {
  48. // $item->coupon_begindate = date('Y-m-d H:i:s', $item->coupon_begindate);
  49. // $item->coupon_enddate = date('Y-m-d H:i:s', $item->coupon_enddate);
  50. // }
  51. //
  52. // return $items;
  53. // }
  54. protected function insertInput(Request $request): array
  55. {
  56. $data = $this->inputFilter($request->post());
  57. $data['coupon_id'] = "CO" . str_pad(SysSerial::getSerial(), 16, '0') . random_string(8, 'up');
  58. $data['coupon_begindate'] = date("Y-m-d H:i:s", strtotime($data['coupon_begindate']));
  59. $data['coupon_enddate'] = date("Y-m-d H:i:s", strtotime($data['coupon_enddate']));
  60. return $data;
  61. }
  62. protected function updateInput(Request $request): array
  63. {
  64. $primary_key = $this->model->getKeyName();
  65. $id = $request->post($primary_key);
  66. $data = $this->inputFilter($request->post());
  67. $data['coupon_begindate'] = date("Y-m-d H:i:s", strtotime($data['coupon_begindate']));
  68. $data['coupon_enddate'] = date("Y-m-d H:i:s", strtotime($data['coupon_enddate']));
  69. $model = $this->model->find($id);
  70. if (!$model) {
  71. throw new BusinessException('记录不存在', 2);
  72. }
  73. unset($data[$primary_key]);
  74. return [$id, $data];
  75. }
  76. }