GoodsService.php 70.9 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;
14
use think\facade\Hook;
15 16 17
use app\service\ResourcesService;
use app\service\BrandService;
use app\service\RegionService;
D
v1.2.0  
devil_gong 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

/**
 * 商品服务层
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class GoodsService
{
    /**
     * 根据id获取一条商品分类
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-08-29
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
37
    public static function GoodsCategoryRow($params = [])
D
v1.2.0  
devil_gong 已提交
38 39 40 41 42 43
    {
        if(empty($params['id']))
        {
            return null;
        }
        $field = empty($params['field']) ? 'id,pid,icon,name,vice_name,describe,bg_color,big_images,sort,is_home_recommended' : $params['field'];
44
        $data = self::GoodsCategoryDataDealWith([Db::name('GoodsCategory')->field($field)->where(['is_enable'=>1, 'id'=>intval($params['id'])])->find()]);
D
v1.2.0  
devil_gong 已提交
45 46 47 48 49 50 51 52 53 54 55 56
        return empty($data[0]) ? null : $data[0];
    }

    /**
     * 获取大分类
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-08-29
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
57
    public static function GoodsCategory($params = [])
D
v1.2.0  
devil_gong 已提交
58 59
    {
        $where = empty($params['where']) ? ['pid'=>0, 'is_enable'=>1] : $params['where'];
60
        $data = self::GoodsCategoryList($where);
D
v1.2.0  
devil_gong 已提交
61 62 63 64
        if(!empty($data))
        {
            foreach($data as &$v)
            {
65
                $v['items'] = self::GoodsCategoryList(['pid'=>$v['id'], 'is_enable'=>1]);
D
v1.2.0  
devil_gong 已提交
66 67 68
                if(!empty($v['items']))
                {
                    // 一次性查出所有二级下的三级、再做归类、避免sql连接超多
69
                    $items = self::GoodsCategoryList(['pid'=>array_column($v['items'], 'id'), 'is_enable'=>1]);
D
v1.2.0  
devil_gong 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
                    if(!empty($items))
                    {
                        foreach($v['items'] as &$vs)
                        {
                            foreach($items as $vss)
                            {
                                if($vs['id'] == $vss['pid'])
                                {
                                    $vs['items'][] = $vss;
                                }
                            }
                        }
                    }
                }
            }
        }
        return $data;
    }

    /**
     * 根据pid获取商品分类列表
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-08-29
     * @desc    description
     * @param   [array]          $where [条件]
     */
98
    public static function GoodsCategoryList($where = [])
D
v1.2.0  
devil_gong 已提交
99 100 101 102
    {
        $where['is_enable'] = 1;
        $field = 'id,pid,icon,name,vice_name,describe,bg_color,big_images,sort,is_home_recommended';
        $data = Db::name('GoodsCategory')->field($field)->where($where)->order('sort asc')->select();
103
        return self::GoodsCategoryDataDealWith($data);
D
v1.2.0  
devil_gong 已提交
104 105 106 107 108 109 110 111 112 113 114
    }

    /**
     * 商品分类数据处理
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-06
     * @desc    description
     * @param   [array]          $data [商品分类数据 二维数组]
     */
115
    private static function GoodsCategoryDataDealWith($data)
D
v1.2.0  
devil_gong 已提交
116 117 118 119 120 121 122 123 124
    {
        if(!empty($data) && is_array($data))
        {
            foreach($data as &$v)
            {
                if(is_array($v))
                {
                    if(isset($v['icon']))
                    {
125
                        $v['icon'] = ResourcesService::AttachmentPathViewHandle($v['icon']);
D
v1.2.0  
devil_gong 已提交
126 127 128 129
                    }
                    if(isset($v['big_images']))
                    {
                        $v['big_images_old'] = $v['big_images'];
130
                        $v['big_images'] = ResourcesService::AttachmentPathViewHandle($v['big_images']);
D
v1.2.0  
devil_gong 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
                    }
                }
            }
        }
        return $data;
    }

    /**
     * 获取首页楼层数据
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-08-29
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
147
    public static function HomeFloorList($params = [])
D
v1.2.0  
devil_gong 已提交
148 149 150
    {
        // 商品大分类
        $params['where'] = ['pid'=>0, 'is_home_recommended'=>1];
151
        $goods_category = self::GoodsCategory($params);
D
v1.2.0  
devil_gong 已提交
152 153 154 155
        if(!empty($goods_category))
        {
            foreach($goods_category as &$v)
            {
156
                $category_ids = self::GoodsCategoryItemsIds([$v['id']], 1);
157 158
                $goods = self::CategoryGoodsList(['where'=>['gci.category_id'=>$category_ids, 'is_home_recommended'=>1], 'm'=>0, 'n'=>6, 'field'=>'g.id,g.title,g.title_color,g.images,g.home_recommended_images,g.original_price,g.price,g.min_price,g.max_price,g.inventory,g.buy_min_number,g.buy_max_number']);
                $v['goods'] = $goods['data'];
D
v1.2.0  
devil_gong 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
            }
        }
        return $goods_category;
    }

    /**
     * 获取商品分类下的所有分类id
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-08-29
     * @desc    description
     * @param   [array]          $ids       [分类id数组]
     * @param   [int]            $is_enable [是否启用 null, 0否, 1是]
     */
174
    public static function GoodsCategoryItemsIds($ids = [], $is_enable = null)
D
v1.2.0  
devil_gong 已提交
175 176 177 178 179 180 181 182 183
    {
        $where = ['pid'=>$ids];
        if($is_enable !== null)
        {
            $where['is_enable'] = $is_enable;
        }
        $data = Db::name('GoodsCategory')->where($where)->column('id');
        if(!empty($data))
        {
184
            $temp = self::GoodsCategoryItemsIds($data, $is_enable);
D
v1.2.0  
devil_gong 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
            if(!empty($temp))
            {
                $data = array_merge($data, $temp);
            }
        }
        return $data;
    }

    /**
     * 获取分类与商品关联总数
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-07
     * @desc    description
     * @param   array           $where [条件]
     */
202
    public static function CategoryGoodsTotal($where = [])
D
v1.2.0  
devil_gong 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215
    {
        return (int) Db::name('Goods')->alias('g')->join(['__GOODS_CATEGORY_JOIN__'=>'gci'], 'g.id=gci.goods_id')->where($where)->count('DISTINCT g.id');
    }

    /**
     * 获取分类与商品关联列表
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-08-29
     * @desc    description
     * @param   array           $params [输入参数: where, field, is_photo]
     */
216
    public static function CategoryGoodsList($params = [])
D
v1.2.0  
devil_gong 已提交
217 218 219 220 221 222 223 224 225
    {
        $where = empty($params['where']) ? [] : $params['where'];
        $field = empty($params['field']) ? 'g.*' : $params['field'];
        $order_by = empty($params['order_by']) ? 'g.id desc' : trim($params['order_by']);

        $m = isset($params['m']) ? intval($params['m']) : 0;
        $n = isset($params['n']) ? intval($params['n']) : 10;
        $data = Db::name('Goods')->alias('g')->join(['__GOODS_CATEGORY_JOIN__'=>'gci'], 'g.id=gci.goods_id')->field($field)->where($where)->group('g.id')->order($order_by)->limit($m, $n)->select();
        
226
        return self::GoodsDataHandle($params, $data);
D
v1.2.0  
devil_gong 已提交
227 228 229 230 231 232 233 234 235 236 237
    }

    /**
     * 商品数据处理
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-12-08T23:16:42+0800
     * @param    [array]                   $params [输入参数]
     * @param    [array]                   $data   [商品列表]
     */
238
    public static function GoodsDataHandle($params, $data)
