BuyService.php 66.6 KB
Newer Older
D
v1.2.0  
devil_gong 已提交
1 2 3 4
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
D
devil_gong 已提交
5
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
D
v1.2.0  
devil_gong 已提交
6 7 8 9 10 11 12 13
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;

use think\Db;
D
devil_gong 已提交
14
use think\facade\Hook;
15 16 17
use app\service\GoodsService;
use app\service\UserService;
use app\service\ResourcesService;
D
devil 已提交
18
use app\service\PaymentService;
D
devil_gong 已提交
19
use app\service\ConfigService;
D
devil 已提交
20
use app\service\OrderSplitService;
D
v1.2.0  
devil_gong 已提交
21 22 23 24 25 26 27 28 29 30 31

/**
 * 购买服务层
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class BuyService
{
    /**
D
devil 已提交
32
     * 购物车添加/更新
D
v1.2.0  
devil_gong 已提交
33 34 35 36 37 38 39
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-08-29
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
D
devil 已提交
40
    public static function CartSave($params = [])
D
v1.2.0  
devil_gong 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'goods_id',
                'error_msg'         => '商品id有误',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'stock',
                'error_msg'         => '购买数量有误',
            ],
D
devil_gong 已提交
54 55 56 57 58 59
            [
                'checked_type'      => 'min',
                'key_name'          => 'stock',
                'checked_data'      => 1,
                'error_msg'         => '购买数量有误',
            ],
D
v1.2.0  
devil_gong 已提交
60 61 62 63 64 65 66 67 68 69 70 71
            [
                'checked_type'      => 'empty',
                'key_name'          => 'user',
                'error_msg'         => '用户信息有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

72 73 74 75 76 77 78
        // 查询用户状态是否正常
        $ret = UserService::UserStatusCheck('id', $params['user']['id']);
        if($ret['code'] != 0)
        {
            return $ret;
        }

D
v1.2.0  
devil_gong 已提交
79 80 81 82 83 84 85 86
        // 获取商品
        $goods_id = intval($params['goods_id']);
        $goods = Db::name('Goods')->where(['id'=>$goods_id, 'is_shelves'=>1, 'is_delete_time'=>0])->find();
        if(empty($goods))
        {
            return DataReturn('商品不存在或已删除', -2);
        }

D
devil 已提交
87 88 89 90 91 92 93
        // 是否支持购物车操作
        $ret = GoodsService::IsGoodsSiteTypeConsistent($goods_id, $goods['site_type']);
        if($ret['code'] != 0)
        {
            return $ret;
        }

D
v1.2.0  
devil_gong 已提交
94
        // 规格处理
95
        $spec = self::GoodsSpecificationsHandle($params);
D
v1.2.0  
devil_gong 已提交
96 97 98 99 100 101 102 103

        // 获取商品基础信息
        $goods_base = GoodsService::GoodsSpecDetail(['id'=>$goods_id, 'spec'=>$spec]);
        if($goods_base['code'] != 0)
        {
            return $goods_base;
        }

D
devil_gong 已提交
104 105 106 107 108 109 110 111 112 113 114
        // 获取商品规格图片
        if(!empty($spec))
        {
            $images = self::BuyGoodsSpecImages($goods_id, $spec);
            if(!empty($images))
            {
                $goods['images'] = $images;
                $goods['images_old'] = ResourcesService::AttachmentPathViewHandle($images);
            }
        }

115 116 117 118 119 120 121 122 123
        // 数量
        $stock = ($goods['buy_max_number'] > 0 && $params['stock'] > $goods['buy_max_number']) ? $goods['buy_max_number'] : $params['stock'];

        // 库存
        if($stock > $goods['inventory'])
        {
            return DataReturn('库存不足', -1);
        }

D
v1.2.0  
devil_gong 已提交
124 125 126 127 128 129
        // 添加购物车
        $data = [
            'user_id'       => $params['user']['id'],
            'goods_id'      => $goods_id,
            'title'         => $goods['title'],
            'images'        => $goods['images'],
D
devil_gong 已提交
130 131
            'original_price'=> $goods_base['data']['spec_base']['original_price'],
            'price'         => $goods_base['data']['spec_base']['price'],
132
            'stock'         => $stock,
D
devil 已提交
133
            'spec'          => empty($spec) ? '' : json_encode($spec, JSON_UNESCAPED_UNICODE),
D
v1.2.0  
devil_gong 已提交
134 135 136 137 138 139 140 141 142 143
        ];

        // 存在则更新
        $where = ['user_id'=>$data['user_id'], 'goods_id'=>$data['goods_id'], 'spec'=>$data['spec']];
        $temp = Db::name('Cart')->where($where)->find();
        if(empty($temp))
        {
            $data['add_time'] = time();
            if(Db::name('Cart')->insertGetId($data) > 0)
            {
144
                return DataReturn('加入成功', 0, self::UserCartTotal($params));
D
v1.2.0  
devil_gong 已提交
145 146 147 148 149 150 151 152
            }
        } else {
            $data['upd_time'] = time();
            $data['stock'] += $temp['stock'];
            if($data['stock'] > $goods['inventory'])
            {
                $data['stock'] = $goods['inventory'];
            }
153 154 155 156
            if($goods['buy_max_number'] > 0 && $data['stock'] > $goods['buy_max_number'])
            {
                $data['stock'] = $goods['buy_max_number'];
            }
D
v1.2.0  
devil_gong 已提交
157 158
            if(Db::name('Cart')->where($where)->update($data))
            {
159
                return DataReturn('加入成功', 0, self::UserCartTotal($params));
D
v1.2.0  
devil_gong 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
            }
        }
        
        return DataReturn('加入失败', -100);
    }

    /**
     * 商品规格解析
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-21
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
D
devil 已提交
175
    public static function GoodsSpecificationsHandle($params = [])
D
v1.2.0  
devil_gong 已提交
176 177 178 179 180 181
    {
        $spec = '';
        if(!empty($params['spec']))
        {
            if(!is_array($params['spec']))
            {
G
gongfuxiang 已提交
182
                $spec = json_decode(htmlspecialchars_decode($params['spec']), true);
D
v1.2.0  
devil_gong 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
            } else {
                $spec = $params['spec'];
            }
        }
        return empty($spec) ? '' : $spec;
    }

    /**
     * 获取购物车列表
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-08-29
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
199
    public static function CartList($params = [])
D
v1.2.0  
devil_gong 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'user',
                'error_msg'         => '用户信息有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        $where = (!empty($params['where']) && is_array($params['where'])) ? $params['where'] : [];
        $where['c.user_id'] = $params['user']['id'];

D
devil 已提交
218
        $field = 'c.*, g.inventory_unit, g.is_shelves, g.is_delete_time, g.buy_min_number, g.buy_max_number, g.model, g.site_type';
D
devil 已提交
219
        $data = Db::name('Cart')->alias('c')->leftJoin(['__GOODS__'=>'g'], 'g.id=c.goods_id')->where($where)->field($field)->order('c.id desc')->select();
D
v1.2.0  
devil_gong 已提交
220 221 222 223 224 225 226 227 228 229 230

        // 数据处理
        if(!empty($data))
        {
            foreach($data as &$v)
            {
                // 规格
                $v['spec'] = empty($v['spec']) ? null : json_decode($v['spec'], true);

                // 获取商品基础信息
                $goods_base = GoodsService::GoodsSpecDetail(['id'=>$v['goods_id'], 'spec'=>$v['spec']]);
D
Devil 已提交
231
                $v['is_invalid'] = 0;
D
v1.2.0  
devil_gong 已提交
232 233
                if($goods_base['code'] == 0)
                {
D
devil_gong 已提交
234 235 236 237 238 239
                    $v['inventory'] = $goods_base['data']['spec_base']['inventory'];
                    $v['price'] = (float) $goods_base['data']['spec_base']['price'];
                    $v['original_price'] = (float) $goods_base['data']['spec_base']['original_price'];
                    $v['spec_weight'] = $goods_base['data']['spec_base']['weight'];
                    $v['spec_coding'] = $goods_base['data']['spec_base']['coding'];
                    $v['spec_barcode'] = $goods_base['data']['spec_base']['barcode'];
D
devil_gong 已提交
240
                    $v['extends'] = $goods_base['data']['spec_base']['extends'];
D
v1.2.0  
devil_gong 已提交
241
                } else {
D
Devil 已提交
242 243 244 245 246
                    $v['is_invalid'] = 1;
                    $v['inventory'] = 0;
                    $v['spec_weight'] = 0;
                    $v['spec_coding'] = '';
                    $v['spec_barcode'] = '';
D
devil_gong 已提交
247
                    $v['extends'] = '';
D
v1.2.0  
devil_gong 已提交
248 249 250 251 252
                }

                // 基础信息
                $v['goods_url'] = MyUrl('index/goods/index', ['id'=>$v['goods_id']]);
                $v['images_old'] = $v['images'];
253
                $v['images'] = ResourcesService::AttachmentPathViewHandle($v['images']);
254
                $v['total_price'] = $v['stock']* ((float) $v['price']);
D
v1.2.0  
devil_gong 已提交
255
                $v['buy_max_number'] = ($v['buy_max_number'] <= 0) ? $v['inventory']: $v['buy_max_number'];
D
Devil 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

                // 错误处理
                $v['is_error'] = 0;
                $v['error_msg'] = '';
                if($v['is_delete_time'] != 0)
                {
                    $v['is_error'] = 1;
                    $v['error_msg'] = '商品已作废';
                }
                if(empty($v['error_msg']) && $v['is_invalid'] == 1)
                {
                    $v['is_error'] = 1;
                    $v['error_msg'] = '商品已失效';
                }
                if(empty($v['error_msg']) && $v['is_shelves'] != 1)
                {
                    $v['is_error'] = 1;
                    $v['error_msg'] = '商品已下架';
                }
D
devil 已提交
275 276 277
                if(empty($v['error_msg']) && $v['inventory'] <= 0)
                {
                    $v['is_error'] = 1;
D
devil 已提交
278
                    $v['error_msg'] = '商品没货了';
D
devil 已提交
279
                }
D
devil 已提交
280
                if(empty($v['error_msg']))
D
devil 已提交
281
                {
D
devil 已提交
282 283 284 285 286 287
                    $ret = GoodsService::IsGoodsSiteTypeConsistent($v['goods_id'], $v['site_type']);
                    if($ret['code'] != 0)
                    {
                        $v['is_error'] = 1;
                        $v['error_msg'] = $ret['msg'];
                    }
D
devil 已提交
288
                }
D
v1.2.0  
devil_gong 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
            }
        }

        return DataReturn('操作成功', 0, $data);
    }

    /**
     * 购物车删除
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-14
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
304
    public static function CartDelete($params = [])
D
v1.2.0  
devil_gong 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '删除数据id有误',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'user',
                'error_msg'         => '用户信息有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

325 326 327 328 329 330 331
        // 查询用户状态是否正常
        $ret = UserService::UserStatusCheck('id', $params['user']['id']);
        if($ret['code'] != 0)
        {
            return $ret;
        }

D
v1.2.0  
devil_gong 已提交
332 333 334 335 336 337 338
        // 删除
        $where = [
            'id'        => explode(',', $params['id']),
            'user_id'   => $params['user']['id']
        ];
        if(Db::name('Cart')->where($where)->delete())
        {
339
            return DataReturn('删除成功', 0, self::UserCartTotal($params));
D
v1.2.0  
devil_gong 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352
        }
        return DataReturn('删除失败或资源不存在', -100);
    }

    /**
     * 购物车数量保存
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-14
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
353
    public static function CartStock($params = [])
D
v1.2.0  
devil_gong 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '数据id有误',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'goods_id',
                'error_msg'         => '商品id有误',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'stock',
                'error_msg'         => '购买数量有误',
            ],
D
devil_gong 已提交
372 373 374 375 376 377
            [
                'checked_type'      => 'min',
                'key_name'          => 'stock',
                'checked_data'      => 1,
                'error_msg'         => '购买数量有误',
            ],
D
v1.2.0  
devil_gong 已提交
378 379 380 381 382 383 384 385 386 387 388 389
            [
                'checked_type'      => 'empty',
                'key_name'          => 'user',
                'error_msg'         => '用户信息有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

390 391 392 393 394 395 396
        // 查询用户状态是否正常
        $ret = UserService::UserStatusCheck('id', $params['user']['id']);
        if($ret['code'] != 0)
        {
            return $ret;
        }

397
        // 条件
D
v1.2.0  
devil_gong 已提交
398 399 400 401 402
        $where = [
            'id'        => intval($params['id']),
            'goods_id'  => intval($params['goods_id']),
            'user_id'   => intval($params['user']['id']),
        ];
403 404 405 406 407 408 409 410 411 412 413 414 415

        // 数量
        $stock = intval($params['stock']);

        // 获取购物车数据
        $cart = Db::name('Cart')->where($where)->select();
        if(empty($cart))
        {
            return DataReturn('请先加入购物车', -1);
        }
        $cart[0]['stock'] = $stock;

        // 商品校验
D
devil 已提交
416 417
        $ret = self::BuyGoodsCheck(['goods'=>$cart]);
        if($ret['code'] != 0)
418
        {
D
devil 已提交
419
            return $ret;
420 421 422
        }

        // 更新数据
D
v1.2.0  
devil_gong 已提交
423
        $data = [
424
            'stock'     => $stock,
D
v1.2.0  
devil_gong 已提交
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
            'upd_time'  => time(),
        ];
        if(Db::name('Cart')->where($where)->update($data))
        {
            return DataReturn('更新成功', 0);
        }
        return DataReturn('更新失败', -100);
    }

    /**
     * 下订单 - 正常购买
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-21
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
443
    public static function BuyGoods($params = [])
D
v1.2.0  
devil_gong 已提交
444 445 446 447 448 449 450 451
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'stock',
                'error_msg'         => '购买数量有误',
            ],
D
devil_gong 已提交
452 453 454 455 456 457
            [
                'checked_type'      => 'min',
                'key_name'          => 'stock',
                'checked_data'      => 1,
                'error_msg'         => '购买数量有误',
            ],
D
v1.2.0  
devil_gong 已提交
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
            [
                'checked_type'      => 'empty',
                'key_name'          => 'goods_id',
                'error_msg'         => '商品id有误',
            ],
            [
                'checked_type'      => 'isset',
                'key_name'          => 'spec',
                'error_msg'         => '规格参数有误',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'user',
                'error_msg'         => '用户信息有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 获取商品
        $p = [
            'where' => [
                'id'                => intval($params['goods_id']),
                'is_delete_time'    => 0,
                'is_shelves'        => 1,
            ],
D
devil_gong 已提交
487
            'field' => 'id, id AS goods_id, title, images, inventory_unit, buy_min_number, buy_max_number, model',
D
v1.2.0  
devil_gong 已提交
488
        ];
489 490
        $ret = GoodsService::GoodsList($p);
        if(empty($ret['data'][0]))
D
v1.2.0  
devil_gong 已提交
491 492 493 494 495
        {
            return DataReturn('资源不存在或已被删除', -10);
        }

        // 规格
496
        $ret['data'][0]['spec'] = self::GoodsSpecificationsHandle($params);
D
v1.2.0  
devil_gong 已提交
497 498

        // 获取商品基础信息
499
        $goods_base = GoodsService::GoodsSpecDetail(['id'=>$ret['data'][0]['goods_id'], 'spec'=>$ret['data'][0]['spec']]);
D
v1.2.0  
devil_gong 已提交
500 501
        if($goods_base['code'] == 0)
        {
D
devil_gong 已提交
502 503 504 505 506 507
            $ret['data'][0]['inventory'] = $goods_base['data']['spec_base']['inventory'];
            $ret['data'][0]['price'] = (float) $goods_base['data']['spec_base']['price'];
            $ret['data'][0]['original_price'] = (float) $goods_base['data']['spec_base']['original_price'];
            $ret['data'][0]['spec_weight'] = $goods_base['data']['spec_base']['weight'];
            $ret['data'][0]['spec_coding'] = $goods_base['data']['spec_base']['coding'];
            $ret['data'][0]['spec_barcode'] = $goods_base['data']['spec_base']['barcode'];
D
devil_gong 已提交
508
            $ret['data'][0]['extends'] = $goods_base['data']['spec_base']['extends'];
D
v1.2.0  
devil_gong 已提交
509 510 511 512
        } else {
            return $goods_base;
        }

D
devil_gong 已提交
513 514 515 516 517 518
        // 获取商品规格图片
        if(!empty($ret['data'][0]['spec']))
        {
            $images = self::BuyGoodsSpecImages($ret['data'][0]['goods_id'], $ret['data'][0]['spec']);
            if(!empty($images))
            {
G
gongfuxiang 已提交
519 520
                $ret['data'][0]['images'] = ResourcesService::AttachmentPathViewHandle($images);
                $ret['data'][0]['images_old'] = $images;
D
devil_gong 已提交
521 522 523
            }
        }

D
v1.2.0  
devil_gong 已提交
524
        // 数量/小计
525
        $ret['data'][0]['stock'] = $params['stock'];
526
        $ret['data'][0]['total_price'] = $params['stock']* ((float) $ret['data'][0]['price']);
D
v1.2.0  
devil_gong 已提交
527

D
devil_gong 已提交
528
        return DataReturn('操作成功', 0, $ret['data']);
D
v1.2.0  
devil_gong 已提交
529 530 531 532 533 534 535 536 537 538 539
    }

    /**
     * 下订单 - 购物车
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-21
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
540
    public static function BuyCart($params = [])
D
v1.2.0  
devil_gong 已提交
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'ids',
                'error_msg'         => '购物车id有误',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'user',
                'error_msg'         => '用户信息有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 获取购物车数据
        $params['where'] = [
            'g.is_delete_time'  => 0,
            'g.is_shelves'      => 1,
            'c.id'              => explode(',', $params['ids']),
        ];
567
        return self::CartList($params);
D
v1.2.0  
devil_gong 已提交
568 569
    }

D
devil_gong 已提交
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
    /**
     * 获取规格图片
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-08-02
     * @desc    description
     * @param   [int]          $goods_id [商品id]
     * @param   [string]       $spec     [图片地址或空字符串]
     */
    public static function BuyGoodsSpecImages($goods_id, $spec)
    {
        if(!empty($spec))
        {
            $data = Db::name('GoodsSpecType')->where(['goods_id'=>$goods_id])->field('name,value')->select();
            if(!empty($data))
            {
                $spec_images = [];
                foreach($data as $v)
                {
                    if(!empty($v['value']))
                    {
                        foreach(json_decode($v['value'], true) as $vs)
                        {
                            if(!empty($vs['images']))
                            {
                                $spec_images[$v['name']][$vs['name']] = $vs['images'];
                            }
                        }
                    }
                }
                if(!empty($spec_images))
                {
                    foreach($spec as $v)
                    {
                        if(array_key_exists($v['type'], $spec_images) && array_key_exists($v['value'], $spec_images[$v['type']]))
                        {
                            return $spec_images[$v['type']][$v['value']];
                        }
                    }
                }
            }
        }
        return '';
    }

