305 lines
10 KiB
PHP
305 lines
10 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
namespace app\index\model;
|
||
|
||
|
||
use app\api\model\GoodsAttribute;
|
||
use app\api\model\GoodsBuyNow;
|
||
use app\api\model\GoodsConnect;
|
||
use app\api\model\GoodsImages;
|
||
use app\api\model\GoodsSoftManual;
|
||
use think\facade\Env;
|
||
|
||
/**
|
||
* 商品模型
|
||
* @mixin \think\Model
|
||
*/
|
||
class Goods extends BaseModel
|
||
{
|
||
protected $schema = [
|
||
'id' =>'int',
|
||
'platform_id' =>'int',
|
||
'language_id' =>'int',
|
||
'site_id' =>'int',
|
||
'name' =>'string',
|
||
'main_img' =>'string',
|
||
'model_number' =>'string',
|
||
'goods_category_id' =>'int',
|
||
'price' =>'int',
|
||
'is_new' =>'int',
|
||
'is_discount' =>'int',
|
||
'details' =>'string',
|
||
'title' =>'string',
|
||
'keywords' =>'string',
|
||
'description' =>'string',
|
||
'sort' =>'int',
|
||
'status' =>'int',
|
||
'create_time' =>'int',
|
||
'update_time' =>'int',
|
||
];
|
||
|
||
/**
|
||
* 商品链接关联模型
|
||
*/
|
||
public function GoodsBuyNow()
|
||
{
|
||
return $this->hasMany(GoodsBuyNow::class);
|
||
}
|
||
|
||
/**
|
||
* 商品关联商品
|
||
*/
|
||
public function GoodsConnect()
|
||
{
|
||
return $this->hasMany(GoodsConnect::class);
|
||
}
|
||
|
||
/**
|
||
* 商品手册关联模型
|
||
* @return \think\model\relation\HasMany
|
||
*/
|
||
public function GoodsSoftManual()
|
||
{
|
||
return $this->hasMany(GoodsSoftManual::class);
|
||
}
|
||
|
||
/**
|
||
* 商品主图关联模型
|
||
* @return \think\model\relation\HasMany
|
||
*/
|
||
public function GoodsImages()
|
||
{
|
||
return $this->hasMany(GoodsImages::class);
|
||
}
|
||
|
||
/**
|
||
* 商品属性关联
|
||
* @return \think\model\relation\HasMany
|
||
*/
|
||
public function goodsAttr()
|
||
{
|
||
return $this->hasMany(GoodsAttribute::class);
|
||
}
|
||
|
||
/**
|
||
* 获取父级分类
|
||
* @param $data
|
||
* @return array
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getFirstLeveCategory($data)
|
||
{
|
||
return GoodsCategory::where('pid',0)->where('status',0)->where('site_id',1)->order('sort','desc')->select()->toArray();
|
||
}
|
||
|
||
/**
|
||
* 获取二级分类
|
||
* @param $data
|
||
* @return array
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getSecondLevelCategory($data)
|
||
{
|
||
return GoodsCategory::where('pid',$data['cate1'])->where('status',0)->where('site_id',1)->order('sort','desc')->select()->toArray();
|
||
}
|
||
|
||
/**
|
||
* 获取三级分类
|
||
* @param $data
|
||
* @return array
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getThreeLevelCategory($data)
|
||
{
|
||
return GoodsCategory::where('pid',$data['cate2'])->where('status',0)->where('site_id',1)->order('sort','desc')->select()->toArray();
|
||
}
|
||
|
||
/**
|
||
* 获取所有子分类
|
||
* @param $id
|
||
* @return array
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getAllSonCategory($id)
|
||
{
|
||
$arr = GoodsCategory::where('status',0)->where('site_id',1)->order('sort','desc')->select()->toArray();
|
||
$feeds = category_to_all_children_id($arr,$id);
|
||
|
||
return $feeds;
|
||
}
|
||
|
||
/**
|
||
* 获取列表
|
||
* @param array $data 查询条件
|
||
* @return \think\Paginator
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getGoodsList($data,$qstring)
|
||
{
|
||
$qs = explode('index/goods/index?',$qstring);
|
||
parse_str($qs[1],$out);
|
||
|
||
$query = $queryOther = [];
|
||
$forth = [
|
||
'is_all','is_recommend','is_hot','is_new'
|
||
];
|
||
foreach ($forth as $f) {
|
||
if (in_array($f,array_keys($data))) {
|
||
$queryOther[$f] = $data[$f];
|
||
}
|
||
}
|
||
// 判断是否选择了分类
|
||
for ($i=1;$i<=3;$i++) {
|
||
if (isset($data['cate'.$i])) {
|
||
// 如果是3级分类
|
||
if ($i === 3 && $data['cate'.$i] != 0) {
|
||
$query['goods_category_id'] = $data['cate'.$i];
|
||
}
|
||
// 如果是2级分类
|
||
if ($i === 2 && $data['cate'.$i] != 0) {
|
||
$arr = array_column($this->getThreeLevelCategory(['cate2'=>$data['cate2']]),'id');
|
||
array_push($arr,(int)$data['cate2']);
|
||
$query['goods_category_id'] = $arr;
|
||
}
|
||
// 如果是1级分类
|
||
if ($i === 1 && $data['cate'.$i] != 0) {
|
||
$query['goods_category_id'] = $this->getAllSonCategory($data['cate1']);
|
||
}
|
||
}
|
||
}
|
||
|
||
$collection = $this->where('status',0);
|
||
if (isset($data['model_number'])) {
|
||
$collection = $collection->where('model_number','like','%'.$data['model_number'].'%')->whereOr('name','like','%'.$data['model_number'].'%')->whereOr('title','like','%'.$data['model_number'].'%');
|
||
} else {
|
||
//查询
|
||
if (!empty($queryOther) && !isset($queryOther['is_all'])) {
|
||
$collection = $collection->where($queryOther);
|
||
}
|
||
|
||
if (!empty($query) && is_array($query['goods_category_id'])) {
|
||
// 表示是非3级分类
|
||
$collection = $collection->whereIn('goods_category_id',$query['goods_category_id']);
|
||
} else {
|
||
$collection = $collection->where($query);
|
||
}
|
||
}
|
||
|
||
$pageNum = SystemConfig::where('pos','goods')->where('name','list_num')->find()->toArray();
|
||
$listRow = [
|
||
'query' => $out, //url额外参数
|
||
'fragment' => '', //url锚点
|
||
'var_page' => 'page', //分页变量
|
||
'list_rows' => (int)$pageNum['value']??4, //每页数量
|
||
];
|
||
|
||
//return $collection->order('create_time','desc')->paginate($listRow);
|
||
$resutl = $collection->order('create_time','desc')->paginate($listRow);
|
||
//dump($this->getLastSql());
|
||
return $resutl;
|
||
}
|
||
|
||
/**
|
||
* 通过商品ID,查出所有父级分类
|
||
*/
|
||
public function getCategoryToParent($id)
|
||
{
|
||
$goods = $this->where('id',$id)->find()->toArray();
|
||
$data = GoodsCategory::where('status',0)->order('sort','desc')->select()->toArray();
|
||
$arr = category_to_all_pid($data,$goods['goods_category_id']);
|
||
$arr = GoodsCategory::whereIn('id',$arr)->order('sort','desc')->select()->toArray();
|
||
|
||
$url = '';
|
||
for ($i=0;$i<=count($arr)-1;$i++) {
|
||
$cate = 'cate'.($i + 1);
|
||
$url .= http_build_query([$cate =>$arr[$i]['id']])."&";
|
||
$arr[$i]['qs'] = rtrim($url,'&');
|
||
}
|
||
$arr[0]['qs'] = 'cate1='.$arr[0]['id'];
|
||
return $arr;
|
||
}
|
||
|
||
public function getInfo($data)
|
||
{
|
||
$result = $this->where('status',0)->find($data['id']);
|
||
if ($result) {
|
||
$goodsAttribute = $result->goodsAttr()->select()->toArray();
|
||
$attr = $img = [];
|
||
if ($goodsAttribute) {
|
||
foreach ($goodsAttribute as $item) {
|
||
$attr[] = array_merge($item,['attribute_name'=>Attribute::where('id',$item['attribute_id'])->value('name')]);
|
||
}
|
||
}
|
||
$images = $result->goodsImages()->select()->toArray();
|
||
$connect = $result->goodsConnect()->select()->toArray();
|
||
if (!empty($connect)) {
|
||
foreach ($connect as $item) {
|
||
$res = $this->where('status',0)->find($item['connect_good_id']);
|
||
if ($item['connect_good_id'] != 0 && $item['connect_good_id'] != '' && $res) {
|
||
$tmp = $this->where('status',0)->find($item['connect_good_id'])->toArray();
|
||
$tmp['main_img'] = Env::get('QINIU.cdn').$tmp['main_img'];
|
||
$connectData[] = array_merge($item,$tmp);
|
||
}
|
||
}
|
||
}
|
||
//处理相关下载
|
||
$relate = $result->GoodsSoftManual()->select()->toArray();
|
||
$newRelate = [];
|
||
foreach ($relate as $item) {
|
||
$soft = SoftManual::where('status',0)->find($item['soft_manual_id']);
|
||
if ($soft) {
|
||
$btn = SoftManualBtn::where('soft_manual_id',$item['soft_manual_id'])->select()->toArray();
|
||
$soft = $soft->toArray();
|
||
$soft['btn'] = $btn;
|
||
$newRelate[] = array_merge($soft,[]);
|
||
}
|
||
}
|
||
|
||
if (!empty($newRelate)) {
|
||
foreach ($newRelate as $key => $item) {
|
||
$newRelate[$key]['image'] = Env::get('QINIU.cdn').$item['image'];
|
||
foreach ($item['btn'] as $item1) {
|
||
$tmp = [
|
||
'url' => Env::get('QINIU.cdn').$item1['url'],
|
||
'name' => $item1['file_name']
|
||
];
|
||
$arr[$item1['btn_tips']][] = array_merge($tmp,[]);
|
||
}
|
||
unset($newRelate[$key]['btn']);
|
||
$newRelate[$key]['getSoftBtn'] = $arr;
|
||
$arr = [];
|
||
}
|
||
}
|
||
//处理图片
|
||
$result['main_img'] = Env::get('QINIU.cdn').$result['main_img'];
|
||
// $img[0] = ['image'=>$result['main_img']];
|
||
foreach ($images as $item) {
|
||
$img[] = ['image'=>Env::get('QINIU.cdn').$item['image']];
|
||
}
|
||
//
|
||
$categoryName = GoodsCategory::find($result['goods_category_id']);
|
||
$result['category_name'] = $categoryName['name'];
|
||
//buynow
|
||
$result['buy_now'] = $result->goodsBuyNow()->select()->toArray();
|
||
$result['goods_attribute'] = $attr;
|
||
$result['relate_download'] = $newRelate;
|
||
$result['images'] = empty($img) ? [['image'=>$result['main_img']]] : $img;
|
||
$result['connect'] = empty($connectData) ? [] : $connectData;
|
||
|
||
return $result->toArray();
|
||
} else {
|
||
return [];
|
||
}
|
||
}
|
||
|
||
} |