shopxo/app/service/RegionService.php

329 lines
11 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;
2022-04-21 22:08:53 +08:00
use app\service\SystemService;
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 RegionService
{
/**
* 获取地区名称
* @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] $region_ids [地区id]
2018-12-28 18:58:37 +08:00
*/
2021-01-27 22:21:04 +08:00
public static function RegionName($region_ids = 0)
2018-12-28 18:58:37 +08:00
{
2021-01-27 22:21:04 +08:00
if(empty($region_ids))
{
return null;
}
// 参数处理查询数据
if(is_array($region_ids))
{
$region_ids = array_filter(array_unique($region_ids));
}
if(!empty($region_ids))
{
$data = Db::name('Region')->where(['id'=>$region_ids])->column('name', 'id');
}
// id数组则直接返回
if(is_array($region_ids))
{
return empty($data) ? [] : $data;
}
return (!empty($data) && is_array($data) && array_key_exists($region_ids, $data)) ? $data[$region_ids] : null;
2018-12-28 18:58:37 +08:00
}
/**
2019-03-19 17:47:02 +08:00
* 获取地区id下列表
2018-12-28 18:58:37 +08:00
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2018-12-09T00:13:02+0800
2019-03-19 17:47:02 +08:00
* @param [array] $params [输入参数]
2018-12-28 18:58:37 +08:00
*/
2019-03-19 17:47:02 +08:00
public static function RegionItems($params = [])
2018-12-28 18:58:37 +08:00
{
2019-03-19 17:47:02 +08:00
$pid = isset($params['pid']) ? intval($params['pid']) : 0;
$field = empty($params['field']) ? '*' : $params['field'];
2020-08-13 23:18:13 +08:00
$order_by = empty($params['order_by']) ? 'sort asc,id asc' : trim($params['order_by']);
2021-07-18 23:42:10 +08:00
return Db::name('Region')->field($field)->where(['pid'=>$pid, 'is_enable'=>1])->order($order_by)->select()->toArray();
2018-12-28 18:58:37 +08:00
}
/**
* 获取地区节点数据
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-21
* @desc description
* @param [array] $params [输入参数]
*/
public static function RegionNode($params = [])
2018-12-28 18:58:37 +08:00
{
2020-07-08 19:47:19 +08:00
// 数据参数
2018-12-28 18:58:37 +08:00
$field = empty($params['field']) ? 'id,name,level,letters' : $params['field'];
$where = empty($params['where']) ? [] : $params['where'];
2020-07-08 19:47:19 +08:00
$order_by = empty($params['order_by']) ? 'sort asc,id asc' : trim($params['order_by']);
2018-12-28 18:58:37 +08:00
2020-07-08 19:47:19 +08:00
// 基础条件
$where[] = ['is_enable', '=', 1];
2021-07-18 23:42:10 +08:00
return Db::name('Region')->where($where)->field($field)->order($order_by)->select()->toArray();
2018-12-28 18:58:37 +08:00
}
/**
* 获取地区节点数据
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2018-12-16T23:54:46+0800
* @param [array] $params [输入参数]
*/
public static function RegionNodeSon($params = [])
2018-12-28 18:58:37 +08:00
{
// id
$id = isset($params['id']) ? intval($params['id']) : 0;
// 获取数据
$field = 'id,pid,name,sort,is_enable';
2021-07-18 23:42:10 +08:00
$data = Db::name('Region')->field($field)->where(['pid'=>$id])->order('sort asc,id asc')->select()->toArray();
2018-12-28 18:58:37 +08:00
if(!empty($data))
{
foreach($data as &$v)
{
$v['is_son'] = (Db::name('Region')->where(['pid'=>$v['id']])->count() > 0) ? 'ok' : 'no';
$v['json'] = json_encode($v);
}
return DataReturn(MyLang('common.operate_success'), 0, $data);
2018-12-28 18:58:37 +08:00
}
return DataReturn(MyLang('common.no_data'), -100);
2018-12-28 18:58:37 +08:00
}
/**
* 地区保存
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2018-12-17T01:04:03+0800
* @param [array] $params [输入参数]
*/
public static function RegionSave($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,
];
// 得到level风车车
2021-09-01 15:35:26 +08:00
$data['level'] = ($data['pid'] > 0) ? (Db::name('Region')->where(['id'=>$data['pid']])->value('level')+1) : 0;
2018-12-28 18:58:37 +08:00
// 添加
if(empty($params['id']))
{
$data['add_time'] = time();
2020-11-19 18:26:30 +08:00
$data['id'] = Db::name('Region')->insertGetId($data);
if($data['id'] <= 0)
2018-12-28 18:58:37 +08:00
{
return DataReturn(MyLang('common.insert_fail'), -100);
2018-12-28 18:58:37 +08:00
}
} else {
$data['upd_time'] = time();
2020-11-19 18:26:30 +08:00
if(Db::name('Region')->where(['id'=>intval($params['id'])])->update($data) === false)
2018-12-28 18:58:37 +08:00
{
return DataReturn(MyLang('common.edit_fail'), -100);
2020-11-19 18:26:30 +08:00
} else {
$data['id'] = $params['id'];
2018-12-28 18:58:37 +08:00
}
}
2022-09-01 15:14:08 +08:00
return DataReturn(MyLang('common.operate_success'), 0, $data);
2018-12-28 18:58:37 +08:00
}
/**
* 地区删除
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2018-12-17T02:40:29+0800
* @param [array] $params [输入参数]
*/
public static function RegionDelete($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);
}
2020-11-19 18:26:30 +08:00
// 获取分类下所有分类id
$ids = self::RegionItemsIds([$params['id']]);
$ids[] = $params['id'];
2018-12-28 18:58:37 +08:00
// 开始删除
2020-11-19 18:26:30 +08:00
if(Db::name('Region')->where(['id'=>$ids])->delete())
2018-12-28 18:58:37 +08:00
{
return DataReturn(MyLang('common.delete_success'), 0);
2018-12-28 18:58:37 +08:00
}
return DataReturn(MyLang('common.delete_fail'), -100);
2018-12-28 18:58:37 +08:00
}
2020-11-19 18:26:30 +08:00
/**
* 获取地区下的所有子级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]
* @param [string] $order_by [排序, 默认sort asc]
*/
public static function RegionItemsIds($ids = [], $is_enable = null, $order_by = 'sort asc')
{
$where = ['pid'=>$ids];
if($is_enable !== null)
{
$where['is_enable'] = $is_enable;
}
$data = Db::name('Region')->where($where)->order($order_by)->column('id');
if(!empty($data))
{
$temp = self::RegionItemsIds($data, $is_enable, $order_by);
if(!empty($temp))
{
$data = array_merge($data, $temp);
}
}
$data = empty($data) ? $ids : array_unique(array_merge($ids, $data));
return $data;
}
2020-12-29 11:52:23 +08:00
/**
* 获取地区所有数据、最多三级
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-12-29
* @desc description
* @param [array] $params [输入参数]
*/
public static function RegionAll($params = [])
{
// 缓存
2022-04-21 22:08:53 +08:00
$key = SystemService::CacheKey('shopxo.cache_region_all_key');
2021-07-18 23:42:10 +08:00
$data = MyCache($key);
2020-12-29 11:52:23 +08:00
if(empty($data))
{
// 所有一级
$field = 'id,pid,name';
$data = self::RegionNode(['field'=>$field,'where'=>[['pid', '=', 0]]]);
2020-12-29 11:52:23 +08:00
if(!empty($data))
{
// 所有二级
$two = self::RegionNode(['field'=>$field,'where'=>[['pid', 'in', array_column($data, 'id')]]]);
2020-12-29 11:52:23 +08:00
$two_group = [];
$three_group = [];
if(!empty($two))
{
// 所有三级
$three = self::RegionNode(['field'=>$field,'where'=>[['pid', 'in', array_column($two, 'id')]]]);
2020-12-29 11:52:23 +08:00
if(!empty($three))
{
// 三级集合组
foreach($three as $v)
{
if(!array_key_exists($v['pid'], $three_group))
{
$three_group[$v['pid']] = [];
}
$pid = $v['pid'];
unset($v['pid']);
$three_group[$pid][] = $v;
}
}
// 二级集合
foreach($two as $v)
{
// 是否存在三级数据
$v['items'] = array_key_exists($v['id'], $three_group) ? $three_group[$v['id']] : [];
// 集合组
if(!array_key_exists($v['pid'], $two_group))
{
$two_group[$v['pid']] = [];
}
$pid = $v['pid'];
unset($v['pid']);
$two_group[$pid][] = $v;
}
}
// 一级集合
foreach($data as $k=>$v)
{
$data[$k]['items'] = array_key_exists($v['id'], $two_group) ? $two_group[$v['id']] : [];
}
// 存储缓存
2021-07-18 23:42:10 +08:00
MyCache($key, $data, 60);
2020-12-29 11:52:23 +08:00
}
}
return DataReturn('success', 0, $data);
}
2018-12-28 18:58:37 +08:00
}
?>