shopxo/application/service/RegionService.php
2019-12-06 15:30:12 +08:00

197 lines
6.2 KiB
PHP
Executable File

<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;
use think\Db;
/**
* 地区服务层
* @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
* @param [int] $region_id [地区id]
*/
public static function RegionName($region_id = 0)
{
return empty($region_id) ? null : Db::name('Region')->where(['id'=>intval($region_id)])->value('name');
}
/**
* 获取地区id下列表
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2018-12-09T00:13:02+0800
* @param [array] $params [输入参数]
*/
public static function RegionItems($params = [])
{
$pid = isset($params['pid']) ? intval($params['pid']) : 0;
$field = empty($params['field']) ? '*' : $params['field'];
return Db::name('Region')->field($field)->where(['pid'=>$pid, 'is_enable'=>1])->select();
}
/**
* 获取地区节点数据
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-21
* @desc description
* @param [array] $params [输入参数]
*/
public static function RegionNode($params = [])
{
$field = empty($params['field']) ? 'id,name,level,letters' : $params['field'];
$where = empty($params['where']) ? [] : $params['where'];
$where['is_enable'] = 1;
return Db::name('Region')->where($where)->field($field)->order('sort asc,id asc')->select();
}
/**
* 获取地区节点数据
* @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 = [])
{
// id
$id = isset($params['id']) ? intval($params['id']) : 0;
// 获取数据
$field = 'id,pid,name,sort,is_enable';
$data = Db::name('Region')->field($field)->where(['pid'=>$id])->order('sort asc,id asc')->select();
if(!empty($data))
{
foreach($data as &$v)
{
$v['is_son'] = (Db::name('Region')->where(['pid'=>$v['id']])->count() > 0) ? 'ok' : 'no';
$v['ajax_url'] = MyUrl('admin/region/getnodeson', array('id'=>$v['id']));
$v['delete_url'] = MyUrl('admin/region/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 RegionSave($params = [])
{
// 请求参数
$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('Region')->insertGetId($data) > 0)
{
return DataReturn('添加成功', 0);
}
return DataReturn('添加失败', -100);
} else {
$data['upd_time'] = time();
if(Db::name('Region')->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 RegionDelete($params = [])
{
// 请求参数
$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);
}
// 是否还有子数据
$temp_count = Db::name('Region')->where(['pid'=>$params['id']])->count();
if($temp_count > 0)
{
return DataReturn('请先删除子数据', -10);
}
// 开始删除
if(Db::name('Region')->where(['id'=>$params['id']])->delete())
{
return DataReturn('删除成功', 0);
}
return DataReturn('删除失败', -100);
}
}
?>