Order.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace app\data\controller\shop;
  3. use app\data\service\ExpressService;
  4. use app\data\service\OrderService;
  5. use app\data\service\PaymentService;
  6. use app\data\service\UserAdminService;
  7. use think\admin\Controller;
  8. use think\admin\extend\CodeExtend;
  9. use think\exception\HttpResponseException;
  10. /**
  11. * 订单数据管理
  12. * Class Order
  13. * @package app\data\controller\shop
  14. */
  15. class Order extends Controller
  16. {
  17. /**
  18. * 绑定数据表
  19. * @var string
  20. */
  21. private $table = 'ShopOrder';
  22. /**
  23. * 支付方式
  24. * @var array
  25. */
  26. protected $payments = [];
  27. /**
  28. * 控制器初始化
  29. */
  30. protected function initialize()
  31. {
  32. parent::initialize();
  33. $this->payments = PaymentService::types();
  34. }
  35. /**
  36. * 订单数据管理
  37. * @auth true
  38. * @menu true
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function index()
  44. {
  45. $this->title = '订单数据管理';
  46. // 状态数据统计
  47. $this->total = ['t0' => 0, 't1' => 0, 't2' => 0, 't3' => 0, 't4' => 0, 't5' => 0, 't6' => 0, 'ta' => 0];
  48. foreach ($this->app->db->name($this->table)->field('status,count(1) total')->group('status')->cursor() as $vo) {
  49. [$this->total["t{$vo['status']}"] = $vo['total'], $this->total["ta"] += $vo['total']];
  50. }
  51. // 订单列表查询
  52. $query = $this->_query($this->table);
  53. $query->like('order_no,truck_name,truck_phone,truck_province|truck_area|truck_address#address,truck_send_no,truck_send_name');
  54. $query->equal('status,payment_type,payment_status')->dateBetween('create_at,payment_datetime,cancel_datetime,truck_datetime,truck_send_datetime');
  55. // 发货信息搜索
  56. $db = $this->_query('ShopOrderSend')->like('address_name#truck_address_name,address_phone#truck_address_phone,address_province|address_city|address_area|address_content#truck_address_content')->db();
  57. if ($db->getOptions('where')) $query->whereRaw("order_no in {$db->field('order_no')->buildSql()}");
  58. // 用户搜索查询
  59. $db = $this->_query('DataUser')->like('phone#user_phone,nickname#user_nickname')->db();
  60. if ($db->getOptions('where')) $query->whereRaw("uid in {$db->field('id')->buildSql()}");
  61. // 代理搜索查询
  62. $db = $this->_query('DataUser')->like('phone#from_phone,nickname#from_nickname')->db();
  63. if ($db->getOptions('where')) $query->whereRaw("puid1 in {$db->field('id')->buildSql()}");
  64. // 列表选项卡
  65. if (is_numeric($this->type = trim(input('type', 'ta'), 't'))) {
  66. $query->where(['status' => $this->type]);
  67. }
  68. // 分页排序处理
  69. $query->where(['deleted_status' => 0])->order('id desc')->page();
  70. }
  71. /**
  72. * 订单列表处理
  73. * @param array $data
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\DbException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. */
  78. protected function _index_page_filter(array &$data)
  79. {
  80. UserAdminService::instance()->buildByUid($data);
  81. UserAdminService::instance()->buildByUid($data, 'puid1', 'from');
  82. OrderService::instance()->buildData($data);
  83. foreach ($data as &$vo) $vo['payment_name'] = PaymentService::name($vo['payment_type']);
  84. }
  85. /**
  86. * 修改快递管理
  87. * @auth true
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\DbException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. */
  92. public function truck()
  93. {
  94. if ($this->request->isGet()) {
  95. $map = ['deleted' => 0, 'status' => 1];
  96. $query = $this->app->db->name('BasePostageCompany')->where($map);
  97. $this->items = $query->order('sort desc,id desc')->select()->toArray();
  98. }
  99. $this->_form('ShopOrderSend', '', 'order_no');
  100. }
  101. /**
  102. * 快递表单处理
  103. * @param array $vo
  104. * @throws \think\db\exception\DataNotFoundException
  105. * @throws \think\db\exception\DbException
  106. * @throws \think\db\exception\ModelNotFoundException
  107. */
  108. protected function _truck_form_filter(array &$vo)
  109. {
  110. if ($this->request->isPost()) {
  111. $map = ['order_no' => $vo['order_no']];
  112. $order = $this->app->db->name('ShopOrder')->where($map)->find();
  113. if (empty($order)) $this->error('订单查询异常,请稍候再试!');
  114. // 配送快递公司填写
  115. $map = ['code_1|code_2|code_3' => $vo['company_code']];
  116. $company = $this->app->db->name('BasePostageCompany')->where($map)->find();
  117. if (empty($company)) $this->error('配送快递公司异常,请重新选择快递公司!');
  118. $vo['status'] = 2;
  119. $vo['company_name'] = $company['name'];
  120. $vo['send_datetime'] = $vo['send_datetime'] ?? date('Y-m-d H:i:s');
  121. // 更新订单发货状态
  122. if ($order['status'] === 3) {
  123. $map = ['order_no' => $vo['order_no']];
  124. $this->app->db->name('ShopOrder')->where($map)->update(['status' => 4]);
  125. }
  126. }
  127. }
  128. /**
  129. * 快递追踪查询
  130. * @auth true
  131. */
  132. public function truckQuery()
  133. {
  134. try {
  135. $data = $this->_vali([
  136. 'code.require' => '快递编号不能为空!',
  137. 'number.require' => '配送单号不能为空!',
  138. ]);
  139. $this->result = ExpressService::instance()->query($data['code'], $data['number']);
  140. if (empty($this->result['code'])) $this->error($this->result['info']);
  141. $this->fetch('truck_query');
  142. } catch (HttpResponseException $exception) {
  143. throw $exception;
  144. } catch (\Exception $exception) {
  145. $this->error($exception->getMessage());
  146. }
  147. }
  148. /**
  149. * 单据凭证支付审核
  150. * @auth true
  151. * @throws \think\db\exception\DataNotFoundException
  152. * @throws \think\db\exception\DbException
  153. * @throws \think\db\exception\ModelNotFoundException
  154. */
  155. public function audit()
  156. {
  157. if ($this->request->isGet()) {
  158. $this->_form($this->table, '', 'order_no');
  159. } else {
  160. $data = $this->_vali([
  161. 'order_no.require' => '订单单号不能为空!',
  162. 'status.in:0,1' => '审核状态数值异常!',
  163. 'status.require' => '审核状态不能为空!',
  164. 'remark.default' => '',
  165. ]);
  166. if (empty($data['status'])) {
  167. $data['status'] = 0;
  168. $data['cancel_status'] = 1;
  169. $data['cancel_remark'] = $data['remark'] ?: '后台审核驳回并取消订单';
  170. $data['cancel_datetime'] = date('Y-m-d H:i:s');
  171. } else {
  172. $data['status'] = 4;
  173. $data['payment_code'] = CodeExtend::uniqidDate(20, 'T');
  174. $data['payment_status'] = 1;
  175. $data['payment_remark'] = $data['remark'] ?: '后台审核支付凭证通过';
  176. $data['payment_datetime'] = date('Y-m-d H:i:s');
  177. }
  178. $order = $this->app->db->name($this->table)->where(['order_no' => $data['order_no']])->find();
  179. if (empty($order) || $order['status'] !== 3) $this->error('不允许操作审核!');
  180. // 无需发货时的处理
  181. if ($data['status'] === 4 && empty($order['truck_type'])) $data['status'] = 6;
  182. // 更新订单支付状态
  183. $map = ['status' => 3, 'order_no' => $data['order_no']];
  184. if ($this->app->db->name($this->table)->strict(false)->where($map)->update($data) !== false) {
  185. if (in_array($data['status'], [4, 5, 6])) {
  186. $this->app->event->trigger('ShopOrderPayment', $data['order_no']);
  187. $this->success('订单审核通过成功!');
  188. } else {
  189. $this->app->event->trigger('ShopOrderCancel');
  190. OrderService::instance()->stock($data['order_no']);
  191. $this->success('审核驳回并取消成功!');
  192. }
  193. } else {
  194. $this->error('订单审核失败!');
  195. }
  196. }
  197. }
  198. /**
  199. * 清理订单数据
  200. * @auth true
  201. */
  202. public function clean()
  203. {
  204. $this->_queue('定时清理无效订单数据', "xdata:OrderClean", 0, [], 0, 60);
  205. }
  206. /**
  207. * 取消未支付的订单
  208. * @auth true
  209. * @throws \think\db\exception\DataNotFoundException
  210. * @throws \think\db\exception\DbException
  211. * @throws \think\db\exception\ModelNotFoundException
  212. */
  213. public function cancel()
  214. {
  215. $map = $this->_vali(['order_no.require' => '订单号不能为空!',]);
  216. $order = $this->app->db->name($this->table)->where($map)->find();
  217. if (empty($order)) $this->error('订单查询异常!');
  218. if (!in_array($order['status'], [1, 2, 3])) $this->error('订单不能取消!');
  219. try {
  220. $result = $this->app->db->name($this->table)->where($map)->update([
  221. 'status' => 0,
  222. 'cancel_status' => 1,
  223. 'cancel_remark' => '后台取消未支付的订单',
  224. 'cancel_datetime' => date('Y-m-d H:i:s'),
  225. ]);
  226. if ($result !== false) {
  227. OrderService::instance()->stock($order['order_no']);
  228. $this->app->event->trigger('ShopOrderCancel', $order['order_no']);
  229. $this->success('取消未支付的订单成功!');
  230. } else {
  231. $this->error('取消支付的订单失败!');
  232. }
  233. } catch (HttpResponseException $exception) {
  234. throw $exception;
  235. } catch (\Exception $exception) {
  236. $this->error($exception->getMessage());
  237. }
  238. }
  239. }