D
v1.2.0  
devil_gong 已提交
616 617 618 619 620 621 622 623
    /**
     * 下订单购物车删除
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-10-12T00:42:49+0800
     * @param   [array]          $params [输入参数]
     */
624
    public static function BuyCartDelete($params = [])
D
v1.2.0  
devil_gong 已提交
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
    {
        if(isset($params['buy_type']) && $params['buy_type'] == 'cart' && !empty($params['ids']))
        {
            Db::name('Cart')->where(['id'=>explode(',', $params['ids'])])->delete();
        }
    }

    /**
     * 根据购买类型获取商品列表
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-26
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
641
    public static function BuyTypeGoodsList($params = [])
D
v1.2.0  
devil_gong 已提交
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
    {
        if(isset($params['buy_type']))
        {
            switch($params['buy_type'])
            {
                // 正常购买
                case 'goods' :
                    $ret = BuyService::BuyGoods($params);
                    break;

                // 购物车
                case 'cart' :
                    $ret = BuyService::BuyCart($params);
                    break;

                // 默认
                default :
                    $ret = DataReturn('参数有误', -1);
            }
        } else {
            $ret = DataReturn('参数有误', -1);
        }
D
devil_gong 已提交
664 665 666 667 668 669 670

        // 数据组装
        if($ret['code'] == 0)
        {
            // 商品数据
            $goods = $ret['data'];

D
devil 已提交
671
            // 站点模式 0销售, 2自提, 4销售+自提, 则其它正常模式
D
devil 已提交
672
            $user_site_model = isset($params['site_model']) ? intval($params['site_model']) : 0;
D
devil 已提交
673
            $common_site_type = MyC('common_site_type', 0, true);
D
devil 已提交
674 675 676 677 678 679 680 681 682 683
            $site_model = ($common_site_type == 4) ? $user_site_model : $common_site_type;

            // 商品销售模式
            // 商品小于等于1则使用商品的类型
            if(count($goods) <= 1 && isset($goods[0]) && isset($goods[0]['goods_id']))
            {
                $ret = GoodsService::GoodsSalesModelType($goods[0]['goods_id']);
                $common_site_type = $ret['data'];
                $site_model = ($ret['data'] == 4) ? $user_site_model : $ret['data'];
            }
D
devil 已提交
684 685

            // 数据处理
D
devil_gong 已提交
686 687
            $address = null;
            $extraction_address = [];
D
devil 已提交
688 689 690

            // 站点模式 - 用户收货地址(未选择则取默认地址)
            // 销售, 销售+自提(指定为销售)
D
devil 已提交
691
            if($site_model == 0)
D
devil_gong 已提交
692
            {
D
devil 已提交
693 694 695 696 697 698 699 700 701 702 703 704 705
                $address_params = [
                    'user'  => $params['user'],
                ];
                if(!empty($params['address_id']))
                {
                    $address_params['where'] = ['id' => $params['address_id']];
                }
                $ads = UserService::UserDefaultAddress($address_params);
                if(!empty($ads['data']))
                {
                    $address = $ads['data'];
                }
            }
D
devil_gong 已提交
706

D
devil 已提交
707 708
            // 自提模式 - 自提点地址
            // 自提, 销售+自提(指定为自提)
D
devil 已提交
709
            if($site_model == 2)
D
devil 已提交
710 711 712 713 714 715 716 717 718 719
            {
                $extraction = self::SiteExtractionAddress($params);
                if(!empty($extraction['data']['data_list']))
                {
                    $extraction_address = $extraction['data']['data_list'];
                }
                if(!empty($extraction['data']['default']))
                {
                    $address = $extraction['data']['default'];
                }
D
devil_gong 已提交
720 721
            }

D
devil 已提交
722
            // 订单拆分
723 724 725 726 727 728 729 730
            $order_split = OrderSplitService::Run([
                'site_model'            => $site_model,
                'common_site_type'      => $common_site_type,
                'address'               => $address,
                'extraction_address'    => $extraction_address,
                'goods'                 => $goods,
                'params'                => $params,
            ]);
D
devil 已提交
731 732 733 734 735
            if($order_split['code'] != 0)
            {
                return $order_split;
            }

736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
            // 订单总计基础信息字段处理
            $base_fields = [
                'total_price'           => 0,
                'actual_price'          => 0,
                'preferential_price'    => 0,
                'increase_price'        => 0,
                'goods_count'           => 0,
                'spec_weight_total'     => 0,
                'buy_count'             => 0,
            ];
            if(!empty($order_split['data']))
            {
                $order_base = array_column($order_split['data'], 'order_base');
                foreach($base_fields as $field=>$value)
                {
                    $base_fields[$field] = array_sum(array_column($order_base, $field));
                }
            }

            // 订单总计基础信息组合
D
devil_gong 已提交
756
            $base = [
D
devil_gong 已提交
757
                // 总价
758
                'total_price'           => $base_fields['total_price'],
D
devil_gong 已提交
759 760

                // 订单实际支付金额(已减去优惠金额, 已加上增加金额)
761
                'actual_price'          => $base_fields['actual_price'],
D
devil_gong 已提交
762 763

                // 优惠金额
764
                'preferential_price'    => $base_fields['preferential_price'],
D
devil_gong 已提交
765 766

                // 增加金额
767
                'increase_price'        => $base_fields['increase_price'],
D
devil_gong 已提交
768 769

                // 商品数量
770
                'goods_count'           => $base_fields['goods_count'],
D
devil_gong 已提交
771 772

                // 规格重量总计
773
                'spec_weight_total'     => $base_fields['spec_weight_total'],
D
devil_gong 已提交
774 775

                // 购买总数
776
                'buy_count'             => $base_fields['buy_count'],
D
devil_gong 已提交
777 778

                // 默认地址
D
devil_gong 已提交
779 780 781 782
                'address'               => $address,

                // 自提地址列表
                'extraction_address'    => $extraction_address,
D
devil 已提交
783

D
devil 已提交
784
                // 当前使用的站点模式
D
devil 已提交
785
                'site_model'            => $site_model,
D
devil 已提交
786 787 788

                // 公共站点模式
                'common_site_type'      => $common_site_type,
D
devil_gong 已提交
789 790 791 792
            ];

            // 返回数据
            $result = [
D
devil 已提交
793
                'goods'             => $order_split['data'],
D
devil_gong 已提交
794 795
                'base'              => $base,
            ];
D
devil_gong 已提交
796 797 798

            // 生成订单数据处理钩子
            $hook_name = 'plugins_service_buy_handle';
D
devil_gong 已提交
799
            $ret = HookReturnHandle(Hook::listen($hook_name, [
D
devil_gong 已提交
800 801
                'hook_name'     => $hook_name,
                'is_backend'    => true,
802
                'params'        => $params,
D
devil_gong 已提交
803
                'data'          => &$result,
D
devil_gong 已提交
804
            ]));
D
devil_gong 已提交
805 806 807 808 809
            if(isset($ret['code']) && $ret['code'] != 0)
            {
                return $ret;
            }

810
            // 返回数据再次处理,防止钩子插件处理不够完善
D
devil 已提交
811 812 813 814
            $result['base']['total_price'] = ($result['base']['total_price'] <= 0) ? 0.00 : PriceNumberFormat($result['base']['total_price']);
            $result['base']['actual_price'] = ($result['base']['actual_price'] <= 0) ? 0.00 : PriceNumberFormat($result['base']['actual_price']);
            $result['base']['preferential_price'] = ($result['base']['preferential_price'] <= 0) ? 0.00 : PriceNumberFormat($result['base']['preferential_price']);
            $result['base']['increase_price'] = ($result['base']['increase_price'] <= 0) ? 0.00 : PriceNumberFormat($result['base']['increase_price']);
D
Devil 已提交
815

D
devil_gong 已提交
816 817 818
            return DataReturn('操作成功', 0, $result);
        }

D
v1.2.0  
devil_gong 已提交
819 820 821 822 823 824 825 826 827 828 829 830
        return $ret;
    }

    /**
     * 购买商品校验
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-26
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
831
    public static function BuyGoodsCheck($params = [])
D
v1.2.0  
devil_gong 已提交
832 833 834 835 836 837
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'goods',
D
devil 已提交
838 839 840 841 842
                'error_msg'         => '商品信息为空',
            ],
            [
                'checked_type'      => 'is_array',
                'key_name'          => 'goods',
D
v1.2.0  
devil_gong 已提交
843 844 845 846 847 848 849 850 851
                'error_msg'         => '商品信息有误',
            ]
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

D
devil 已提交
852 853 854 855 856 857 858
        // 商品总数
        $count = count($params['goods']);

        // 提交订单则需要存在多个商品的时候校验
        // 是否需要校验商品类型
        $is_check_goods_site_type = (!isset($params['is_buy']) || $params['is_buy'] != 1 ||  ($count > 1 && $params['is_buy'] == 1));

D
v1.2.0  
devil_gong 已提交
859 860 861 862
        // 数据校验
        foreach($params['goods'] as $v)
        {
            // 获取商品信息
D
devil 已提交
863
            $goods = Db::name('Goods')->field('id,title,price,is_shelves,inventory,buy_min_number,buy_max_number,site_type')->find($v['goods_id']);
864 865
            if(empty($goods))
            {
D
devil 已提交
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
                return DataReturn('商品不存在['.$v['title'].']', -1);
            }

            // 基础判断
            if($goods['is_shelves'] != 1)
            {
                return DataReturn('商品已下架['.$goods['title'].']', -1);
            }

            // 限购
            if($goods['buy_min_number'] > 1 && $v['stock'] < $goods['buy_min_number'])
            {
                return DataReturn('低于商品起购数量['.$goods['title'].']['.$v['stock'].'<'.$goods['buy_min_number'].']', -1);
            }
            if($goods['buy_max_number'] > 0 && $v['stock'] > $goods['buy_max_number'])
            {
                return DataReturn('超过商品限购数量['.$goods['title'].']['.$v['stock'].'>'.$goods['buy_max_number'].']', -1);
            }

            // 是否支持购物车操作
            if($is_check_goods_site_type)
            {
                $ret = GoodsService::IsGoodsSiteTypeConsistent($goods['id'], $goods['site_type']);
                if($ret['code'] != 0)
                {
                    return DataReturn($ret['msg'].'['.$v['title'].']', $ret['code']);
                }
893
            }
D
v1.2.0  
devil_gong 已提交
894 895 896 897 898

            // 规格
            $goods_base = GoodsService::GoodsSpecDetail(['id'=>$v['goods_id'], 'spec'=>isset($v['spec']) ? $v['spec'] : []]);
            if($goods_base['code'] == 0)
            {
D
devil_gong 已提交
899 900
                $goods['price'] = $goods_base['data']['spec_base']['price'];
                $goods['inventory'] = $goods_base['data']['spec_base']['inventory'];
D
v1.2.0  
devil_gong 已提交
901 902 903 904
            } else {
                return $goods_base;
            }

D
devil 已提交
905
            // 库存
D
v1.2.0  
devil_gong 已提交
906 907
            if($v['stock'] > $goods['inventory'])
            {
908
                return DataReturn('购买数量超过商品库存数量['.$goods['title'].']['.$v['stock'].'>'.$goods['inventory'].']', -1);
D
v1.2.0  
devil_gong 已提交
909 910 911
            }
        }

912
        return DataReturn('校验成功', 0);
D
v1.2.0  
devil_gong 已提交
913 914 915 916 917 918 919 920 921 922 923
    }

    /**
     * 订单添加
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-26
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
D
devil_gong 已提交
924
    public static function OrderInsert($params = [])
D
v1.2.0  
devil_gong 已提交
925
    {
D
devil_gong 已提交
926
        // 站点类型,是否开启了展示型
D
devil_gong 已提交
927 928
        $common_site_type = MyC('common_site_type', 0, true);
        if($common_site_type == 1)
D
devil_gong 已提交
929 930 931 932
        {
            return DataReturn('展示型不允许提交订单', -1);
        }

D
devil 已提交
933
        // 销售+自提, 用户自选站点类型
D
devil 已提交
934 935
        $user_site_model = isset($params['site_model']) ? intval($params['site_model']) : 0;
        $site_model = ($common_site_type == 4) ? $user_site_model : $common_site_type;
D
devil 已提交
936

D
v1.2.0  
devil_gong 已提交
937 938 939 940 941 942 943 944
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'user',
                'error_msg'         => '用户信息有误',
            ],
        ];
D
devil_gong 已提交
945

D
devil 已提交
946 947
        // // 销售型,自提点,销售+自提 则校验地址
        if(in_array($site_model, [0,2]))
D
devil_gong 已提交
948 949 950 951 952 953 954 955 956
        {
            $p[] = [
                'checked_type'      => 'isset',
                'key_name'          => 'address_id',
                'error_msg'         => '请选择地址',
            ];
        }

        // 非预约模式则校验支付方式
D
v1.2.0  
devil_gong 已提交
957 958 959 960 961 962 963 964 965 966 967 968 969 970
        if(MyC('common_order_is_booking', 0) != 1)
        {
            $p[] = [
                'checked_type'      => 'empty',
                'key_name'          => 'payment_id',
                'error_msg'         => '支付方式有误',
            ];
        }
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

971 972 973 974 975 976 977
        // 查询用户状态是否正常
        $ret = UserService::UserStatusCheck('id', $params['user']['id']);
        if($ret['code'] != 0)
        {
            return $ret;
        }

D
devil 已提交
978
        // 支付方式
979 980
        $payment_id = 0;
        $is_under_line = 0;
D
devil 已提交
981 982
        if(!empty($params['payment_id']))
        {
983 984
            $payment = PaymentService::PaymentList(['where'=>['id'=>intval($params['payment_id'])]]);
            if(empty($payment[0]))
D
devil 已提交
985 986 987
            {
                return DataReturn('支付方式有误', -1);
            }
988 989
            $payment_id = $payment[0]['id'];
            $is_under_line = in_array($payment[0]['payment'], config('shopxo.under_line_list')) ? 1 : 0;
D
devil 已提交
990 991
        }

992 993 994 995
        // 清单商品
        $params['is_order_submit'] = 1;
        $buy = self::BuyTypeGoodsList($params);
        if(!isset($buy['code']) || $buy['code'] != 0)
D
devil 已提交
996
        {
997
            return $buy;
D
devil 已提交
998 999 1000
        }


1001 1002
        // 用户留言
        $user_note = empty($params['user_note']) ? '' : str_replace(['"', "'"], '', strip_tags($params['user_note']));
D
devil 已提交
1003

1004 1005
        // 订单默认状态
        $status = (intval(MyC('common_order_is_booking', 0)) == 1) ? 0 : 1;
D
devil 已提交
1006

1007 1008
        // 订单来源
        $client_type = (APPLICATION_CLIENT_TYPE == 'pc' && IsMobile()) ? 'h5' : APPLICATION_CLIENT_TYPE;
D
devil_gong 已提交
1009

D
v1.2.0  
devil_gong 已提交
1010 1011 1012
        // 开始事务
        Db::startTrans();

1013 1014 1015
        // 循环处理
        $order_ids = [];
        foreach($buy['data']['goods'] as $v)
D
v1.2.0  
devil_gong 已提交
1016
        {
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
            // 商品销售模式
            // 商品小于等于1则使用商品的类型
            if($v['order_base']['goods_count'] == 1)
            {
                $ret = GoodsService::GoodsSalesModelType($v['goods_items'][0]['goods_id']);
                $site_model = ($ret['data'] == 4) ? $user_site_model : $ret['data'];
            }

            // 商品校验
            $check = self::BuyGoodsCheck(['goods'=>$v['goods_items'], 'is_buy'=>1]);
            if($check['code'] != 0)
            {
                return $check;
            }

            // 销售型,自提点,销售+自提 地址处理
            $address = [];
            if(in_array($site_model, [0,2]))
D
v1.2.0  
devil_gong 已提交
1035
            {
1036
                if(empty($v['order_base']['address']))
D
v1.2.0  
devil_gong 已提交
1037
                {
1038
                    return DataReturn('地址有误', -1);
D
devil_gong 已提交
1039
                } else {
1040
                    $address = $v['order_base']['address'];
D
devil_gong 已提交
1041
                }
1042
            }
D
devil_gong 已提交
1043

1044 1045 1046 1047
            // 订单主信息
            $order = [
                'order_no'              => date('YmdHis').GetNumberCode(6),
                'user_id'               => $params['user']['id'],
D
devil 已提交
1048
                'warehouse_id'          => $v['id'],
1049 1050 1051 1052 1053 1054
                'user_note'             => $user_note,
                'status'                => $status,
                'preferential_price'    => ($v['order_base']['preferential_price'] <= 0.00) ? 0.00 : $v['order_base']['preferential_price'],
                'increase_price'        => ($v['order_base']['increase_price'] <= 0.00) ? 0.00 : $v['order_base']['increase_price'],
                'price'                 => ($v['order_base']['total_price'] <= 0.00) ? 0.00 : $v['order_base']['total_price'],
                'total_price'           => ($v['order_base']['actual_price'] <= 0.00) ? 0.00 : $v['order_base']['actual_price'],
D
devil 已提交
1055
                'extension_data'        => empty($v['order_base']['extension_data']) ? '' : json_encode($v['order_base']['extension_data'], JSON_UNESCAPED_UNICODE),
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
                'payment_id'            => $payment_id,
                'buy_number_count'      => $v['order_base']['buy_count'],
                'client_type'           => $client_type,
                'order_model'           => $site_model,
                'is_under_line'         => $is_under_line,
                'add_time'              => time(),
            ];
            if($order['status'] == 1)
            {
                $order['confirm_time'] = time();
D
v1.2.0  
devil_gong 已提交
1066
            }
D
devil_gong 已提交
1067

1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
            // 订单添加前钩子
            $hook_name = 'plugins_service_buy_order_insert_begin';
            $ret = HookReturnHandle(Hook::listen($hook_name, [
                'hook_name'     => $hook_name,
                'is_backend'    => true,
                'data'          => $v,
                'order'         => &$order,
                'goods'         => &$v['goods_items'],
                'params'        => $params,
                
            ]));
            if(isset($ret['code']) && $ret['code'] != 0)
D
devil_gong 已提交
1080
            {
1081 1082 1083 1084 1085 1086 1087 1088
                return $ret;
            }

            // 订单添加
            $order_id = Db::name('Order')->insertGetId($order);
            if($order_id > 0)
            {
                foreach($v['goods_items'] as $k=>$v)
D
devil_gong 已提交
1089
                {
1090 1091 1092
                    // 添加订单详情数据,data返回自增id
                    $detail_ret = self::OrderDetailInsert($order_id, $params['user']['id'], $v);
                    if($detail_ret['code'] == 0)
D
devil_gong 已提交
1093
                    {
1094 1095
                        $v['goods_items'][$k]['id'] = $detail_ret['data'];
                    } else {
D
devil_gong 已提交
1096 1097 1098
                        Db::rollback();
                        return $ret;
                    }
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109

                    // 订单模式 - 虚拟信息添加
                    if($site_model == 3)
                    {
                        $ret = self::OrderFictitiousValueInsert($order_id, $detail_ret['data'], $params['user']['id'], $v['goods_id']);
                        if($ret['code'] != 0)
                        {
                            Db::rollback();
                            return $ret;
                        }
                    }
D
devil_gong 已提交
1110 1111
                }

1112 1113 1114
                // 订单模式处理
                // 销售型模式,自提模式,销售+自提
                if(in_array($site_model, [0,2]))
D
devil_gong 已提交
1115
                {
1116 1117
                    // 添加订单(收货|取货)地址
                    if(!empty($address))
D
devil_gong 已提交
1118
                    {
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
                        $ret = self::OrderAddressInsert($order_id, $params['user']['id'], $address);
                        if($ret['code'] != 0)
                        {
                            Db::rollback();
                            return $ret;
                        }
                    }

                    // 自提模式 添加订单取货码
                    if($site_model == 2)
                    {
                        $ret = self::OrderExtractionCcodeInsert($order_id, $params['user']['id']);
                        if($ret['code'] != 0)
                        {
                            Db::rollback();
                            return $ret;
                        }
D
devil_gong 已提交
1136
                    }
D
devil_gong 已提交
1137
                }
1138 1139 1140
            } else {
                Db::rollback();
                return DataReturn('订单添加失败', -1);
D
devil_gong 已提交
1141
            }
D
v1.2.0  
devil_gong 已提交
1142

1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
            // 库存扣除
            if($order['status'] == 1)
            {
                $ret = self::OrderInventoryDeduct(['order_id'=>$order_id, 'order_data'=>$order]);
                if($ret['code'] != 0)
                {
                    // 事务回滚
                    Db::rollback();
                    return DataReturn($ret['msg'], -10);
                }
            }

            // 订单添加成功钩子
            $hook_name = 'plugins_service_buy_order_insert_end';
            $ret = HookReturnHandle(Hook::listen($hook_name, [
                'hook_name'     => $hook_name,
                'is_backend'    => true,
                'order_id'      => $order_id,
                'order'         => $order,
                'data'          => $v,
                'goods'         => $v['goods_items'],
                'address'       => $address,
                'params'        => $params,
            ]));
            if(isset($ret['code']) && $ret['code'] != 0)
D
v1.2.0  
devil_gong 已提交
1168 1169 1170
            {
                // 事务回滚
                Db::rollback();
1171
                return $ret;
D
v1.2.0  
devil_gong 已提交
1172 1173
            }

1174 1175
            // 订单id集合
            $order_ids[] = $order_id;
D
devil_gong 已提交
1176 1177
        }

1178
        // 订单全部提交成功
D
v1.2.0  
devil_gong 已提交
1179 1180
        Db::commit();

D
devil_gong 已提交
1181 1182 1183 1184 1185
        // 订单添加成功钩子, 不校验返回值
        $hook_name = 'plugins_service_buy_order_insert_success';
        Hook::listen($hook_name, [
            'hook_name'     => $hook_name,
            'is_backend'    => true,
1186
            'order_ids'     => $order_ids,
D
devil_gong 已提交
1187 1188 1189
            'params'        => $params,
        ]);

1190 1191 1192
        // 删除购物车
        self::BuyCartDelete($params);

D
v1.2.0  
devil_gong 已提交
1193 1194
        // 返回信息
        $result = [
1195
            'order_ids' => $order_ids,
D
v1.2.0  
devil_gong 已提交
1196 1197 1198 1199 1200
            'jump_url'  => MyUrl('index/order/index'),
        ];


        // 获取订单信息
1201
        switch($status)
D
v1.2.0  
devil_gong 已提交
1202 1203 1204 1205 1206 1207
        {
            // 预约成功
            case 0 :
                $msg = '预约成功';
                break;

1208
            // 提交成功,进入合并支付
D
v1.2.0  
devil_gong 已提交
1209 1210
            case 1 :
                $msg = '提交成功';
1211
                $result['jump_url'] = MyUrl('index/order/pay', ['ids'=>implode(',', $order_ids)]);
D
v1.2.0  
devil_gong 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
                break;

            // 默认操作成功
            default :
                $msg = '操作成功';
        }

        return DataReturn($msg, 0, $result);
    }

D
devil_gong 已提交
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
    /**
     * 订单详情添加
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-11-20
     * @desc    description
     * @param   [int]          $order_id    [订单id]
     * @param   [int]          $user_id     [用户id]
     * @param   [array]        $detail      [商品详情数据]
     */
    private static function OrderDetailInsert($order_id, $user_id, $detail)
    {
        $data = [
            'order_id'          => $order_id,
            'user_id'           => $user_id,
            'goods_id'          => $detail['goods_id'],
            'title'             => $detail['title'],
            'images'            => $detail['images_old'],
            'original_price'    => $detail['original_price'],
            'price'             => $detail['price'],
            'total_price'       => PriceNumberFormat($detail['stock']*$detail['price']),
D
devil 已提交
1244
            'spec'              => empty($detail['spec']) ? '' : json_encode($detail['spec'], JSON_UNESCAPED_UNICODE),
D
devil_gong 已提交
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
            'spec_weight'       => empty($detail['spec_weight']) ? 0.00 : (float) $detail['spec_weight'],
            'spec_coding'       => empty($detail['spec_coding']) ? '' : $detail['spec_coding'],
            'spec_barcode'      => empty($detail['spec_barcode']) ? '' : $detail['spec_barcode'],
            'buy_number'        => intval($detail['stock']),
            'model'             => $detail['model'],
            'add_time'          => time(),
        ];

        // 订单详情添加前钩子
        $hook_name = 'plugins_service_buy_order_detail_insert_begin';
D
devil_gong 已提交
1255
        $ret = HookReturnHandle(Hook::listen($hook_name, [
D
devil_gong 已提交
1256 1257 1258 1259 1260
            'hook_name'     => $hook_name,
            'is_backend'    => true,
            'user_id'       => $user_id,
            'order_id'      => $order_id,
            'data'          => &$data,
D
devil_gong 已提交
1261
        ]));
D
devil_gong 已提交
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
        if(isset($ret['code']) && $ret['code'] != 0)
        {
            return $ret;
        }

        // 添加订单详情数据
        $order_detail_id = Db::name('OrderDetail')->insertGetId($data);
        if($order_detail_id > 0)
        {
            return DataReturn('添加成功', 0, $order_detail_id);
        }
        return DataReturn('订单详情添加失败', -1);
D
devil_gong 已提交
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
    }

    /**
     * 订单关联自提取货码添加
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-11-20
     * @desc    description
     * @param   [int]          $order_id            [订单id]
     * @param   [int]          $user_id             [用户id]
     */
    private static function OrderExtractionCcodeInsert($order_id, $user_id)
    {
        $data = [
            'order_id'      => $order_id,
            'user_id'       => $user_id,
            'code'          => GetNumberCode(4),
            'add_time'      => time(),
        ];

        // 订单取货码添加前钩子
        $hook_name = 'plugins_service_buy_order_extraction_code_insert_begin';
D
devil_gong 已提交
1297
        $ret = HookReturnHandle(Hook::listen($hook_name, [
D
devil_gong 已提交
1298 1299 1300 1301 1302
            'hook_name'             => $hook_name,
            'is_backend'            => true,
            'user_id'               => $user_id,
            'order_id'              => $order_id,
            'data'                  => &$data,
D
devil_gong 已提交
1303
        ]));
D
devil_gong 已提交
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
        if(isset($ret['code']) && $ret['code'] != 0)
        {
            return $ret;
        }

        // 添加订单虚拟数据
        if(Db::name('OrderExtractionCode')->insertGetId($data) > 0)
        {
            return DataReturn('添加成功', 0);
        }
        return DataReturn('订单取货码添加失败', -1);
D
devil_gong 已提交
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
    }

    /**
     * 订单关联虚拟销售数据添加
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-11-20
     * @desc    description
     * @param   [int]          $order_id            [订单id]
     * @param   [int]          $order_detail_id     [订单详情id]
     * @param   [int]          $user_id             [用户id]
     * @param   [int]          $goods_id            [商品id]
     */
    private static function OrderFictitiousValueInsert($order_id, $order_detail_id, $user_id, $goods_id)
    {
        $data = [
            'order_id'              => $order_id,
            'order_detail_id'       => $order_detail_id,
            'user_id'               => $user_id,
            'value'                 => Db::name('Goods')->where(['id'=>$goods_id])->value('fictitious_goods_value'),
            'add_time'              => time(),
        ];

        // 订单虚拟数据添加前钩子
        $hook_name = 'plugins_service_buy_order_fictitious_insert_begin';
D
devil_gong 已提交
1341
        $ret = HookReturnHandle(Hook::listen($hook_name, [
D
devil_gong 已提交
1342 1343 1344 1345 1346 1347 1348
            'hook_name'             => $hook_name,
            'is_backend'            => true,
            'user_id'               => $user_id,
            'order_id'              => $order_id,
            'order_detail_id'       => $order_detail_id,
            'goods_id'              => $goods_id,
            'data'                  => &$data,
D
devil_gong 已提交
1349
        ]));
D
devil_gong 已提交
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
        if(isset($ret['code']) && $ret['code'] != 0)
        {
            return $ret;
        }

        // 添加订单虚拟数据
        if(Db::name('OrderFictitiousValue')->insertGetId($data) > 0)
        {
            return DataReturn('添加成功', 0);
        }
        return DataReturn('订单虚拟信息添加失败', -1);
    }

    /**
     * 订单关联地址添加
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-11-20
     * @desc    description
     * @param   [int]          $order_id [订单id]
     * @param   [int]          $user_id  [用户id]
     * @param   [array]        $address  [地址]
     */
