Order.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace app\data\controller\shop;
  3. use app\data\model\BaseUserPayment;
  4. use app\data\model\DataUser;
  5. use app\data\model\ShopOrder;
  6. use app\data\model\ShopOrderSend;
  7. use app\data\service\OrderService;
  8. use app\data\service\PaymentService;
  9. use app\data\service\UserAdminService;
  10. use think\admin\Controller;
  11. use think\admin\extend\CodeExtend;
  12. use think\admin\service\AdminService;
  13. use think\exception\HttpResponseException;
  14. /**
  15. * 订单数据管理
  16. * Class Order
  17. * @package app\data\controller\shop
  18. */
  19. class Order extends Controller
  20. {
  21. /**
  22. * 支付方式
  23. * @var array
  24. */
  25. protected $payments = [];
  26. /**
  27. * 控制器初始化
  28. */
  29. protected function initialize()
  30. {
  31. parent::initialize();
  32. //$this->payments = PaymentService::getTypeAll();
  33. $this->payments = [
  34. [
  35. 'name'=>'支付宝支付'
  36. ],
  37. [
  38. 'name'=>'微信支付'
  39. ]
  40. ];
  41. // // 读取支付通道配置
  42. // $query = BaseUserPayment::mk()->where(['status' => 1, 'deleted' => 0]);
  43. // //$query->whereIn('code', str2arr($payments))->whereIn('type', PaymentService::getTypeApi($this->type));
  44. // $result = $query->order('sort desc,id desc')->column('type,code,name,cover,content,remark', 'code');
  45. // foreach ($result as &$vo) $vo['content'] = ['voucher_qrcode' => json_decode($vo['content'])->voucher_qrcode ?? ''];
  46. // $this->payments2 = array_values($result);
  47. // dump($this->payments2);
  48. }
  49. /**
  50. * 订单数据管理
  51. * @auth true
  52. * @menu true
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. */
  57. public function index()
  58. {
  59. $this->title = '订单数据管理';
  60. if(AdminService::getUserId()==10000){
  61. $admin_id = [];
  62. }
  63. else{
  64. $admin_id['admin_id']=AdminService::getUserId();
  65. }
  66. // 状态数据统计
  67. $this->total = ['t0' => 0, 't2' => 0, 't3' => 0, 't4' => 0, 't5' => 0, 't6' => 0, 't7' => 0, 't8' => 0, 'ta' => 0];
  68. foreach (ShopOrder::mk()->where($admin_id)->field('status,count(1) total')
  69. ->group('status')->cursor() as $vo) {
  70. [$this->total["t{$vo['status']}"] = $vo['total'], $this->total["ta"] += $vo['total']];
  71. }
  72. // 订单列表查询
  73. $query = ShopOrder::mQuery()->where($admin_id);
  74. $query->like('order_no,truck_name,truck_phone,truck_province|truck_area|truck_address#address,truck_send_no,truck_send_name');
  75. $query->equal('status,payment_type,payment_status')->dateBetween('create_at,payment_datetime,cancel_datetime,truck_datetime,truck_send_datetime');
  76. // 发货信息搜索
  77. $db = ShopOrderSend::mQuery()->like('address_name#truck_address_name,address_phone#truck_address_phone,address_province|address_city|address_area|address_content#truck_address_content')->db();
  78. if ($db->getOptions('where')) $query->whereRaw("order_no in {$db->field('order_no')->buildSql()}");
  79. // 用户搜索查询
  80. $db = DataUser::mQuery()->like('phone|nickname#user_keys')->db();
  81. if ($db->getOptions('where')) $query->whereRaw("uuid in {$db->field('id')->buildSql()}");
  82. // 代理搜索查询
  83. $db = DataUser::mQuery()->like('phone|nickname#from_keys')->db();
  84. if ($db->getOptions('where')) $query->whereRaw("puid1 in {$db->field('id')->buildSql()}");
  85. // 列表选项卡
  86. if (is_numeric($this->type = trim(input('type', 'ta'), 't'))) {
  87. $query->where(['status' => $this->type]);
  88. }
  89. $this->amount = $query->db()->sum('amount_total');
  90. // 分页排序处理
  91. $query->where(['deleted_status' => 0])->order('id desc')->page();
  92. }
  93. /**
  94. * 订单列表处理
  95. * @param array $data
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\DbException
  98. * @throws \think\db\exception\ModelNotFoundException
  99. */
  100. protected function _index_page_filter(array &$data)
  101. {
  102. UserAdminService::buildByUid($data);
  103. UserAdminService::buildByUid($data, 'puid1', 'from');
  104. OrderService::buildData($data);
  105. foreach ($data as &$vo) {
  106. if (!is_null($vo['payment_type']) and '' != $vo['payment_type']) {
  107. $vo['payment_name'] = PaymentService::name($vo['payment_type']);
  108. }
  109. }
  110. }
  111. /**
  112. * 单据凭证支付审核
  113. * @auth true
  114. * @throws \think\db\exception\DataNotFoundException
  115. * @throws \think\db\exception\DbException
  116. * @throws \think\db\exception\ModelNotFoundException
  117. */
  118. public function audit()
  119. {
  120. if ($this->request->isGet()) {
  121. ShopOrder::mForm('', 'order_no');
  122. } else {
  123. $data = $this->_vali([
  124. 'order_no.require' => '订单单号不能为空!',
  125. 'status.in:0,1' => '审核状态数值异常!',
  126. 'status.require' => '审核状态不能为空!',
  127. 'remark.default' => '',
  128. ]);
  129. if (empty($data['status'])) {
  130. $data['status'] = 0;
  131. $data['cancel_status'] = 1;
  132. $data['cancel_remark'] = $data['remark'] ?: '后台审核驳回并取消订单';
  133. $data['cancel_datetime'] = date('Y-m-d H:i:s');
  134. } else {
  135. $data['status'] = 4;
  136. $data['payment_code'] = CodeExtend::uniqidDate(20, 'T');
  137. $data['payment_status'] = 1;
  138. $data['payment_remark'] = $data['remark'] ?: '后台审核支付凭证通过';
  139. $data['payment_datetime'] = date('Y-m-d H:i:s');
  140. }
  141. $order = ShopOrder::mk()->where(['order_no' => $data['order_no']])->find();
  142. if (empty($order) || $order['status'] !== 3) $this->error('不允许操作审核!');
  143. // 无需发货时的处理
  144. if ($data['status'] === 4 && empty($order['truck_type'])) $data['status'] = 6;
  145. // 更新订单支付状态
  146. $map = ['status' => 3, 'order_no' => $data['order_no']];
  147. if (ShopOrder::mk()->strict(false)->where($map)->update($data) !== false) {
  148. if (in_array($data['status'], [4, 5, 6])) {
  149. $this->app->event->trigger('ShopOrderPayment', $data['order_no']);
  150. $this->success('订单审核通过成功!');
  151. } else {
  152. $this->app->event->trigger('ShopOrderCancel');
  153. OrderService::stock($data['order_no']);
  154. $this->success('审核驳回并取消成功!');
  155. }
  156. } else {
  157. $this->error('订单审核失败!');
  158. }
  159. }
  160. }
  161. /**
  162. * 清理订单数据
  163. * @auth true
  164. */
  165. public function clean()
  166. {
  167. $this->_queue('定时清理无效订单数据', "xdata:OrderClean", 0, [], 0, 60);
  168. }
  169. /**
  170. * 取消未支付的订单
  171. * @auth true
  172. * @throws \think\db\exception\DataNotFoundException
  173. * @throws \think\db\exception\DbException
  174. * @throws \think\db\exception\ModelNotFoundException
  175. */
  176. public function cancel()
  177. {
  178. //$map = $this->_vali(['order_no.require' => '订单号不能为空!',]);
  179. $data = input();
  180. $map = [];
  181. if(!isset($data['order_no']) || $data == '' || $data == null){
  182. $this->error('订单号不能为空!');
  183. }
  184. $map['order_no'] = $data['order_no'];
  185. $order = ShopOrder::mk()->where($map)->find();
  186. if (empty($order)) $this->error('订单查询异常!');
  187. if (!in_array($order['status'], [1, 2, 3])) $this->error('订单不能取消!');
  188. try {
  189. $result = $order->save([
  190. 'status' => 0,
  191. 'cancel_status' => 1,
  192. 'cancel_remark' => '后台取消未支付的订单',
  193. 'cancel_datetime' => date('Y-m-d H:i:s'),
  194. ]);
  195. if ($result !== false) {
  196. OrderService::stock($order['order_no']);
  197. $this->app->event->trigger('ShopOrderCancel', $order['order_no']);
  198. $this->success('取消未支付的订单成功!');
  199. } else {
  200. $this->error('取消支付的订单失败!');
  201. }
  202. } catch (HttpResponseException $exception) {
  203. throw $exception;
  204. } catch (\Exception $exception) {
  205. $this->error($exception->getMessage());
  206. }
  207. }
  208. }