酒店预订平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

OrderMain.php 18 KiB

3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
3年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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','order_main.create_id = hbp_admin.id','left')
  55. ->field("order_main.*,b.channel_name,hbp_admin.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. $result[$k]["hbp_admin.nickname"] = $item["nickname"];
  87. }
  88. $result = array("total" => $list->total(), "rows" =>$result);
  89. return json($result);
  90. }
  91. return $this->view->fetch();
  92. }
  93. /**
  94. * 生成查询所需要的条件,排序方式
  95. * @param mixed $searchfields 快速查询的字段
  96. * @param boolean $relationSearch 是否关联查询
  97. * @return array
  98. */
  99. protected function buildparams($searchfields = null, $relationSearch = null)
  100. {
  101. $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
  102. $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
  103. $search = $this->request->get("search", '');
  104. $filter = $this->request->get("filter", '');
  105. $op = $this->request->get("op", '', 'trim');
  106. $sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id');
  107. $order = $this->request->get("order", "DESC");
  108. $offset = $this->request->get("offset/d", 0);
  109. $limit = $this->request->get("limit/d", 999999);
  110. //新增自动计算页码
  111. $page = $limit ? intval($offset / $limit) + 1 : 1;
  112. if ($this->request->has("page")) {
  113. $page = $this->request->get("page/d", 1);
  114. }
  115. $this->request->get([config('paginate.var_page') => $page]);
  116. $filter = (array)json_decode($filter, true);
  117. $op = (array)json_decode($op, true);
  118. $filter = $filter ? $filter : [];
  119. $where = [];
  120. $alias = [];
  121. $bind = [];
  122. $name = '';
  123. $aliasName = '';
  124. if (!empty($this->model) && $this->relationSearch) {
  125. $name = $this->model->getTable();
  126. $alias[$name] = Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
  127. $aliasName = $alias[$name] . '.';
  128. }
  129. $sortArr = explode(',', $sort);
  130. foreach ($sortArr as $index => & $item) {
  131. $item = stripos($item, ".") === false ? $aliasName . trim($item) : $item;
  132. }
  133. unset($item);
  134. $sort = implode(',', $sortArr);
  135. $adminIds = $this->getDataLimitAdminIds();
  136. if (is_array($adminIds)) {
  137. $where[] = [$aliasName . $this->dataLimitField, 'in', $adminIds];
  138. }
  139. if ($search) {
  140. $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
  141. foreach ($searcharr as $k => &$v) {
  142. $v = stripos($v, ".") === false ? $aliasName . $v : $v;
  143. }
  144. unset($v);
  145. $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
  146. }
  147. $index = 0;
  148. foreach ($filter as $k => $v) {
  149. if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $k)) {
  150. continue;
  151. }
  152. $sym = isset($op[$k]) ? $op[$k] : '=';
  153. if (stripos($k, ".") === false) {
  154. $k = $aliasName . $k;
  155. }
  156. $v = !is_array($v) ? trim($v) : $v;
  157. $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym);
  158. //null和空字符串特殊处理
  159. if (!is_array($v)) {
  160. if (in_array(strtoupper($v), ['NULL', 'NOT NULL'])) {
  161. $sym = strtoupper($v);
  162. }
  163. if (in_array($v, ['""', "''"])) {
  164. $v = '';
  165. $sym = '=';
  166. }
  167. }
  168. switch ($sym) {
  169. case '=':
  170. case '<>':
  171. $where[] = [$k, $sym, (string)$v];
  172. break;
  173. case 'LIKE':
  174. case 'NOT LIKE':
  175. case 'LIKE %...%':
  176. case 'NOT LIKE %...%':
  177. $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
  178. break;
  179. case '>':
  180. case '>=':
  181. case '<':
  182. case '<=':
  183. $where[] = [$k, $sym, intval($v)];
  184. break;
  185. case 'FINDIN':
  186. case 'FINDINSET':
  187. case 'FIND_IN_SET':
  188. $v = is_array($v) ? $v : explode(',', str_replace(' ', ',', $v));
  189. $findArr = array_values($v);
  190. foreach ($findArr as $idx => $item) {
  191. $bindName = "item_" . $index . "_" . $idx;
  192. $bind[$bindName] = $item;
  193. $where[] = "FIND_IN_SET(:{$bindName}, `" . str_replace('.', '`.`', $k) . "`)";
  194. }
  195. break;
  196. case 'IN':
  197. case 'IN(...)':
  198. case 'NOT IN':
  199. case 'NOT IN(...)':
  200. $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)];
  201. break;
  202. case 'BETWEEN':
  203. case 'NOT BETWEEN':
  204. $arr = array_slice(explode(',', $v), 0, 2);
  205. if (stripos($v, ',') === false || !array_filter($arr)) {
  206. continue 2;
  207. }
  208. //当出现一边为空时改变操作符
  209. if ($arr[0] === '') {
  210. $sym = $sym == 'BETWEEN' ? '<=' : '>';
  211. $arr = $arr[1];
  212. } elseif ($arr[1] === '') {
  213. $sym = $sym == 'BETWEEN' ? '>=' : '<';
  214. $arr = $arr[0];
  215. }
  216. $where[] = [$k, $sym, $arr];
  217. break;
  218. case 'RANGE':
  219. case 'NOT RANGE':
  220. $v = str_replace(' - ', ',', $v);
  221. $arr = array_slice(explode(',', $v), 0, 2);
  222. if (stripos($v, ',') === false || !array_filter($arr)) {
  223. continue 2;
  224. }
  225. //当出现一边为空时改变操作符
  226. if ($arr[0] === '') {
  227. $sym = $sym == 'RANGE' ? '<=' : '>';
  228. $arr = $arr[1];
  229. } elseif ($arr[1] === '') {
  230. $sym = $sym == 'RANGE' ? '>=' : '<';
  231. $arr = $arr[0];
  232. }
  233. $tableArr = explode('.', $k);
  234. if (count($tableArr) > 1 && $tableArr[0] != $name && !in_array($tableArr[0], $alias) && !empty($this->model)) {
  235. //修复关联模型下时间无法搜索的BUG
  236. $relation = Loader::parseName($tableArr[0], 1, false);
  237. $alias[$this->model->$relation()->getTable()] = $tableArr[0];
  238. }
  239. $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' TIME', $arr];
  240. break;
  241. case 'NULL':
  242. case 'IS NULL':
  243. case 'NOT NULL':
  244. case 'IS NOT NULL':
  245. $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
  246. break;
  247. default:
  248. break;
  249. }
  250. $index++;
  251. }
  252. if (!empty($this->model)) {
  253. $this->model->alias($alias);
  254. }
  255. $model = $this->model;
  256. $where = function ($query) use ($where, $alias, $bind, &$model) {
  257. if (!empty($model)) {
  258. $model->alias($alias);
  259. $model->bind($bind);
  260. }
  261. foreach ($where as $k => $v) {
  262. if (is_array($v)) {
  263. call_user_func_array([$query, 'where'], $v);
  264. } else {
  265. $query->where($v);
  266. }
  267. }
  268. };
  269. return [$where, $sort, $order, $offset, $limit, $page, $alias, $bind];
  270. }
  271. /**
  272. * 添加
  273. */
  274. public function add()
  275. {
  276. if ($this->request->isPost()) {
  277. $params = $this->request->post("row/a");
  278. if ($params) {
  279. $params = $this->preExcludeFields($params);
  280. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  281. $params[$this->dataLimitField] = $this->auth->id;
  282. }
  283. $result = false;
  284. Db::startTrans();
  285. try {
  286. //是否采用模型验证
  287. if ($this->modelValidate) {
  288. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  289. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  290. $this->model->validateFailException(true)->validate($validate);
  291. }
  292. $params['create_id']=$this->auth->id;
  293. $params['group_id']=$this->auth->getGroupId();
  294. $result = $this->model->allowField(true)->save($params);
  295. Db::commit();
  296. } catch (ValidateException $e) {
  297. Db::rollback();
  298. $this->error($e->getMessage());
  299. } catch (PDOException $e) {
  300. Db::rollback();
  301. $this->error($e->getMessage());
  302. } catch (Exception $e) {
  303. Db::rollback();
  304. $this->error($e->getMessage());
  305. }
  306. if ($result !== false) {
  307. $this->success();
  308. } else {
  309. $this->error(__('No rows were inserted'));
  310. }
  311. }
  312. $this->error(__('Parameter %s can not be empty', ''));
  313. }
  314. return $this->view->fetch();
  315. }
  316. /**
  317. * 编辑
  318. */
  319. public function edit($ids = null)
  320. {
  321. $row = $this->model->get($ids);
  322. if (!$row) {
  323. $this->error(__('No Results were found'));
  324. }
  325. $adminIds = $this->getDataLimitAdminIds();
  326. if (is_array($adminIds)) {
  327. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  328. $this->error(__('You have no permission'));
  329. }
  330. }
  331. if ($this->request->isPost()) {
  332. $params = $this->request->post("row/a");
  333. if ($params) {
  334. $params = $this->preExcludeFields($params);
  335. $result = false;
  336. Db::startTrans();
  337. try {
  338. //是否采用模型验证
  339. if ($this->modelValidate) {
  340. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  341. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  342. $row->validateFailException(true)->validate($validate);
  343. }
  344. $result = $row->allowField(true)->save($params);
  345. Db::commit();
  346. } catch (ValidateException $e) {
  347. Db::rollback();
  348. $this->error($e->getMessage());
  349. } catch (PDOException $e) {
  350. Db::rollback();
  351. $this->error($e->getMessage());
  352. } catch (Exception $e) {
  353. Db::rollback();
  354. $this->error($e->getMessage());
  355. }
  356. if ($result !== false) {
  357. $this->success();
  358. } else {
  359. $this->error(__('No rows were updated'));
  360. }
  361. }
  362. $this->error(__('Parameter %s can not be empty', ''));
  363. }
  364. $this->view->assign("row", $row);
  365. return $this->view->fetch();
  366. }
  367. public function newEdit($id =null){
  368. $orderMain = $this->model->find($id);
  369. if (!$orderMain){
  370. return null;
  371. }
  372. $orderHotelList=(new \app\admin\model\OrderHotel())
  373. ->where("order_id","=",$id)
  374. ->find();
  375. $orderItemList=(new \app\admin\model\OrderItem())
  376. ->where("order_id","=",$id)
  377. ->find();
  378. $result = [
  379. "orderMain"=>$orderMain,
  380. "hotel"=>$orderHotelList,
  381. "item"=>$orderItemList
  382. ];
  383. return json($result);
  384. }
  385. /**
  386. * 订单页面保存接口
  387. * @return \think\response\Json
  388. */
  389. public function save(){
  390. $params=$this->request->post();
  391. $params['create_id']=$this->auth->id;
  392. $params['group_id']=$this->auth->getGroupId();
  393. $orderMainService = new OrderMainService();
  394. Db::startTrans();
  395. $result = $orderMainService->saveOrder($params);
  396. if (!$result['flag']) {
  397. Db::rollback();
  398. } else {
  399. Db::commit();
  400. }
  401. return json($result);
  402. }
  403. /**
  404. * 子订单保存接口
  405. * @return \think\response\Json
  406. */
  407. public function subOrderSave(){
  408. $params=$this->request->post();
  409. $params['create_id']=$this->auth->id;
  410. $params['group_id']=$this->auth->getGroupId();
  411. $orderMainService = new OrderMainService();
  412. Db::startTrans();
  413. $result = $orderMainService->subOrderSave($params);
  414. if (!$result['flag']) {
  415. Db::rollback();
  416. } else {
  417. Db::commit();
  418. }
  419. return json($result);
  420. }
  421. /**
  422. * 删除子订单
  423. * @return \think\response\Json
  424. */
  425. public function delSubOrder(){
  426. $params=$this->request->post();
  427. $orderMainService = new OrderMainService();
  428. $result = $orderMainService->delSubOrder($params);
  429. return json($result);
  430. }
  431. /**
  432. * 获取订单详情
  433. * @return \think\response\Json
  434. */
  435. public function getShowInfo(){
  436. $params=$this->request->post();
  437. $orderMainService = new OrderMainService();
  438. $result = $orderMainService->getOrderInfo($params['id']);
  439. return json($result);
  440. }
  441. public function newAdd(){
  442. $params=$this->request->post();
  443. $hotelMain = $params["orderMain"];
  444. $this->insertOrderMain($hotelMain);
  445. return ;
  446. }
  447. private function insertOrderMain($params){
  448. $orderMain = new \app\admin\model\OrderMain();
  449. $params['create_id']=$this->auth->id;
  450. $params['group_id']=$this->auth->getGroupId();
  451. $result = $orderMain->allowField(true)->save($params);
  452. }
  453. /**
  454. * 获取订单列表
  455. * @return \think\response\Json
  456. */
  457. public function getOrderList(){
  458. $params=$this->request->post();
  459. $orderMainService = new OrderMainService();
  460. $result = $orderMainService->getOrderList($params);
  461. return json($result);
  462. }
  463. }