You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
7.4 KiB

<?php
namespace app\mall\model;
use think\model\concern\SoftDelete;
class MallProduct extends Base
{
use SoftDelete;
protected $type = [
'img_path' => 'serialize',
'label' => 'serialize',
];
/**
* 获取商品列表
* @param array $where 查询条件
* @param array $where_product_category 关联表查询条件
* @param int $page 第X页
* @param string $field
* @param string $order 排序
* @date 2022-10-27
*/
public function listProductWithPage($where, $where_product_category, $page, $field = '*', $order = '')
{
// 未定义排序时 按创建时间倒序排序
if (empty($order)) {
$order = 'publish_time desc,id desc';
}
$data_list = $this
->hasWhere('mallProductCategory', $where_product_category, $field)
->with(['mallLabel'])
->where($where)->order($order)
->paginate(['list_rows' => 10, 'page' => $page], false)->each(function ($item) {
foreach ($item->mallLabel as $k=>$v){
// 删除无用数据
unset($item->mallLabel[$k]['uid']);
unset($item->mallLabel[$k]['create_time']);
unset($item->mallLabel[$k]['update_time']);
unset($item->mallLabel[$k]['delete_time']);
unset($item->mallLabel[$k]['pivot']);
}
});
return $data_list;
}
/**
* API-获取商品详情
* @date 2021-03-01
*/
public function getOneProduct($where = [], $field = '*')
{
$order = 'id desc';
$array = explode(',', $field);
if ($field != '*') {
if (count($array) <= 1) {
return $this->with(['mallAttach', 'mallProductSku'])->where($where)->order($order)->value($field);
} else {
$data = $this->with(['mallAttach', 'mallProductSku'])->field($field)->where($where)->order($order)->find();
if(!empty($data->mallAttach)){
foreach ($data->mallAttach as $k=>$v){
unset($data->mallAttach[$k]['uid']);
unset($data->mallAttach[$k]['sort']);
unset($data->mallAttach[$k]['pivot']);
unset($data->mallAttach[$k]['create_time']);
unset($data->mallAttach[$k]['update_time']);
unset($data->mallAttach[$k]['delete_time']);
}
}
return $data;
}
} else {
return $this->with(['mallAttach', 'mallProductSku'])->where($where)->order($order)->find();
}
}
/**
* API-获取秒杀商品详情
* @date 2021-03-01
*/
public function getOneSpikeProduct($where = [], $field = '*', $order = '')
{
$primary_key_name = $this->getPk();
if (empty($order)) {
$order = (empty($data[$primary_key_name]) ? 'id' : $data[$primary_key_name]) . ' desc';
}
$array = explode(',', $field);
if ($field != '*') {
if (count($array) <= 1) {
return $this->with(['mallAttach', 'mallSpikeProductSku'])->where($where)->order($order)->value($field);
} else {
return $this->with(['mallAttach', 'mallSpikeProductSku'])->field($field)->where($where)->order($order)->find();
}
} else {
return $this->with(['mallAttach', 'mallSpikeProductSku'])->where($where)->order($order)->find();
}
}
/**
* 获取订单格式的产品数据
* @date 2021-03-01
*/
public function getOrderData($product_id, $sku, $number = 1)
{
return $this->with(['mallAttach', 'mallProductSku'])->where($where)->order($order)->value($field);
}
/**
* 多对多关联 标签
* @date 2022-10-27
*/
public function mallLabel()
{
// 参数依次为:关联模型、 中间表、 关联模型在中间表的外键、 主模型在中间表的外键
return $this->belongsToMany('MallLabel', 'MallProductLabel', 'label_id', 'product_id');
}
/**
* 多对多关联 服务保障
* @date 2022-10-27
*/
public function mallAttach()
{
// 参数依次为:关联模型、 中间表、 关联模型在中间表的外键、 主模型在中间表的外键
return $this->belongsToMany('MallAttach', 'MallProductAttach', 'attach_id', 'product_id');
}
/**
* API-一对多关联分类
* @date 2021-03-01
*/
public function mallProductCategory()
{
// 参数依次为:关联模型、 关联模型的外键、 主模型在中间表的外键
return $this->hasMany('MallProductCategory', 'product_id', 'id')->field('category_id,product_id');
}
/**
* API-一对多关联SKU
* @date 2021-03-01
*/
public function mallProductSku()
{
return $this->hasMany('MallProductSku', 'product_id', 'id')->field('product_id,sku,price,stock,cover_img');
}
/**
* 后台异步加载列表
* @param array $where 筛选条件s
* @param array $search 底部分页条件
* @param string $field 筛选字段
* @param string $order 排序方式
* @param int $per_page_number
* @return \think\Paginator
* @throws \think\db\exception\DbException
*/
public function getMemberList($where, $search, $field = "*", $order = '', $per_page_number = 15)
{
if (empty($order)) {
if ($this->getPk() != null) {
$order = $this->getPk() . ' desc';
} else {
$order = 'id desc';
}
}
$isset_category_id = isset($where['category_id']);
$exit_category_id = $this->uidExit($where, 'category_id');
if ($isset_category_id || ($exit_category_id !== true && $exit_category_id !== false)) {
if ($isset_category_id) {
$category_id = $where['category_id'];
unset($where['category_id']);
} elseif ($exit_category_id !== true && $exit_category_id !== false) {
$category_id = $where[$exit_category_id][2];
unset($where[$exit_category_id]);
} else {
$category_id = '';
}
return $this::hasWhere('mallProductCategory', ['category_id' => $category_id])->where($where)->field($field)->order($order)
->paginate(['page' => array_key_exists("page", $search) ? $search['page'] : 1, 'list_rows' => $per_page_number, 'query' => $search], false);
} else {
return $this->with('mallProductCategory')->where($where)->field($field)->order($order)
->paginate(['page' => array_key_exists("page", $search) ? $search['page'] : 1, 'list_rows' => $per_page_number, 'query' => $search], false);
}
}
/**
* 遍历查询条件中是否有$field字段 默认为uid
* @param $where
* @param $field
* @return bool
*/
public function uidExit($where, $field = 'uid')
{
foreach ($where as $key => $value) {
if (is_numeric($key)) {
return true;
}
if (!is_array($value)) {
return true;
}
if (in_array($field, $value, true)) {
return $key;
}
}
return false;
}
}