ShopOrder.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace app\admin\controller\mall;
  3. use app\common\controller\AdminController;
  4. use EasyAdmin\annotation\ControllerAnnotation;
  5. use EasyAdmin\annotation\NodeAnotation;
  6. use think\App;
  7. use think\facade\Db;
  8. /**
  9. * @ControllerAnnotation(title="shop_order")
  10. */
  11. class ShopOrder extends AdminController
  12. {
  13. use \app\admin\traits\Curd;
  14. public function __construct (App $app)
  15. {
  16. parent::__construct($app);
  17. $this->model = new \app\common\model\ShopOrder();
  18. }
  19. /**
  20. * @NodeAnotation(title="列表")
  21. */
  22. public function index ()
  23. {
  24. if ($this->request->isAjax()) {
  25. // if (input('selectFields')) {
  26. // return $this->selectList();
  27. // }
  28. // return json_decode(htmlspecialchars_decode($this->request->param('filter')));
  29. // return $this->reque('filter');
  30. // return json_decode(input('op'));
  31. list($page, $limit, $where) = $this->buildTableParames();
  32. foreach ($where as $key=>$val){
  33. if ($val[0] == 'status'){
  34. $where[$key][0] = 'o.status';
  35. }
  36. if ($val[0] == 'created_time'){
  37. $where[$key][0] = 'o.created_time';
  38. $where[$key][2] = sr_getcurtime($where[$key][2]);
  39. }
  40. if ($val[0] == 'order_type'){
  41. $where[$key][0] = 'o.order_type';
  42. }
  43. }
  44. $count = $this->model
  45. ->alias('o')
  46. ->with(['goods'])
  47. ->where($where)
  48. // ->where('o.status', '<>', 3)
  49. ->leftJoin('user u', 'u.id = o.user_id')
  50. ->leftJoin('shop_order_shipping s', 's.order_id = o.order_id')
  51. ->field('o.*,u.mobile,s.sp_id,s.sp_name,s.sp_mergename,s.sp_mobile')
  52. ->count();
  53. $list = $this->model
  54. ->alias('o')
  55. ->with(['goods'])
  56. ->where($where)
  57. // ->where('o.status', '<>', 3)
  58. ->leftJoin('user u', 'u.id = o.user_id')
  59. ->leftJoin('shop_order_shipping s', 's.order_id = o.order_id')
  60. ->field('o.*,u.mobile,s.sp_id,s.sp_name,s.sp_mergename,s.sp_mobile')
  61. ->order('o.order_id desc')
  62. ->withAttr('order_sn', function($val, $data){
  63. return '编号:'.$val;
  64. })
  65. ->page($page, $limit)
  66. ->select()->toArray();
  67. foreach ($list as $k => $v) {
  68. $goods_id = $v['goods'][0]['goods_id'];
  69. $goods_info = Db::name('shop_goods')->where('goods_id', $goods_id)->find();
  70. if ($goods_info){
  71. $list[$k]['cost_price'] = $goods_info['cost_price'];
  72. }else{
  73. $list[$k]['cost_price'] = 0;
  74. }
  75. if ($v['status'] == 5 || $v['status'] == 6){
  76. // 新的地址
  77. $ygOrderInfo = Db::name('yg_order')->where('order_sn', str_replace('编号:', '', $v['order_sn']))->find();
  78. $newAddress_info = Db::name('user_address')->where('id', $ygOrderInfo['address_id'])->find();
  79. $list[$k]['sp_id'] = 99999;
  80. $list[$k]['sp_mobile'] = $newAddress_info['mobile'];
  81. $list[$k]['sp_name'] = $newAddress_info['name'];
  82. $list[$k]['sp_mergename'] = $newAddress_info['mergename'];
  83. }
  84. }
  85. $data = [
  86. 'code' => 0,
  87. 'msg' => '',
  88. 'count' => $count,
  89. 'data' => $list,
  90. ];
  91. return json($data);
  92. }
  93. return $this->fetch();
  94. }
  95. /**
  96. * @NodeAnotation(title="修改备注")
  97. */
  98. public function editdesc ($id)
  99. {
  100. if ($this->request->isPost()){
  101. $post = $this->request->post();
  102. $row = $this->model->where('order_id', $id)->find();
  103. empty($row) && $this->error('取消失败');
  104. $row->order_remark = $post['error_text'];
  105. Db::startTrans();
  106. try {
  107. $row->save();
  108. Db::commit();
  109. } catch (\Exception $e) {
  110. Db::rollback();
  111. $this->error('设置失败'.$e->getMessage());
  112. }
  113. $this->success('设置成功');
  114. }
  115. $row = $this->model->where('order_id', $id)->find();
  116. $this->assign('info', $row);
  117. return $this->fetch();
  118. }
  119. /**
  120. * @NodeAnotation(title="列表")
  121. */
  122. public function details ($orderId)
  123. {
  124. $order = $this->model->where(['order_id' => $orderId])->with(['user', 'goods', 'shipping'])->find()->toArray();
  125. if ($order['status'] == 5 || $order['status'] == 6){
  126. // 新的地址
  127. $ygOrderInfo = Db::name('yg_order')->where('order_sn', str_replace('编号:', '', $order['order_sn']))->find();
  128. $newAddress_info = Db::name('user_address')->where('id', $ygOrderInfo['address_id'])->find();
  129. $order['shipping']['sp_id'] = 99999;
  130. $order['shipping']['sp_mobile'] = $newAddress_info['mobile'];
  131. $order['shipping']['sp_name'] = $newAddress_info['name'];
  132. $order['shipping']['sp_mergename'] = $newAddress_info['mergename'];
  133. }
  134. $this->assign('data', $order);
  135. return $this->fetch();
  136. }
  137. /**
  138. * @NodeAnotation(title="列表")
  139. */
  140. public function delivery ($orderId)
  141. {
  142. $express = Db::name('express')->field('id,name,code')->select()->toArray();
  143. $shipping = Db::name('shop_order_shipping')->where(['order_id' => $orderId])->findOrEmpty();
  144. $order = Db::name('shop_order')->where('order_id', $shipping['order_id'])->find();
  145. $this->assign('express', $express);
  146. if ($order['status'] == 5 || $order['status'] == 6){
  147. // 新的地址
  148. $ygOrderInfo = Db::name('yg_order')->where('order_sn', str_replace('编号:', '', $order['order_sn']))->find();
  149. $newAddress_info = Db::name('user_address')->where('id', $ygOrderInfo['address_id'])->find();
  150. $shipping['sp_id'] = 99999;
  151. $shipping['sp_mobile'] = $newAddress_info['mobile'];
  152. $shipping['sp_name'] = $newAddress_info['name'];
  153. $shipping['sp_mergename'] = $newAddress_info['mergename'];
  154. }
  155. $this->assign('shipping', $shipping);
  156. return $this->fetch();
  157. }
  158. /**
  159. * @NodeAnotation(title="列表")
  160. */
  161. public function deliverySave ()
  162. {
  163. if ($this->request->isPost()) {
  164. $data = $this->request->post();
  165. if (empty($data['order_id'])) {
  166. return json(['code' => 0, 'msg' => '参数错误']);
  167. }
  168. if (empty($data['ship_number']) || empty($data['ship_name'])) {
  169. return json(['code' => 0, 'msg' => '物流信息不能为空']);
  170. }
  171. $ex = explode('_', $data['ship_name']);
  172. // 发货
  173. if ($order_info = Db::name('shop_order')->where(['order_id' => $data['order_id']])->where('status', 'in', [5])->find()) {
  174. $res = Db::name('shop_order')->where(['order_id' => $data['order_id']])->save(['ship_code' => $ex[0], 'ship_name' => $ex[1], 'ship_number' => $data['ship_number'], 'status' => 6]);
  175. $yg_orderinfo = Db::name('yg_order')->where('order_sn', $order_info['order_sn'])->find();
  176. Db::name('yg_buy_record')->where('yg_id', $yg_orderinfo['id'])->where('status', 3)->save(['ship_code' => $ex[0], 'ship_name' => $ex[1], 'ship_number' => $data['ship_number'], 'status' => 4]);
  177. // 重新发货
  178. }elseif($order_info = Db::name('shop_order')->where(['order_id' => $data['order_id']])->where('status', 'in', [6])->find()){
  179. $res = Db::name('shop_order')->where(['order_id' => $data['order_id']])->save(['ship_code' => $ex[0], 'ship_name' => $ex[1], 'ship_number' => $data['ship_number'], 'status' => 6]);
  180. $yg_orderinfo = Db::name('yg_order')->where('order_sn', $order_info['order_sn'])->find();
  181. Db::name('yg_buy_record')->where('yg_id', $yg_orderinfo['id'])->where('status', 4)->save(['ship_code' => $ex[0], 'ship_name' => $ex[1], 'ship_number' => $data['ship_number']]);
  182. }else{
  183. $res = Db::name('shop_order')->where(['order_id' => $data['order_id']])->save(['ship_code' => $ex[0], 'ship_name' => $ex[1], 'ship_number' => $data['ship_number'],'status' => 2]);
  184. }
  185. if (!$res) {
  186. return json(['code' => 0, 'msg' => '操作失败']);
  187. }
  188. return json(['code' => 1, 'msg' => '操作成功']);
  189. }
  190. }
  191. }