酒店预订平台
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

481 lignes
17 KiB

  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\Area;
  4. use app\admin\service\OrderMainService;
  5. use app\common\controller\Backend;
  6. use think\Db;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. use think\Loader;
  10. /**
  11. * 订单主管理
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class OrderMain extends Backend
  16. {
  17. /**
  18. * OrderMain模型对象
  19. * @var \app\admin\model\OrderMain
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\OrderMain;
  26. $this->relationSearch = true;
  27. }
  28. public function import()
  29. {
  30. parent::import();
  31. }
  32. /**
  33. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  34. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  35. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  36. */
  37. /**
  38. * 查看
  39. */
  40. public function index()
  41. {
  42. //设置过滤方法
  43. $this->request->filter(['strip_tags', 'trim']);
  44. if ($this->request->isAjax()) {
  45. //如果发送的来源是Selectpage,则转发到Selectpage
  46. if ($this->request->request('keyField')) {
  47. return $this->selectpage();
  48. }
  49. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  50. $group_id=$this->auth->getGroupId();
  51. $list = $this->model
  52. ->alias("order_main")
  53. ->join('hbp_cf_channel_info b','order_main.channel_id = b.id','left')
  54. ->join('hbp_admin c','order_main.create_id = c.id','left')
  55. ->field("order_main.*,b.channel_name,c.nickname")
  56. ->where($where);
  57. if ($group_id){
  58. $list = $list
  59. ->where("group_id","=",$group_id)
  60. ->order($sort, $order)
  61. ->paginate($limit);
  62. }else{
  63. $list = $list
  64. ->order($sort, $order)
  65. ->paginate($limit);
  66. }
  67. $result = $list->items();
  68. foreach ($result as $k=>$item){
  69. switch ($item["order_status"]){
  70. case 0:
  71. $result[$k]["order_status"]="待处理";
  72. break;
  73. case 1:
  74. $result[$k]["order_status"]="已确认";
  75. break;
  76. case 2:
  77. $result[$k]["order_status"]="部分取消";
  78. break;
  79. case 10:
  80. $result[$k]["order_status"]="已完成";
  81. break;
  82. case 11:
  83. $result[$k]["order_status"]="已取消";
  84. break;
  85. }
  86. }
  87. $result = array("total" => $list->total(), "rows" =>$result);
  88. return json($result);
  89. }
  90. return $this->view->fetch();
  91. }
  92. /**
  93. * 生成查询所需要的条件,排序方式
  94. * @param mixed $searchfields 快速查询的字段
  95. * @param boolean $relationSearch 是否关联查询
  96. * @return array
  97. */
  98. protected function buildparams($searchfields = null, $relationSearch = null)
  99. {
  100. $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
  101. $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
  102. $search = $this->request->get("search", '');
  103. $filter = $this->request->get("filter", '');
  104. $op = $this->request->get("op", '', 'trim');
  105. $sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id');
  106. $order = $this->request->get("order", "DESC");
  107. $offset = $this->request->get("offset/d", 0);
  108. $limit = $this->request->get("limit/d", 999999);
  109. //新增自动计算页码
  110. $page = $limit ? intval($offset / $limit) + 1 : 1;
  111. if ($this->request->has("page")) {
  112. $page = $this->request->get("page/d", 1);
  113. }
  114. $this->request->get([config('paginate.var_page') => $page]);
  115. $filter = (array)json_decode($filter, true);
  116. $op = (array)json_decode($op, true);
  117. $filter = $filter ? $filter : [];
  118. $where = [];
  119. $alias = [];
  120. $bind = [];
  121. $name = '';
  122. $aliasName = '';
  123. if (!empty($this->model) && $this->relationSearch) {
  124. $name = $this->model->getTable();
  125. $alias[$name] = Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
  126. $aliasName = $alias[$name] . '.';
  127. }
  128. $sortArr = explode(',', $sort);
  129. foreach ($sortArr as $index => & $item) {
  130. $item = stripos($item, ".") === false ? $aliasName . trim($item) : $item;
  131. }
  132. unset($item);
  133. $sort = implode(',', $sortArr);
  134. $adminIds = $this->getDataLimitAdminIds();
  135. if (is_array($adminIds)) {
  136. $where[] = [$aliasName . $this->dataLimitField, 'in', $adminIds];
  137. }
  138. if ($search) {
  139. $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
  140. foreach ($searcharr as $k => &$v) {
  141. $v = stripos($v, ".") === false ? $aliasName . $v : $v;
  142. }
  143. unset($v);
  144. $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
  145. }
  146. $index = 0;
  147. foreach ($filter as $k => $v) {
  148. if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $k)) {
  149. continue;
  150. }
  151. $sym = isset($op[$k]) ? $op[$k] : '=';
  152. if (stripos($k, ".") === false) {
  153. $k = $aliasName . $k;
  154. }
  155. $v = !is_array($v) ? trim($v) : $v;
  156. $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym);
  157. //null和空字符串特殊处理
  158. if (!is_array($v)) {
  159. if (in_array(strtoupper($v), ['NULL', 'NOT NULL'])) {
  160. $sym = strtoupper($v);
  161. }
  162. if (in_array($v, ['""', "''"])) {
  163. $v = '';
  164. $sym = '=';
  165. }
  166. }
  167. switch ($sym) {
  168. case '=':
  169. case '<>':
  170. $where[] = [$k, $sym, (string)$v];
  171. break;
  172. case 'LIKE':
  173. case 'NOT LIKE':
  174. case 'LIKE %...%':
  175. case 'NOT LIKE %...%':
  176. $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
  177. break;
  178. case '>':
  179. case '>=':
  180. case '<':
  181. case '<=':
  182. $where[] = [$k, $sym, intval($v)];
  183. break;
  184. case 'FINDIN':
  185. case 'FINDINSET':
  186. case 'FIND_IN_SET':
  187. $v = is_array($v) ? $v : explode(',', str_replace(' ', ',', $v));
  188. $findArr = array_values($v);
  189. foreach ($findArr as $idx => $item) {
  190. $bindName = "item_" . $index . "_" . $idx;
  191. $bind[$bindName] = $item;
  192. $where[] = "FIND_IN_SET(:{$bindName}, `" . str_replace('.', '`.`', $k) . "`)";
  193. }
  194. break;
  195. case 'IN':
  196. case 'IN(...)':
  197. case 'NOT IN':
  198. case 'NOT IN(...)':
  199. $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)];
  200. break;
  201. case 'BETWEEN':
  202. case 'NOT BETWEEN':
  203. $arr = array_slice(explode(',', $v), 0, 2);
  204. if (stripos($v, ',') === false || !array_filter($arr)) {
  205. continue 2;
  206. }
  207. //当出现一边为空时改变操作符
  208. if ($arr[0] === '') {
  209. $sym = $sym == 'BETWEEN' ? '<=' : '>';
  210. $arr = $arr[1];
  211. } elseif ($arr[1] === '') {
  212. $sym = $sym == 'BETWEEN' ? '>=' : '<';
  213. $arr = $arr[0];
  214. }
  215. $where[] = [$k, $sym, $arr];
  216. break;
  217. case 'RANGE':
  218. case 'NOT RANGE':
  219. $v = str_replace(' - ', ',', $v);
  220. $arr = array_slice(explode(',', $v), 0, 2);
  221. if (stripos($v, ',') === false || !array_filter($arr)) {
  222. continue 2;
  223. }
  224. //当出现一边为空时改变操作符
  225. if ($arr[0] === '') {
  226. $sym = $sym == 'RANGE' ? '<=' : '>';
  227. $arr = $arr[1];
  228. } elseif ($arr[1] === '') {
  229. $sym = $sym == 'RANGE' ? '>=' : '<';
  230. $arr = $arr[0];
  231. }
  232. $tableArr = explode('.', $k);
  233. if (count($tableArr) > 1 && $tableArr[0] != $name && !in_array($tableArr[0], $alias) && !empty($this->model)) {
  234. //修复关联模型下时间无法搜索的BUG
  235. $relation = Loader::parseName($tableArr[0], 1, false);
  236. $alias[$this->model->$relation()->getTable()] = $tableArr[0];
  237. }
  238. $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' TIME', $arr];
  239. break;
  240. case 'NULL':
  241. case 'IS NULL':
  242. case 'NOT NULL':
  243. case 'IS NOT NULL':
  244. $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
  245. break;
  246. default:
  247. break;
  248. }
  249. $index++;
  250. }
  251. if (!empty($this->model)) {
  252. $this->model->alias($alias);
  253. }
  254. $model = $this->model;
  255. $where = function ($query) use ($where, $alias, $bind, &$model) {
  256. if (!empty($model)) {
  257. $model->alias($alias);
  258. $model->bind($bind);
  259. }
  260. foreach ($where as $k => $v) {
  261. if (is_array($v)) {
  262. call_user_func_array([$query, 'where'], $v);
  263. } else {
  264. $query->where($v);
  265. }
  266. }
  267. };
  268. return [$where, $sort, $order, $offset, $limit, $page, $alias, $bind];
  269. }
  270. /**
  271. * 添加
  272. */
  273. public function add()
  274. {
  275. if ($this->request->isPost()) {
  276. $params = $this->request->post("row/a");
  277. if ($params) {
  278. $params = $this->preExcludeFields($params);
  279. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  280. $params[$this->dataLimitField] = $this->auth->id;
  281. }
  282. $result = false;
  283. Db::startTrans();
  284. try {
  285. //是否采用模型验证
  286. if ($this->modelValidate) {
  287. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  288. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  289. $this->model->validateFailException(true)->validate($validate);
  290. }
  291. $params['create_id']=$this->auth->id;
  292. $params['group_id']=$this->auth->getGroupId();
  293. $result = $this->model->allowField(true)->save($params);
  294. Db::commit();
  295. } catch (ValidateException $e) {
  296. Db::rollback();
  297. $this->error($e->getMessage());
  298. } catch (PDOException $e) {
  299. Db::rollback();
  300. $this->error($e->getMessage());
  301. } catch (Exception $e) {
  302. Db::rollback();
  303. $this->error($e->getMessage());
  304. }
  305. if ($result !== false) {
  306. $this->success();
  307. } else {
  308. $this->error(__('No rows were inserted'));
  309. }
  310. }
  311. $this->error(__('Parameter %s can not be empty', ''));
  312. }
  313. return $this->view->fetch();
  314. }
  315. /**
  316. * 编辑
  317. */
  318. public function edit($ids = null)
  319. {
  320. $row = $this->model->get($ids);
  321. if (!$row) {
  322. $this->error(__('No Results were found'));
  323. }
  324. $adminIds = $this->getDataLimitAdminIds();
  325. if (is_array($adminIds)) {
  326. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  327. $this->error(__('You have no permission'));
  328. }
  329. }
  330. if ($this->request->isPost()) {
  331. $params = $this->request->post("row/a");
  332. if ($params) {
  333. $params = $this->preExcludeFields($params);
  334. $result = false;
  335. Db::startTrans();
  336. try {
  337. //是否采用模型验证
  338. if ($this->modelValidate) {
  339. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  340. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  341. $row->validateFailException(true)->validate($validate);
  342. }
  343. $result = $row->allowField(true)->save($params);
  344. Db::commit();
  345. } catch (ValidateException $e) {
  346. Db::rollback();
  347. $this->error($e->getMessage());
  348. } catch (PDOException $e) {
  349. Db::rollback();
  350. $this->error($e->getMessage());
  351. } catch (Exception $e) {
  352. Db::rollback();
  353. $this->error($e->getMessage());
  354. }
  355. if ($result !== false) {
  356. $this->success();
  357. } else {
  358. $this->error(__('No rows were updated'));
  359. }
  360. }
  361. $this->error(__('Parameter %s can not be empty', ''));
  362. }
  363. $this->view->assign("row", $row);
  364. return $this->view->fetch();
  365. }
  366. public function newEdit($id =null){
  367. $orderMain = $this->model->find($id);
  368. if (!$orderMain){
  369. return null;
  370. }
  371. $orderHotelList=(new \app\admin\model\OrderHotel())
  372. ->where("order_id","=",$id)
  373. ->find();
  374. $orderItemList=(new \app\admin\model\OrderItem())
  375. ->where("order_id","=",$id)
  376. ->find();
  377. $result = [
  378. "orderMain"=>$orderMain,
  379. "hotel"=>$orderHotelList,
  380. "item"=>$orderItemList
  381. ];
  382. return json($result);
  383. }
  384. /**
  385. * 订单页面保存接口
  386. * @return \think\response\Json
  387. */
  388. public function save(){
  389. $params=$this->request->post();
  390. $params['create_id']=$this->auth->id;
  391. $params['group_id']=$this->auth->getGroupId();
  392. $orderMainService = new OrderMainService();
  393. Db::startTrans();
  394. $result = $orderMainService->saveOrder($params);
  395. if (!$result['flag']) {
  396. Db::rollback();
  397. } else {
  398. Db::commit();
  399. }
  400. return json($result);
  401. }
  402. /**
  403. * 子订单保存接口
  404. * @return \think\response\Json
  405. */
  406. public function subOrderSave(){
  407. $params=$this->request->post();
  408. $params['create_id']=$this->auth->id;
  409. $params['group_id']=$this->auth->getGroupId();
  410. $orderMainService = new OrderMainService();
  411. Db::startTrans();
  412. $result = $orderMainService->subOrderSave($params);
  413. if (!$result['flag']) {
  414. Db::rollback();
  415. } else {
  416. Db::commit();
  417. }
  418. return json($result);
  419. }
  420. /**
  421. * 删除子订单
  422. * @return \think\response\Json
  423. */
  424. public function delSubOrder(){
  425. $params=$this->request->post();
  426. $orderMainService = new OrderMainService();
  427. $result = $orderMainService->delSubOrder($params);
  428. return json($result);
  429. }
  430. /**
  431. * 获取订单详情
  432. * @return \think\response\Json
  433. */
  434. public function getShowInfo(){
  435. $params=$this->request->post();
  436. $orderMainService = new OrderMainService();
  437. $result = $orderMainService->getOrderInfo($params['id']);
  438. return json($result);
  439. }
  440. public function newAdd(){
  441. $params=$this->request->post();
  442. $hotelMain = $params["orderMain"];
  443. $this->insertOrderMain($hotelMain);
  444. return ;
  445. }
  446. private function insertOrderMain($params){
  447. $orderMain = new \app\admin\model\OrderMain();
  448. $params['create_id']=$this->auth->id;
  449. $params['group_id']=$this->auth->getGroupId();
  450. $result = $orderMain->allowField(true)->save($params);
  451. }
  452. }