Files
ntonpower/app/api/model/GoodsCategory.php
2026-10-14 14:00:29 +08:00

134 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
namespace app\api\model;
/**
* 商品分类
* @mixin \think\Model
*/
class GoodsCategory extends BaseModel
{
protected $schema = [
'id' =>'int',
'platform_id' =>'int',
'language_id' =>'int',
'site_id' =>'int',
'name' =>'int',
'desc' =>'int',
'pid' =>'int',
'icon' =>'int',
'sort' =>'int',
'status' =>'int',
'create_time' =>'int',
'update_time' =>'int',
];
/**
* 分类 商品数据
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getGategoryListData($data)
{
return (new Goods())->where(['goods_category_id'=>$data['goods_category_id'],'status'=>0])->select()->toArray();
}
/**
* 分类 商品明细数据
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getGategoryListDataInfo($data)
{
if (isset($data['id']) && $data['id'] != 0) {
$result = GoodsConnect::where('goods_id',$data['id'])->find();
return $result ? $result->toArray() : [] ;
}
return [];
}
/**
* 启用禁用
* @param $data
* @param bool $type
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function changeStatus($data,bool $type = false):bool
{
if (!$type) {
$result = $this->find($data['id']);
//正常更新
if ($result) {
return $result->save($data);
}
} else {
$ret = $this->saveAll($data);
return (bool) $ret;
}
return false;
}
/**
* 获取详情
* @param array $data
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getInfo(array $data):array
{
$allCategory = $this->select()->toArray();
$result = $this->find($data['id']);
$newPid = [];
if (!empty($allCategory)) {
if ($result['pid'] != 0) {
$newPid = category_to_all_pid($allCategory,$result['pid']);
} else {
$newPid = [0];
}
}
$result['pid'] = $newPid;
return $result->toArray();
}
/**
* 查找所有子项
* @param $data
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getAllSonCategory($data):array
{
$newData = $this->select()->toArray();
if (!empty($newData)) {
return category_to_all_children_id($newData,$data['id']);
}
return [];
}
/**
* 查询当前分类下是否在商品
* @param $data
* @return array|\think\Model|null
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function hasGoodsOnCategory($data)
{
return Goods::where('goods_category_id',$data['id'])->find();
}
}