shopxo/app/service/BrandService.php

460 lines
15 KiB
PHP
Raw Normal View History

2018-12-28 18:58:37 +08:00
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
2018-12-28 18:58:37 +08:00
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Licensed ( https://opensource.org/licenses/mit-license.php )
2018-12-28 18:58:37 +08:00
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;
2021-07-18 23:42:10 +08:00
use think\facade\Db;
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,id asc' : trim($params['order_by']);
2018-12-28 18:58:37 +08:00
$m = isset($params['m']) ? intval($params['m']) : 0;
$n = isset($params['n']) ? intval($params['n']) : 10;
// 品牌列表读取前钩子
$hook_name = 'plugins_service_brand_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,
]);
// 获取列表
$data = Db::name('Brand')->where($where)->field($field)->order($order_by)->limit($m, $n)->select()->toArray();
// 数据处理
return DataReturn('处理成功', 0, self::DataHandle($data, $params));
}
/**
* 数据处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2021-01-11
* @desc description
* @param [array] $data [列表数据]
* @param [array] $params [输入参数]
*/
public static function DataHandle($data, $params = [])
{
2018-12-28 18:58:37 +08:00
if(!empty($data))
{
2021-03-18 13:22:26 +08:00
// 字段列表
$keys = ArrayKeys($data);
// 分类信息
// 获取所有品牌关联的分类数据
$category_group = [];
if(in_array('id', $keys))
2020-06-19 18:44:38 +08:00
{
2021-07-18 23:42:10 +08:00
$category = Db::name('BrandCategoryJoin')->where(['brand_id'=>array_column($data, 'id')])->field('brand_id,brand_category_id')->select()->toArray();
2021-03-18 13:22:26 +08:00
if(!empty($category))
{
$ids = array_unique(array_column($category, 'brand_category_id'));
$names = Db::name('BrandCategory')->where(['id'=>$ids])->column('name', 'id');
if(!empty($names))
2021-03-18 13:22:26 +08:00
{
foreach($category as $c)
2021-03-18 13:22:26 +08:00
{
if(array_key_exists($c['brand_category_id'], $names))
{
if(!array_key_exists($c['brand_id'], $category_group))
{
$category_group[$c['brand_id']]['ids'] = [];
$category_group[$c['brand_id']]['names'] = [];
}
$category_group[$c['brand_id']]['ids'][] = $c['brand_category_id'];
$category_group[$c['brand_id']]['names'][] = $names[$c['brand_category_id']];
}
2021-03-18 13:22:26 +08:00
}
}
}
2020-06-19 18:44:38 +08:00
}
2021-03-18 13:22:26 +08:00
// 数处理
2018-12-28 18:58:37 +08:00
foreach($data as &$v)
{
// 分类名称
2020-08-01 22:17:32 +08:00
if(isset($v['id']))
2018-12-28 18:58:37 +08:00
{
2021-03-18 13:22:26 +08:00
if(array_key_exists($v['id'], $category_group))
{
$v['brand_category_ids'] = $category_group[$v['id']]['ids'];
$v['brand_category_text'] = implode('', $category_group[$v['id']]['names']);
} else {
$v['brand_category_ids'] = [];
$v['brand_category_text'] = '';
}
2018-12-28 18:58:37 +08:00
}
// logo
if(isset($v['logo']))
{
2021-01-11 22:38:52 +08:00
$v['logo'] = ResourcesService::AttachmentPathViewHandle($v['logo']);
2018-12-28 18:58:37 +08:00
}
// 品牌官方地址
if(isset($v['website_url']))
{
$v['website_url'] = empty($v['website_url']) ? null : $v['website_url'];
}
2018-12-28 18:58:37 +08:00
// 时间
if(isset($v['add_time']))
{
2020-06-19 18:44:38 +08:00
$v['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
2018-12-28 18:58:37 +08:00
}
if(isset($v['upd_time']))
{
2020-06-19 18:44:38 +08:00
$v['upd_time'] = empty($v['upd_time']) ? '' : date('Y-m-d H:i:s', $v['upd_time']);
2018-12-28 18:58:37 +08:00
}
}
}
return $data;
2018-12-28 18:58:37 +08:00
}
/**
* 品牌总数
* @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 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
{
2021-07-18 23:42:10 +08:00
return Db::name('Brand')->field('id,name')->where(['is_enable'=>1])->order('sort asc')->select()->toArray();
2018-12-28 18:58:37 +08:00
}
/**
* 获取品牌名称
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-19
* @desc description
2021-01-27 22:21:04 +08:00
* @param [array|int] $brand_ids [快递id]
2018-12-28 18:58:37 +08:00
*/
2021-01-27 22:21:04 +08:00
public static function BrandName($brand_ids = 0)
2018-12-28 18:58:37 +08:00
{
2021-01-27 22:21:04 +08:00
if(empty($brand_ids))
{
return null;
}
// 参数处理查询数据
if(is_array($brand_ids))
{
$brand_ids = array_filter(array_unique($brand_ids));
}
if(!empty($brand_ids))
{
$data = Db::name('Brand')->where(['id'=>$brand_ids])->column('name', 'id');
}
// id数组则直接返回
if(is_array($brand_ids))
{
return empty($data) ? [] : $data;
}
return (!empty($data) && is_array($data) && array_key_exists($brand_ids, $data)) ? $data[$brand_ids] : null;
2018-12-28 18:58:37 +08:00
}
/**
* 保存
* @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' => 'length',
'key_name' => 'describe',
'checked_data' => '230',
'is_checked' => 1,
'error_msg' => '描述最多200个字符',
],
2020-08-24 23:39:14 +08:00
[
'checked_type' => 'unique',
'key_name' => 'name',
'checked_data' => 'Brand',
'checked_key' => 'id',
'error_msg' => '品牌已存在[{$var}]',
],
2018-12-28 18:58:37 +08:00
[
'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'],
'describe' => $params['describe'],
2018-12-28 18:58:37 +08:00
'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';
2021-07-18 23:42:10 +08:00
$ret = EventReturnHandle(MyEventTrigger($hook_name, [
2019-08-18 12:12:47 +08:00
'hook_name' => $hook_name,
'is_backend' => true,
'params' => &$params,
'data' => &$data,
'data_id' => isset($params['id']) ? intval($params['id']) : 0,
2019-12-02 20:58:24 +08:00
]));
2019-08-18 12:12:47 +08:00
if(isset($ret['code']) && $ret['code'] != 0)
{
return $ret;
}
2020-08-01 22:17:32 +08:00
// 启动事务
Db::startTrans();
2018-12-28 18:58:37 +08:00
if(empty($params['id']))
{
$data['add_time'] = time();
2020-08-01 22:17:32 +08:00
$brand_id = Db::name('Brand')->insertGetId($data);
if($brand_id <= 0)
2018-12-28 18:58:37 +08:00
{
2020-08-01 22:17:32 +08:00
Db::rollback();
return DataReturn('添加失败', -100);
2018-12-28 18:58:37 +08:00
}
} else {
$data['upd_time'] = time();
2020-08-01 22:17:32 +08:00
$brand_id = intval($params['id']);
if(Db::name('Brand')->where(['id'=>$brand_id])->update($data) === false)
2018-12-28 18:58:37 +08:00
{
2020-08-01 22:17:32 +08:00
Db::rollback();
return DataReturn('编辑失败', -100);
2018-12-28 18:58:37 +08:00
}
}
2020-08-01 22:17:32 +08:00
// 添加分类
$ret = self::BrandCategoryInsert(explode(',', $params['brand_category_id']), $brand_id);
if($ret['code'] != 0)
{
// 回滚事务
Db::rollback();
return $ret;
}
// 提交事务
Db::commit();
return DataReturn('操作成功', 0);
}
/**
* 品牌分类添加
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-08-01
* @desc description
* @param [array] $data [分类数据]
* @param [int] $brand_id [品牌id]
*/
private static function BrandCategoryInsert($data, $brand_id)
{
Db::name('BrandCategoryJoin')->where(['brand_id'=>$brand_id])->delete();
if(!empty($data))
{
foreach($data as $category_id)
{
$temp_category = [
'brand_id' => $brand_id,
'brand_category_id' => $category_id,
'add_time' => time(),
];
if(Db::name('BrandCategoryJoin')->insertGetId($temp_category) <= 0)
{
return DataReturn('品牌分类添加失败', -1);
}
}
}
return DataReturn('添加成功', 0);
2018-12-28 18:58:37 +08:00
}
/**
* 删除
* @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
{
2020-06-19 18:44:38 +08:00
// 参数是否有误
if(empty($params['ids']))
2018-12-28 18:58:37 +08:00
{
2020-06-19 18:44:38 +08:00
return DataReturn('操作id有误', -1);
}
// 是否数组
if(!is_array($params['ids']))
{
$params['ids'] = explode(',', $params['ids']);
2018-12-28 18:58:37 +08:00
}
// 删除操作
2020-06-19 18:44:38 +08:00
if(Db::name('Brand')->where(['id'=>$params['ids']])->delete())
2018-12-28 18:58:37 +08:00
{
return DataReturn('删除成功');
}
2020-06-19 18:44:38 +08:00
return DataReturn('删除失败', -100);
2018-12-28 18:58:37 +08:00
}
/**
* 状态更新
* @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有误',
],
2020-06-19 18:44:38 +08:00
[
'checked_type' => 'empty',
'key_name' => 'field',
'error_msg' => '未指定操作字段',
],
2018-12-28 18:58:37 +08:00
[
'checked_type' => 'in',
'key_name' => 'state',
'checked_data' => [0,1],
'error_msg' => '状态有误',
],
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 数据更新
2020-06-19 18:44:38 +08:00
if(Db::name('Brand')->where(['id'=>intval($params['id'])])->update([$params['field']=>intval($params['state']), 'upd_time'=>time()]))
2018-12-28 18:58:37 +08:00
{
2020-06-19 18:44:38 +08:00
return DataReturn('操作成功');
2018-12-28 18:58:37 +08:00
}
2020-06-19 18:44:38 +08:00
return DataReturn('操作失败', -100);
2018-12-28 18:58:37 +08:00
}
}
?>