D
v1.2.0  
devil_gong 已提交
239 240 241 242 243 244 245 246 247 248 249 250
    {
        if(!empty($data))
        {
            // 其它额外处理
            $is_photo = (isset($params['is_photo']) && $params['is_photo'] == true) ? true : false;
            $is_spec = (isset($params['is_spec']) && $params['is_spec'] == true) ? true : false;
            $is_content_app = (isset($params['is_content_app']) && $params['is_content_app'] == true) ? true : false;
            $is_category = (isset($params['is_category']) && $params['is_category'] == true) ? true : false;

            // 开始处理数据
            foreach($data as &$v)
            {
251 252 253 254 255 256 257 258 259 260 261 262 263 264
                // 商品处理前钩子
                $hook_name = 'plugins_service_goods_handle_begin';
                $ret = Hook::listen($hook_name, [
                    'hook_name'     => $hook_name,
                    'is_backend'    => true,
                    'params'        => &$params,
                    'goods'         => &$v,
                    'goods_id'      => $v['id']
                ]);
                if(isset($ret['code']) && $ret['code'] != 0)
                {
                    return $ret;
                }

D
v1.2.0  
devil_gong 已提交
265 266 267 268 269 270 271 272 273 274
                // 商品url地址
                if(!empty($v['id']))
                {
                    $v['goods_url'] = MyUrl('index/goods/index', ['id'=>$v['id']]);
                }

                // 商品封面图片
                if(isset($v['images']))
                {
                    $v['images_old'] = $v['images'];
275
                    $v['images'] = ResourcesService::AttachmentPathViewHandle($v['images']);
D
v1.2.0  
devil_gong 已提交
276 277 278 279 280 281
                }

                // 视频
                if(isset($v['video']))
                {
                    $v['video_old'] = $v['video'];
282
                    $v['video'] = ResourcesService::AttachmentPathViewHandle($v['video']);
D
v1.2.0  
devil_gong 已提交
283 284 285 286 287
                }

                // 商品首页推荐图片,不存在则使用商品封面图片
                if(isset($v['home_recommended_images']))
                {
G
gongfuxiang 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
                    if(empty($v['home_recommended_images']))
                    {
                        if(isset($v['images']))
                        {
                            $v['home_recommended_images'] = $v['images'];
                        } else {
                            if(!empty($v['id']))
                            {
                                $images = Db::name('Goods')->where(['id'=>$v['id']])->value('images');
                                $v['home_recommended_images'] = ResourcesService::AttachmentPathViewHandle($images);
                            }
                        }
                    } else {
                        $v['home_recommended_images_old'] = $v['home_recommended_images'];
                        $v['home_recommended_images'] = ResourcesService::AttachmentPathViewHandle($v['home_recommended_images']);
                    }
D
v1.2.0  
devil_gong 已提交
304 305 306 307 308 309 310 311 312
                }

                // PC内容处理
                if(isset($v['content_web']))
                {
                    $v['content_web'] = ResourcesService::ContentStaticReplace($v['content_web'], 'get');
                }

                // 产地
G
gongfuxiang 已提交
313 314 315 316
                if(isset($v['place_origin_name']))
                {
                    $v['place_origin_name'] = empty($v['place_origin']) ? null : RegionService::RegionName($v['place_origin']);
                }
D
v1.2.0  
devil_gong 已提交
317 318

                // 品牌
G
gongfuxiang 已提交
319 320 321 322
                if(isset($v['brand_id']))
                {
                    $v['brand_name'] = empty($v['brand_id']) ? null : BrandService::BrandName($v['brand_id']);
                }
D
v1.2.0  
devil_gong 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350

                // 时间
                if(!empty($v['add_time']))
                {
                    $v['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
                }
                if(!empty($v['upd_time']))
                {
                    $v['upd_time'] = date('Y-m-d H:i:s', $v['upd_time']);
                }

                // 是否需要分类名称
                if($is_category && !empty($v['id']))
                {
                    $v['category_ids'] = Db::name('GoodsCategoryJoin')->where(['goods_id'=>$v['id']])->column('category_id');
                    $category_name = Db::name('GoodsCategory')->where(['id'=>$v['category_ids']])->column('name');
                    $v['category_text'] = implode(',', $category_name);
                }

                // 获取相册
                if($is_photo && !empty($v['id']))
                {
                    $v['photo'] = Db::name('GoodsPhoto')->where(['goods_id'=>$v['id'], 'is_show'=>1])->order('sort asc')->select();
                    if(!empty($v['photo']))
                    {
                        foreach($v['photo'] as &$vs)
                        {
                            $vs['images_old'] = $vs['images'];
351
                            $vs['images'] = ResourcesService::AttachmentPathViewHandle($vs['images']);
D
v1.2.0  
devil_gong 已提交
352 353 354 355 356 357 358
                        }
                    }
                }

                // 获取规格
                if($is_spec && !empty($v['id']))
                {
359
                    $v['specifications'] = self::GoodsSpecifications(['goods_id'=>$v['id']]);
D
v1.2.0  
devil_gong 已提交
360 361 362 363 364
                }

                // 获取app内容
                if($is_content_app && !empty($v['id']))
                {
365
                    $v['content_app'] = self::GoodsContentApp(['goods_id'=>$v['id']]);
D
v1.2.0  
devil_gong 已提交
366
                }
367 368 369 370 371 372 373 374 375 376 377 378 379 380

                // 商品处理前钩子
                $hook_name = 'plugins_service_goods_handle_end';
                $ret = Hook::listen($hook_name, [
                    'hook_name'     => $hook_name,
                    'is_backend'    => true,
                    'params'        => &$params,
                    'goods'         => &$v,
                    'goods_id'      => $v['id']
                ]);
                if(isset($ret['code']) && $ret['code'] != 0)
                {
                    return $ret;
                }
D
v1.2.0  
devil_gong 已提交
381 382
            }
        }
383
        return DataReturn('处理成功', 0, $data);
D
v1.2.0  
devil_gong 已提交
384 385 386 387 388 389 390 391 392 393 394 395
    }

    /**
     * 获取商品手机详情
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-07-10
     * @desc    description
     * @param   [array]          $params [输入参数]
     * @return  [array]                  [app内容]
     */
396
    public static function GoodsContentApp($params = [])
D
v1.2.0  
devil_gong 已提交
397 398 399 400 401 402 403
    {
        $data = Db::name('GoodsContentApp')->where(['goods_id'=>$params['goods_id']])->field('id,images,content')->order('sort asc')->select();
        if(!empty($data))
        {
            foreach($data as &$v)
            {
                $v['images_old'] = $v['images'];
404
                $v['images'] = ResourcesService::AttachmentPathViewHandle($v['images']);
D
v1.2.0  
devil_gong 已提交
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
                $v['content_old'] = $v['content'];
                $v['content'] = empty($v['content']) ? null : explode("\n", $v['content']);
            }
        }
        return $data;
    }

    /**
     * 获取商品属性
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-08-29
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
421
    public static function GoodsSpecifications($params = [])
D
v1.2.0  
devil_gong 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435
    {
        // 条件
        $where = ['goods_id'=>$params['goods_id']];

        // 规格类型
        $choose = Db::name('GoodsSpecType')->where($where)->order('id asc')->select();
        if(!empty($choose))
        {
            // 数据处理
            foreach($choose as &$temp_type)
            {
                $temp_type_value = json_decode($temp_type['value'], true);
                foreach($temp_type_value as &$vs)
                {
436
                    $vs['images'] = ResourcesService::AttachmentPathViewHandle($vs['images']);
D
v1.2.0  
devil_gong 已提交
437 438 439 440
                }
                $temp_type['value'] = $temp_type_value;
                $temp_type['add_time'] = date('Y-m-d H:i:s');
            }
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460

            // 只有一个规格的时候直接获取规格值的库存数
            if(count($choose) == 1)
            {
                foreach($choose[0]['value'] as &$temp_spec)
                {
                    $temp_spec_params = [
                        'id'    => $params['goods_id'],
                        'spec'  => [
                            ['type' => $choose[0]['name'], 'value' => $temp_spec['name']]
                        ],
                    ];
                    $temp = self::GoodsSpecDetail($temp_spec_params);
                    if($temp['code'] == 0)
                    {
                        $temp_spec['is_only_level_one'] = 1;
                        $temp_spec['inventory'] = $temp['data']['inventory'];
                    }
                }
            }
D
v1.2.0  
devil_gong 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473
        }
        return ['choose'=>$choose];
    }

    /**
     * 商品收藏
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-08-29
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
474
    public static function GoodsFavor($params = [])
D
v1.2.0  
devil_gong 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
    {
        // 请求参数
        $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);
        }

495 496 497 498 499 500 501
        // 查询用户状态是否正常
        $ret = UserService::UserStatusCheck('id', $params['user']['id']);
        if($ret['code'] != 0)
        {
            return $ret;
        }

D
v1.2.0  
devil_gong 已提交
502 503 504 505 506 507 508 509 510 511 512 513
        // 开始操作
        $data = ['goods_id'=>intval($params['id']), 'user_id'=>$params['user']['id']];
        $temp = Db::name('GoodsFavor')->where($data)->find();
        if(empty($temp))
        {
            // 添加收藏
            $data['add_time'] = time();
            if(Db::name('GoodsFavor')->insertGetId($data) > 0)
            {
                return DataReturn('收藏成功', 0, [
                    'text'      => '已收藏',
                    'status'    => 1,
514
                    'count'     => self::GoodsFavorTotal(['goods_id'=>$data['goods_id']]),
D
v1.2.0  
devil_gong 已提交
515 516 517 518 519 520 521 522 523 524 525
                ]);
            } else {
                return DataReturn('收藏失败');
            }
        } else {
            // 是否强制收藏
            if(isset($params['is_mandatory_favor']) && $params['is_mandatory_favor'] == 1)
            {
                return DataReturn('收藏成功', 0, [
                    'text'      => '已收藏',
                    'status'    => 1,
526
                    'count'     => self::GoodsFavorTotal(['goods_id'=>$data['goods_id']]),
D
v1.2.0  
devil_gong 已提交
527 528 529 530 531 532 533 534 535
                ]);
            }

            // 删除收藏
            if(Db::name('GoodsFavor')->where($data)->delete() > 0)
            {
                return DataReturn('取消成功', 0, [
                    'text'      => '收藏',
                    'status'    => 0,
536
                    'count'     => self::GoodsFavorTotal(['goods_id'=>$data['goods_id']]),
D
v1.2.0  
devil_gong 已提交
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
                ]);
            } else {
                return DataReturn('取消失败');
            }
        }
    }

    /**
     * 用户是否收藏了商品
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-08-29
     * @desc    description
     * @param   [array]          $params [输入参数]
     * @return  [int]                    [1已收藏, 0未收藏]
     */
554
    public static function IsUserGoodsFavor($params = [])
D
v1.2.0  
devil_gong 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'goods_id',
                'error_msg'         => '商品id有误',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'user',
                'error_msg'         => '用户信息有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        $data = ['goods_id'=>intval($params['goods_id']), 'user_id'=>$params['user']['id']];
        $temp = Db::name('GoodsFavor')->where($data)->find();
        return DataReturn('操作成功', 0, empty($temp) ? 0 : 1);
    }

    /**
     * 商品评论总数
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [array]          $where [条件]
     */
