| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- // +----------------------------------------------------------------------
- // | likeshop开源商城系统
- // +----------------------------------------------------------------------
- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
- // | gitee下载:https://gitee.com/likeshop_gitee
- // | github下载:https://github.com/likeshop-github
- // | 访问官网:https://www.likeshop.cn
- // | 访问社区:https://home.likeshop.cn
- // | 访问手册:http://doc.likeshop.cn
- // | 微信公众号:likeshop技术社区
- // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
- // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
- // | 禁止对系统程序代码以任何目的,任何形式的再发布
- // | likeshop团队版权所有并拥有最终解释权
- // +----------------------------------------------------------------------
- // | author: likeshop.cn.team
- // +----------------------------------------------------------------------
- namespace app\shop\logic\order;
- use app\common\basics\Logic;
- use app\common\model\Express;
- use app\common\model\order\DeliveryBatch;
- use app\common\model\order\DeliveryBatchInfo;
- use app\common\model\order\Order;
- use app\shopapi\validate\OrderValidate;
- use think\facade\Db;
- class DeliveryBatchLogic extends Logic
- {
- static function lists($get)
- {
- $lists = DeliveryBatch::where('shop_id', session('shop_info.shop_id'))
- ->order('id desc')
- ->paginate([
- 'page' => $get['page'] ?? 1,
- 'list_rows' => $get['limit'] ?? 10,
- 'var_page' => 'page'
- ])->toArray();
-
- return [ 'count'=>$lists['total'], 'lists'=>$lists['data'] ];
- }
-
- static function detail($id)
- {
- return DeliveryBatch::findOrEmpty($id)->append([ 'progress' ])->toArray();
- }
-
- static function getFailInfoLists($id)
- {
- return DeliveryBatchInfo::where('batch_id', $id)->where('status', 2)->select()->toArray();
- }
-
- static function delivery($detail)
- {
- if ($detail['status'] == 1) {
- return true;
- }
- $infoLists = DeliveryBatchInfo::where('batch_id', $detail['id'])->select()->toArray();
- foreach ($infoLists as $info) {
- // 订单
- $order = Order::where('order_sn', $info['sn'])->find();
- if (empty($order['id'])) {
- DeliveryBatch::update([ 'fail' => Db::raw('fail+1') ], [ [ 'id' ,'=', $detail['id'] ] ]);
- DeliveryBatchInfo::update([ 'status' => 2, 'fail_content' => '订单不存在' ], [ [ 'id', '=', $info['id'] ] ]);
- continue;
- }
- // 快递
- $express = Express::where('name', $info['express_name'])->find();
- if (empty($express['id'])) {
- DeliveryBatch::update([ 'fail' => Db::raw('fail+1') ], [ [ 'id' ,'=', $detail['id'] ] ]);
- DeliveryBatchInfo::update([ 'status' => 2, 'fail_content' => '快递公司不存在' ], [ [ 'id', '=', $info['id'] ] ]);
- continue;
- }
- // 检测
- $validate = new orderValidate;
- $data = [
- 'id' => $order['id'],
- 'send_type' => 1,
- 'shipping_id' => $express['id'],
- 'invoice_no' => $info['express_no'],
- 'shop_id' => session('shop_info.shop_id'),
- 'order_id' => $order['id'],
- ];
- if (true !== $validate->scene('delivery')->check($data)) {
- DeliveryBatch::update([ 'fail' => Db::raw('fail+1') ], [ [ 'id' ,'=', $detail['id'] ] ]);
- DeliveryBatchInfo::update([ 'status' => 2, 'fail_content' => $validate->getError() ], [ [ 'id', '=', $info['id'] ] ]);
- continue;
- }
-
- // 发货
- OrderLogic::deliveryHandle($data, session('admin_info.id'));
- // 成功
- DeliveryBatch::update([ 'success' => Db::raw('success+1') ], [ [ 'id' ,'=', $detail['id'] ] ]);
- DeliveryBatchInfo::update([ 'status' => 1 ], [ [ 'id', '=', $info['id'] ] ]);
- }
-
- // 更新 已执行导入
- DeliveryBatch::update([ 'status' => 1 ], [ [ 'id' ,'=', $detail['id'] ] ]);
-
- return true;
- }
-
- static function importLists(array $lists, $filename)
- {
- try {
- Db::startTrans();
-
- array_shift($lists);
- $lists = array_values($lists);
- $nums = count($lists);
-
- $batch = DeliveryBatch::create([
- 'filename' => $filename,
- 'nums' => $nums,
- 'success' => 0,
- 'fail' => 0,
- 'shop_id' => session('shop_info.shop_id')
- ]);
-
- $batchInfoArrays = [];
-
- foreach ($lists as $info) {
-
- $batchInfoArrays[] = [
- 'batch_id' => $batch->id,
- 'sn' => trim($info['A']),
- 'express_name' => trim($info['B']),
- 'express_no' => trim($info['C']),
- 'status' => 0,
- 'fail_content' => '',
- 'shop_id' => session('shop_info.shop_id')
- ];
- }
-
- (new DeliveryBatchInfo())->saveAll($batchInfoArrays);
-
- Db::commit();
- return [ 'id' => $batch->id ];
- } catch(\Throwable $e) {
- Db::rollback();
- return $e->getMessage();
- }
- }
- }
|