vr-shopxo-source/application/service/GoodsService.php

1977 lines
66 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2018 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;
use think\Db;
use app\service\ResourcesService;
use app\service\BrandService;
use app\service\RegionService;
/**
* 商品服务层
* @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 [输入参数]
*/
public static function GoodsCategoryRow($params = [])
{
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'];
$data = self::GoodsCategoryDataDealWith([Db::name('GoodsCategory')->field($field)->where(['is_enable'=>1, 'id'=>intval($params['id'])])->find()]);
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 [输入参数]
*/
public static function GoodsCategory($params = [])
{
$where = empty($params['where']) ? ['pid'=>0, 'is_enable'=>1] : $params['where'];
$data = self::GoodsCategoryList($where);
if(!empty($data))
{
foreach($data as &$v)
{
$v['items'] = self::GoodsCategoryList(['pid'=>$v['id'], 'is_enable'=>1]);
if(!empty($v['items']))
{
// 一次性查出所有二级下的三级、再做归类、避免sql连接超多
$items = self::GoodsCategoryList(['pid'=>array_column($v['items'], 'id'), 'is_enable'=>1]);
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 [条件]
*/
public static function GoodsCategoryList($where = [])
{
$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();
return self::GoodsCategoryDataDealWith($data);
}
/**
* 商品分类数据处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-06
* @desc description
* @param [array] $data [商品分类数据 二维数组]
*/
private static function GoodsCategoryDataDealWith($data)
{
if(!empty($data) && is_array($data))
{
$images_host = config('shopxo.images_host');
foreach($data as &$v)
{
if(is_array($v))
{
if(isset($v['icon']))
{
$v['icon'] = empty($v['icon']) ? null : $images_host.$v['icon'];
}
if(isset($v['big_images']))
{
$v['big_images_old'] = $v['big_images'];
$v['big_images'] = empty($v['big_images']) ? null : $images_host.$v['big_images'];
}
}
}
}
return $data;
}
/**
* 获取首页楼层数据
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-08-29
* @desc description
* @param [array] $params [输入参数]
*/
public static function HomeFloorList($params = [])
{
// 商品大分类
$params['where'] = ['pid'=>0, 'is_home_recommended'=>1];
$goods_category = self::GoodsCategory($params);
if(!empty($goods_category))
{
foreach($goods_category as &$v)
{
$category_ids = self::GoodsCategoryItemsIds([$v['id']], 1);
$v['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']);
}
}
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是]
*/
public static function GoodsCategoryItemsIds($ids = [], $is_enable = null)
{
$where = ['pid'=>$ids];
if($is_enable !== null)
{
$where['is_enable'] = $is_enable;
}
$data = Db::name('GoodsCategory')->where($where)->column('id');
if(!empty($data))
{
$temp = self::GoodsCategoryItemsIds($data, $is_enable);
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 [条件]
*/
public static function CategoryGoodsTotal($where = [])
{
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]
*/
public static function CategoryGoodsList($params = [])
{
$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();
return self::GoodsDataHandle($params, $data);
}
/**
* 商品数据处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2018-12-08T23:16:42+0800
* @param [array] $params [输入参数]
* @param [array] $data [商品列表]
*/
public static function GoodsDataHandle($params, $data)
{
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;
// 开始处理数据
$images_host = config('shopxo.images_host');
foreach($data as &$v)
{
// 商品url地址
if(!empty($v['id']))
{
$v['goods_url'] = MyUrl('index/goods/index', ['id'=>$v['id']]);
}
// 商品封面图片
if(isset($v['images']))
{
$v['images_old'] = $v['images'];
$v['images'] = empty($v['images']) ? null : $images_host.$v['images'];
}
// 视频
if(isset($v['video']))
{
$v['video_old'] = $v['video'];
$v['video'] = empty($v['video']) ? null : $images_host.$v['video'];
}
// 商品首页推荐图片,不存在则使用商品封面图片
if(isset($v['home_recommended_images']))
{
$v['home_recommended_images_old'] = $v['home_recommended_images'];
$v['home_recommended_images'] = empty($v['home_recommended_images']) ? (empty($v['images']) ? null : $v['images']) : $images_host.$v['home_recommended_images'];
}
// PC内容处理
if(isset($v['content_web']))
{
$v['content_web'] = ResourcesService::ContentStaticReplace($v['content_web'], 'get');
}
// 产地
$v['place_origin_name'] = empty($v['place_origin']) ? null : RegionService::RegionName($v['place_origin']);
// 品牌
$v['brand_name'] = empty($v['brand_id']) ? null : BrandService::BrandName($v['brand_id']);
// 时间
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'];
$vs['images'] = $images_host.$vs['images'];
}
}
}
// 获取规格
if($is_spec && !empty($v['id']))
{
$v['specifications'] = self::GoodsSpecifications(['goods_id'=>$v['id']]);
}
// 获取app内容
if($is_content_app && !empty($v['id']))
{
$v['content_app'] = self::GoodsContentApp(['goods_id'=>$v['id']]);
}
}
}
return $data;
}
/**
* 获取商品手机详情
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-07-10
* @desc description
* @param [array] $params [输入参数]
* @return [array] [app内容]
*/
public static function GoodsContentApp($params = [])
{
$data = Db::name('GoodsContentApp')->where(['goods_id'=>$params['goods_id']])->field('id,images,content')->order('sort asc')->select();
if(!empty($data))
{
$images_host = config('shopxo.images_host');
foreach($data as &$v)
{
$v['images_old'] = $v['images'];
$v['images'] = empty($v['images']) ? null : $images_host.$v['images'];
$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 [输入参数]
*/
public static function GoodsSpecifications($params = [])
{
// 条件
$where = ['goods_id'=>$params['goods_id']];
// 规格类型
$choose = Db::name('GoodsSpecType')->where($where)->order('id asc')->select();
if(!empty($choose))
{
// 数据处理
$images_host = config('shopxo.images_host');
foreach($choose as &$temp_type)
{
$temp_type_value = json_decode($temp_type['value'], true);
foreach($temp_type_value as &$vs)
{
$vs['images'] = empty($vs['images']) ? '' : $images_host.$vs['images'];
}
$temp_type['value'] = $temp_type_value;
$temp_type['add_time'] = date('Y-m-d H:i:s');
}
}
return ['choose'=>$choose];
}
/**
* 商品收藏
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-08-29
* @desc description
* @param [array] $params [输入参数]
*/
public static function GoodsFavor($params = [])
{
// 请求参数
$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);
}
// 开始操作
$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,
'count' => self::GoodsFavorTotal(['goods_id'=>$data['goods_id']]),
]);
} else {
return DataReturn('收藏失败');
}
} else {
// 是否强制收藏
if(isset($params['is_mandatory_favor']) && $params['is_mandatory_favor'] == 1)
{
return DataReturn('收藏成功', 0, [
'text' => '已收藏',
'status' => 1,
'count' => self::GoodsFavorTotal(['goods_id'=>$data['goods_id']]),
]);
}
// 删除收藏
if(Db::name('GoodsFavor')->where($data)->delete() > 0)
{
return DataReturn('取消成功', 0, [
'text' => '收藏',
'status' => 0,
'count' => self::GoodsFavorTotal(['goods_id'=>$data['goods_id']]),
]);
} 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未收藏]
*/
public static function IsUserGoodsFavor($params = [])
{
// 请求参数
$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 [条件]
*/
public static function GoodsCommentsTotal($goods_id)
{
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 [输入参数]
*/
public static function UserGoodsFavorListWhere($params = [])
{
$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 [条件]
*/
public static function GoodsFavorTotal($where = [])
{
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 [输入参数]
*/
public static function GoodsFavorList($params = [])
{
$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))
{
$images_host = config('shopxo.images_host');
foreach($data as &$v)
{
// 图片
$v['images_old'] = $v['images'];
$v['images'] = empty($v['images']) ? null : $images_host.$v['images'];
$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 [输入参数]
*/
public static function GoodsAccessCountInc($params = [])
{
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 [输入参数]
*/
public static function GoodsBrowseSave($params = [])
{
// 请求参数
$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 [输入参数]
*/
public static function UserGoodsBrowseListWhere($params = [])
{
$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;
}