589
    public static function GoodsCommentsTotal($goods_id)
D
v1.2.0  
devil_gong 已提交
590 591 592 593 594 595 596 597 598 599 600 601 602
    {
        return (int) Db::name('OrderComments')->where(['goods_id'=>intval($goods_id)])->count();
    }

    /**
     * 前端商品收藏列表条件
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
603
    public static function UserGoodsFavorListWhere($params = [])
D
v1.2.0  
devil_gong 已提交
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
    {
        $where = [
            ['g.is_delete_time', '=', 0]
        ];

        // 用户id
        if(!empty($params['user']))
        {
            $where[]= ['f.user_id', '=', $params['user']['id']];
        }

        if(!empty($params['keywords']))
        {
            $where[] = ['g.title', 'like', '%'.$params['keywords'].'%'];
        }

        return $where;
    }

    /**
     * 商品收藏总数
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [array]          $where [条件]
     */
632
    public static function GoodsFavorTotal($where = [])
D
v1.2.0  
devil_gong 已提交
633 634 635 636 637 638 639 640 641 642 643 644 645
    {
        return (int) Db::name('GoodsFavor')->alias('f')->join(['__GOODS__'=>'g'], 'g.id=f.goods_id')->where($where)->count();
    }

    /**
     * 商品收藏列表
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
646
    public static function GoodsFavorList($params = [])
D
v1.2.0  
devil_gong 已提交
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
    {
        $where = empty($params['where']) ? [] : $params['where'];
        $m = isset($params['m']) ? intval($params['m']) : 0;
        $n = isset($params['n']) ? intval($params['n']) : 10;
        $order_by = empty($params['order_by']) ? 'f.id desc' : $params['order_by'];
        $field = 'f.*, g.title, g.original_price, g.price, g.images';

        // 获取数据
        $data = Db::name('GoodsFavor')->alias('f')->join(['__GOODS__'=>'g'], 'g.id=f.goods_id')->field($field)->where($where)->limit($m, $n)->order($order_by)->select();
        if(!empty($data))
        {
            foreach($data as &$v)
            {
                // 图片
                $v['images_old'] = $v['images'];
662
                $v['images'] = ResourcesService::AttachmentPathViewHandle($v['images']);
D
v1.2.0  
devil_gong 已提交
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678

                $v['goods_url'] = MyUrl('index/goods/index', ['id'=>$v['goods_id']]);
            }
        }
        return DataReturn('处理成功', 0, $data);
    }

    /**
     * 商品访问统计加1
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-10-15
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
679
    public static function GoodsAccessCountInc($params = [])
D
v1.2.0  
devil_gong 已提交
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
    {
        if(!empty($params['goods_id']))
        {
            return Db::name('Goods')->where(array('id'=>intval($params['goods_id'])))->setInc('access_count');
        }
        return false;
    }

    /**
     * 商品浏览保存
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-10-15
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
697
    public static function GoodsBrowseSave($params = [])
D
v1.2.0  
devil_gong 已提交
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'goods_id',
                'error_msg'         => '商品id有误',
            ],
            [
                'checked_type'      => 'is_array',
                'key_name'          => 'user',
                'error_msg'         => '用户信息有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        $where = ['goods_id'=>intval($params['goods_id']), 'user_id'=>$params['user']['id']];
        $temp = Db::name('GoodsBrowse')->where($where)->find();

        $data = [
            'goods_id'  => intval($params['goods_id']),
            'user_id'   => $params['user']['id'],
            'upd_time'  => time(),
        ];
        if(empty($temp))
        {
            $data['add_time'] = time();
            $status = Db::name('GoodsBrowse')->insertGetId($data) > 0;
        } else {
            $status = Db::name('GoodsBrowse')->where($where)->update($data) !== false;
        }
        if($status)
        {
            return DataReturn('处理成功', 0);
        }
        return DataReturn('处理失败', -100);
    }

    /**
     * 前端商品浏览列表条件
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
749
    public static function UserGoodsBrowseListWhere($params = [])
D
v1.2.0  
devil_gong 已提交
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
    {
        $where = [
            ['g.is_delete_time', '=', 0]
        ];

        // 用户id
        if(!empty($params['user']))
        {
            $where[] = ['b.user_id', '=', $params['user']['id']];
        }

        if(!empty($params['keywords']))
        {
            $where[] = ['g.title', 'like', '%'.$params['keywords'].'%'];
        }

        return $where;
    }

    /**
     * 商品浏览总数
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [array]          $where [条件]
     */
