nk-lihaink-cn/app/admin/model/EbStoreProduct.php
2023-03-10 16:18:11 +08:00

178 lines
5.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* 时间2023年03月04日
* 作者:墨楠小
* 邮箱monanxiao@qq.com
* 订单模型
*
*/
namespace app\admin\model;
use think\Model;
use StoreCategory as StoreCategoryModel;
class EbStoreProduct extends Model
{
// 设置当前模型的数据库连接
protected $connection = 'shop';
// 设置当前模型对应的完整数据表名称
protected $table = 'eb_store_product';
protected $pk = 'product_id';
protected $filed = 'Product.product_id,Product.mer_id,brand_id,unit_name,spec_type,mer_status,rate,reply_count,store_info,cate_id,Product.image,slider_image,Product.store_name,Product.keyword,Product.sort,Product.is_show,Product.sales,Product.price,extension_type,refusal,cost,ot_price,stock,is_gift_bag,Product.care_count,Product.status,is_used,Product.create_time,Product.product_type,old_product_id,integral_total,integral_price_total,mer_labels,Product.is_good,Product.is_del,type,param_temp_id';
/**
*
* 关联商户
*
*/
public function merchant()
{
return $this->hasOne(Merchant::class, 'mer_id', 'mer_id');
}
/**
* TODO 商户商品列表
* @Author:Qinii
* @Date: 2020/5/11
* @param int $merId
* @param array $where
* @param int $page
* @param int $limit
* @return array
*/
public function getList(?int $mer_id, array $where, int $page, int $limit)
{
$query = self::search($mer_id, $where);
// ->with(['merCateId.category', 'storeCategory', 'brand']);
$count = $query->count();
$data = $query->field($this->filed)->page($page, $limit)->select();
// $data->append(['us_status']);
// $list = hasMany(
// $data,
// 'mer_labels',
// ProductLabel::class,
// 'product_label_id',
// 'mer_labels',
// ['status' => 1],
// 'product_label_id,product_label_id id,label_name name'
// );
return compact('count', 'list');
}
// public function StoreSpu()
// {}
/**
* @Author:Qinii
* @Date: 2020/5/11
* @param int $merId
* @param array $where
* @return mixed
*/
protected function search(?int $merId, array $where)
{
$keyArray = $whereArr = [];
unset($where['type']);
// 以下字段为要搜索的字段
$out = ['soft', 'us_status', 'mer_labels', 'sys_labels', 'order', 'hot_type'];
foreach ($where as $key => $item) {
if ($item !== '' && !in_array($key, $out)) {
$keyArray[] = $key;
$whereArr[$key] = $item;
}
}
// $query = isset($where['soft']) ? model::onlyTrashed()->alias('Product') : model::alias('Product');
$query = self::alias('Product');
if (isset($where['is_trader']) && $where['is_trader'] !== '') {
$query->hasWhere('merchant', function ($query) use ($where) {
$query->where('is_trader', $where['is_trader']);
});
}
$query->withSearch($keyArray, $whereArr)
->Join('StoreSpu U', 'Product.product_id = U.product_id')->where('U.product_type', $where['product_type'] ?? 0)
->when(($merId !== null), function ($query) use ($merId) {
$query->where('Product.mer_id', $merId);
})
->when(isset($where['hot_type']) && $where['hot_type'] !== '', function ($query) use ($where) {
if ($where['hot_type'] == 'new')
$query->where('is_new', 1);
else if ($where['hot_type'] == 'hot')
$query->where('is_hot', 1);
else if ($where['hot_type'] == 'best')
$query->where('is_best', 1);
else if ($where['hot_type'] == 'good')
$query->where('is_benefit', 1);
});
$query->when(
isset($where['pid']) && $where['pid'] !== '',
function ($query) use ($where) {
$ids = array_merge(self::findChildrenId((int)$where['pid']), [(int)$where['pid']]);
if (count($ids)) $query->whereIn('cate_id', $ids);
}
)
->when(isset($where['us_status']) && $where['us_status'] !== '', function ($query) use ($where) {
if ($where['us_status'] == 0) {
$query->where('Product.is_show', 0)->where('Product.is_used', 1)->where('Product.status', 1);
}
if ($where['us_status'] == 1) {
$query->where('Product.is_show', 1)->where('Product.is_used', 1)->where('Product.status', 1);
}
if ($where['us_status'] == -1) {
$query->where(function ($query) {
$query->where('Product.is_used', 0)->whereOr('Product.status', '<>', 1);
});
}
})
->when(isset($where['mer_labels']) && $where['mer_labels'] !== '', function ($query) use ($where) {
$query->whereLike('U.mer_labels', "%,{$where['mer_labels']},%");
})
->when(isset($where['sys_labels']) && $where['sys_labels'] !== '', function ($query) use ($where) {
$query->whereLike('U.sys_labels', "%,{$where['sys_labels']},%");
})
->when(isset($where['order']), function ($query) use ($where, $merId) {
if (in_array($where['order'], ['is_new', 'price_asc', 'price_desc', 'rate', 'sales'])) {
if ($where['order'] == 'price_asc') {
$where['order'] = 'price ASC';
} else if ($where['order'] == 'price_desc') {
$where['order'] = 'price DESC';
} else {
$where['order'] = $where['order'] . ' DESC';
}
$query->order($where['order'] . ',rank DESC ,create_time DESC ');
} else if ($where['order'] !== '') {
$query->order('U.' . $where['order'] . ' DESC,U.create_time DESC');
} else {
$query->order('U.create_time DESC');
}
})
->when(isset($where['star']), function ($query) use ($where) {
$query->when($where['star'] !== '', function ($query) use ($where) {
$query->where('U.star', $where['star']);
});
$query->order('U.star DESC,U.rank DESC,Product.create_time DESC');
});
return $query;
}
public function findChildrenId($id)
{
return StoreCategoryModel::whereLike('path', '%/'. $id . '/%')->column('store_category_id');
}
}