D
devil_gong 已提交
1374
    private static function OrderAddressInsert($order_id, $user_id, $address)
D
devil_gong 已提交
1375
    {
D
devil 已提交
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
        // 坐标处理
        if(in_array(APPLICATION_CLIENT_TYPE, config('shopxo.coordinate_transformation')))
        {
            // 坐标转换 火星(高德,谷歌,腾讯坐标) 转 百度
            if(isset($address['lng']) && isset($address['lat']) && $address['lng'] > 0 && $address['lat'] > 0)
            {
                $map = \base\GeoTransUtil::GcjToBd($address['lng'], $address['lat']);
                if(isset($map['lng']) && isset($map['lat']))
                {
                    $address['lng'] = $map['lng'];
                    $address['lat'] = $map['lat'];
                }
            }
        }

D
devil_gong 已提交
1391 1392
        // 订单收货地址
        $data = [
D
devil_gong 已提交
1393 1394
            'order_id'      => $order_id,
            'user_id'       => $user_id,
D
devil_gong 已提交
1395
            'address_id'    => isset($address['id']) ? intval($address['id']) : 0,
D
devil_gong 已提交
1396
            'alias'         => isset($address['alias']) ? $address['alias'] : '',
D
devil_gong 已提交
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
            'name'          => isset($address['name']) ? $address['name'] : '',
            'tel'           => isset($address['tel']) ? $address['tel'] : '',
            'province'      => isset($address['province']) ? intval($address['province']) : 0,
            'city'          => isset($address['city']) ? intval($address['city']) : 0,
            'county'        => isset($address['county']) ? intval($address['county']) : 0,
            'address'       => isset($address['address']) ? $address['address'] : '',
            'province_name' => isset($address['province_name']) ? $address['province_name'] : '',
            'city_name'     => isset($address['city_name']) ? $address['city_name'] : '',
            'county_name'   => isset($address['county_name']) ? $address['county_name'] : '',
            'lng'           => isset($address['lng']) ? (float) $address['lng'] : '0.0000000000',
            'lat'           => isset($address['lat']) ? (float) $address['lat'] : '0.0000000000',
            'add_time'      => time(),
D
devil_gong 已提交
1409 1410 1411
        ];
        
        // 订单地址添加前钩子
D
devil_gong 已提交
1412
        $hook_name = 'plugins_service_buy_order_address_insert_begin';
D
devil_gong 已提交
1413
        $ret = HookReturnHandle(Hook::listen($hook_name, [
D
devil_gong 已提交
1414 1415 1416 1417 1418
            'hook_name'     => $hook_name,
            'is_backend'    => true,
            'user_id'       => $user_id,
            'order_id'      => $order_id,
            'data'          => &$data,
D
devil_gong 已提交
1419
        ]));
D
devil_gong 已提交
1420 1421 1422 1423 1424 1425
        if(isset($ret['code']) && $ret['code'] != 0)
        {
            return $ret;
        }

        // 添加订单地址
D
devil_gong 已提交
1426
        if(Db::name('OrderAddress')->insertGetId($data) > 0)
D
devil_gong 已提交
1427 1428 1429
        {
            return DataReturn('添加成功', 0);
        }
D
devil_gong 已提交
1430
        return DataReturn('订单地址添加失败', -1);
D
devil_gong 已提交
1431 1432
    }