778
    public static function GoodsBrowseTotal($where = [])
D
v1.2.0  
devil_gong 已提交
779 780 781 782 783 784 785 786 787 788 789 790 791
    {
        return (int) Db::name('GoodsBrowse')->alias('b')->join(['__GOODS__'=>'g'], 'g.id=b.goods_id')->where($where)->count();
    }

    /**
     * 商品浏览列表
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-29
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
792
    public static function GoodsBrowseList($params = [])
D
v1.2.0  
devil_gong 已提交
793 794 795 796 797 798 799 800 801 802 803 804 805 806
    {
        $where = empty($params['where']) ? [] : $params['where'];
        $m = isset($params['m']) ? intval($params['m']) : 0;
        $n = isset($params['n']) ? intval($params['n']) : 10;
        $order_by = empty($params['order_by']) ? 'b.id desc' : $params['order_by'];
        $field = 'b.*, g.title, g.original_price, g.price, g.images';

        // 获取数据
        $data = Db::name('GoodsBrowse')->alias('b')->join(['__GOODS__'=>'g'], 'g.id=b.goods_id')->field($field)->where($where)->limit($m, $n)->order($order_by)->select();
        if(!empty($data))
        {
            foreach($data as &$v)
            {
                $v['images_old'] = $v['images'];
807
                $v['images'] = ResourcesService::AttachmentPathViewHandle($v['images']);
D
v1.2.0  
devil_gong 已提交
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
                $v['goods_url'] = MyUrl('index/goods/index', ['id'=>$v['goods_id']]);
            }
        }
        return DataReturn('处理成功', 0, $data);
    }

    /**
     * 商品浏览删除
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-14
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
823
    public static function GoodsBrowseDelete($params = [])
D
v1.2.0  
devil_gong 已提交
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
    {
        // 请求参数
        $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);
        }

        // 删除
        $where = [
            'id'        => explode(',', $params['id']),
            'user_id'   => $params['user']['id']
        ];
        if(Db::name('GoodsBrowse')->where($where)->delete())
        {
            return DataReturn('删除成功', 0);
        }
        return DataReturn('删除失败或资源不存在', -100);
    }

    /**
     * 获取商品总数
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-07
     * @desc    description
     * @param   [array]           $where [条件]
     */
865
    public static function GoodsTotal($where = [])
D
v1.2.0  
devil_gong 已提交
866 867 868 869 870 871 872 873 874 875 876 877 878
    {
        return (int) Db::name('Goods')->where($where)->count();
    }

    /**
     * 获取商品列表
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-08-29
     * @desc    description
     * @param   array           $params [输入参数: where, field, is_photo]
     */
879
    public static function GoodsList($params = [])
D
v1.2.0  
devil_gong 已提交
880 881 882 883 884 885 886 887 888
    {
        $where = empty($params['where']) ? [] : $params['where'];
        $field = empty($params['field']) ? '*' : $params['field'];
        $order_by = empty($params['order_by']) ? 'id desc' : trim($params['order_by']);

        $m = isset($params['m']) ? intval($params['m']) : 0;
        $n = isset($params['n']) ? intval($params['n']) : 10;
        $data = Db::name('Goods')->field($field)->where($where)->order($order_by)->limit($m, $n)->select();
        
889
        return self::GoodsDataHandle($params, $data);
D
v1.2.0  
devil_gong 已提交
890 891 892 893 894 895 896 897 898 899
    }

    /**
     * 后台管理商品列表条件
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-10T22:16:29+0800
     * @param    [array]          $params [输入参数]
     */
900
    public static function GetAdminIndexWhere($params = [])
D
v1.2.0  
devil_gong 已提交
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
    {
        $where = [
            ['is_delete_time', '=', 0],
        ];

        // 模糊
        if(!empty($params['keywords']))
        {
            $where[] = ['title|model', 'like', '%'.$params['keywords'].'%'];
        }

        // 是否更多条件
        if(isset($params['is_more']) && $params['is_more'] == 1)
        {
            // 等值
            if(isset($params['is_shelves']) && $params['is_shelves'] > -1)
            {
                $where[] = ['is_shelves', '=', intval($params['is_shelves'])];
            }
            if(isset($params['is_home_recommended']) && $params['is_home_recommended'] > -1)
            {
                $where[] = ['is_home_recommended', '=', intval($params['is_home_recommended'])];
            }

            // 时间
            if(!empty($params['time_start']))
            {
                $where[] = ['add_time', '>', strtotime($params['time_start'])];
            }
            if(!empty($params['time_end']))
            {
                $where[] = ['add_time', '<', strtotime($params['time_end'])];
            }
        }
        return $where;
    }

    /**
     * 商品保存
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-12-10T01:02:11+0800
     * @param    [array]          $params [输入参数]
     */
946
    public static function GoodsSave($params = [])
