123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- use think\Exception;
- class PreOrder extends Api
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = '*';
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = Db::name('pre_order');
- }
- /**
- * 列表
- * @return void
- */
- public function getList(){
- try {
- $preOrderStatus = input('pre_order_status','');
- $where['user_id'] = $this->auth->id;
- if ($preOrderStatus != '') {
- $where['pre_order_status'] = $preOrderStatus;
- }
- $po = 'pre_order';
- $st = 'servicetype';
- $field = $po.'.id,name,mobile,car_number,pre_time,order_time,cancel_time,'.$st.'.title,pre_order_status';
- $result = $this->model->alias($po)->field($field)
- ->join($st,$st.'.id = '.$po.'.servicetype_id','LEFT')
- ->where($where)->order($po.'.createtime desc')->autopage()->select();
- if (!empty($result)) {
- $model = model('PreOrder');
- $statusArr = $model->getPreOrderStatusList();
- $timeArr = ['pre_time','order_time','cancel_time'];
- foreach ($result as $key => &$value) {
- foreach ($timeArr as $k => $v) {
- $value[$v] = !empty($value[$v]) ? date('Y年m月d日 H:i:s', $value[$v]) : '';
- }
- $value['pre_order_status_text'] = isset($statusArr[$value['pre_order_status']]) ? $statusArr[$value['pre_order_status']] : '';
- }
- }
- $this->success('获取成功', $result);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- }
- /**
- * 保存
- * @return void
- */
- public function save()
- {
- try {
- //验证参数
- $id = $this->request->param('id',0);
- $carId = $this->request->param('car_id',0);
- $preTime = $this->request->param('pre_time','');
- $userId = $this->auth->id;
- $scene = !empty($id) ? 'edit' : 'add';
- $validate = validate('PreOrder');
- if(!$validate->check($this->request->param(),[],$scene)){
- throw new Exception($validate->getError());
- }
- $preTime = strtotime($preTime);
- //获取车辆信息
- $userCarWhere['user_id'] = $userId;
- $userCarWhere['id'] = $carId;
- $userCar = Db::name('user_car')->where($userCarWhere)->find();
- if (empty($userCar)) {
- throw new Exception('未找到车辆信息');
- }
- $time = time();
- $data = [
- 'company_id' => $this->request->param('company_id', 0),
- 'name' => $this->request->param('name', ''),
- 'mobile' => $this->request->param('mobile', ''),
- 'address' => $this->request->param('address', ''),
- 'remark' => $this->request->param('remark', ''),
- 'car_id' => $carId,
- 'car_number' => isset($userCar['car_number']) ? $userCar['car_number'] : '',
- 'servicetype_id' => $this->request->param('servicetype_id', 0),
- 'pre_time' => $preTime,
- ];
- if (empty($id)) {
- $data['user_id'] = $userId;
- $data['createtime'] = $time;
- $res = $this->model->insertGetId($data);
- } else {
- $data['updatetime'] = $time;
- $where['id'] = $id;
- $where['user_id'] = $userId;
- $res = $this->model->where($where)->update($data);
- }
- if (!$res) {
- throw new Exception('操作失败');
- }
- $this->success('操作成功');
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- }
- /**
- * 详情
- * @return void
- */
- public function getInfo()
- {
- try {
- $id = $this->request->param('id',0);
- $po = 'pre_order';
- $st = 'servicetype';
- $where[$po.'.user_id'] = $this->auth->id;
- $where[$po.'.id'] = $id;
- $field = $po.'.id,name,mobile,address,remark,car_number,pre_time,order_time,cancel_time,cancel_reason,'.
- $po.'.createtime,'.$st.'.title,pre_order_status';
- $result = $this->model->alias($po)->field($field)
- ->join($st,$st.'.id = '.$po.'.servicetype_id','LEFT')
- ->where($where)->order($po.'.createtime desc')->find();
- if (!empty($result)) {
- $model = model('PreOrder');
- $statusArr = $model->getPreOrderStatusList();
- $timeArr = ['pre_time','order_time','cancel_time','createtime'];
- foreach ($timeArr as $k => $v) {
- $result[$v] = !empty($result[$v]) ? date('Y年m月d日 H:i:s', $result[$v]) : '';
- }
- $result['pre_order_status_text'] = isset($statusArr[$result['pre_order_status']]) ? $statusArr[$result['pre_order_status']] : '';
- }
- $this->success('获取成功',$result);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- }
- /**
- * 取消
- * @return void
- */
- public function cancel()
- {
- try {
- $id = $this->request->param('id',0);
- $cancelReason = $this->request->param('cancel_reason','');
- $time = time();
- $where['user_id'] = $this->auth->id;
- $where['id'] = $id;
- $modelData = model('PreOrder')->where($where)->find();
- if (empty($modelData)) {
- throw new Exception('未找到预约信息');
- }
- if ($modelData['pre_order_status'] != 1) {
- throw new Exception('该预约单无法取消或已取消');
- }
- $modelData->pre_order_status = 0;
- $modelData->cancel_reason = $cancelReason;
- $modelData->cancel_time = $time;
- $res = $modelData->save();
- if (!$res) {
- throw new Exception('操作失败');
- }
- $this->success('操作成功');
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- }
- }
|