vr-shopxo-plugin/shopxo/app/service/GoodsCategoryService.php

733 lines
25 KiB
PHP
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~2099 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://opensource.org/licenses/mit-license.php )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;
use think\facade\Db;
use app\service\ResourcesService;
/**
* 商品分类服务层
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-10-09
* @desc description
*/
class GoodsCategoryService
{
/**
* 获取分类名称
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-19
* @desc description
* @param [array|int] $category_ids [分类id]
*/
public static function GoodsCategoryName($category_ids = 0)
{
if(empty($category_ids))
{
return null;
}
// 参数处理查询数据
if(is_array($category_ids))
{
$category_ids = array_filter(array_unique($category_ids));
}
if(!empty($category_ids))
{
$data = Db::name('GoodsCategory')->where(['id'=>$category_ids])->column('name', 'id');
}
// id数组则直接返回
if(is_array($category_ids))
{
return empty($data) ? [] : $data;
}
return (!empty($data) && is_array($data) && array_key_exists($category_ids, $data)) ? $data[$category_ids] : null;
}
/**
* 根据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,icon_active,realistic_images,name,vice_name,describe,bg_color,big_images,sort,is_home_recommended' : $params['field'];
$data = self::GoodsCategoryDataHandle([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 GoodsCategoryAll($params = [])
{
// 从缓存获取
$key = SystemService::CacheKey('shopxo.cache_goods_category_key');
$data = MyCache($key);
if($data === null || MyEnv('app_debug') || MyC('common_data_is_use_cache') != 1)
{
// 获取分类
$params['where'] = [
['pid', '=', 0],
['is_enable', '=', 1],
];
$data = self::GoodsCategory($params);
// 存储缓存
MyCache($key, $data, 180);
}
// 所有商品分类数据钩子
$hook_name = 'plugins_service_goods_category_all_data';
MyEventTrigger($hook_name, [
'hook_name' => $hook_name,
'is_backend' => true,
'params' => $params,
'data' => &$data,
]);
return $data;
}
/**
* 获取分类
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-08-29
* @desc description
* @param [array] $params [输入参数]
*/
public static function GoodsCategory($params = [])
{
// 获取分类
if(empty($params['where']))
{
$params['where'] = [
['pid', '=', 0],
['is_enable', '=', 1],
];
}
$data = self::GoodsCategoryList($params);
if(!empty($data))
{
// 基础条件、去除pid
$where_base = $params['where'];
$temp_column = array_column($where_base, 0);
if(in_array('pid', $temp_column))
{
unset($where_base[array_search('pid', $temp_column)]);
sort($where_base);
}
// 获取所有二级
$two_group = [];
$params['where'] = array_merge($where_base, [['pid', 'in', array_column($data, 'id')]]);
$two = self::GoodsCategoryList($params);
if(!empty($two))
{
// 二级分组
foreach($two as $tv)
{
if(!array_key_exists($tv['pid'], $two_group))
{
$two_group[$tv['pid']] = [];
}
$two_group[$tv['pid']][] = $tv;
}
// 获取所有三级
$three_group = [];
$params['where'] = array_merge($where_base, [['pid', 'in', array_column($two, 'id')]]);
$three = self::GoodsCategoryList($params);
if(!empty($three))
{
// 三级分组
foreach($three as $tv)
{
if(!array_key_exists($tv['pid'], $three_group))
{
$three_group[$tv['pid']] = [];
}
$three_group[$tv['pid']][] = $tv;
}
}
// 数据组合
foreach($data as &$v)
{
$v['items'] = (empty($two_group) || !array_key_exists($v['id'], $two_group)) ? [] : $two_group[$v['id']];
if(!empty($v['items']))
{
foreach($v['items'] as &$vs)
{
$vs['items'] = (empty($three_group) || !array_key_exists($vs['id'], $three_group)) ? [] : $three_group[$vs['id']];
}
}
}
}
} else {
$data = [];
}
return $data;
}
/**
* 根据pid获取商品分类列表
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-08-29
* @desc description
* @param [array] $params [输入参数]
*/
public static function GoodsCategoryList($params = [])
{
$where = empty($params['where']) ? [] : $params['where'];
$where[] = ['is_enable', '=', 1];
$key = md5(json_encode($where));
static $goods_category_list_data = [];
if(!array_key_exists($key, $goods_category_list_data))
{
$order_by = empty($params['order_by']) ? 'sort asc' : trim($params['order_by']);
$field = empty($params['field']) ? 'id,pid,icon,icon_active,realistic_images,name,vice_name,describe,bg_color,big_images,sort,is_home_recommended,seo_title,seo_keywords,seo_desc' : $params['field'];
$m = isset($params['m']) ? intval($params['m']) : 0;
$n = isset($params['n']) ? intval($params['n']) : 0;
// 商品分类列表读取前钩子
$hook_name = 'plugins_service_goods_category_list_begin';
MyEventTrigger($hook_name, [
'hook_name' => $hook_name,
'is_backend' => true,
'params' => &$params,
'where' => &$where,
'field' => &$field,
'order_by' => &$order_by,
'm' => &$m,
'n' => &$n,
]);
// 获取商品分类数据
$goods_category_list_data[$key] = self::GoodsCategoryDataHandle(Db::name('GoodsCategory')->field($field)->where($where)->order($order_by)->limit($m, $n)->select()->toArray());
}
return $goods_category_list_data[$key];
}
<