D
v1.2.0  
devil_gong 已提交
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'length',
                'key_name'          => 'title',
                'checked_data'      => '2,60',
                'error_msg'         => '标题名称格式 2~60 个字符',
            ],
            [
                'checked_type'      => 'length',
                'key_name'          => 'model',
                'checked_data'      => '30',
                'is_checked'        => 1,
                'error_msg'         => '商品型号格式 最多30个字符',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'category_id',
                'error_msg'         => '请至少选择一个商品分类',
            ],
            [
                'checked_type'      => 'length',
                'key_name'          => 'inventory_unit',
                'checked_data'      => '1,6',
                'error_msg'         => '库存单位格式 1~6 个字符',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'buy_min_number',
                'error_msg'         => '请填写有效的最低起购数量',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 规格
987
        $specifications = self::GetFormGoodsSpecificationsParams($params);
D
v1.2.0  
devil_gong 已提交
988 989 990 991 992 993
        if($specifications['code'] != 0)
        {
            return $specifications;
        }
        
        // 相册
994
        $photo = self::GetFormGoodsPhotoParams($params);
D
v1.2.0  
devil_gong 已提交
995 996 997 998 999 1000
        if($photo['code'] != 0)
        {
            return $photo;
        }

        // 手机端详情
1001
        $content_app = self::GetFormGoodsContentAppParams($params);
D
v1.2.0  
devil_gong 已提交
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
        if($content_app['code'] != 0)
        {
            return $content_app;
        }

        // 其它附件
        $data_fields = ['home_recommended_images', 'video'];
        $attachment = ResourcesService::AttachmentParams($params, $data_fields);
        if($attachment['code'] != 0)
        {
            return $attachment;
        }

D
devil_gong 已提交
1015 1016 1017
        // 编辑器内容
        $content_web = isset($_POST['content_web']) ? $_POST['content_web'] : '';

D
v1.2.0  
devil_gong 已提交
1018 1019 1020
        // 基础数据
        $data = [
            'title'                     => $params['title'],
D
devil_gong 已提交
1021
            'title_color'               => empty($params['title_color']) ? '' : $params['title_color'],
D
v1.2.0  
devil_gong 已提交
1022
            'model'                     => $params['model'],
D
devil_gong 已提交
1023
            'place_origin'              => isset($params['place_origin']) ? intval($params['place_origin']) : 0,
D
v1.2.0  
devil_gong 已提交
1024 1025
            'inventory_unit'            => $params['inventory_unit'],
            'give_integral'             => intval($params['give_integral']),
D
devil_gong 已提交
1026 1027
            'buy_min_number'            => max(1, isset($params['buy_min_number']) ? intval($params['buy_min_number']) : 1),
            'buy_max_number'            => isset($params['buy_max_number']) ? intval($params['buy_max_number']) : 0,
D
v1.2.0  
devil_gong 已提交
1028 1029
            'is_deduction_inventory'    => isset($params['is_deduction_inventory']) ? intval($params['is_deduction_inventory']) : 0,
            'is_shelves'                => isset($params['is_shelves']) ? intval($params['is_shelves']) : 0,
D
devil_gong 已提交
1030
            'content_web'               => ResourcesService::ContentStaticReplace($content_web, 'add'),
D
v1.2.0  
devil_gong 已提交
1031 1032 1033 1034
            'images'                    => isset($photo['data'][0]) ? $photo['data'][0] : '',
            'photo_count'               => count($photo['data']),
            'is_home_recommended'       => isset($params['is_home_recommended']) ? intval($params['is_home_recommended']) : 0,
            'home_recommended_images'   => $attachment['data']['home_recommended_images'],
D
devil_gong 已提交
1035
            'brand_id'                  => isset($params['brand_id']) ? intval($params['brand_id']) : 0,
D
v1.2.0  
devil_gong 已提交
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
            'video'                     => $attachment['data']['video'],
        ];

        // 启动事务
        Db::startTrans();

        // 添加/编辑
        if(empty($params['id']))
        {
            $data['add_time'] = time();
            $goods_id = Db::name('Goods')->insertGetId($data);
        } else {
            $goods = Db::name('Goods')->find($params['id']);
            $data['upd_time'] = time();
            if(Db::name('Goods')->where(['id'=>intval($params['id'])])->update($data))
            {
                $goods_id = $params['id'];
            }
        }

        // 是否成功
        if(isset($goods_id) && $goods_id > 0)
        {
            // 分类
1060
            $ret = self::GoodsCategoryInsert(explode(',', $params['category_id']), $goods_id);
D
v1.2.0  
devil_gong 已提交
1061 1062 1063 1064 1065 1066 1067 1068
            if($ret['code'] != 0)
            {
                // 回滚事务
                Db::rollback();
                return $ret;
            }

            // 规格
1069
            $ret = self::GoodsSpecificationsInsert($specifications['data'], $goods_id);
D
v1.2.0  
devil_gong 已提交
1070 1071 1072 1073 1074 1075 1076
            if($ret['code'] != 0)
            {
                // 回滚事务
                Db::rollback();
                return $ret;
            } else {
                // 更新商品基础信息
1077
                $ret = self::GoodsSaveBaseUpdate($params, $goods_id);
D
v1.2.0  
devil_gong 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086
                if($ret['code'] != 0)
                {
                    // 回滚事务
                    Db::rollback();
                    return $ret;
                }
            }

            // 相册
1087
            $ret = self::GoodsPhotoInsert($photo['data'], $goods_id);
D
v1.2.0  
devil_gong 已提交
1088 1089 1090 1091 1092 1093 1094 1095
            if($ret['code'] != 0)
            {
                // 回滚事务
                Db::rollback();
                return $ret;
            }

            // 手机详情
1096
            $ret = self::GoodsContentAppInsert($content_app['data'], $goods_id);
D
v1.2.0  
devil_gong 已提交
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
            if($ret['code'] != 0)
            {
                // 回滚事务
                Db::rollback();
                return $ret;
            }

            // 提交事务
            Db::commit();
            return DataReturn('操作成功', 0);
        }

        // 回滚事务
        Db::rollback();
        return DataReturn('操作失败', -100);
    }

    /**
     * 商品保存基础信息更新
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-12-16T01:56:42+0800
     * @param    [array]          $params   [输入参数]
     * @param    [int]            $goods_id [商品id]
     */
1123
    private static function GoodsSaveBaseUpdate($params, $goods_id)
D
v1.2.0  
devil_gong 已提交
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
    {
        $data = Db::name('GoodsSpecBase')->field('min(price) AS min_price, max(price) AS max_price, sum(inventory) AS inventory, min(original_price) AS min_original_price, max(original_price) AS max_original_price')->where(['goods_id'=>$goods_id])->find();
        if(empty($data))
        {
            return DataReturn('没找到商品基础信息', -1);
        }

        // 销售价格 - 展示价格
        $data['price'] = (!empty($data['max_price']) && $data['min_price'] != $data['max_price']) ? $data['min_price'].'-'.$data['max_price'] : $data['min_price'];

        // 原价价格 - 展示价格
        $data['original_price'] = (!empty($data['max_original_price']) && $data['min_original_price'] != $data['max_original_price']) ? $data['min_original_price'].'-'.$data['max_original_price'] : $data['min_original_price'];

        // 更新商品表
        $data['upd_time'] = time();
        if(Db::name('Goods')->where(['id'=>$goods_id])->update($data))
        {
            return DataReturn('操作成功', 0);
        }
        return DataReturn('操作失败', 0);
    }

    /**
     * 获取规格参数
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-07-09
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
1155
    private static function GetFormGoodsSpecificationsParams($params = [])
D
v1.2.0  
devil_gong 已提交
1156 1157 1158 1159 1160
    {
        $data = [];
        $title = [];
        $images = [];

1161 1162 1163
        // 基础字段数据字段长度
        $base_count = 6;

D
v1.2.0  
devil_gong 已提交
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
        // 规格值
        foreach($params as $k=>$v)
        {
            if(substr($k, 0, 15) == 'specifications_')
            {
                $keys = explode('_', $k);
                if(count($keys) > 1)
                {
                    if($keys[1] != 'name')
                    {
                        foreach($v as $ks=>$vs)
                        {
                            $data[$ks][] = $vs;
                        }
                    }
                }
            }
        }

1183
        // 规格处理
D
v1.2.0  
devil_gong 已提交
1184 1185
        if(!empty($data[0]))
        {
1186
            $count = count($data[0])-$base_count;
D
v1.2.0  
devil_gong 已提交
1187 1188
            if($count > 0)
            {
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
                // 列之间是否存在相同的值
                $column_value = [];
                foreach($data as $data_value)
                {
                    foreach($data_value as $temp_key=>$temp_value)
                    {
                        if($temp_key < $count)
                        {
                            $column_value[$temp_key][] = $temp_value;
                        }
                    }
                }
                if(!empty($column_value) && count($column_value) > 1)
                {
                    $temp_column = [];
                    foreach($column_value as $column_key=>$column_val)
                    {
                        foreach($column_value as $column_keys=>$column_vals)
                        {
                            if($column_key != $column_keys)
                            {
                                $temp = array_intersect($column_val, $column_vals);
                                $temp_column = array_merge($temp_column, $temp);
                            }
                        }
                    }
                    if(!empty($temp_column))
                    {
1217
                        return DataReturn('规格值列之间不能重复['.implode(',', array_unique($temp_column)).']', -1);
1218 1219 1220 1221
                    }
                }
                
                // 规格名称
1222
                $names_value = [];
D
v1.2.0  
devil_gong 已提交
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
                $names = array_slice($data[0], 0, $count);
                foreach($names as $v)
                {
                    foreach($params as $ks=>$vs)
                    {
                        if(substr($ks, 0, 21) == 'specifications_value_')
                        {
                            if(in_array($v, $vs))
                            {
                                $key = substr($ks, 21);
                                if(!empty($params['specifications_name_'.$key]))
                                {
                                    $title[$params['specifications_name_'.$key]] = [
                                        'name'  => $params['specifications_name_'.$key],
                                        'value' => array_unique($vs),
                                    ];
1239
                                    $names_value[] = $params['specifications_name_'.$key];
D
v1.2.0  
devil_gong 已提交
1240 1241 1242 1243 1244
                                }
                            }
                        }
                    }
                }
1245

1246
                // 规格名称列之间是否存在重复
1247 1248 1249 1250 1251 1252
                $unique_all = array_unique($names_value);
                $repeat_names_all = array_diff_assoc($names_value, $unique_all); 
                if(!empty($repeat_names_all))
                {
                    return DataReturn('规格名称列之间不能重复['.implode(',', $repeat_names_all).']', -1);
                }
D
v1.2.0  
devil_gong 已提交
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
            } else {
                if(empty($data[0][0]) || $data[0][0] <= 0)
                {
                    return DataReturn('请填写有效的规格销售价格', -1);
                }
                if(empty($data[0][1]))
                {
                    return DataReturn('请填写规格库存', -1);
                }
            }
        } else {
            return DataReturn('请填写规格', -1);
        }

        // 规格图片
        if(!empty($params['spec_images_name']) && !empty($params['spec_images']))
        {
            foreach($params['spec_images_name'] as $k=>$v)
            {
                if(!empty($params['spec_images'][$k]))
                {
                    $images[$v] = $params['spec_images'][$k];
                }
            }
        }

        return DataReturn('success', 0, ['data'=>$data, 'title'=>$title, 'images'=>$images]);
    }

    /**
     * 获取商品相册
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-07-10
     * @desc    description
     * @param   [array]          $params [输入参数]
     * @return  [array]                  [一维数组但图片地址]
     */
