OrderLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace app\admin\logic;
  3. use app\model\CommonModel;
  4. use app\model\GoodModel;
  5. use app\model\MasterOrder;
  6. use app\model\MasterOrderModel;
  7. use app\model\OrderModel;
  8. use app\model\ServiceModel;
  9. use think\Exception;
  10. use think\exception\ValidateException;
  11. use think\facade\Db;
  12. use think\facade\Validate;
  13. use think\response\Json;
  14. class OrderLogic extends BaseLogic
  15. {
  16. //列表
  17. public static function list(array $data = []): Json
  18. {
  19. $db = OrderModel::alias('a')
  20. ->leftJoin('company b', 'b.id=a.company_id AND b.is_del=' . CommonModel::$del_normal)
  21. ->leftJoin('card c', 'c.id=a.card_id AND c.is_del=' . CommonModel::$del_normal)
  22. ->leftJoin('account d', 'd.id=a.uid AND d.is_del=' . CommonModel::$del_normal)
  23. ->leftJoin('addr e', 'e.id=a.addr_id AND e.is_del=' . CommonModel::$del_normal)
  24. ->where('a.is_del', CommonModel::$del_normal);
  25. if ($data['masterOrderCode'] != '') $db->whereLike('a.masterOrderCode', '%' . $data['masterOrderCode'] . '%');
  26. if ($data['orderCode'] != '') $db->whereLike('a.orderCode', '%' . $data['orderCode'] . '%');
  27. if ($data['status'] != '') $db->where('a.status', $data['status']);
  28. if ($data['name'] !== '') $db->whereLike('d.name', '%' . $data['name'] . '%');
  29. if ($data['username']) $db->whereLike('d.username', '%' . $data['username'] . '%');
  30. if (($data['start_date'] != '') && ($data['end_date'] != '')) $db->whereBetween('a.addtime', [$data['start_date'], $data['end_date']]);
  31. if ($data['type'] !== '') $db->where('a.type', $data['type']);
  32. $count = $db->count('a.id');
  33. $list = $db
  34. ->field('a.id,a.masterOrderCode,a.orderCode,a.type,b.title company_title,c.title card_title,d.name,d.username,a.num,e.contactor,e.mobile,a.status,a.post_name,a.post_code,a.addtime,e.addr_code,"" addr_name,e.addr,"" good_code,"" good_name,""good_cover_img,a.child_id')
  35. ->page($data['page'], $data['size'])
  36. ->withAttr('addr_code', function ($val) {
  37. return explode(',', $val);
  38. })
  39. ->withAttr('addr_name', function ($val, $da) {
  40. return $da['addr_code'] ? get_addr_name($da['addr_code']) : '';
  41. })
  42. ->order('a.addtime desc')
  43. ->select()
  44. ->toArray();
  45. //所有服务
  46. $service = ServiceModel::where('is_del', CommonModel::$del_normal)
  47. ->whereIn('id', array_column($list, 'child_id'))
  48. ->column('title', 'id');
  49. $good = GoodModel::where('is_del', CommonModel::$del_normal)
  50. ->whereIn('id', array_column($list, 'child_id'))
  51. ->column('good_cover_img,good_code,good_name', 'id');
  52. foreach ($list as &$order) {
  53. if ($order['type'] == MasterOrderModel::$type_service) $order['good_name'] = $service[$order['child_id']] ?? '';
  54. else $order = array_merge($order, $good[$order['child_id']]);
  55. }
  56. return json_show(CommonModel::$success, '获取订单列表成功', ['count' => $count, 'list' => $list]);
  57. }
  58. //详情
  59. public static function read(int $id = 0): Json
  60. {
  61. $rs = $db = OrderModel::alias('a')
  62. ->field('a.id,a.orderCode,a.type,b.title company_title,c.title card_title,d.name,d.username,a.num,e.contactor,e.mobile,e.addr_code,e.addr,a.status,a.post_name,a.post_code,a.addtime,"" addr_name,"" good_code,"" good_name,"" good_cover_img,a.child_id')
  63. ->leftJoin('company b', 'b.id=a.company_id AND b.is_del=' . CommonModel::$del_normal)
  64. ->leftJoin('card c', 'c.id=a.card_id AND c.is_del=' . CommonModel::$del_normal)
  65. ->leftJoin('account d', 'd.id=a.uid AND d.is_del=' . CommonModel::$del_normal)
  66. ->leftJoin('addr e', 'e.id=a.addr_id AND e.is_del=' . CommonModel::$del_normal)
  67. ->where(['a.is_del' => CommonModel::$del_normal, 'a.id' => $id])
  68. ->withAttr('addr_code', function ($val) {
  69. return explode(',', $val);
  70. })
  71. ->withAttr('addr_name', function ($val, $da) {
  72. return $da['addr_code'] ? get_addr_name($da['addr_code']) : '';
  73. })
  74. ->findOrEmpty()
  75. ->toArray();
  76. if ($rs['type'] == MasterOrderModel::$type_service) $rs['good_name'] = ServiceModel::where(['is_del' => CommonModel::$del_normal, 'id' => $rs['child_id']])->value('title');
  77. else $rs = array_merge($rs, GoodModel::where(['is_del' => CommonModel::$del_normal, 'id' => $rs['child_id']])->field('good_code,good_name,good_cover_img')->findOrEmpty()->toArray());
  78. return json_show(CommonModel::$success, '获取商品详情成功', $rs);
  79. }
  80. //导出
  81. public static function export(array $data = []): Json
  82. {
  83. $da = [];
  84. try {
  85. verify_token($data['token']);
  86. $db = OrderModel::alias('a')
  87. ->leftJoin('company b', 'b.id=a.company_id AND b.is_del=' . CommonModel::$del_normal)
  88. ->leftJoin('card c', 'c.id=a.card_id AND c.is_del=' . CommonModel::$del_normal)
  89. ->leftJoin('account d', 'd.id=a.uid AND d.is_del=' . CommonModel::$del_normal)
  90. ->leftJoin('addr e', 'e.id=a.addr_id AND e.is_del=' . CommonModel::$del_normal)
  91. ->where('a.is_del', CommonModel::$del_normal);
  92. if ($data['masterOrderCode'] != '') $db->whereLike('a.masterOrderCode', '%' . $data['masterOrderCode'] . '%');
  93. if ($data['orderCode'] != '') $db->whereLike('a.orderCode', '%' . $data['orderCode'] . '%');
  94. if ($data['status'] != '') $db->where('a.status', $data['status']);
  95. if ($data['name'] !== '') $db->whereLike('d.name', '%' . $data['name'] . '%');
  96. if ($data['username']) $db->whereLike('d.username', '%' . $data['username'] . '%');
  97. if (($data['start_date'] != '') && ($data['end_date'] != '')) $db->whereBetween('a.addtime', [$data['start_date'], $data['end_date']]);
  98. if ($data['type'] !== '') $db->where('a.type', $data['type']);
  99. $list = $db
  100. ->field('a.id `订单ID`,a.masterOrderCode `主订单编号`,a.orderCode `订单编号`,a.type `订单类型`,b.title `业务公司`,c.title `卡类型`,d.name `购买用户名`,d.username `购买账号`,a.num `购买数量`,e.contactor `收货联系人`,e.mobile `收货联系电话`,a.status `状态`,a.post_name `物流公司`,a.post_code `物流单号`,a.addtime `创建时间`,e.addr_code,"" `收货地址`,e.addr,"" `商品编码`,"" `商品(服务)名称`,a.child_id')
  101. ->order('a.addtime desc')
  102. ->cursor();
  103. $status = [
  104. MasterOrderModel::$order_status_wait_pay => '待支付',
  105. MasterOrderModel::$order_status_pay_fail => '支付失败',
  106. MasterOrderModel::$order_status_not_deliver => '待发货',
  107. MasterOrderModel::$order_status_deliver => '已发货',
  108. MasterOrderModel::$order_status_receipt => '已收货',
  109. ];
  110. $type = [MasterOrderModel::$type_exchange_good => '兑换商品订单', MasterOrderModel::$type_shopping_good => '商城商品订单', MasterOrderModel::$type_service => '服务订单'];
  111. foreach ($list as $v) {
  112. $v = $v->toArray();
  113. if ($v['订单类型'] == MasterOrderModel::$type_service) $v['商品(服务)名称'] = ServiceModel::where(['is_del' => CommonModel::$del_normal, 'id' => $v['child_id']])->value('title', '');
  114. else {
  115. $good = GoodModel::where(['is_del' => CommonModel::$del_normal, 'id' => $v['child_id']])
  116. ->field('good_code,good_name')
  117. ->findOrEmpty()
  118. ->toArray();
  119. $v['商品编码'] = $good['good_code'] ?? '';
  120. $v['商品(服务)名称'] = $good['good_name'] ?? '';
  121. };
  122. $v['收货地址'] = $v['addr_code'] ? get_addr_name($v['addr_code']) . $v['addr'] : $v['addr'];
  123. $v['状态'] = $status[$v['状态']] ?? '';
  124. $v['订单类型'] = $type[$v['订单类型']] ?? '';
  125. unset($v['addr_code']);
  126. unset($v['addr']);
  127. unset($v['child_id']);
  128. $da[] = $v;
  129. }
  130. if (empty($da)) throw new Exception('没有可供导出的数据');
  131. } catch (Exception $exception) {
  132. $da = [[$exception->getMessage()]];
  133. } catch (ValidateException $validateException) {
  134. $da = [[$validateException->getError()]];
  135. }
  136. $headerArr = array_keys($da[0]);
  137. excel_save('订单导出' . date('YmdHis'), $headerArr, $da);
  138. }
  139. //发货
  140. public static function deliver(array $list = [])
  141. {
  142. $db = new OrderModel();
  143. $temp = $db
  144. ->field('id,orderCode')
  145. ->where(['is_del' => CommonModel::$del_normal])
  146. ->where('status', '<>', MasterOrderModel::$order_status_not_deliver)
  147. ->whereIn('orderCode', array_column($list, 'orderCode'))
  148. ->findOrEmpty();
  149. if (!$temp->isEmpty()) return json_show(CommonModel::$error_param, $temp->orderCode . '订单并不是未发货状态');
  150. $val = Validate::rule([
  151. 'orderCode|订单编号' => 'require|length:18',
  152. 'post_name|物流公司' => 'require|max:255',
  153. 'post_code|物流单号' => 'require|max:255',
  154. ]);
  155. Db::startTrans();
  156. try {
  157. $date = date('Y-m-d H:i:s');
  158. $insert_express_data = [];
  159. foreach ($list as $order) {
  160. if (!$val->check($order)) throw new Exception($val->getError());
  161. $db->where([
  162. 'is_del' => CommonModel::$del_normal,
  163. 'orderCode' => $order['orderCode'],
  164. 'status' => MasterOrderModel::$order_status_not_deliver,
  165. ])->save([
  166. 'status' => MasterOrderModel::$order_status_deliver,
  167. 'post_name' => $order['post_name'],
  168. 'post_code' => $order['post_code'],
  169. 'updater' => self::$uname,
  170. 'updaterid' => self::$uid,
  171. 'updatetime' => $date,
  172. ]);
  173. $insert_express_data[] = [
  174. 'post_code' => $order['post_code'],
  175. 'post_data' => '',
  176. 'status' => 1,
  177. 'addtime' => $date,
  178. 'updatetime' => $date,
  179. ];
  180. }
  181. if ($insert_express_data) Db::name('express_data')->insertAll($insert_express_data);
  182. Db::commit();
  183. return json_show(CommonModel::$success, '发货成功');
  184. } catch (Exception $exception) {
  185. Db::rollback();
  186. return json_show(CommonModel::$error_param, '发货失败,' . $exception->getMessage());
  187. }
  188. }
  189. }