DeliveryBatchLogic.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\shop\logic\order;
  20. use app\common\basics\Logic;
  21. use app\common\model\Express;
  22. use app\common\model\order\DeliveryBatch;
  23. use app\common\model\order\DeliveryBatchInfo;
  24. use app\common\model\order\Order;
  25. use app\shopapi\validate\OrderValidate;
  26. use think\facade\Db;
  27. class DeliveryBatchLogic extends Logic
  28. {
  29. static function lists($get)
  30. {
  31. $lists = DeliveryBatch::where('shop_id', session('shop_info.shop_id'))
  32. ->order('id desc')
  33. ->paginate([
  34. 'page' => $get['page'] ?? 1,
  35. 'list_rows' => $get['limit'] ?? 10,
  36. 'var_page' => 'page'
  37. ])->toArray();
  38. return [ 'count'=>$lists['total'], 'lists'=>$lists['data'] ];
  39. }
  40. static function detail($id)
  41. {
  42. return DeliveryBatch::findOrEmpty($id)->append([ 'progress' ])->toArray();
  43. }
  44. static function getFailInfoLists($id)
  45. {
  46. return DeliveryBatchInfo::where('batch_id', $id)->where('status', 2)->select()->toArray();
  47. }
  48. static function delivery($detail)
  49. {
  50. if ($detail['status'] == 1) {
  51. return true;
  52. }
  53. $infoLists = DeliveryBatchInfo::where('batch_id', $detail['id'])->select()->toArray();
  54. foreach ($infoLists as $info) {
  55. // 订单
  56. $order = Order::where('order_sn', $info['sn'])->find();
  57. if (empty($order['id'])) {
  58. DeliveryBatch::update([ 'fail' => Db::raw('fail+1') ], [ [ 'id' ,'=', $detail['id'] ] ]);
  59. DeliveryBatchInfo::update([ 'status' => 2, 'fail_content' => '订单不存在' ], [ [ 'id', '=', $info['id'] ] ]);
  60. continue;
  61. }
  62. // 快递
  63. $express = Express::where('name', $info['express_name'])->find();
  64. if (empty($express['id'])) {
  65. DeliveryBatch::update([ 'fail' => Db::raw('fail+1') ], [ [ 'id' ,'=', $detail['id'] ] ]);
  66. DeliveryBatchInfo::update([ 'status' => 2, 'fail_content' => '快递公司不存在' ], [ [ 'id', '=', $info['id'] ] ]);
  67. continue;
  68. }
  69. // 检测
  70. $validate = new orderValidate;
  71. $data = [
  72. 'id' => $order['id'],
  73. 'send_type' => 1,
  74. 'shipping_id' => $express['id'],
  75. 'invoice_no' => $info['express_no'],
  76. 'shop_id' => session('shop_info.shop_id'),
  77. 'order_id' => $order['id'],
  78. ];
  79. if (true !== $validate->scene('delivery')->check($data)) {
  80. DeliveryBatch::update([ 'fail' => Db::raw('fail+1') ], [ [ 'id' ,'=', $detail['id'] ] ]);
  81. DeliveryBatchInfo::update([ 'status' => 2, 'fail_content' => $validate->getError() ], [ [ 'id', '=', $info['id'] ] ]);
  82. continue;
  83. }
  84. // 发货
  85. OrderLogic::deliveryHandle($data, session('admin_info.id'));
  86. // 成功
  87. DeliveryBatch::update([ 'success' => Db::raw('success+1') ], [ [ 'id' ,'=', $detail['id'] ] ]);
  88. DeliveryBatchInfo::update([ 'status' => 1 ], [ [ 'id', '=', $info['id'] ] ]);
  89. }
  90. // 更新 已执行导入
  91. DeliveryBatch::update([ 'status' => 1 ], [ [ 'id' ,'=', $detail['id'] ] ]);
  92. return true;
  93. }
  94. static function importLists(array $lists, $filename)
  95. {
  96. try {
  97. Db::startTrans();
  98. array_shift($lists);
  99. $lists = array_values($lists);
  100. $nums = count($lists);
  101. $batch = DeliveryBatch::create([
  102. 'filename' => $filename,
  103. 'nums' => $nums,
  104. 'success' => 0,
  105. 'fail' => 0,
  106. 'shop_id' => session('shop_info.shop_id')
  107. ]);
  108. $batchInfoArrays = [];
  109. foreach ($lists as $info) {
  110. $batchInfoArrays[] = [
  111. 'batch_id' => $batch->id,
  112. 'sn' => trim($info['A']),
  113. 'express_name' => trim($info['B']),
  114. 'express_no' => trim($info['C']),
  115. 'status' => 0,
  116. 'fail_content' => '',
  117. 'shop_id' => session('shop_info.shop_id')
  118. ];
  119. }
  120. (new DeliveryBatchInfo())->saveAll($batchInfoArrays);
  121. Db::commit();
  122. return [ 'id' => $batch->id ];
  123. } catch(\Throwable $e) {
  124. Db::rollback();
  125. return $e->getMessage();
  126. }
  127. }
  128. }