ShopOrder.php 9.8 KB

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