460 行
18 KiB
PHP
可执行文件
460 行
18 KiB
PHP
可执行文件
<?php
|
||
|
||
namespace Retail\Business;
|
||
|
||
use Common\Model\RetailGoodsClassModel;
|
||
use Common\Model\RetailGoodsModel;
|
||
use Common\Model\RetailMgoodsModel;
|
||
use Common\Model\RetailStoreModel;
|
||
use Retail\Model\StoreIndustryModel;
|
||
|
||
//[1.9.10]
|
||
|
||
/**
|
||
* 商品分类
|
||
* Class GoodsClass
|
||
* [
|
||
* setClassify 设置分类
|
||
* delClassify 删除分类
|
||
* getClassList 获取分类
|
||
* ]
|
||
* @package Retail\Business
|
||
*/
|
||
class GoodsClass
|
||
{
|
||
/**
|
||
* 设置分类
|
||
* @param $param
|
||
* $param['postData'] post请求参数
|
||
* $param['merchant_id'] 商户id
|
||
* @return array
|
||
*/
|
||
public function setClassify($param)
|
||
{
|
||
$GoodsModel = new RetailGoodsModel();
|
||
$MGoodsModel = new RetailMgoodsModel();
|
||
$ClassModel = new RetailGoodsClassModel();
|
||
|
||
$data = $param['postData'];
|
||
$merchant_id = $param['merchant_id'];
|
||
|
||
if (!$merchant_id || empty($data)) {
|
||
return ['status' => 0, 'msg' => '缺少必要参数'];
|
||
}
|
||
|
||
$map = [
|
||
'merchantid' => $merchant_id,
|
||
'status' => 0,
|
||
'class_name' => $data['class_name'],
|
||
'pid' => $data['pid'] ? $data['pid'] : 0,
|
||
'industry_id' => $data['industry_id'] ? $data['industry_id'] : 0,
|
||
];
|
||
|
||
$item = [
|
||
'code' => $data['code'],
|
||
'banner_id' => isset($data['banner_id']) ? $data['banner_id'] : 0,
|
||
'class_name' => $data['class_name'],
|
||
'pid' => $data['pid'] ? $data['pid'] : 0,
|
||
'type' => 1,
|
||
];
|
||
// 行业id,只有一级分类能设置行业,子分类都跟随一级分类的行业
|
||
if ($data['industry_id'] && !$data['pid']) {
|
||
$item['industry_id'] = $data['industry_id'];
|
||
}
|
||
// 门店添加分类时存储门店id
|
||
if ($param['store_id']) {
|
||
$RetailStoreModel = new RetailStoreModel();
|
||
$storeIndustryId = $RetailStoreModel->getFindField(['id' => $param['store_id']], 'industry_id');
|
||
if ($storeIndustryId) {
|
||
$item['industry_id'] = $storeIndustryId;
|
||
}
|
||
$item['store_id'] = $param['store_id'];
|
||
$item['add_type'] = 2; // 门店添加
|
||
}
|
||
//判断上级分类是否存在商品
|
||
if (!empty($data['pid']) && $data['pid'] != 0) {
|
||
$gMap = ['goods_class_id' => $data['pid'], 'status' => ['neq', 2]];
|
||
$mGoods = $MGoodsModel->getFind($gMap);
|
||
if (!empty($mGoods)) {
|
||
return ['status' => 0, 'msg' => '商户在该上级分类中已存在商品,不能添加下级分类'];
|
||
}
|
||
$goods = $GoodsModel->getFind($gMap);
|
||
if (!empty($goods)) {
|
||
return ['status' => 0, 'msg' => '门店在该上级分类中已存在商品,不能添加下级分类'];
|
||
}
|
||
}
|
||
//处理分类的类型、行业
|
||
if (!empty($data['pid'])) {
|
||
$cate = $ClassModel->getFind(['id' => $data['pid'], 'merchantid' => $param['merchant_id'], 'status' => 0]);
|
||
if (empty($cate)) {
|
||
return ['status' => 0, 'msg' => '上级分类不存在'];
|
||
}
|
||
switch ($cate['type']) {
|
||
case '1':
|
||
$map['pid'] = $cate['id'];
|
||
$item['type'] = 2;
|
||
break;
|
||
case '2':
|
||
$map['pid'] = $cate['id'];
|
||
$item['type'] = 3;
|
||
break;
|
||
default:
|
||
return ['status' => 0, 'msg' => '暂不支持在该上级分类中添加子分类'];
|
||
}
|
||
$map['industry_id'] = $cate['industry_id'];
|
||
$item['industry_id'] = $cate['industry_id'];
|
||
}
|
||
if (!empty($data['id'])) {
|
||
$map['id'] = ['neq', $data['id']];
|
||
}
|
||
$class_name = $ClassModel->getFind($map);
|
||
if (!empty($class_name)) {
|
||
return ['status' => 0, 'msg' => '同级分类下已存在该名称'];
|
||
}
|
||
//修改
|
||
if (!empty($data['id'])) {
|
||
// 禁止修改行业
|
||
unset($item['industry_id']);
|
||
|
||
$create = $ClassModel->updateOrder(['id' => $data['id']], $item);
|
||
if ($create) {
|
||
//操作日志记录
|
||
$source = 0; //请求来源: 0pc系统 1小助手
|
||
$type = $param['store_id'] ? 4 : 3; //平台:1合伙人端 2代理商端 3商户端 4门店端
|
||
$request_data = json_encode($param); //请求参数 json数据
|
||
$desc = '商品分类-分类修改'; //操作行为描述
|
||
action_log($source, $type, $request_data, $desc);
|
||
|
||
$res = ['status' => 1, 'msg' => '修改成功'];
|
||
} else {
|
||
$res = ['status' => 0, 'msg' => '修改失败'];
|
||
}
|
||
} else {
|
||
//添加
|
||
$item['merchantid'] = $merchant_id;
|
||
$item['addtime'] = time();
|
||
$create = $ClassModel->addOrder($item);
|
||
if ($create) {
|
||
//操作日志记录
|
||
$source = 0; //请求来源: 0pc系统 1小助手
|
||
$type = $param['store_id'] ? 4 : 3; //平台:1合伙人端 2代理商端 3商户端 4门店端
|
||
$request_data = json_encode($param); //请求参数 json数据
|
||
$desc = '商品分类-分类添加'; //操作行为描述
|
||
action_log($source, $type, $request_data, $desc);
|
||
|
||
$item['id'] = $create;
|
||
$res = ['status' => 1, 'msg' => '添加成功'];
|
||
} else {
|
||
$res = ['status' => 0, 'msg' => '添加失败'];
|
||
}
|
||
}
|
||
return $res;
|
||
}
|
||
|
||
/**
|
||
* 删除分类
|
||
* @param $param
|
||
* $param['id'] 分类id
|
||
* $param['store_id'] 门店id
|
||
* $param['merchant_id'] 商户id
|
||
* @return array
|
||
*/
|
||
public function delClassify($param)
|
||
{
|
||
$id = $param['id'];
|
||
if (empty($id)) {
|
||
return ['status' => 0, 'msg' => '参数错误,请重试'];
|
||
}
|
||
//需要判断分类是否被使用中
|
||
//首先判断是否被子级分类使用
|
||
$RetailStoreModel = new RetailStoreModel();
|
||
$RetailGoodsModel = new RetailGoodsModel();
|
||
$RetailMgoodsModel = new RetailMgoodsModel();
|
||
$RetailGoodsClassModel = new RetailGoodsClassModel();
|
||
$belong_store_id = $param['store_id'];
|
||
if ($belong_store_id) {
|
||
$classInfo = $RetailGoodsClassModel->getFind(['id' => $id], 'add_type,store_id');
|
||
if ($classInfo['add_type'] == 1 || $belong_store_id != $classInfo['store_id']) {
|
||
return (['status' => 0, 'msg' => '门店只能删除自己添加的分类']);
|
||
}
|
||
}
|
||
$find = $RetailGoodsClassModel->getFind(['pid' => $id, 'status' => 0]);
|
||
if (empty($find)) {
|
||
//分类查询不到,有可能是三级分类,需要到商品表查询
|
||
$mgoodsInfo = $RetailMgoodsModel->getFind(['goods_class_id' => $id, 'status' => ['neq', 2]]);
|
||
$map = [
|
||
'merchantid' => $param['merchant_id'],
|
||
'state' => 0
|
||
];
|
||
$store_id = $RetailStoreModel->getFindField($map, 'id', true);
|
||
$map = [
|
||
'goods_class_id' => $id,
|
||
'status' => ['neq', 2],
|
||
'store_id' => ['in', $store_id ? $store_id : '0']
|
||
];
|
||
$res['store_id'] = $store_id;
|
||
$goodsInfo = $RetailGoodsModel->getFind($map);
|
||
if (!empty($mgoodsInfo)) {
|
||
return ['status' => 0, 'msg' => '存在商户商品使用该分类,暂时无法删除'];
|
||
}
|
||
if (!empty($goodsInfo)) {
|
||
return ['status' => 0, 'msg' => '存在门店商品使用该分类,暂时无法删除'];
|
||
}
|
||
$del = $RetailGoodsClassModel->updateOrder(['id' => $id], ['status' => 1]);
|
||
if ($del) {
|
||
//操作日志记录
|
||
$source = 0; //请求来源: 0pc系统 1小助手
|
||
$type = $param['store_id'] ? 4 : 3; //平台:1合伙人端 2代理商端 3商户端 4门店端
|
||
$request_data = json_encode($param); //请求参数 json数据
|
||
$desc = '商品分类-分类删除'; //操作行为描述
|
||
action_log($source, $type, $request_data, $desc);
|
||
|
||
$res['status'] = 1;
|
||
$res['msg'] = '删除完成';
|
||
} else {
|
||
$res['status'] = 0;
|
||
$res['msg'] = '删除失败';
|
||
}
|
||
return $res;
|
||
} else {
|
||
return ['status' => 0, 'msg' => '该分类下有子分类,暂时无法删除'];
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取分类列表-商户
|
||
* @param $param
|
||
* $param['merchant_id']商户id
|
||
* $param['store_id'] 门店id
|
||
* $param['type'] 分类类型
|
||
* $param['pid'] 上级分类ID
|
||
* $param['page'] 页码
|
||
* $param['search_name']分类名
|
||
* $param['industry_id']行业id
|
||
* $param['store_industry_id']门店行业id
|
||
* @return array
|
||
*/
|
||
public function getClassList($param)
|
||
{
|
||
if (!empty($param['type']) && !empty($param['merchant_id']) && (!empty($param['pid']) || empty($param['pid']) && $param['type'] == 1) && !empty($param['page'])) {
|
||
$GoodsClassModel = new RetailGoodsClassModel();
|
||
$map = [
|
||
'merchantid' => $param['merchant_id'],
|
||
'type' => $param['type'],
|
||
'status' => 0,
|
||
];
|
||
// 额外搜索条件
|
||
if (!empty($param['pid'])) {
|
||
$map['pid'] = $param['pid'];
|
||
}
|
||
// 分类名
|
||
if (trim($param['search_name'])) {
|
||
$map['class_name'] = ['like', '%' . trim($param['search_name']) . '%'];
|
||
}
|
||
// 行业
|
||
if ($param['industry_id'] != '') {
|
||
$map['industry_id'] = $param['industry_id'];
|
||
}
|
||
// 编码
|
||
if ($param['code']) {
|
||
$map['code'] = ['like', '%' . $param['code'] . '%'];
|
||
}
|
||
|
||
$limit = 20;
|
||
$fields = [
|
||
'id',
|
||
'class_name',
|
||
'banner_id',
|
||
'code',
|
||
'pid',
|
||
'industry_id',
|
||
];
|
||
$ImageGalleryModel = new \Common\Model\ImageGalleryModel();
|
||
$StoreIndustryModel = new StoreIndustryModel();
|
||
$list = $GoodsClassModel->getSelect($map, $fields, 'code asc', $param['page'] . ',' . $limit);
|
||
foreach ($list as &$value) {
|
||
if ($value['industry_id']) {
|
||
$value['industry_name'] = $StoreIndustryModel->getFindField(['id' => $value['industry_id']], 'name');
|
||
} else {
|
||
$value['industry_name'] = '-';
|
||
}
|
||
$value['online_state'] = $value['online_state'] == 1 ? '1' : '0';
|
||
if ($value['banner_id']) {
|
||
$value['banner_url'] = $ImageGalleryModel->getOneField(['id' => $value['banner_id']], 'path');
|
||
} else {
|
||
$value['banner_url'] = '';
|
||
}
|
||
}
|
||
|
||
return [
|
||
'status' => 1,
|
||
'msg' => $list ? '获取成功' : '暂无分类',
|
||
'limit' => $limit,
|
||
'data' => $list
|
||
];
|
||
} else {
|
||
return [
|
||
'status' => 0,
|
||
'msg' => '请求参数有误'
|
||
];
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取分类列表-门店
|
||
* @param $param
|
||
* $param['merchant_id']商户id
|
||
* $param['store_id'] 门店id
|
||
* $param['type'] 分类类型
|
||
* $param['pid'] 上级分类ID
|
||
* $param['page'] 页码
|
||
* $param['search_name']分类名
|
||
* $param['industry_id']行业id
|
||
* @return array
|
||
*/
|
||
public function getStoreClassList($param)
|
||
{
|
||
if (!empty($param['type']) && !empty($param['store_id']) && (!empty($param['pid']) || empty($param['pid']) && $param['type'] == 1) && !empty($param['page'])) {
|
||
$GoodsClassModel = new RetailGoodsClassModel();
|
||
$map = [
|
||
'rclass.merchantid' => $param['merchant_id'],
|
||
'rclass.type' => $param['type'],
|
||
'rclass.status' => 0,
|
||
'sclass.store_id' => $param['store_id'],
|
||
];
|
||
// 额外搜索条件
|
||
if (!empty($param['pid'])) {
|
||
$map['rclass.pid'] = $param['pid'];
|
||
}
|
||
// 分类名
|
||
if (trim($param['search_name'])) {
|
||
$map['rclass.class_name'] = ['like', '%' . trim($param['search_name']) . '%'];
|
||
}
|
||
// 门店登录只能获取自己的行业以及公共行业下的分类
|
||
if ($param['industry_id'] != '') {
|
||
$map['rclass.industry_id'] = $param['industry_id'];
|
||
} else if ($param['store_industry_id']) {
|
||
$map['rclass.industry_id'] = ['in', [$param['store_industry_id'], 0]];
|
||
}
|
||
// 编码
|
||
if ($param['code']) {
|
||
$map['rclass.code'] = ['like', '%' . $param['code'] . '%'];
|
||
}
|
||
|
||
$limit = 20;
|
||
$fields = [
|
||
'rclass.id',
|
||
'rclass.class_name',
|
||
'rclass.code',
|
||
'rclass.pid',
|
||
'rclass.store_id',
|
||
'rclass.industry_id',
|
||
'sclass.online_state',
|
||
];
|
||
$StoreIndustryModel = new StoreIndustryModel();
|
||
$list = $GoodsClassModel->goodsClassLink($map, $fields, 'rclass.code asc', $param['page'], $limit);
|
||
if ($param['type'] == 1 || $param['type'] == 2) {
|
||
foreach ($list as &$value) {
|
||
if ($value['industry_id']) {
|
||
$value['industry_name'] = $StoreIndustryModel->getFindField(['id' => $value['industry_id']], 'name');
|
||
} else {
|
||
$value['industry_name'] = '-';
|
||
}
|
||
$value['online_state'] = $value['online_state'] == 1 ? '1' : '0';
|
||
}
|
||
}
|
||
|
||
return [
|
||
'status' => 1,
|
||
'msg' => $list ? '获取成功' : '暂无分类',
|
||
'limit' => $limit,
|
||
'data' => $list
|
||
];
|
||
} else {
|
||
return [
|
||
'status' => 0,
|
||
'msg' => '请求参数有误',
|
||
];
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取用来选择的分类
|
||
* @param $param
|
||
* $param['user_type'] 获取的类型 1商户(不限制行业) 2门店(限制行业)
|
||
* $param['merchant_id'] 商户ID
|
||
* $param['store_id'] 门店ID
|
||
* $param['type'] 分类的类型
|
||
* $param['pid'] 上级分类ID
|
||
* @return array
|
||
*/
|
||
public function getChooseClassList($param)
|
||
{
|
||
if (
|
||
!in_array($param['user_type'], ['1', '2']) || empty($param['merchant_id'])
|
||
|| $param['user_type'] == 2 && empty($param['store_id'])
|
||
) {
|
||
return callBack(statusCode()['error'], '请求参数有误');
|
||
}
|
||
$map1 = ['merchantid' => $param['merchant_id'], 'type' => 1, 'status' => 0];
|
||
$map2 = ['c1.merchantid' => $param['merchant_id'], 'c2.type' => 2, 'c1.id' => $param['pid'], 'c2.status' => 0];
|
||
$map3 = ['c2.merchantid' => $param['merchant_id'], 'c3.type' => 3, 'c2.id' => $param['pid'], 'c3.status' => 0];
|
||
//如果是获取门店的分类,则判断行业
|
||
if ($param['user_type'] == 2) {
|
||
$StoreModel = new RetailStoreModel();
|
||
$industry_id = $StoreModel->getFindField(['id' => $param['store_id']], 'industry_id');
|
||
if (intval($industry_id) > 0) {
|
||
$map1['industry_id'] = ['in', [$industry_id, 0]];
|
||
$map2['c1.industry_id'] = ['in', [$industry_id, 0]];
|
||
$map3['c2.industry_id'] = ['in', [$industry_id, 0]];
|
||
}
|
||
}
|
||
switch ($param['type']) {
|
||
case '1':
|
||
$class = M('retail_goods_class')
|
||
->where($map1)
|
||
->field('id,class_name,industry_id')
|
||
->select();
|
||
break;
|
||
case '2':
|
||
$class = M('retail_goods_class as c1')
|
||
->join('ls_retail_goods_class as c2 on c1.id=c2.pid')
|
||
->where($map2)
|
||
->field('c2.id,c2.class_name,c2.industry_id')
|
||
->order('c2.code asc')
|
||
->select();
|
||
break;
|
||
case '3':
|
||
$class = M('retail_goods_class as c2')
|
||
->join('ls_retail_goods_class as c3 on c2.id=c3.pid')
|
||
->where($map3)
|
||
->field('c3.id,c3.class_name,c3.industry_id')
|
||
->order('c3.code asc')
|
||
->select();
|
||
break;
|
||
default:
|
||
$class = [];
|
||
}
|
||
if ($param['type'] == 1) {
|
||
$groupList = [];
|
||
$ids = [];
|
||
$IndustryModel = new StoreIndustryModel();
|
||
foreach ($class as $val) {
|
||
if (!isset($ids[$val['industry_id']])) {
|
||
$ids[$val['industry_id']] = count($ids);
|
||
$groupList[$ids[$val['industry_id']]]['id'] = $val['industry_id'];
|
||
$groupList[$ids[$val['industry_id']]]['name'] = $val['industry_id'] ? $IndustryModel->getFindField(['id' => $val['industry_id']], 'name') : '公共';
|
||
$groupList[$ids[$val['industry_id']]]['list'] = [];
|
||
}
|
||
$groupList[$ids[$val['industry_id']]]['list'][] = [
|
||
'id' => $val['id'],
|
||
'class_name' => $val['class_name'],
|
||
'industry_id' => $val['industry_id'],
|
||
];
|
||
}
|
||
$class = $groupList;
|
||
}
|
||
return callBack(statusCode()['success'], (!empty($class) ? '获取成功' : '暂无数据'), $class);
|
||
}
|
||
}
|