1292
    private static function GetFormGoodsPhotoParams($params = [])
D
v1.2.0  
devil_gong 已提交
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
    {
        if(empty($params['photo']))
        {
            return DataReturn('请上传相册', -1);
        }

        $result = [];
        if(!empty($params['photo']) && is_array($params['photo']))
        {
            foreach($params['photo'] as $v)
            {
                $result[] = ResourcesService::AttachmentPathHandle($v);
            }
        }
        return DataReturn('success', 0, $result);
    }

    /**
     * 获取app内容
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-07-09
     * @desc    description
     * @param    [array]          $params [输入参数]
     */
1319
    private static function GetFormGoodsContentAppParams($params = [])
D
v1.2.0  
devil_gong 已提交
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
    {
        // 开始处理
        $result = [];
        $name = 'content_app_';
        foreach($params AS $k=>$v)
        {
            if(substr($k, 0, 12) == $name)
            {
                $key = explode('_', str_replace($name, '', $k));
                if(count($key) == 2)
                {
                    $result[$key[1]][$key[0]] = $v;
                    if($key[0] == 'images')
                    {
                        $result[$key[1]][$key[0]] = ResourcesService::AttachmentPathHandle($v);
                    }
                }
            }
        }
        return DataReturn('success', 0, $result);
    }

    /**
     * 商品分类添加
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-07-10
     * @desc    description
     * @param   [array]          $data     [数据]
     * @param   [int]            $goods_id [商品id]
     * @return  [array]                    [boolean | msg]
     */
1353
    private static function GoodsCategoryInsert($data, $goods_id)
D
v1.2.0  
devil_gong 已提交
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
    {
        Db::name('GoodsCategoryJoin')->where(['goods_id'=>$goods_id])->delete();
        if(!empty($data))
        {
            foreach($data as $category_id)
            {
                $temp_category = [
                    'goods_id'      => $goods_id,
                    'category_id'   => $category_id,
                    'add_time'      => time(),
                ];
                if(Db::name('GoodsCategoryJoin')->insertGetId($temp_category) <= 0)
                {
                    return DataReturn('商品分类添加失败', -1);
                }
            }
        }
        return DataReturn('添加成功', 0);
    }

    /**
     * 商品手机详情添加
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-07-10
     * @desc    description
     * @param   [array]          $data     [数据]
     * @param   [int]            $goods_id [商品id]
     * @return  [array]                    [boolean | msg]
     */
1385
    private static function GoodsContentAppInsert($data, $goods_id)
D
v1.2.0  
devil_gong 已提交
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
    {
        Db::name('GoodsContentApp')->where(['goods_id'=>$goods_id])->delete();
        if(!empty($data))
        {
            foreach(array_values($data) as $k=>$v)
            {
                $temp_content = [
                    'goods_id'  => $goods_id,
                    'images'    => empty($v['images']) ? '' : $v['images'],
                    'content'   => $v['text'],
                    'sort'      => $k,
                    'add_time'  => time(),
                ];
                if(Db::name('GoodsContentApp')->insertGetId($temp_content) <= 0)
                {
                    return DataReturn('手机详情添加失败', -1);
                }
            }
        }
        return DataReturn('添加成功', 0);
    }

    /**
     * 商品相册添加
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-07-10
     * @desc    description
     * @param   [array]          $data     [数据]
     * @param   [int]            $goods_id [商品id]
     * @return  [array]                    [boolean | msg]
     */
1419
    private static function GoodsPhotoInsert($data, $goods_id)
D
v1.2.0  
devil_gong 已提交
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
    {
        Db::name('GoodsPhoto')->where(['goods_id'=>$goods_id])->delete();
        if(!empty($data))
        {
            foreach($data as $k=>$v)
            {
                $temp_photo = [
                    'goods_id'  => $goods_id,
                    'images'    => $v,
                    'is_show'   => 1,
                    'sort'      => $k,
                    'add_time'  => time(),
                ];
                if(Db::name('GoodsPhoto')->insertGetId($temp_photo) <= 0)
                {
                    return DataReturn('相册添加失败', -1);
                }
            }
        }
        return DataReturn('添加成功', 0);
    }

    /**
     * 商品规格添加
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-07-10
     * @desc    description
     * @param   [array]          $data     [数据]
     * @param   [int]            $goods_id [商品id]
     * @return  [array]                    [boolean | msg]
     */
1453
    private static function GoodsSpecificationsInsert($data, $goods_id)
D
v1.2.0  
devil_gong 已提交
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 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 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
    {
        // 删除原来的数据
        Db::name('GoodsSpecType')->where(['goods_id'=>$goods_id])->delete();
        Db::name('GoodsSpecValue')->where(['goods_id'=>$goods_id])->delete();
        Db::name('GoodsSpecBase')->where(['goods_id'=>$goods_id])->delete();

        // 类型
        if(!empty($data['title']))
        {
            foreach($data['title'] as &$v)
            {
                $spec = [];
                foreach($v['value'] as $vs)
                {
                    $spec[] = [
                        'name'      => $vs,
                        'images'    => isset($data['images'][$vs]) ? ResourcesService::AttachmentPathHandle($data['images'][$vs]) : '',
                    ];
                }
                $v['goods_id']  = $goods_id;
                $v['value']     = json_encode($spec);
                $v['add_time']  = time();
            }
            if(Db::name('GoodsSpecType')->insertAll($data['title']) < count($data['title']))
            {
                return DataReturn('规格类型添加失败', -1);
            }
        }

        // 基础/规格值
        if(!empty($data['data']))
        {
            // 基础字段
            $count = count($data['data'][0]);
            $temp_key = ['price', 'inventory', 'weight', 'coding', 'barcode', 'original_price'];
            $key_count = count($temp_key);

            // 等于key总数则只有一列基础规格
            if($count == $key_count)
            {
                $temp_data = [
                    'goods_id' => $goods_id,
                    'add_time' => time(),
                ];
                for($i=0; $i<$count; $i++)
                {
                    $temp_data[$temp_key[$i]] = $data['data'][0][$i];
                }
                // 规格基础添加
                if(Db::name('GoodsSpecBase')->insertGetId($temp_data) <= 0)
                {
                    return DataReturn('规格基础添加失败', -1);
                }

            // 多规格操作
            } else {
                $base_start = $count-$key_count;
                $value = [];
                $base = [];
                foreach($data['data'] as $v)
                {
                    $temp_value = [];
                    $temp_data = [
                        'goods_id' => $goods_id,
                        'add_time' => time(),
                    ];
                    for($i=0; $i<$count; $i++)
                    {
                        if($i < $base_start)
                        {
                            $temp_value[] = [
                                'goods_id'  => $goods_id,
                                'value'     => $v[$i],
                                'add_time'  => time()
                            ];
                        } else {
                            $temp_data[$temp_key[$i-$base_start]] = $v[$i];
                        }
                    }
                    
                    // 规格基础添加
                    $base_id = Db::name('GoodsSpecBase')->insertGetId($temp_data);
                    if(empty($base_id))
                    {
                        return DataReturn('规格基础添加失败', -1);
                    }

                    // 规格值添加
                    foreach($temp_value as &$value)
                    {
                        $value['goods_spec_base_id'] = $base_id;
                    }
                    if(Db::name('GoodsSpecValue')->insertAll($temp_value) < count($temp_value))
                    {
                        return DataReturn('规格值添加失败', -1);
                    }
                }
            }
        }

        return DataReturn('添加成功', 0);
    }

    /**
     * 商品删除
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-12-07T00:24:14+0800
     * @param    [array]          $params [输入参数]
     */
