Order.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. <?php
  2. namespace app\admin\controller\unishop;
  3. use app\admin\model\unishop\Area;
  4. use app\admin\model\unishop\OrderRefund;
  5. use app\common\controller\Backend;
  6. use think\Db;
  7. use think\Exception;
  8. use think\exception\PDOException;
  9. use think\exception\ValidateException;
  10. use think\Hook;
  11. use addons\epay\library\Service;
  12. /**
  13. * 订单管理
  14. *
  15. * @icon fa fa-circle-o
  16. */
  17. class Order extends Backend
  18. {
  19. /**
  20. * 是否是关联查询
  21. */
  22. protected $relationSearch = true;
  23. /**
  24. * Order模型对象
  25. * @var \app\admin\model\unishop\Order
  26. */
  27. protected $model = null;
  28. public function _initialize()
  29. {
  30. parent::_initialize();
  31. $this->model = new \app\admin\model\unishop\Order;
  32. $this->view->assign("payTypeList", $this->model->getPayTypeList());
  33. $this->view->assign("statusList", $this->model->getStatusList());
  34. $this->view->assign("refundStatusList", $this->model->getRefundStatusList());
  35. }
  36. /**
  37. * 查看
  38. */
  39. public function index()
  40. {
  41. //设置过滤方法
  42. $this->request->filter(['strip_tags']);
  43. if ($this->request->isAjax()) {
  44. //如果发送的来源是Selectpage,则转发到Selectpage
  45. if ($this->request->request('keyField')) {
  46. return $this->selectpage();
  47. }
  48. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  49. $total = $this->model
  50. ->alias('order')
  51. ->join('user', 'user.id = order.user_id')
  52. ->where($where)
  53. ->count();
  54. $list = $this->model
  55. ->alias('order')
  56. ->join('user', 'user.id = order.user_id')
  57. ->where($where)
  58. ->order($sort, $order)
  59. ->limit($offset, $limit)
  60. ->field('order.*,user.username')
  61. ->select();
  62. $list = collection($list)->toArray();
  63. foreach ($list as &$item) {
  64. $item['id'] = (string)$item['id']; // 整形数字太大js会失准
  65. $item['user'] = [];
  66. $item['user']['username'] = $item['username'] ? $item['username'] : __('Tourist');
  67. $item['have_paid_status'] = $item['have_paid'];
  68. $item['have_delivered_status'] = $item['have_delivered'];
  69. $item['have_received_status'] = $item['have_received'];
  70. $item['have_commented_status'] = $item['have_commented'];
  71. }
  72. $result = array("total" => $total, "rows" => $list);
  73. return json($result);
  74. }
  75. return $this->view->fetch();
  76. }
  77. /**
  78. * 生成查询所需要的条件,排序方式
  79. * @param mixed $searchfields 快速查询的字段
  80. * @param boolean $relationSearch 是否关联查询
  81. * @return array
  82. */
  83. protected function buildparams($searchfields = null, $relationSearch = null)
  84. {
  85. $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
  86. $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
  87. $search = $this->request->get("search", '');
  88. $filter = $this->request->get("filter", '');
  89. $op = $this->request->get("op", '', 'trim');
  90. $sort = $this->request->get("sort", "id");
  91. $order = $this->request->get("order", "DESC");
  92. $offset = $this->request->get("offset", 0);
  93. $limit = $this->request->get("limit", 0);
  94. $filter = (array)json_decode($filter, true);
  95. $op = (array)json_decode($op, true);
  96. $filter = $filter ? $filter : [];
  97. $where = [];
  98. $tableName = '';
  99. if ($relationSearch) {
  100. if (!empty($this->model)) {
  101. $name = \think\Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
  102. $tableName = '' . $name . '.';
  103. }
  104. $sortArr = explode(',', $sort);
  105. foreach ($sortArr as $index => & $item) {
  106. $item = stripos($item, ".") === false ? $tableName . trim($item) : $item;
  107. }
  108. unset($item);
  109. $sort = implode(',', $sortArr);
  110. }
  111. $adminIds = $this->getDataLimitAdminIds();
  112. if (is_array($adminIds)) {
  113. $where[] = [$tableName . $this->dataLimitField, 'in', $adminIds];
  114. }
  115. if ($search) {
  116. $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
  117. foreach ($searcharr as $k => &$v) {
  118. $v = stripos($v, ".") === false ? $tableName . $v : $v;
  119. }
  120. unset($v);
  121. $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
  122. }
  123. foreach ($filter as $k => $v) {
  124. // 搜索订单状态
  125. if (in_array($k, ['have_paid_status', 'have_delivered_status', 'have_received_status', 'have_commented_status'])) {
  126. switch ($k) {
  127. case 'have_paid_status':
  128. $k = 'have_paid';
  129. break;
  130. case 'have_delivered_status':
  131. $k = 'have_delivered';
  132. break;
  133. case 'have_received_status':
  134. $k = 'have_received';
  135. break;
  136. case 'have_commented_status':
  137. $k = 'have_commented';
  138. break;
  139. }
  140. $v == 0 ? ($op[$k] = '=') : ($op[$k] = '>');
  141. $v = 0;
  142. }
  143. $sym = isset($op[$k]) ? $op[$k] : '=';
  144. if (stripos($k, ".") === false) {
  145. $k = $tableName . $k;
  146. }
  147. $v = !is_array($v) ? trim($v) : $v;
  148. $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym);
  149. switch ($sym) {
  150. case '=':
  151. case '<>':
  152. $where[] = [$k, $sym, (string)$v];
  153. break;
  154. case 'LIKE':
  155. case 'NOT LIKE':
  156. case 'LIKE %...%':
  157. case 'NOT LIKE %...%':
  158. $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
  159. break;
  160. case '>':
  161. case '>=':
  162. case '<':
  163. case '<=':
  164. $where[] = [$k, $sym, intval($v)];
  165. break;
  166. case 'FINDIN':
  167. case 'FINDINSET':
  168. case 'FIND_IN_SET':
  169. $where[] = "FIND_IN_SET('{$v}', " . ($relationSearch ? $k : '`' . str_replace('.', '`.`', $k) . '`') . ")";
  170. break;
  171. case 'IN':
  172. case 'IN(...)':
  173. case 'NOT IN':
  174. case 'NOT IN(...)':
  175. $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)];
  176. break;
  177. case 'BETWEEN':
  178. case 'NOT BETWEEN':
  179. $arr = array_slice(explode(',', $v), 0, 2);
  180. if (stripos($v, ',') === false || !array_filter($arr)) {
  181. continue 2;
  182. }
  183. //当出现一边为空时改变操作符
  184. if ($arr[0] === '') {
  185. $sym = $sym == 'BETWEEN' ? '<=' : '>';
  186. $arr = $arr[1];
  187. } elseif ($arr[1] === '') {
  188. $sym = $sym == 'BETWEEN' ? '>=' : '<';
  189. $arr = $arr[0];
  190. }
  191. $where[] = [$k, $sym, $arr];
  192. break;
  193. case 'RANGE':
  194. case 'NOT RANGE':
  195. $v = str_replace(' - ', ',', $v);
  196. $arr = array_slice(explode(',', $v), 0, 2);
  197. if (stripos($v, ',') === false || !array_filter($arr)) {
  198. continue 2;
  199. }
  200. //当出现一边为空时改变操作符
  201. if ($arr[0] === '') {
  202. $sym = $sym == 'RANGE' ? '<=' : '>';
  203. $arr = $arr[1];
  204. } elseif ($arr[1] === '') {
  205. $sym = $sym == 'RANGE' ? '>=' : '<';
  206. $arr = $arr[0];
  207. }
  208. $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' time', $arr];
  209. break;
  210. case 'LIKE':
  211. case 'LIKE %...%':
  212. $where[] = [$k, 'LIKE', "%{$v}%"];
  213. break;
  214. case 'NULL':
  215. case 'IS NULL':
  216. case 'NOT NULL':
  217. case 'IS NOT NULL':
  218. $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
  219. break;
  220. default:
  221. break;
  222. }
  223. }
  224. $where = function ($query) use ($where) {
  225. foreach ($where as $k => $v) {
  226. if (is_array($v)) {
  227. call_user_func_array([$query, 'where'], $v);
  228. } else {
  229. $query->where($v);
  230. }
  231. }
  232. };
  233. return [$where, $sort, $order, $offset, $limit];
  234. }
  235. /**
  236. * 编辑
  237. */
  238. public function edit($ids = null)
  239. {
  240. $row = $this->model->get($ids);
  241. if (!$row) {
  242. $this->error(__('No Results were found'));
  243. }
  244. $adminIds = $this->getDataLimitAdminIds();
  245. if (is_array($adminIds)) {
  246. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  247. $this->error(__('You have no permission'));
  248. }
  249. }
  250. if ($this->request->isPost()) {
  251. $params = $this->request->post("row/a");
  252. if ($params) {
  253. $params = $this->preExcludeFields($params);
  254. $result = false;
  255. Db::startTrans();
  256. try {
  257. //是否采用模型验证
  258. if ($this->modelValidate) {
  259. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  260. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  261. $row->validateFailException(true)->validate($validate);
  262. }
  263. $updatetime = $this->request->post('updatetime');
  264. // 乐观锁
  265. $result = $this->model->allowField(true)->save($params, ['id' => $ids, 'updatetime' => $updatetime]);
  266. if (!$result) {
  267. throw new Exception(__('Data had been update before saved, close windows and do it again'));
  268. }
  269. Db::commit();
  270. } catch (ValidateException $e) {
  271. Db::rollback();
  272. $this->error($e->getMessage());
  273. } catch (PDOException $e) {
  274. Db::rollback();
  275. $this->error($e->getMessage());
  276. } catch (Exception $e) {
  277. Db::rollback();
  278. $this->error($e->getMessage());
  279. }
  280. if ($result !== false) {
  281. $this->success();
  282. } else {
  283. $this->error(__('No rows were updated'));
  284. }
  285. }
  286. $this->error(__('Parameter %s can not be empty', ''));
  287. }
  288. $this->view->assign("row", $row);
  289. return $this->view->fetch();
  290. }
  291. /**
  292. * 物流管理
  293. */
  294. public function delivery($ids = null)
  295. {
  296. $row = $this->model->get($ids, ['extend']);
  297. if (!$row) {
  298. $this->error(__('No Results were found'));
  299. }
  300. $adminIds = $this->getDataLimitAdminIds();
  301. if (is_array($adminIds)) {
  302. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  303. $this->error(__('You have no permission'));
  304. }
  305. }
  306. if ($this->request->isPost()) {
  307. $result = false;
  308. Db::startTrans();
  309. try {
  310. $express_number = $this->request->post('express_number','');
  311. $express_company = $this->request->post('express_company','');
  312. // $have_delivered = $express_number ? time() : 0;
  313. $have_delivered = time();
  314. $res1 = $row->allowField(true)->save(['have_delivered' => $have_delivered]);
  315. $res2 = $row->extend->allowField(true)->save(['express_number' => $express_number, 'express_company' => $express_company]);
  316. if ($res1 !== false && $res2 !== false) {
  317. $result = true;
  318. } else {
  319. throw new Exception(__('No rows were updated'));
  320. }
  321. Db::commit();
  322. } catch (ValidateException $e) {
  323. Db::rollback();
  324. $this->error($e->getMessage());
  325. } catch (PDOException $e) {
  326. Db::rollback();
  327. $this->error($e->getMessage());
  328. } catch (Exception $e) {
  329. Db::rollback();
  330. $this->error($e->getMessage());
  331. }
  332. if ($result !== false) {
  333. $this->success();
  334. } else {
  335. $this->error(__('No rows were updated'));
  336. }
  337. $this->error(__('Parameter %s can not be empty', ''));
  338. }
  339. $address = json_decode($row->extend->address_json,true);
  340. if ($address) {
  341. // $area = (new Area)->whereIn('id',[$address['province_id'],$address['city_id'],$address['area_id']])->column('name', 'id');
  342. // $row['addressText'] = $area[$address['province_id']].$area[$address['city_id']].$area[$address['area_id']].' '.$address['address'];
  343. $row['addressText'] = $address['province_name'].$address['city_name'].$address['area_name'].$address['address'];
  344. $row['address'] = $address;
  345. }
  346. $this->view->assign("row", $row);
  347. // 快递公司
  348. if (!class_exists(\addons\expressquery\library\Expressquery::class)) {
  349. $expressInfo = array_merge(['' => '请先安装插件《物流信息接口》']);
  350. } else {
  351. $expressInfo = Db::name('expressquery')->column('name', 'express');
  352. $expressInfo = $expressInfo ?? [];
  353. $expressInfo = array_merge(['' => '请选择快递公司'], $expressInfo);
  354. }
  355. $this->view->assign('expressCompany', $expressInfo);
  356. return $this->view->fetch();
  357. }
  358. /**
  359. * 商品管理
  360. */
  361. public function product($ids = null)
  362. {
  363. if ($this->request->isPost()) {
  364. $this->success();
  365. }
  366. $row = $this->model->get($ids, ['product','evaluate','extend']);
  367. $this->view->assign('product', $row->product);
  368. $evaluate = [];
  369. foreach ($row->evaluate as $key => $item) {
  370. $evaluate[$item['product_id']] = $item;
  371. }
  372. //地址
  373. $row['addressText'] = '';
  374. $address = json_decode($row->extend->address_json,true);
  375. if ($address) {
  376. $row['addressText'] = $address['province_name'].$address['city_name'].$address['area_name'].$address['address'];
  377. $row['address'] = $address;
  378. }
  379. $this->view->assign('order', $row);
  380. $this->view->assign('evaluate', $evaluate);
  381. return $this->view->fetch();
  382. }
  383. /**
  384. * 退货管理
  385. */
  386. public function refund($ids = null)
  387. {
  388. $row = $this->model->get($ids, ['refund']);
  389. if ($row['status'] != \app\admin\model\unishop\Order::STATUS_REFUND) {
  390. $this->error(__('This order is not returned'));
  391. }
  392. if ($this->request->isPost()) {
  393. Db::startTrans();
  394. try {
  395. $updatetime = $this->request->post('updatetime');
  396. $refund_amount = $this->request->post('refund_amount');
  397. $refund_status = $this->request->post('refund_status');
  398. if ($refund_amount > $row['total_price']) {
  399. $this->error('退款金额不能大于实际支付金额');
  400. }
  401. // 退款
  402. $params = [
  403. 'refund_status' => $refund_status,
  404. 'updatetime' => time(),
  405. ];
  406. if($refund_status == 3) {
  407. $params['had_refund'] = time();
  408. }
  409. // 乐观锁
  410. $result = Db::name('unishop_order')->where(['id' => $ids, 'updatetime' => $updatetime])->update($params);
  411. if (!$result) {
  412. throw new Exception(__('Data had been update before saved, close windows and do it again'));
  413. }
  414. //修改退款金额
  415. $rs2 = Db::name('unishop_order_refund')->where('order_id',$ids)->update(['amount'=>$refund_amount]);
  416. if ($rs2 === false) {
  417. throw new Exception('操作失败');
  418. }
  419. //同意并执行退款
  420. if($refund_status == 3 && $refund_amount > 0){
  421. $order = Db::name('unishop_order')->where('id',$ids)->find();
  422. if($order['pay_type'] == 2){
  423. $wallet_rs = model('wallet')->lockChangeAccountRemain($order['user_id'],'money',$refund_amount,32,$remark='商城订单退款','unishop_order',$ids);
  424. if($wallet_rs['status'] === false){
  425. throw new Exception($wallet_rs['msg']);
  426. }
  427. }elseif($order['pay_type'] == 3 || $order['pay_type'] == 4){
  428. $refund_result = $this->old_refund($order,$refund_amount);
  429. if($refund_result !== true){
  430. throw new Exception($refund_result);
  431. }
  432. }
  433. }
  434. Db::commit();
  435. } catch (ValidateException $e) {
  436. Db::rollback();
  437. $this->error($e->getMessage());
  438. } catch (PDOException $e) {
  439. Db::rollback();
  440. $this->error($e->getMessage());
  441. } catch (Exception $e) {
  442. Db::rollback();
  443. $this->error($e->getMessage());
  444. }
  445. $this->success();
  446. }
  447. $products = $row->product;
  448. $refundProducts = $row->refundProduct;
  449. foreach ($products as &$product) {
  450. $product['choose'] = 0;
  451. foreach ($refundProducts as $refundProduct) {
  452. if ($product['id'] == $refundProduct['order_product_id']) {
  453. $product['choose'] = 1;
  454. }
  455. }
  456. }
  457. if ($row->refund) {
  458. $refund = $row->refund->append(['receiving_status_text', 'service_type_text'])->toArray();
  459. } else {
  460. $refund = [
  461. 'service_type' => 0,
  462. 'express_number' => -1,
  463. 'receiving_status_text' => -1,
  464. 'receiving_status' => -1,
  465. 'service_type_text' => -1,
  466. 'amount' => -1,
  467. 'reason_type' => -1,
  468. 'refund_explain' => -1,
  469. ];
  470. }
  471. $this->view->assign('row', $row);
  472. $this->view->assign('product', $products);
  473. $this->view->assign('refund', $refund);
  474. return $this->view->fetch();
  475. }
  476. // 退款
  477. public function old_refund($order, $refund_price)
  478. {
  479. $table = 'unishop_order';
  480. $remark = '订单退款';
  481. if($order['pay_type'] == 3){
  482. $order['pay_type'] = 'wechat';
  483. }
  484. if($order['pay_type'] == 4){
  485. $order['pay_type'] = 'alipay';
  486. }
  487. // 生成退款单
  488. $refund_data = [
  489. 'order_id' => $order['id'],
  490. 'out_refund_no'=> createUniqueNo('R'),
  491. 'pay_fee' => $order['total_price'],
  492. 'refund_price' => $refund_price,
  493. 'pay_type' => $order['pay_type'],
  494. 'status' => 0,
  495. 'createtime' => time(),
  496. 'updatetime' => time(),
  497. 'table' => $table,
  498. 'table_id' => $order['id'],
  499. ];
  500. $refund_log_id = Db::name('pay_order_refund_log')->insertGetId($refund_data);
  501. if(!$refund_log_id){
  502. return '退款失败';
  503. }
  504. if ($order['pay_type'] == 'wechat' || $order['pay_type'] == 'alipay') {
  505. // 微信|支付宝退款
  506. // 退款数据
  507. $order_data = [
  508. 'out_trade_no' => $order['pay_out_trade_no']
  509. ];
  510. if ($order['pay_type'] == 'wechat') {
  511. $total_fee = $order['total_price'] * 100;
  512. $refund_fee = $refund_price * 100;
  513. $order_data = array_merge($order_data, [
  514. 'out_refund_no' => $refund_data['out_refund_no'],
  515. 'total_fee' => $total_fee,
  516. 'refund_fee' => $refund_fee,
  517. 'refund_desc' => $remark,
  518. ]);
  519. } else {
  520. $order_data = array_merge($order_data, [
  521. 'out_request_no' => $refund_data['out_refund_no'],
  522. 'refund_amount' => $refund_price,
  523. ]);
  524. }
  525. //
  526. if ($order['pay_type'] == 'wechat') {
  527. $wxpay = new \app\common\library\Wxpay;
  528. $result = $wxpay->WxPayRefund($order_data);
  529. // 微信通知回调 pay->notifyr
  530. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  531. Db::name('pay_order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
  532. return true;
  533. } else {
  534. return $result['return_msg'];
  535. }
  536. } else {
  537. $result = Service::submitRefund($order['total_price'],$refund_price,$order['pay_out_trade_no'],$refund_data['out_refund_no'],$order['pay_type'],$remark,'');
  538. if($result['code'] == '10000'){
  539. Db::name('pay_order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
  540. return true;
  541. }else{
  542. return $result['msg'];
  543. }
  544. /* return 'alipay wrong way';
  545. $alipay = new \app\common\library\AliPay;
  546. $result = $alipay->AliPayRefund($order_data);
  547. // 支付宝通知回调 pay->notifyx
  548. return $result;*/
  549. /*if ($result['code'] == "10000") {
  550. return true;
  551. } else {
  552. throw new \Exception($result['msg']);
  553. }*/
  554. }
  555. // { // 微信返回结果
  556. // "return_code":"SUCCESS",
  557. // "return_msg":"OK",
  558. // "appid":"wx39cd0799d4567dd0",
  559. // "mch_id":"1481069012",
  560. // "nonce_str":"huW9eIAb5BDPn0Ma",
  561. // "sign":"250316740B263FE53F5DFF50AF5A8FA1",
  562. // "result_code":"SUCCESS",
  563. // "transaction_id":"4200000497202004072822298902",
  564. // "out_trade_no":"202010300857029180027000",
  565. // "out_refund_no":"1586241595",
  566. // "refund_id":"50300603862020040700031444448",
  567. // "refund_channel":[],
  568. // "refund_fee":"1",
  569. // "coupon_refund_fee":"0",
  570. // "total_fee":"1",
  571. // "cash_fee":"1",
  572. // "coupon_refund_count":"0",
  573. // "cash_refund_fee":"1
  574. // }
  575. // { // 支付宝返回结果
  576. // "code": "10000",
  577. // "msg": "Success",
  578. // "buyer_logon_id": "157***@163.com",
  579. // "buyer_user_id": "2088902485164146",
  580. // "fund_change": "Y",
  581. // "gmt_refund_pay": "2020-08-15 16:11:45",
  582. // "out_trade_no": "202002460317545607015300",
  583. // "refund_fee": "0.01",
  584. // "send_back_fee": "0.00",
  585. // "trade_no": "2020081522001464141438570535"
  586. // }
  587. }
  588. return true;
  589. }
  590. /**
  591. * 回收站
  592. */
  593. public function recyclebin()
  594. {
  595. //设置过滤方法
  596. $this->request->filter(['strip_tags']);
  597. if ($this->request->isAjax()) {
  598. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  599. $total = $this->model
  600. ->onlyTrashed()
  601. ->alias('order')
  602. ->join('user', 'user.id = order.user_id')
  603. ->where($where)
  604. ->count();
  605. $list = $this->model
  606. ->onlyTrashed()
  607. ->alias('order')
  608. ->join('user', 'user.id = order.user_id')
  609. ->where($where)
  610. ->field('order.*,user.username')
  611. ->order($sort, $order)
  612. ->limit($offset, $limit)
  613. ->select();
  614. $list = collection($list)->toArray();
  615. foreach ($list as &$item) {
  616. $item['id'] = (string)$item['id'];
  617. $item['user'] = [];
  618. $item['user']['username'] = $item['username'] ? $item['username'] : __('Tourist');
  619. $item['have_paid_status'] = $item['have_paid'];
  620. $item['have_delivered_status'] = $item['have_delivered'];
  621. $item['have_received_status'] = $item['have_received'];
  622. $item['have_commented_status'] = $item['have_commented'];
  623. }
  624. $result = array("total" => $total, "rows" => $list);
  625. return json($result);
  626. }
  627. return $this->view->fetch();
  628. }
  629. }