|
- <?php
-
- namespace app\admin\controller;
-
- use app\admin\model\Area;
- use app\admin\service\OrderMainService;
- use app\common\controller\Backend;
- use think\Db;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- use think\Loader;
-
- /**
- * 订单主管理
- *
- * @icon fa fa-circle-o
- */
- class OrderMain extends Backend
- {
- /**
- * OrderMain模型对象
- * @var \app\admin\model\OrderMain
- */
- protected $model = null;
-
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\OrderMain;
- $this->relationSearch = true;
- }
-
- public function import()
- {
- parent::import();
- }
-
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
-
-
- /**
- * 查看
- */
- public function index()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $group_id=$this->auth->getGroupId();
- $list = $this->model
- ->alias("order_main")
- ->join('hbp_cf_channel_info b','order_main.channel_id = b.id','left')
- ->join('hbp_admin','order_main.create_id = hbp_admin.id','left')
- ->field("order_main.*,b.channel_name,hbp_admin.nickname")
- ->where($where);
- if ($group_id){
- $list = $list
- ->where("group_id","=",$group_id)
- ->order($sort, $order)
- ->paginate($limit);
- }else{
- $list = $list
- ->order($sort, $order)
- ->paginate($limit);
- }
- $result = $list->items();
- foreach ($result as $k=>$item){
- switch ($item["order_status"]){
- case 0:
- $result[$k]["order_status"]="待处理";
- break;
- case 1:
- $result[$k]["order_status"]="已确认";
- break;
- case 2:
- $result[$k]["order_status"]="部分取消";
- break;
- case 10:
- $result[$k]["order_status"]="已完成";
- break;
- case 11:
- $result[$k]["order_status"]="已取消";
- break;
- }
- $result[$k]["hbp_admin.nickname"] = $item["nickname"];
- }
-
- $result = array("total" => $list->total(), "rows" =>$result);
- return json($result);
- }
- return $this->view->fetch();
- }
-
-
- /**
- * 生成查询所需要的条件,排序方式
- * @param mixed $searchfields 快速查询的字段
- * @param boolean $relationSearch 是否关联查询
- * @return array
- */
- protected function buildparams($searchfields = null, $relationSearch = null)
- {
- $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
- $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
- $search = $this->request->get("search", '');
- $filter = $this->request->get("filter", '');
- $op = $this->request->get("op", '', 'trim');
- $sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id');
- $order = $this->request->get("order", "DESC");
- $offset = $this->request->get("offset/d", 0);
- $limit = $this->request->get("limit/d", 999999);
- //新增自动计算页码
- $page = $limit ? intval($offset / $limit) + 1 : 1;
- if ($this->request->has("page")) {
- $page = $this->request->get("page/d", 1);
- }
- $this->request->get([config('paginate.var_page') => $page]);
- $filter = (array)json_decode($filter, true);
- $op = (array)json_decode($op, true);
- $filter = $filter ? $filter : [];
- $where = [];
- $alias = [];
- $bind = [];
- $name = '';
- $aliasName = '';
- if (!empty($this->model) && $this->relationSearch) {
- $name = $this->model->getTable();
- $alias[$name] = Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
- $aliasName = $alias[$name] . '.';
- }
- $sortArr = explode(',', $sort);
- foreach ($sortArr as $index => & $item) {
- $item = stripos($item, ".") === false ? $aliasName . trim($item) : $item;
- }
- unset($item);
- $sort = implode(',', $sortArr);
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- $where[] = [$aliasName . $this->dataLimitField, 'in', $adminIds];
- }
- if ($search) {
- $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
- foreach ($searcharr as $k => &$v) {
- $v = stripos($v, ".") === false ? $aliasName . $v : $v;
- }
- unset($v);
- $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
- }
- $index = 0;
- foreach ($filter as $k => $v) {
- if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $k)) {
- continue;
- }
- $sym = isset($op[$k]) ? $op[$k] : '=';
- if (stripos($k, ".") === false) {
- $k = $aliasName . $k;
- }
- $v = !is_array($v) ? trim($v) : $v;
- $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym);
- //null和空字符串特殊处理
- if (!is_array($v)) {
- if (in_array(strtoupper($v), ['NULL', 'NOT NULL'])) {
- $sym = strtoupper($v);
- }
- if (in_array($v, ['""', "''"])) {
- $v = '';
- $sym = '=';
- }
- }
-
- switch ($sym) {
- case '=':
- case '<>':
- $where[] = [$k, $sym, (string)$v];
- break;
- case 'LIKE':
- case 'NOT LIKE':
- case 'LIKE %...%':
- case 'NOT LIKE %...%':
- $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
- break;
- case '>':
- case '>=':
- case '<':
- case '<=':
- $where[] = [$k, $sym, intval($v)];
- break;
- case 'FINDIN':
- case 'FINDINSET':
- case 'FIND_IN_SET':
- $v = is_array($v) ? $v : explode(',', str_replace(' ', ',', $v));
- $findArr = array_values($v);
- foreach ($findArr as $idx => $item) {
- $bindName = "item_" . $index . "_" . $idx;
- $bind[$bindName] = $item;
- $where[] = "FIND_IN_SET(:{$bindName}, `" . str_replace('.', '`.`', $k) . "`)";
- }
- break;
- case 'IN':
- case 'IN(...)':
- case 'NOT IN':
- case 'NOT IN(...)':
- $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)];
- break;
- case 'BETWEEN':
- case 'NOT BETWEEN':
- $arr = array_slice(explode(',', $v), 0, 2);
- if (stripos($v, ',') === false || !array_filter($arr)) {
- continue 2;
- }
- //当出现一边为空时改变操作符
- if ($arr[0] === '') {
- $sym = $sym == 'BETWEEN' ? '<=' : '>';
- $arr = $arr[1];
- } elseif ($arr[1] === '') {
- $sym = $sym == 'BETWEEN' ? '>=' : '<';
- $arr = $arr[0];
- }
- $where[] = [$k, $sym, $arr];
- break;
- case 'RANGE':
- case 'NOT RANGE':
- $v = str_replace(' - ', ',', $v);
- $arr = array_slice(explode(',', $v), 0, 2);
- if (stripos($v, ',') === false || !array_filter($arr)) {
- continue 2;
- }
- //当出现一边为空时改变操作符
- if ($arr[0] === '') {
- $sym = $sym == 'RANGE' ? '<=' : '>';
- $arr = $arr[1];
- } elseif ($arr[1] === '') {
- $sym = $sym == 'RANGE' ? '>=' : '<';
- $arr = $arr[0];
- }
- $tableArr = explode('.', $k);
- if (count($tableArr) > 1 && $tableArr[0] != $name && !in_array($tableArr[0], $alias) && !empty($this->model)) {
- //修复关联模型下时间无法搜索的BUG
- $relation = Loader::parseName($tableArr[0], 1, false);
- $alias[$this->model->$relation()->getTable()] = $tableArr[0];
- }
- $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' TIME', $arr];
- break;
- case 'NULL':
- case 'IS NULL':
- case 'NOT NULL':
- case 'IS NOT NULL':
- $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
- break;
- default:
- break;
- }
- $index++;
- }
- if (!empty($this->model)) {
- $this->model->alias($alias);
- }
- $model = $this->model;
- $where = function ($query) use ($where, $alias, $bind, &$model) {
- if (!empty($model)) {
- $model->alias($alias);
- $model->bind($bind);
- }
- foreach ($where as $k => $v) {
- if (is_array($v)) {
- call_user_func_array([$query, 'where'], $v);
- } else {
- $query->where($v);
- }
- }
- };
- return [$where, $sort, $order, $offset, $limit, $page, $alias, $bind];
- }
-
-
- /**
- * 添加
- */
- public function add()
- {
- if ($this->request->isPost()) {
-
- $params = $this->request->post("row/a");
- if ($params) {
- $params = $this->preExcludeFields($params);
-
- if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- $result = false;
- Db::startTrans();
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
- $this->model->validateFailException(true)->validate($validate);
- }
- $params['create_id']=$this->auth->id;
- $params['group_id']=$this->auth->getGroupId();
- $result = $this->model->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success();
- } else {
- $this->error(__('No rows were inserted'));
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- return $this->view->fetch();
- }
-
-
- /**
- * 编辑
- */
- public function edit($ids = null)
- {
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- if (!in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error(__('You have no permission'));
- }
- }
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- $params = $this->preExcludeFields($params);
- $result = false;
- Db::startTrans();
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
- $row->validateFailException(true)->validate($validate);
- }
- $result = $row->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success();
- } else {
- $this->error(__('No rows were updated'));
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $this->view->assign("row", $row);
- return $this->view->fetch();
- }
-
-
- public function newEdit($id =null){
- $orderMain = $this->model->find($id);
- if (!$orderMain){
- return null;
- }
- $orderHotelList=(new \app\admin\model\OrderHotel())
- ->where("order_id","=",$id)
- ->find();
-
- $orderItemList=(new \app\admin\model\OrderItem())
- ->where("order_id","=",$id)
- ->find();
- $result = [
- "orderMain"=>$orderMain,
- "hotel"=>$orderHotelList,
- "item"=>$orderItemList
- ];
- return json($result);
- }
-
- /**
- * 订单页面保存接口
- * @return \think\response\Json
- */
- public function save(){
- $params=$this->request->post();
- $params['create_id']=$this->auth->id;
- $params['group_id']=$this->auth->getGroupId();
- $orderMainService = new OrderMainService();
- Db::startTrans();
- $result = $orderMainService->saveOrder($params);
- if (!$result['flag']) {
- Db::rollback();
- } else {
- Db::commit();
- }
- return json($result);
- }
-
- /**
- * 子订单保存接口
- * @return \think\response\Json
- */
- public function subOrderSave(){
- $params=$this->request->post();
- $params['create_id']=$this->auth->id;
- $params['group_id']=$this->auth->getGroupId();
- $orderMainService = new OrderMainService();
- Db::startTrans();
- $result = $orderMainService->subOrderSave($params);
- if (!$result['flag']) {
- Db::rollback();
- } else {
- Db::commit();
- }
- return json($result);
- }
-
- /**
- * 删除子订单
- * @return \think\response\Json
- */
- public function delSubOrder(){
- $params=$this->request->post();
- $orderMainService = new OrderMainService();
- $result = $orderMainService->delSubOrder($params);
- return json($result);
- }
-
- /**
- * 获取订单详情
- * @return \think\response\Json
- */
- public function getShowInfo(){
- $params=$this->request->post();
- $orderMainService = new OrderMainService();
- $result = $orderMainService->getOrderInfo($params['id']);
- return json($result);
- }
-
- public function newAdd(){
- $params=$this->request->post();
- $hotelMain = $params["orderMain"];
- $this->insertOrderMain($hotelMain);
- return ;
- }
-
- private function insertOrderMain($params){
- $orderMain = new \app\admin\model\OrderMain();
- $params['create_id']=$this->auth->id;
- $params['group_id']=$this->auth->getGroupId();
- $result = $orderMain->allowField(true)->save($params);
- }
-
- /**
- * 获取订单列表
- * @return \think\response\Json
- */
- public function getOrderList(){
- $params=$this->request->post();
- $orderMainService = new OrderMainService();
- $result = $orderMainService->getOrderList($params);
- return json($result);
- }
-
- }
|