Order.php 25 KB

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