1565
    public static function GoodsDelete($params = [])
D
v1.2.0  
devil_gong 已提交
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 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
    {
        // 参数是否有误
        if(empty($params['id']))
        {
            return DataReturn('商品id有误', -1);
        }

        // 开启事务
        Db::startTrans();

        // 删除商品
        if(Db::name('Goods')->delete(intval($params['id'])))
        {
            // 商品规格
            if(Db::name('GoodsSpecType')->where(['goods_id'=>intval($params['id'])])->delete() === false)
            {
                Db::rollback();
                return DataReturn('规格类型删除失败', -100);
            }
            if(Db::name('GoodsSpecValue')->where(['goods_id'=>intval($params['id'])])->delete() === false)
            {
                Db::rollback();
                return DataReturn('规格值删除失败', -100);
            }
            if(Db::name('GoodsSpecBase')->where(['goods_id'=>intval($params['id'])])->delete() === false)
            {
                Db::rollback();
                return DataReturn('规格基础删除失败', -100);
            }

            // 相册
            if(Db::name('GoodsPhoto')->where(['goods_id'=>intval($params['id'])])->delete() === false)
            {
                Db::rollback();
                return DataReturn('相册删除失败', -100);
            }

            // app内容
            if(Db::name('GoodsContentApp')->where(['goods_id'=>intval($params['id'])])->delete() === false)
            {
                Db::rollback();
                return DataReturn('相册删除失败', -100);
            }

            // 提交事务
            Db::commit();
            return DataReturn('删除成功', 0);
        }

        Db::rollback();
        return DataReturn('删除失败', -100);
    }

    /**
     * 商品状态更新
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     * @param    [array]          $params [输入参数]
     */
1627
    public static function GoodsStatusUpdate($params = [])
D
v1.2.0  
devil_gong 已提交
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '操作id有误',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'field',
                'error_msg'         => '未指定操作字段',
            ],
            [
                'checked_type'      => 'in',
                'key_name'          => 'state',
                'checked_data'      => [0,1],
                'error_msg'         => '状态有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 数据更新
        if(Db::name('Goods')->where(['id'=>intval($params['id'])])->update([$params['field']=>intval($params['state']), 'upd_time'=>time()]))
        {
            return DataReturn('操作成功');
        }
        return DataReturn('操作失败', -100);
    }

    /**
     * 获取商品编辑规格
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-14
     * @desc    description
     * @param   [int]          $goods_id [商品id]
     */
1671
    public static function GoodsEditSpecifications($goods_id)
D
v1.2.0  
devil_gong 已提交
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
    {
        $where = ['goods_id'=>$goods_id];

        // 获取规格类型
        $type = Db::name('GoodsSpecType')->where($where)->order('id asc')->field('id,name,value')->select();
        $value = [];
        if(!empty($type))
        {
            // 数据处理
            foreach($type as &$temp_type)
            {
                $temp_type_value = json_decode($temp_type['value'], true);
                foreach($temp_type_value as &$vs)
                {
                    $vs['images_old'] = $vs['images'];
1687
                    $vs['images'] = ResourcesService::AttachmentPathViewHandle($vs['images']);
D
v1.2.0  
devil_gong 已提交
1688 1689 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 1717 1718 1719 1720 1721 1722 1723 1724 1725
                }
                $temp_type['value'] = $temp_type_value;
            }


            // 获取规格值
            $temp_value = Db::name('GoodsSpecValue')->where($where)->field('goods_spec_base_id,value')->order('id asc')->select();
            if(!empty($temp_value))
            {
                foreach($temp_value as $value_v)
                {
                    $key = '';
                    foreach($type as $type_v)
                    {
                        foreach($type_v['value'] as $type_vs)
                        {
                            if($type_vs['name'] == $value_v['value'])
                            {
                                $key = $type_v['id'];
                                break;
                            }
                        }
                    }
                    $value[$value_v['goods_spec_base_id']][] = [
                        'data_type' => 'spec',
                        'data'  => [
                            'key'       => $key,
                            'value'     => $value_v['value'],
                        ],
                    ];
                }
            }

            if(!empty($value))
            {
                foreach($value as $k=>&$v)
                {
                    $base = Db::name('GoodsSpecBase')->find($k);
1726
                    $base['weight'] = PriceBeautify($base['weight']);
D
v1.2.0  
devil_gong 已提交
1727 1728 1729 1730 1731 1732 1733 1734
                    $v[] = [
                        'data_type' => 'base',
                        'data'      => $base,
                    ];
                }
            }
        } else {
            $base = Db::name('GoodsSpecBase')->where($where)->find();
1735
            $base['weight'] = PriceBeautify($base['weight']);
D
v1.2.0  
devil_gong 已提交
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
            $value[][] = [
                'data_type' => 'base',
                'data'      => $base,
            ];
        }

        return [
            'type'      => $type,
            'value'     => array_values($value),
        ];
    }

    /**
     * 商品规格信息
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-14
     * @desc    description
     * @param    [array]          $params [输入参数]
     */
1757
    public static function GoodsSpecDetail($params = [])
D
v1.2.0  
devil_gong 已提交
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '商品id有误',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'spec',
                'is_checked'        => 1,
                'error_msg'         => '请选择规格',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 条件
        $goods_id = intval($params['id']);
        $where = [
            'goods_id'  => intval($params['id']),
        ];

        // 有规格值
        if(!empty($params['spec']))
        {
            $value = [];
            // 规格不为数组则为json字符串
            if(!is_array($params['spec']))
            {
                $params['spec'] = json_decode($params['spec'], true);
            }
            foreach($params['spec'] as $v)
            {
                $value[] = $v['value'];
            }
            $where['value'] = $value;

            // 获取规格值基础值id
            $ids = Db::name('GoodsSpecValue')->where($where)->column('goods_spec_base_id');
            if(!empty($ids))
            {
                // 根据基础值id获取规格值列表
                $temp_data = Db::name('GoodsSpecValue')->where(['goods_spec_base_id'=>$ids])->field('goods_spec_base_id,value')->select();
                if(!empty($temp_data))
                {
                    // 根据基础值id分组
                    $data = [];
                    foreach($temp_data as $v)
                    {
                        $data[$v['goods_spec_base_id']][] = $v;
                    }

                    // 从条件中匹配对应的规格值得到最终的基础值id
                    $base_id = 0;
                    $spec_str = implode('', array_column($params['spec'], 'value'));
                    foreach($data as $value_v)
                    {
                        $temp_str = implode('', array_column($value_v, 'value'));
                        if($temp_str == $spec_str)
                        {
                            $base_id = $value_v[0]['goods_spec_base_id'];
                            break;
                        }
                    }
                    
                    // 获取基础值数据
                    if(!empty($base_id))
                    {
1831
                        $base = Db::name('GoodsSpecBase')->find($base_id);
D
v1.2.0  
devil_gong 已提交
1832 1833 1834 1835
                    }
                }
            }
        } else {
1836
            $base = Db::name('GoodsSpecBase')->where($where)->find();
1837 1838 1839 1840 1841 1842
        }

        // 是否有规格
        if(!empty($base))
        {
            // 单位 .00 处理
1843
            $base['weight'] = PriceBeautify($base['weight']);
1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854

            // 商品处理前钩子
            $hook_name = 'plugins_service_goods_spec_base';
            $ret = Hook::listen($hook_name, [
                'hook_name'     => $hook_name,
                'is_backend'    => true,
                'params'        => &$params,
                'spec_base'     => &$base,
                'goods_id'      => $goods_id
            ]);
            if(isset($ret['code']) && $ret['code'] != 0)
D
v1.2.0  
devil_gong 已提交
1855
            {
1856
                return $ret;
D
v1.2.0  
devil_gong 已提交
1857
            }
1858 1859 1860

            // 返回成功
            return DataReturn('操作成功', 0, $base);
D
v1.2.0  
devil_gong 已提交
1861
        }
