init
This commit is contained in:
293
app/admin/model/Product.php
Executable file
293
app/admin/model/Product.php
Executable file
@@ -0,0 +1,293 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use think\Model;
|
||||
use think\Request;
|
||||
use think\Config;
|
||||
|
||||
class Product extends Model {
|
||||
|
||||
use \app\common\traits\AdminModel;
|
||||
|
||||
protected $insert = ['createtime', 'updatetime'];
|
||||
protected $update = ['updatetime'];
|
||||
|
||||
public function getCategoryProductList($where = null, $order = null, $field = null, $limit = null) {
|
||||
$this->alias('a')->join('product_category c', 'a.cid=c.id', 'LEFT');
|
||||
if (is_array($where)) {
|
||||
$where = array_merge(['a.stat' => ['eq', '0'], 'c.stat' => ['eq', '0']], $where);
|
||||
}
|
||||
if ($where) {
|
||||
$this->where($where);
|
||||
}
|
||||
if ($order) {
|
||||
$this->order($order);
|
||||
}
|
||||
if ($field) {
|
||||
$this->field($field);
|
||||
}
|
||||
if ($limit) {
|
||||
$this->limit($limit);
|
||||
}
|
||||
$data = $this->select();
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function getProductLists($where = null, $order = null, $field = null, $limit = null) {
|
||||
$this->alias('a')
|
||||
// ->join('product_category c', 'a.cid=c.id', 'LEFT');
|
||||
->join('product_category c', 'a.cid=c.id AND c.stat=0', 'LEFT');
|
||||
//->join('user u', 'a.id=u.id', 'LEFT');
|
||||
//$this->where('c.stat', ['eq', '0'], ['null', ''], 'or');
|
||||
if (is_array($where)) {
|
||||
// $where = array_merge(['a.stat' => ['eq', '0'], 'c.stat' => ['eq', '0']], $where);
|
||||
$where = array_merge(['a.stat' => ['eq', '0']], $where);
|
||||
}
|
||||
if ($where) {
|
||||
$this->where($where);
|
||||
}
|
||||
if ($order) {
|
||||
$this->order($order);
|
||||
}
|
||||
if ($field) {
|
||||
$this->field($field);
|
||||
}
|
||||
if (empty($limit)) {
|
||||
$limit = Config::get('list_rows');
|
||||
}
|
||||
//$this->fetchsql(true);
|
||||
$object = $this->paginate($limit);
|
||||
// header("content-type:text/html;charset=utf8;");
|
||||
// print_r($object);
|
||||
// exit;
|
||||
return $object;
|
||||
}
|
||||
|
||||
public function getRelatedProductList($where = null, $order = null, $field = null, $limit = null) {
|
||||
$this->alias('p')->join('product_related pr', 'p.id=pr.related_product_id', 'LEFT');
|
||||
if (is_array($where)) {
|
||||
$where = array_merge(['p.stat' => ['eq', '0'], 'pr.stat' => ['eq', '0'], 'pr.product_id' => '0'], $where);
|
||||
}
|
||||
if ($where) {
|
||||
$this->where($where);
|
||||
}
|
||||
if ($order) {
|
||||
$this->order($order);
|
||||
}
|
||||
if ($field) {
|
||||
$this->field($field);
|
||||
}
|
||||
if ($limit) {
|
||||
$this->limit($limit);
|
||||
}
|
||||
$data = $this->select();
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function getAllCate($pid, &$cate_name='') {
|
||||
if ($pid) {
|
||||
$cate_info = model('product_category')->field('name, pid')->find($pid);
|
||||
if ($cate_name == '') {
|
||||
$cate_name = $cate_info['name'];
|
||||
} else {
|
||||
$cate_name = $cate_info['name'] . '->' . $cate_name;
|
||||
}
|
||||
self::getAllCate($cate_info['pid'], $cate_name);
|
||||
}
|
||||
return $cate_name;
|
||||
}
|
||||
|
||||
public function getExportSearchProductLists($where = null, $order = null, $limit = null, $count = false, $field = '*') {
|
||||
# 是否需要有关联产品
|
||||
$flag2 = 0;
|
||||
# 是否需要有相关驱动
|
||||
$flag3 = 0;
|
||||
# 是否需要有多颜色
|
||||
$flag4 = 0;
|
||||
# 是否需要有二级列表图
|
||||
$flag5 = 0;
|
||||
# 是否需要有链接
|
||||
$flag6 = 0;
|
||||
if (is_array($field)) {
|
||||
foreach ($field as $key => $value) {
|
||||
if ($value == 'a.is_comment') {
|
||||
unset($field[$key]);
|
||||
$flag1 = 1;
|
||||
continue;
|
||||
}
|
||||
if ($value == 'a.related_product') {
|
||||
unset($field[$key]);
|
||||
$flag2 = 1;
|
||||
continue;
|
||||
}
|
||||
if ($value == 'a.driver') {
|
||||
$flag3 = 1;
|
||||
unset($field[$key]);
|
||||
continue;
|
||||
}
|
||||
if ($value == 'a.multi_color') {
|
||||
$flag4 = 1;
|
||||
unset($field[$key]);
|
||||
continue;
|
||||
}
|
||||
if ($value == 'a.is_list2') {
|
||||
$flag5 = 1;
|
||||
unset($field[$key]);
|
||||
continue;
|
||||
}
|
||||
if ($value == 'a.url') {
|
||||
$flag6 = 1;
|
||||
unset($field[$key]);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->alias('a')->join('product_category c', 'a.cid = c.id', 'LEFT');
|
||||
|
||||
// if ($flag === 0) {
|
||||
// // 如果不需要有评论的产品列表
|
||||
// $this->alias('a')
|
||||
// ->join('product_category c', 'a.cid = c.id', 'LEFT')
|
||||
// ->join('product_related r', 'a.id=r.related_product_id', 'LEFT')
|
||||
// ->join('product_dl d', 'a.id=d.product_id', 'LEFT');
|
||||
// } else {
|
||||
// // 需要有评论的产品列表
|
||||
// $this->alias('a')
|
||||
// ->join('product_category c', 'a.cid = c.id', 'LEFT')
|
||||
// ->join('product_related r', 'a.id=r.product_id', 'LEFT')
|
||||
// ->join('product_dl d', 'a.id=d.product_id', 'LEFT')
|
||||
// ->join('shopselle s', 'a.id=s.pid', 'RIGHT');
|
||||
// }
|
||||
|
||||
if (is_array($where)) {
|
||||
$this->where($where);
|
||||
}
|
||||
if ($order) {
|
||||
$this->order($order);
|
||||
}
|
||||
//$object->fetchsql(true);
|
||||
if ($count) {
|
||||
$result = $this->count();
|
||||
return $result;
|
||||
}
|
||||
if ($field) {
|
||||
$this->field($field);
|
||||
}
|
||||
if ($limit) {
|
||||
if (is_array($limit)) {
|
||||
//$limit = ['offset' => 1, 'length' => 1];
|
||||
$limit = array_values($limit);
|
||||
$this->limit($limit[0], $limit[1]);
|
||||
} else {
|
||||
$this->limit($limit);
|
||||
}
|
||||
}
|
||||
$result = $this->select();
|
||||
foreach ($result as $key => $value) {
|
||||
if($this->getAllCate($value['pid'])) {
|
||||
$result[$key]['categoryname'] = $this->getAllCate($value['pid']) . '->' . $value['categoryname'];
|
||||
}
|
||||
else{
|
||||
$result[$key]['categoryname'] = $value['categoryname'];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
foreach ($result as $key => $value) {
|
||||
if ($flag2 === 1) {
|
||||
// 有关联产品
|
||||
$related_product_list = model('product_related')->alias('a')->join('product b', 'a.related_product_id = b.id', 'LEFT')->where(['a.stat' => 0, 'b.stat' => 0, 'a.product_id' => $value['id']])->field('b.brand_id')->select();
|
||||
if (!empty($related_product_list)) {
|
||||
$related_product = '';
|
||||
foreach ($related_product_list as $k => $v) {
|
||||
$related_product .= $v['brand_id'] . ', ';
|
||||
}
|
||||
$result[$key]['related_product'] = rtrim($related_product, ', ');
|
||||
} else {
|
||||
$result[$key]['related_product'] = '';
|
||||
}
|
||||
|
||||
}
|
||||
if ($flag3 == 1) {
|
||||
// 有相关驱动
|
||||
$driver = model('product_dl')->where(['product_id' => $value['id'], 'stat' => 0])->limit(1)->select();
|
||||
if (!empty($driver)) {
|
||||
$result[$key]['driver'] = 1;
|
||||
} else {
|
||||
$result[$key]['driver'] = 0;
|
||||
}
|
||||
}
|
||||
if ($flag4 == 1) {
|
||||
// 有多颜色
|
||||
$count_product_two_img = model('product_two_img')->where(['product_id' => $value['id'], 'stat' => 0])->count();
|
||||
if ($count_product_two_img > 1) {
|
||||
$result[$key]['multi_color'] = 1;
|
||||
} else {
|
||||
$result[$key]['multi_color'] = 0;
|
||||
}
|
||||
}
|
||||
if ($flag5 == 1) {
|
||||
// 有二级列表图
|
||||
if ($count_product_two_img > 0) {
|
||||
$result[$key]['is_list2'] = 1;
|
||||
} else {
|
||||
$count_product_two_img = model('product_two_img')->where(['product_id' => $value['id'], 'stat' => 0])->count();
|
||||
if ($count_product_two_img > 0) {
|
||||
$result[$key]['is_list2'] = 1;
|
||||
} else {
|
||||
$result[$key]['is_list2'] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($flag6 == 1) {
|
||||
// 有链接
|
||||
$result[$key]['url'] = $_SERVER['HTTP_HOST'] . '/product/detail/' . $value['id'] . '.html';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getRecycleLists($where = null, $order = null, $field = null, $limit = null) {
|
||||
$this->alias('a')
|
||||
->join('product_category c', 'a.cid=c.id', 'LEFT')
|
||||
->join('user u', 'a.id=u.id', 'LEFT');
|
||||
if ($where) {
|
||||
$this->where($where);
|
||||
}
|
||||
if ($order) {
|
||||
$this->order($order);
|
||||
}
|
||||
if ($field) {
|
||||
$this->field($field);
|
||||
}
|
||||
if (empty($limit)) {
|
||||
$limit = Config::get('list_rows');
|
||||
}
|
||||
//$this->fetchsql(true);
|
||||
$object = $this->paginate($limit);
|
||||
// header("content-type:text/html;charset=utf8;");
|
||||
// print_r($object);
|
||||
// exit;
|
||||
return $object;
|
||||
}
|
||||
|
||||
// 属性修改器 创建时间
|
||||
protected function setCreatetimeAttr($value, $data) {
|
||||
return time();
|
||||
}
|
||||
|
||||
// 属性修改器 更新时间
|
||||
protected function setUpdatetimeAttr($value, $data) {
|
||||
return time();
|
||||
}
|
||||
|
||||
// 属性修改器 设置注册ip
|
||||
protected function setIpAttr() {
|
||||
return Request::instance()->ip();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user