1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace app\admin\controller\coupon;
- use app\admin\validate\coupon\CouponValidate;
- use app\admin\validate\device\DeviceValidate;
- use app\controller\Curd;
- use app\model\Coupon;
- use app\model\Device;
- use app\model\SysSerial;
- use support\exception\BusinessException;
- use support\Request;
- use support\Response;
- class CouponController extends Curd
- {
- public function __construct()
- {
- $this->model = new Coupon();
- $this->validate = true;
- $this->validateClass = new CouponValidate();
- }
- /**
- * @Desc 列表
- * @Author Gorden
- * @Date 2024/3/28 15:01
- *
- * @param Request $request
- * @return Response
- * @throws \support\exception\BusinessException
- */
- public function select(Request $request): Response
- {
- [$where, $format, $limit, $field, $order] = $this->selectInput($request);
- $order = $request->get('order', 'desc');
- $field = $field ?? 'coupon_addtimes';
- $query = $this->doSelect($where, $field, $order);
- return $this->doFormat($query, $format, $limit);
- }
- public function selectList()
- {
- $coupons = Coupon::where('coupon_status','ACTIVED')
- ->select('coupon_id','coupon_name')
- ->get()
- ->toArray();
- return json_success('',$coupons);
- }
- // public function afterQuery($items)
- // {
- // foreach ($items as &$item) {
- // $item->coupon_begindate = date('Y-m-d H:i:s', $item->coupon_begindate);
- // $item->coupon_enddate = date('Y-m-d H:i:s', $item->coupon_enddate);
- // }
- //
- // return $items;
- // }
- protected function insertInput(Request $request): array
- {
- $data = $this->inputFilter($request->post());
- $data['coupon_id'] = "CO" . str_pad(SysSerial::getSerial(), 16, '0') . random_string(8, 'up');
- $data['coupon_begindate'] = date("Y-m-d H:i:s", strtotime($data['coupon_begindate']));
- $data['coupon_enddate'] = date("Y-m-d H:i:s", strtotime($data['coupon_enddate']));
- return $data;
- }
- protected function updateInput(Request $request): array
- {
- $primary_key = $this->model->getKeyName();
- $id = $request->post($primary_key);
- $data = $this->inputFilter($request->post());
- $data['coupon_begindate'] = date("Y-m-d H:i:s", strtotime($data['coupon_begindate']));
- $data['coupon_enddate'] = date("Y-m-d H:i:s", strtotime($data['coupon_enddate']));
- $model = $this->model->find($id);
- if (!$model) {
- throw new BusinessException('记录不存在', 2);
- }
- unset($data[$primary_key]);
- return [$id, $data];
- }
- }
|