CRMEB/application/routine/model/store/StoreProduct.php
2018-09-10 09:04:02 +08:00

148 lines
4.5 KiB
PHP

<?php
/**
*
* @author: xaboy<365615158@qq.com>
* @day: 2017/12/12
*/
namespace app\routine\model\store;
use app\admin\model\store\StoreProductAttrValue as StoreProductAttrValuemodel;
use basic\ModelBasic;
use traits\ModelTrait;
class StoreProduct extends ModelBasic
{
use ModelTrait;
protected function getSliderImageAttr($value)
{
return json_decode($value,true)?:[];
}
public static function getValidProduct($productId,$field = '*')
{
return self::where('is_del',0)->where('is_show',1)->where('id',$productId)->field($field)->find();
}
public static function validWhere()
{
return self::where('is_del',0)->where('is_show',1)->where('mer_id',0);
}
/**
* 新品产品
* @param string $field
* @param int $limit
* @return false|\PDOStatement|string|\think\Collection
*/
public static function getNewProduct($field = '*',$limit = 0)
{
$model = self::where('is_new',1)->where('is_del',0)->where('mer_id',0)
->where('stock','>',0)->where('is_show',1)->field($field)
->order('sort DESC, id DESC');
if($limit) $model->limit($limit);
return $model->select();
}
/**
* 热卖产品
* @param string $field
* @param int $limit
* @return false|\PDOStatement|string|\think\Collection
*/
public static function getHotProduct($field = '*',$limit = 0)
{
$model = self::where('is_hot',1)->where('is_del',0)->where('mer_id',0)
->where('stock','>',0)->where('is_show',1)->field($field)
->order('sort DESC, id DESC');
if($limit) $model->limit($limit);
return $model->select();
}
/**
* 热卖产品
* @param string $field
* @param int $limit
* @return false|\PDOStatement|string|\think\Collection
*/
public static function getHotProductLoading($field = '*',$offset = 0,$limit = 0)
{
$model = self::where('is_hot',1)->where('is_del',0)->where('mer_id',0)
->where('stock','>',0)->where('is_show',1)->field($field)
->order('sort DESC, id DESC');
if($limit) $model->limit($offset,$limit);
return $model->select();
}
/**
* 精品产品
* @param string $field
* @param int $limit
* @return false|\PDOStatement|string|\think\Collection
*/
public static function getBestProduct($field = '*',$limit = 0)
{
$model = self::where('is_best',1)->where('is_del',0)->where('mer_id',0)
->where('stock','>',0)->where('is_show',1)->field($field)
->order('sort DESC, id DESC');
if($limit) $model->limit($limit);
return $model->select();
}
/**
* 优惠产品
* @param string $field
* @param int $limit
* @return false|\PDOStatement|string|\think\Collection
*/
public static function getBenefitProduct($field = '*',$limit = 0)
{
$model = self::where('is_benefit',1)
->where('is_del',0)->where('mer_id',0)->where('stock','>',0)
->where('is_show',1)->field($field)
->order('sort DESC, id DESC');
if($limit) $model->limit($limit);
return $model->select();
}
public static function cateIdBySimilarityProduct($cateId,$field='*',$limit = 0)
{
$pid = StoreCategory::cateIdByPid($cateId)?:$cateId;
$cateList = StoreCategory::pidByCategory($pid,'id') ?:[];
$cid = [$pid];
foreach ($cateList as $cate){
$cid[] = $cate['id'];
}
$model = self::where('cate_id','IN',$cid)->where('is_show',1)->where('is_del',0)
->field($field)->order('sort DESC,id DESC');
if($limit) $model->limit($limit);
return $model->select();
}
public static function isValidProduct($productId)
{
return self::be(['id'=>$productId,'is_del'=>0,'is_show'=>1]) > 0;
}
public static function getProductStock($productId,$uniqueId = '')
{
return $uniqueId == '' ?
self::where('id',$productId)->value('stock')?:0
: StoreProductAttr::uniqueByStock($uniqueId);
}
public static function decProductStock($num,$productId,$unique = '')
{
if($unique){
$res = false !== StoreProductAttrValuemodel::decProductAttrStock($productId,$unique,$num);
$res = $res && self::where('id',$productId)->setInc('sales',$num);
}else{
$res = false !== self::where('id',$productId)->dec('stock',$num)->inc('sales',$num)->update();
}
return $res;
}
}