235 lines
6.9 KiB
PHP
235 lines
6.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @author Any
|
|
* @description KISS
|
|
* @date 2021年10月5日
|
|
* @version 1.0.0
|
|
*
|
|
* _____LOG_____
|
|
*
|
|
*/
|
|
|
|
namespace app\models\common;
|
|
|
|
use app\models\Goods;
|
|
use app\models\GoodsAttr;
|
|
use app\models\GoodsCat;
|
|
use app\models\GoodsHub;
|
|
use app\models\integral\mall\IntegralMallGoods;
|
|
use app\models\Cat;
|
|
use app\models\Model;
|
|
use yii\data\Pagination;
|
|
use app\components\SiteHelper;
|
|
use app\components\SysConst;
|
|
|
|
|
|
class CommonGoodsListForm extends Model
|
|
{
|
|
public $keywords;
|
|
public $limit;
|
|
public $page;
|
|
|
|
public $cat_id;
|
|
public $cx_mch_id;
|
|
public $status;
|
|
public $goods_id;
|
|
|
|
public $start_time;
|
|
public $end_time;
|
|
|
|
|
|
public $plugin_sign;
|
|
|
|
private $fields;
|
|
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['keywords', 'plugin_sign',], 'trim'],
|
|
[['keywords', 'plugin_sign',], 'string'],
|
|
[['limit', 'page', 'cx_mch_id', 'status', 'goods_id', 'cat_id'], 'integer'],
|
|
[['page'], 'default', 'value' => 1],
|
|
[['limit'], 'default', 'value' => 20],
|
|
[['status'], 'in', 'range' => [0, 1, 2]],
|
|
[['cx_mch_id'], 'required'],
|
|
[['start_time', 'end_time',], 'safe'],
|
|
[['goods_id'], 'required', 'on' => 'detail'],
|
|
];
|
|
}
|
|
|
|
public function search()
|
|
{
|
|
if (!$this->validate()) {
|
|
return $this->getModelError();
|
|
}
|
|
$cat_query = GoodsCat::find()
|
|
->where([
|
|
'is_delete' => 0,
|
|
])
|
|
->select('goods_hub_id,cat_id');
|
|
$query = $this->getQuery($cat_query);
|
|
$query = $query->where([
|
|
'g.cx_mch_id' => $this->cx_mch_id,
|
|
'g.is_delete' => 0,
|
|
'gh.is_delete' => 0,
|
|
// 'g.plugin_sign' => $this->plugin_sign,
|
|
])
|
|
->andFilterWhere([
|
|
'OR',
|
|
['LIKE', 'gh.title', $this->keywords],
|
|
['LIKE', 'gh.subtitle', $this->keywords],
|
|
])
|
|
->andFilterWhere([
|
|
'g.id' => $this->goods_id,
|
|
'gc.cat_id' => $this->cat_id
|
|
]);
|
|
if ($this->status == 2) {
|
|
$query = $query->andWhere(['g.goods_stock' => 0]);//售罄
|
|
} else {
|
|
$query = $query->andFilterWhere(['g.status' => $this->status]);
|
|
}
|
|
$query = $this->switchTime($query);
|
|
$query = $this->getFields($query);
|
|
$count_query = clone $query;
|
|
$count = $count_query->count();
|
|
$pagination = new Pagination(['pageSize' => $this->limit, 'totalCount' => $count, 'page' => $this->page - 1]);
|
|
$list = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy(['g.sort' => SORT_ASC, 'g.created_at' => SORT_DESC])->asArray()->all();
|
|
|
|
|
|
$list = $this->handleList($list);
|
|
//是否已经全部加载
|
|
$end_flag = $this->page > $pagination->pageCount ? true : false;
|
|
return [
|
|
'code' => 0,
|
|
'msg' => 'ok',
|
|
'data' => $list,
|
|
'count' => $count,
|
|
'page_size' => $this->limit,
|
|
'page_count' => $pagination->pageCount,
|
|
'page_no' => $this->page,
|
|
'end_flag' => $end_flag
|
|
];
|
|
}
|
|
|
|
protected function getQuery($cat_query)
|
|
{
|
|
$query = Goods::find()->alias('g')
|
|
->leftJoin(['gh' => GoodsHub::tableName()], 'gh.id=g.goods_hub_id')
|
|
->leftJoin(['gc' => $cat_query], 'gc.goods_hub_id=gh.id');
|
|
return $query;
|
|
}
|
|
|
|
public function setFields($fields)
|
|
{
|
|
$this->fields = $fields;
|
|
}
|
|
|
|
protected function getFields($query)
|
|
{
|
|
$fields = is_array($this->fields) ? implode(',', $this->fields) : $this->fields;
|
|
return $query->select($fields);
|
|
}
|
|
|
|
protected function handleList($list)
|
|
{
|
|
foreach ($list as $index => $item) {
|
|
$item['status_cn'] = $item['status'] == Goods::STATUS_ONLINE ? '已上架' : '已下架';
|
|
$item['created_at_cn'] = date('Y-m-d H:i:s', $item['created_at']);
|
|
$item['cat_list'] = $this->getCatList($item['goods_hub_id']);
|
|
$item['cover_pic'] = SiteHelper::getFullUrl($item['cover_pic']);
|
|
if ($this->scenario == 'detail') {
|
|
$item['detail'] = SiteHelper::repairContent($item['detail']);
|
|
$item['pic_urls'] = $this->fixPicUrls($item['pic_urls']);
|
|
$item['video_url'] = SiteHelper::getFullUrl($item['video_url']);
|
|
$item['goods_sales'] = $item['virtaul_sales'] + $item['sales'];
|
|
$item['attr_groups'] = empty($item['attr_groups']) ? [] : json_decode($item['attr_groups'], true);
|
|
$item['attr'] = $this->getAttr($item['id']);
|
|
}
|
|
$list[$index] = $item;
|
|
}
|
|
return $list;
|
|
}
|
|
|
|
|
|
protected function switchTime($query)
|
|
{
|
|
if (!empty($this->start_time)) {
|
|
$query = $query->andWhere([
|
|
'>',
|
|
'g.created_at',
|
|
strtotime($this->start_time)
|
|
]);
|
|
}
|
|
if (!empty($this->end_time)) {
|
|
$query = $query->andWhere([
|
|
'<',
|
|
'g.created_at',
|
|
strtotime($this->end_time)
|
|
]);
|
|
}
|
|
return $query;
|
|
}
|
|
|
|
protected function getCatList($goods_hub_id)
|
|
{
|
|
switch ($this->plugin_sign) {
|
|
case SysConst::$cxPluginSceneMall:
|
|
$type = Cat::TYPE_GOODS;
|
|
break;
|
|
case SysConst::$cxPluginSceneIntegralMall:
|
|
$type = Cat::TYPE_INTEGRAL_GOODS;
|
|
break;
|
|
//@TODO 其他
|
|
default:
|
|
$type = Cat::TYPE_GOODS;
|
|
}
|
|
$list = GoodsCat::find()->alias('gc')
|
|
->leftJoin(['c' => Cat::tableName()], 'c.id=gc.cat_id')
|
|
->where([
|
|
'gc.is_delete' => 0,
|
|
'gc.goods_hub_id' => $goods_hub_id,
|
|
'c.is_delete' => 0,
|
|
'c.type' => $type,
|
|
])
|
|
->select('c.name,c.id as cat_id')->asArray()->all();
|
|
return $list;
|
|
}
|
|
|
|
protected function fixPicUrls($pic_urls)
|
|
{
|
|
$pic_urls = is_string($pic_urls) && !empty($pic_urls) ? json_decode($pic_urls, true) : $pic_urls;
|
|
foreach ($pic_urls as $index => $item) {
|
|
$item = SiteHelper::getFullUrl($item);
|
|
$pic_urls[$index] = $item;
|
|
}
|
|
return $pic_urls;
|
|
}
|
|
|
|
//获取商品规格
|
|
private function getAttr($goods_id)
|
|
{
|
|
$attr = GoodsAttr::find()->alias('gr')
|
|
->where([
|
|
'gr.goods_id' => $goods_id,
|
|
'gr.is_delete' => 0
|
|
])
|
|
->select('gr.id,gr.goods_id,gr.price,gr.sign_id,gr.stock,gr.serial_no,gr.weight,gr.cover_pic')
|
|
->asArray()->all();
|
|
foreach ($attr as $index => $item) {
|
|
$item['cover_pic'] = SiteHelper::getFullUrl($item['cover_pic']);
|
|
$attr[$index] = $item;
|
|
}
|
|
return $attr;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|