D
v1.2.0  
devil_gong 已提交
1433 1434 1435 1436 1437 1438 1439 1440 1441
    /**
     * 购物车总数
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [array]          $where [条件]
     */
1442
    public static function CartTotal($where = [])
D
v1.2.0  
devil_gong 已提交
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
    {
        return (int) Db::name('Cart')->where($where)->count();
    }

    /**
     * 用户购物车总数
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [array]          $params [输入参数]
D
devil 已提交
1455
     * @return  [int|string]             [超过99则返回 99+]
D
v1.2.0  
devil_gong 已提交
1456
     */
1457
    public static function UserCartTotal($params = [])
D
v1.2.0  
devil_gong 已提交
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'user',
                'error_msg'         => '用户信息有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return 0;
        }
D
devil 已提交
1472 1473 1474 1475

        // 获取购物车总数
        $total = self::CartTotal(['user_id'=>$params['user']['id']]);
        return ($total > 99) ? '99+' : $total;
D
v1.2.0  
devil_gong 已提交
1476 1477
    }

D
devil 已提交
1478
    /**
D
devil 已提交
1479
     * 订单支付前校验
D
devil 已提交
1480 1481 1482 1483 1484 1485 1486
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-11-09
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
D
devil 已提交
1487
    public static function OrderPayBeginCheck($params = [])
D
devil 已提交
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'order_id',
                'error_msg'         => '订单id有误',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'order_data',
                'error_msg'         => '订单更新数据不能为空',
            ],
            [
                'checked_type'      => 'is_array',
                'key_name'          => 'order_data',
                'error_msg'         => '订单更新数据有误',
            ]
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 是否扣除库存
        $common_is_deduction_inventory = MyC('common_is_deduction_inventory', 0);
        if($common_is_deduction_inventory != 1)
        {
            return DataReturn('未开启扣除库存', 0);
        }

        // 获取订单商品
        $order_detail = Db::name('OrderDetail')->field('id,goods_id,buy_number,spec')->where(['order_id'=>$params['order_id']])->select();
        if(!empty($order_detail))
        {
            foreach($order_detail as $v)
            {
D
devil 已提交
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
                // 获取商品
                $goods = Db::name('Goods')->field('is_shelves,is_deduction_inventory,inventory,title')->find($v['goods_id']);
                if(empty($goods))
                {
                    return DataReturn('商品不存在', -10);
                }

                // 商品状态
                if($goods['is_shelves'] != 1)
                {
                    return DataReturn('商品已下架['.$goods['title'].']', -10);
                }

                // 库存
D
devil 已提交
1540 1541 1542 1543 1544
                if(isset($goods['is_deduction_inventory']) && $goods['is_deduction_inventory'] == 1)
                {
                    // 先判断商品库存是否不足
                    if($goods['inventory'] < $v['buy_number'])
                    {
D
devil 已提交
1545
                        return DataReturn('库存不足['.$goods['title'].'('.$goods['inventory'].'<'.$v['buy_number'].')]', -10);
D
devil 已提交
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
                    }

                    // 规格库存
                    $spec = empty($v['spec']) ? '' : json_decode($v['spec'], true);
                    $base = GoodsService::GoodsSpecDetail(['id'=>$v['goods_id'], 'spec'=>$spec]);
                    if($base['code'] == 0)
                    {
                        // 先判断商品规格库存是否不足
                        $inventory = Db::name('GoodsSpecBase')->where(['id'=>$base['data']['spec_base']['id'], 'goods_id'=>$v['goods_id']])->value('inventory');
                        if($inventory < $v['buy_number'])
                        {
D
devil 已提交
1557
                            return DataReturn('库存不足['.$goods['title'].'('.$inventory.'<'.$v['buy_number'].')]', -10);
D
devil 已提交
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
                        }
                    } else {
                        return $base;
                    }
                }
            }
            return DataReturn('校验成功', 0);
        }
        return DataReturn('没有需要扣除库存的数据', 0);
    }

D
v1.2.0  
devil_gong 已提交
1569 1570 1571 1572 1573 1574 1575 1576 1577
    /**
     * 库存扣除
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-11-09
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
1578
    public static function OrderInventoryDeduct($params = [])
D
v1.2.0  
devil_gong 已提交
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'order_id',
                'error_msg'         => '订单id有误',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'order_data',
                'error_msg'         => '订单更新数据不能为空',
            ],
            [
                'checked_type'      => 'is_array',
                'key_name'          => 'order_data',
                'error_msg'         => '订单更新数据有误',
            ]
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 是否扣除库存
        $common_is_deduction_inventory = MyC('common_is_deduction_inventory', 0);
        if($common_is_deduction_inventory != 1)
        {
            return DataReturn('未开启扣除库存', 0);
        }

        // 扣除库存规则
        $common_deduction_inventory_rules = MyC('common_deduction_inventory_rules', 1);
        switch($common_deduction_inventory_rules)
        {
            // 订单确认成功
            case 0 :
                if($params['order_data']['status'] != 1)
                {
                    return DataReturn('当前订单状态未操作确认-不扣除库存['.$params['order_id'].']', 0);
                }
                break;

            // 订单支付成功
            case 1 :
                if($params['order_data']['status'] != 2)
                {
                    return DataReturn('当前订单状态未操作支付-不扣除库存['.$params['order_id'].']', 0);
                }
                break;

            // 订单发货
            case 2 :
                if($params['order_data']['status'] != 3)
                {
                    return DataReturn('当前订单状态未操作发货-不扣除库存['.$params['order_id'].']', 0);
                }
                break;
        }

        // 获取订单商品
D
Devil 已提交
1641
        $order_detail = Db::name('OrderDetail')->field('id,goods_id,buy_number,spec')->where(['order_id'=>$params['order_id']])->select();
D
v1.2.0  
devil_gong 已提交
1642 1643 1644 1645 1646
        if(!empty($order_detail))
        {
            foreach($order_detail as $v)
            {
                // 查看是否已扣除过库存,避免更改模式导致重复扣除
D
Devil 已提交
1647
                $temp = Db::name('OrderGoodsInventoryLog')->where(['order_id'=>$params['order_id'], 'order_detail_id'=>$v['id'], 'goods_id'=>$v['goods_id']])->find();
D
v1.2.0  
devil_gong 已提交
1648 1649
                if(empty($temp))
                {
D
devil 已提交
1650
                    $goods = Db::name('Goods')->field('is_deduction_inventory,inventory,title')->find($v['goods_id']);
D
v1.2.0  
devil_gong 已提交
1651 1652
                    if(isset($goods['is_deduction_inventory']) && $goods['is_deduction_inventory'] == 1)
                    {
D
devil 已提交
1653
                        // 先判断商品库存是否不足
D
devil 已提交
1654
                        if($goods['inventory'] < $v['buy_number'])
D
devil 已提交
1655
                        {
D
devil 已提交
1656
                            return DataReturn('商品库存不足['.$goods['title'].'('.$goods['inventory'].'<'.$v['buy_number'].')]', -10);
D
devil 已提交
1657 1658
                        }

D
v1.2.0  
devil_gong 已提交
1659 1660 1661
                        // 扣除操作
                        if(!Db::name('Goods')->where(['id'=>$v['goods_id']])->setDec('inventory', $v['buy_number']))
                        {
D
Devil 已提交
1662
                            return DataReturn('商品库存扣减失败['.$params['order_id'].'-'.$v['id'].'-'.$v['goods_id'].'('.$goods['inventory'].'-'.$v['buy_number'].')]', -10);
D
v1.2.0  
devil_gong 已提交
1663 1664 1665 1666 1667 1668 1669
                        }

                        // 扣除规格库存
                        $spec = empty($v['spec']) ? '' : json_decode($v['spec'], true);
                        $base = GoodsService::GoodsSpecDetail(['id'=>$v['goods_id'], 'spec'=>$spec]);
                        if($base['code'] == 0)
                        {
D
devil 已提交
1670 1671 1672 1673
                            // 先判断商品规格库存是否不足
                            $inventory = Db::name('GoodsSpecBase')->where(['id'=>$base['data']['spec_base']['id'], 'goods_id'=>$v['goods_id']])->value('inventory');
                            if($inventory < $v['buy_number'])
                            {
D
devil 已提交
1674
                                return DataReturn('商品规格库存不足['.$goods['title'].'('.$inventory.'<'.$v['buy_number'].']', -10);
D
devil 已提交
1675 1676
                            }

D
v1.2.0  
devil_gong 已提交
1677
                            // 扣除规格操作
D
devil_gong 已提交
1678
                            if(!Db::name('GoodsSpecBase')->where(['id'=>$base['data']['spec_base']['id'], 'goods_id'=>$v['goods_id']])->setDec('inventory', $v['buy_number']))
D
v1.2.0  
devil_gong 已提交
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
                            {
                                return DataReturn('规格库存扣减失败['.$params['order_id'].'-'.$v['goods_id'].'('.$goods['inventory'].'-'.$v['buy_number'].')]', -10);
                            }
                        } else {
                            return $base;
                        }

                        // 扣除日志添加
                        $log_data = [
                            'order_id'              => $params['order_id'],
D
Devil 已提交
1689
                            'order_detail_id'       => $v['id'],
D
v1.2.0  
devil_gong 已提交
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
                            'goods_id'              => $v['goods_id'],
                            'order_status'          => $params['order_data']['status'],
                            'original_inventory'    => $goods['inventory'],
                            'new_inventory'         => Db::name('Goods')->where(['id'=>$v['goods_id']])->value('inventory'),
                            'add_time'              => time(),
                        ];
                        if(Db::name('OrderGoodsInventoryLog')->insertGetId($log_data) <= 0)
                        {
                            return DataReturn('库存扣减日志添加失败['.$params['order_id'].'-'.$v['goods_id'].']', -100);
                        }
                    }
                }
            }
            return DataReturn('操作成功', 0);
        }
        return DataReturn('没有需要扣除库存的数据', 0);
    }

    /**
     * 库存回滚
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-11-09
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
1717
    public static function OrderInventoryRollback($params = [])
D
v1.2.0  
devil_gong 已提交
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'order_id',
                'error_msg'         => '订单id有误',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'order_data',
                'error_msg'         => '订单更新数据不能为空',
            ],
            [
                'checked_type'      => 'is_array',
                'key_name'          => 'order_data',
                'error_msg'         => '订单更新数据有误',
            ]
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 订单状态
G
gongfuxiang 已提交
1744
        if(isset($params['order_data']['status']))
D
v1.2.0  
devil_gong 已提交
1745
        {
G
gongfuxiang 已提交
1746 1747 1748 1749
            if(!in_array($params['order_data']['status'], [5,6]))
            {
                return DataReturn('当前订单状态不允许回滚库存['.$params['order_id'].'-'.$params['order_data']['status'].']', 0);
            }
D
v1.2.0  
devil_gong 已提交
1750 1751
        }

D
devil_gong 已提交
1752 1753 1754 1755 1756 1757 1758 1759
        // 是否指定商品和数量
        $appoint_buy_number = empty($params['appoint_buy_number']) ? 0 : intval($params['appoint_buy_number']);
        $detail_where = ['order_id' => $params['order_id']];
        if(!empty($params['appoint_order_detail_id']))
        {
            $detail_where['id'] = intval($params['appoint_order_detail_id']);
        }

D
v1.2.0  
devil_gong 已提交
1760
        // 获取订单商品
D
devil_gong 已提交
1761
        $order_detail = Db::name('OrderDetail')->field('goods_id,buy_number,spec')->where($detail_where)->select();
D
v1.2.0  
devil_gong 已提交
1762 1763 1764 1765 1766 1767 1768 1769
        if(!empty($order_detail))
        {
            foreach($order_detail as $v)
            {
                // 查看是否已扣除过库存
                $temp = Db::name('OrderGoodsInventoryLog')->where(['order_id'=>$params['order_id'], 'goods_id'=>$v['goods_id'], 'is_rollback'=>0])->find();
                if(!empty($temp))
                {
D
devil_gong 已提交
1770 1771 1772
                    // 数量
                    $buy_number = ($appoint_buy_number == 0) ? $v['buy_number'] : $appoint_buy_number;

D
v1.2.0  
devil_gong 已提交
1773
                    // 回滚操作
D
devil_gong 已提交
1774
                    if(!Db::name('Goods')->where(['id'=>$v['goods_id']])->setInc('inventory', $buy_number))
D
v1.2.0  
devil_gong 已提交
1775 1776 1777 1778
                    {
                        return DataReturn('商品库存回滚失败['.$params['order_id'].'-'.$v['goods_id'].']', -10);
                    }

D
devil_gong 已提交
1779
                    // 回滚规格库存
D
v1.2.0  
devil_gong 已提交
1780 1781 1782 1783
                    $spec = empty($v['spec']) ? '' : json_decode($v['spec'], true);
                    $base = GoodsService::GoodsSpecDetail(['id'=>$v['goods_id'], 'spec'=>$spec]);
                    if($base['code'] == 0)
                    {
D
devil_gong 已提交
1784
                        // 回滚规格操作
D
devil_gong 已提交
1785
                        if(!Db::name('GoodsSpecBase')->where(['id'=>$base['data']['spec_base']['id'], 'goods_id'=>$v['goods_id']])->setInc('inventory', $buy_number))
D
v1.2.0  
devil_gong 已提交
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
                        {
                            return DataReturn('规格库存回滚失败['.$params['order_id'].'-'.$v['goods_id'].']', -10);
                        }
                    } else {
                        return $base;
                    }

                    // 回滚日志更新
                    $log_data = [
                        'is_rollback'   => 1,
                        'rollback_time' => time(),
                    ];
                    if(!Db::name('OrderGoodsInventoryLog')->where(['id'=>$temp['id']])->update($log_data))
                    {
                        return DataReturn('库存回滚日志更新失败['.$temp['id'].'-'.$params['order_id'].']', -100);
                    }
                }
            }
            return DataReturn('操作成功', 0);
        }
        return DataReturn('没有需要回滚的数据', 0);
    }
D
devil_gong 已提交
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825

    /**
     * 自提点地址选中地址获取
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-11-18
     * @desc    description
     * @param   [int]       $params['address_id'] [自提点地址索引值]
     * @param   [array]     $params['address_id'] [自提点地址列表]
     */
    public static function SiteExtractionAddress($params = [])
    {
        // 自提地址列表
        $address = ConfigService::SiteTypeExtractionAddressList();

        // 选中地址处理
        $default = null;
D
devil 已提交
1826
        if(isset($params['address_id']) && $params['address_id'] !== null && !empty($address['data']) && is_array($address['data']))
D
devil_gong 已提交
1827 1828 1829 1830 1831 1832 1833
        {
            if(isset($address['data'][$params['address_id']]))
            {
                $default = $address['data'][$params['address_id']];
            }
        }

D
devil 已提交
1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
        // 默认地址
        if(empty($default) && !empty($address['data']))
        {
            foreach($address['data'] as $v)
            {
                if(isset($v['is_default']) && $v['is_default'] == 1)
                {
                    $default = $v;
                    break;
                }
            }
        }

D
devil_gong 已提交
1847 1848 1849 1850 1851
        // 返回数据
        $result = [
            'data_list'     => $address['data'],
            'default'       => $default,
        ];
D
devil_gong 已提交
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861

        // 自提点地址数据钩子
        $hook_name = 'plugins_service_site_extraction_address_handle';
        $ret = Hook::listen($hook_name, [
            'hook_name'     => $hook_name,
            'is_backend'    => true,
            'params'        => $params,
            'data'          => &$result,
        ]);

D
devil_gong 已提交
1862 1863
        return DataReturn('操作成功', 0, $result);
    }
D
v1.2.0  
devil_gong 已提交
1864 1865
}
?>