1862

D
v1.2.0  
devil_gong 已提交
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
        return DataReturn('没有相关规格', -100);
    }

    /**
     * 商品规格类型
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-14
     * @desc    description
     * @param    [array]          $params [输入参数]
     */
1875
    public static function GoodsSpecType($params = [])
D
v1.2.0  
devil_gong 已提交
1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '商品id有误',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'spec',
                'error_msg'         => '请选择规格',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 条件
        $goods_id = intval($params['id']);
        $where = [
            'goods_id'  => intval($params['id']),
        ];
        $value = [];

        // 规格不为数组则为json字符串
        if(!is_array($params['spec']))
        {
            $params['spec'] = json_decode($params['spec'], true);
        }
        foreach($params['spec'] as $v)
        {
            $value[] = $v['value'];
        }
        $where['value'] = $value;

        // 获取规格值基础值id
        $ids = Db::name('GoodsSpecValue')->where($where)->column('goods_spec_base_id');
        if(!empty($ids))
        {
            // 根据基础值id获取规格值列表
            $temp_data = Db::name('GoodsSpecValue')->where(['goods_spec_base_id'=>$ids])->field('goods_spec_base_id,value')->select();
            if(!empty($temp_data))
            {
                // 根据基础值id分组
                $data = [];
                foreach($temp_data as $v)
                {
                    $data[$v['goods_spec_base_id']][] = $v;
                }

                // 获取当前操作元素索引
                $last  = end($params['spec']);
                $index = count($params['spec'])-1;
                $spec_str = implode('', array_column($params['spec'], 'value'));
                $value = [];
                foreach($data as $v)
                {
                    $temp_str = implode('', array_column($v, 'value'));
                    if(isset($v[$index+1]) && stripos($temp_str, $spec_str) !== false)
                    {
                        // 判断是否还有库存
                        $inventory = Db::name('GoodsSpecBase')->where(['id'=>$v[$index+1]['goods_spec_base_id']])->value('inventory');
                        if($inventory > 0)
                        {
                            $value[$v[$index+1]['value']] = $v[$index+1]['value'];
                        }
                    }
                }
                return DataReturn('操作成功', 0, array_values($value));
            }
        }
        return DataReturn('没有相关规格类型', -100);
    }

    /**
     * 获取商品分类节点数据
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-12-16T23:54:46+0800
     * @param    [array]          $params [输入参数]
     */
1961
    public static function GoodsCategoryNodeSon($params = [])
D
v1.2.0  
devil_gong 已提交
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
    {
        // id
        $id = isset($params['id']) ? intval($params['id']) : 0;

        // 获取数据
        $field = 'id,pid,icon,name,sort,is_enable,bg_color,big_images,vice_name,describe,is_home_recommended';
        $data = Db::name('GoodsCategory')->field($field)->where(['pid'=>$id])->order('sort asc')->select();
        if(!empty($data))
        {
            foreach($data as &$v)
            {
                $v['is_son']            =   (Db::name('GoodsCategory')->where(['pid'=>$v['id']])->count() > 0) ? 'ok' : 'no';
                $v['ajax_url']          =   MyUrl('admin/goodscategory/getnodeson', array('id'=>$v['id']));
                $v['delete_url']        =   MyUrl('admin/goodscategory/delete');
1976 1977
                $v['icon_url']          =   ResourcesService::AttachmentPathViewHandle($v['icon']);
                $v['big_images_url']    =   ResourcesService::AttachmentPathViewHandle($v['big_images']);
D
v1.2.0  
devil_gong 已提交
1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
                $v['json']              =   json_encode($v);
            }
            return DataReturn('操作成功', 0, $data);
        }
        return DataReturn('没有相关数据', -100);
    }

    /**
     * 商品分类保存
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-12-17T01:04:03+0800
     * @param    [array]          $params [输入参数]
     */
1993
    public static function GoodsCategorySave($params = [])
D
v1.2.0  
devil_gong 已提交
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'length',
                'key_name'          => 'name',
                'checked_data'      => '2,16',
                'error_msg'         => '名称格式 2~16 个字符',
            ],
            [
                'checked_type'      => 'length',
                'key_name'          => 'vice_name',
                'checked_data'      => '60',
                'is_checked'        => 1,
                'error_msg'         => '副名称格式 最多30个字符',
            ],
            [
                'checked_type'      => 'length',
                'key_name'          => 'describe',
                'checked_data'      => '200',
                'is_checked'        => 1,
                'error_msg'         => '描述格式 最多200个字符',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 其它附件
        $data_fields = ['icon', 'big_images'];
        $attachment = ResourcesService::AttachmentParams($params, $data_fields);
        if($attachment['code'] != 0)
        {
            return $attachment;
        }

        // 数据
        $data = [
            'name'                  => $params['name'],
            'pid'                   => isset($params['pid']) ? intval($params['pid']) : 0,
            'vice_name'             => isset($params['vice_name']) ? $params['vice_name'] : '',
            'describe'              => isset($params['describe']) ? $params['describe'] : '',
            'bg_color'              => isset($params['bg_color']) ? $params['bg_color'] : '',
            'is_home_recommended'   => isset($params['is_home_recommended']) ? intval($params['is_home_recommended']) : 0,
            'sort'                  => isset($params['sort']) ? intval($params['sort']) : 0,
            'is_enable'             => isset($params['is_enable']) ? intval($params['is_enable']) : 0,
            'icon'                  => $attachment['data']['icon'],
            'big_images'            => $attachment['data']['big_images'],
        ];

        // 添加
        if(empty($params['id']))
        {
            $data['add_time'] = time();
            if(Db::name('GoodsCategory')->insertGetId($data) > 0)
            {
                return DataReturn('添加成功', 0);
            }
            return DataReturn('添加失败', -100);
        } else {
            $data['upd_time'] = time();
            if(Db::name('GoodsCategory')->where(['id'=>intval($params['id'])])->update($data))
            {
                return DataReturn('编辑成功', 0);
            }
            return DataReturn('编辑失败', -100);
        }
    }

    /**
     * 商品分类删除
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-12-17T02:40:29+0800
     * @param    [array]          $params [输入参数]
     */
2073
    public static function GoodsCategoryDelete($params = [])
D
v1.2.0  
devil_gong 已提交
2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '删除数据id有误',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'admin',
                'error_msg'         => '用户信息有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 获取分类下所有分类id
2095
        $ids = self::GoodsCategoryItemsIds([$params['id']]);
D
v1.2.0  
devil_gong 已提交
2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106
        $ids[] = $params['id'];

        // 开始删除
        if(Db::name('GoodsCategory')->where(['id'=>$ids])->delete())
        {
            return DataReturn('删除成功', 0);
        }
        return DataReturn('删除失败', -100);
    }
}
?>