shopxo/application/service/BrandService.php

548 lines
18 KiB
PHP
Raw Normal View History

2018-12-28 18:58:37 +08:00
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
2019-02-18 13:52:07 +08:00
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
2018-12-28 18:58:37 +08:00
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;
use think\Db;
2019-08-20 14:08:20 +08:00
use think\facade\Hook;
use app\service\GoodsService;
use app\service\ResourcesService;
2018-12-28 18:58:37 +08:00
/**
* 品牌服务层
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-01T21:51:08+0800
*/
class BrandService
{
/**
* 品牌列表
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-06T21:31:53+0800
* @param [array] $params [输入参数]
*/
public static function BrandList($params = [])
2018-12-28 18:58:37 +08:00
{
$where = empty($params['where']) ? [] : $params['where'];
$field = empty($params['field']) ? '*' : $params['field'];
$order_by = empty($params['order_by']) ? 'sort asc' : trim($params['order_by']);
$m = isset($params['m']) ? intval($params['m']) : 0;
$n = isset($params['n']) ? intval($params['n']) : 10;
// 获取品牌列表
$data = Db::name('Brand')->where($where)->order($order_by)->limit($m, $n)->select();
if(!empty($data))
{
$common_is_enable_tips = lang('common_is_enable_tips');
foreach($data as &$v)
{
// 是否启用
if(isset($v['is_enable']))
{
$v['is_enable_text'] = $common_is_enable_tips[$v['is_enable']]['name'];
}
// 分类名称
if(isset($v['brand_category_id']))
{
$v['brand_category_name'] = Db::name('BrandCategory')->where(['id'=>$v['brand_category_id']])->value('name');
}
// logo
if(isset($v['logo']))
{
$v['logo_old'] = $v['logo'];
$v['logo'] = ResourcesService::AttachmentPathViewHandle($v['logo']);
2018-12-28 18:58:37 +08:00
}
// 时间
if(isset($v['add_time']))
{
$v['add_time_time'] = date('Y-m-d H:i:s', $v['add_time']);
$v['add_time_date'] = date('Y-m-d', $v['add_time']);
}
if(isset($v['upd_time']))
{
$v['upd_time_time'] = date('Y-m-d H:i:s', $v['upd_time']);
$v['upd_time_date'] = date('Y-m-d', $v['upd_time']);
}
}
}
return DataReturn('处理成功', 0, $data);
}
/**
* 品牌总数
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-10T22:16:29+0800
* @param [array] $where [条件]
*/
public static function BrandTotal($where)
2018-12-28 18:58:37 +08:00
{
return (int) Db::name('Brand')->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 BrandListWhere($params = [])
2018-12-28 18:58:37 +08:00
{
$where = [];
if(!empty($params['keywords']))
{
$where[] = ['name', 'like', '%'.$params['keywords'].'%'];
}
// 是否更多条件
if(isset($params['is_more']) && $params['is_more'] == 1)
{
// 等值
if(isset($params['is_enable']) && $params['is_enable'] > -1)
{
$where[] = ['is_enable', '=', intval($params['is_enable'])];
}
if(isset($params['brand_category_id']) && $params['brand_category_id'] > -1)
{
$where[] = ['brand_category_id', '=', intval($params['brand_category_id'])];
}
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 0.0.1
* @datetime 2016-12-10T22:16:29+0800
* @param [array] $where [条件]
*/
public static function CategoryBrand($params = [])
2018-12-28 18:58:37 +08:00
{
$data = Db::name('BrandCategory')->where(['is_enable'=>1])->select();
if(!empty($data))
{
foreach($data as &$v)
{
$v['items'] = Db::name('Brand')->field('id,name')->where(['is_enable'=>1, 'brand_category_id'=>$v['id']])->order('sort asc')->select();
}
}
return $data;
}
/**
* 分类下品牌列表
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-08-29
* @desc description
* @param [array] $params [输入参数]
*/
public static function CategoryBrandList($params = [])
2018-12-28 18:58:37 +08:00
{
$brand_where = ['is_enable'=>1];
2019-01-22 13:38:23 +08:00
// 分类id
2018-12-28 18:58:37 +08:00
if(!empty($params['category_id']))
{
// 根据分类获取品牌id
$category_ids = GoodsService::GoodsCategoryItemsIds([$params['category_id']], 1);
$category_ids[] = $params['category_id'];
2018-12-28 18:58:37 +08:00
$where = ['g.is_delete_time'=>0, 'g.is_shelves'=>1, 'gci.category_id'=>$category_ids];
$brand_where['id'] = Db::name('Goods')->alias('g')->join(['__GOODS_CATEGORY_JOIN__'=>'gci'], 'g.id=gci.goods_id')->field('g.brand_id')->where($where)->group('g.brand_id')->column('brand_id');
}
2019-01-22 13:38:23 +08:00
// 关键字
if(!empty($params['keywords']))
{
$where = [
['title', 'like', '%'.$params['keywords'].'%']
];
$brand_where['id'] = Db::name('Goods')->where($where)->group('brand_id')->column('brand_id');
}
2018-12-28 18:58:37 +08:00
// 获取品牌列表
$brand = Db::name('Brand')->where($brand_where)->field('id,name,logo,website_url')->select();
if(!empty($brand))
{
foreach($brand as &$v)
{
$v['logo_old'] = $v['logo'];
$v['logo'] = ResourcesService::AttachmentPathViewHandle($v['logo']);
2018-12-28 18:58:37 +08:00
$v['website_url'] = empty($v['website_url']) ? null : $v['website_url'];
}
}
return $brand;
}
/**
* 获取品牌名称
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-19
* @desc description
* @param [int] $brand_id [地区id]
*/
public static function BrandName($brand_id = 0)
2018-12-28 18:58:37 +08:00
{
return empty($brand_id) ? null : Db::name('Brand')->where(['id'=>intval($brand_id)])->value('name');
}
/**
* 品牌分类
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-08-29
* @desc description
* @param [array] $params [输入参数]
*/
public static function BrandCategoryList($params = [])
2018-12-28 18:58:37 +08:00
{
$field = empty($params['field']) ? '*' : $params['field'];
$order_by = empty($params['order_by']) ? 'sort asc' : trim($params['order_by']);
$data = Db::name('BrandCategory')->where(['is_enable'=>1])->field($field)->order($order_by)->select();
return DataReturn('处理成功', 0, $data);
}
/**
* 保存
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-12-18
* @desc description
* @param [array] $params [输入参数]
*/
public static function BrandSave($params = [])
2018-12-28 18:58:37 +08:00
{
// 请求类型
$p = [
[
'checked_type' => 'length',
'key_name' => 'name',
'checked_data' => '2,30',
'error_msg' => '名称格式 2~30 个字符',
],
[
'checked_type' => 'empty',
'key_name' => 'brand_category_id',
'error_msg' => '请选择品牌分类',
],
[
'checked_type' => 'fun',
'key_name' => 'website_url',
'checked_data' => 'CheckUrl',
'is_checked' => 1,
'error_msg' => '官网地址格式有误',
],
[
'checked_type' => 'length',
'key_name' => 'sort',
'checked_data' => '3',
'error_msg' => '顺序 0~255 之间的数值',
],
2019-08-15 18:56:05 +08:00
[
'checked_type' => 'length',
'key_name' => 'seo_title',
'checked_data' => '100',
'is_checked' => 1,
'error_msg' => 'SEO标题格式 最多100个字符',
],
[
'checked_type' => 'length',
'key_name' => 'seo_keywords',
'checked_data' => '130',
'is_checked' => 1,
'error_msg' => 'SEO关键字格式 最多130个字符',
],
[
'checked_type' => 'length',
'key_name' => 'seo_desc',
'checked_data' => '230',
'is_checked' => 1,
'error_msg' => 'SEO描述格式 最多230个字符',
],
2018-12-28 18:58:37 +08:00
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 附件
$data_fields = ['logo'];
$attachment = ResourcesService::AttachmentParams($params, $data_fields);
// 数据
$data = [
'name' => $params['name'],
'brand_category_id' => intval($params['brand_category_id']),
'logo' => $attachment['data']['logo'],
'website_url' => empty($params['website_url']) ? '' : $params['website_url'],
'sort' => intval($params['sort']),
'is_enable' => isset($params['is_enable']) ? intval($params['is_enable']) : 0,
2019-08-15 18:56:05 +08:00
'seo_title' => empty($params['seo_title']) ? '' : $params['seo_title'],
'seo_keywords' => empty($params['seo_keywords']) ? '' : $params['seo_keywords'],
'seo_desc' => empty($params['seo_desc']) ? '' : $params['seo_desc'],
2018-12-28 18:58:37 +08:00
];
2019-08-18 12:12:47 +08:00
// 品牌保存处理钩子
$hook_name = 'plugins_service_brand_save_handle';
$ret = Hook::listen($hook_name, [
'hook_name' => $hook_name,
'is_backend' => true,
'params' => &$params,
'data' => &$data,
'brand_id' => isset($params['id']) ? intval($params['id']) : 0,
]);
if(isset($ret['code']) && $ret['code'] != 0)
{
return $ret;
}
2018-12-28 18:58:37 +08:00
if(empty($params['id']))
{
$data['add_time'] = time();
if(Db::name('Brand')->insertGetId($data) > 0)
{
return DataReturn('添加成功', 0);
}
return DataReturn('添加失败', -100);
} else {
$data['upd_time'] = time();
if(Db::name('Brand')->where(['id'=>intval($params['id'])])->update($data))
{
return DataReturn('编辑成功', 0);
}
return DataReturn('编辑失败', -100);
}
}
/**
* 删除
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-12-18
* @desc description
* @param [array] $params [输入参数]
*/
public static function BrandDelete($params = [])
2018-12-28 18:58:37 +08:00
{
// 请求参数
$p = [
[
'checked_type' => 'empty',
'key_name' => 'id',
'error_msg' => '操作id有误',
],
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 删除操作
if(Db::name('Brand')->where(['id'=>$params['id']])->delete())
{
return DataReturn('删除成功');
}
return DataReturn('删除失败或资源不存在', -100);
}
/**
* 状态更新
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-06T21:31:53+0800
* @param [array] $params [输入参数]
*/
public static function BrandStatusUpdate($params = [])
2018-12-28 18:58:37 +08:00
{
// 请求参数
$p = [
[
'checked_type' => 'empty',
'key_name' => 'id',
'error_msg' => '操作id有误',
],
[
'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('Brand')->where(['id'=>intval($params['id'])])->update(['is_enable'=>intval($params['state'])]))
{
return DataReturn('编辑成功');
}
return DataReturn('编辑失败或数据未改变', -100);
}
/**
* 获取品牌分类节点数据
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2018-12-16T23:54:46+0800
* @param [array] $params [输入参数]
*/
public static function BrandCategoryNodeSon($params = [])
2018-12-28 18:58:37 +08:00
{
// id
$id = isset($params['id']) ? intval($params['id']) : 0;
// 获取数据
$field = '*';
$data = Db::name('BrandCategory')->field($field)->where(['pid'=>$id])->order('sort asc')->select();
if(!empty($data))
{
foreach($data as &$v)
{
$v['is_son'] = (Db::name('BrandCategory')->where(['pid'=>$v['id']])->count() > 0) ? 'ok' : 'no';
$v['ajax_url'] = MyUrl('admin/brandcategory/getnodeson', array('id'=>$v['id']));
$v['delete_url'] = MyUrl('admin/brandcategory/delete');
$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 [输入参数]
*/
public static function BrandCategorySave($params = [])
2018-12-28 18:58:37 +08:00
{
// 请求参数
$p = [
[
'checked_type' => 'length',
'key_name' => 'name',
'checked_data' => '2,16',
'error_msg' => '名称格式 2~16 个字符',
],
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 数据
$data = [
'name' => $params['name'],
'pid' => isset($params['pid']) ? intval($params['pid']) : 0,
'sort' => isset($params['sort']) ? intval($params['sort']) : 0,
'is_enable' => isset($params['is_enable']) ? intval($params['is_enable']) : 0,
];
// 添加
if(empty($params['id']))
{
$data['add_time'] = time();
if(Db::name('BrandCategory')->insertGetId($data) > 0)
{
return DataReturn('添加成功', 0);
}
return DataReturn('添加失败', -100);
} else {
$data['upd_time'] = time();
if(Db::name('BrandCategory')->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 [输入参数]
*/
public static function BrandCategoryDelete($params = [])
2018-12-28 18:58:37 +08:00
{
// 请求参数
$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);
}
// 开始删除
if(Db::name('BrandCategory')->where(['id'=>intval($params['id'])])->delete())
{
return DataReturn('删除成功', 0);
}
return DataReturn('删除失败', -100);
}
}
?>