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.

129 lines
4.5 KiB

<?php
namespace app\model;
use app\BaseModel;
class Goods extends BaseModel
{
public function goodsDetail(){
return $this->hasOne(GoodsDetail::class, 'id', 'goods_detail_id')
->bind(['goods_name', 'goods_image', 'total_inventory', 'surplus_inventory', 'goods_ownership', 'goods_ownership_str', 'goods_type', 'charges_type',
'goods_entrust', 'goods_exceptions', 'entrust_month', 'earnest_money', 'canceltime', 'stock',
'contractual_period', 'contract', 'transaction_class', 'otherIdentifiers', 'classification', 'price']);
}
// public function goodsData(){
// return $this->hasMany(GoodsSource::class, "goods_detail_id", 'goods_id');
// }
public function User(){
return $this->hasOne(User::class, 'user_isli', 'user_islicode')
->bind(['username', 'agency_type', 'attest_status', 'registertime', 'attesttime']);
}
public function searchGoodsNameAttr($query, $value){
if($value){
$query->whereLike('goodsDetail.goods_name', '%'.$value.'%');
}
}
public function searchUserNameAttr($query, $value){
if($value){
$query->whereLike('goods.username', '%'.$value.'%');
}
}
public function searchGoodsIsLiCodeAttr($query, $value){
if($value){
$query->whereIn('goods.goods_islicode', $value);
}
}
public function searchOrderAttr($query, $value){
if ($value){
$query->orderRaw($value);
}else{
$query->order('goods.id','desc');
}
}
public function searchCreatetimeAttr($query, $value){
if (isset($value[1]) && preg_match("/^\d{4}-\d{2}-\d{2}$/", $value[1])){
$value[1] = $value[1] . ' 23:59:59';
}
if (isset($value[0]) && isset($value[1]) && $value[0] && $value[1]){
$query->whereBetweenTime('goods.createtime', $value[0], $value[1]);
}elseif (isset($value[0]) && $value[0]){
$query->whereTime('goods.createtime', '>=', $value[0]);
}elseif (isset($value[1]) && $value[1]){
$query->whereTime('goods.createtime', '<=', $value[1]);
}
}
public function searchGoodsStatusAttr($query, $value){
if($value){
$query->whereIn('goods.goods_status', $value);
}
}
public function searchSearchUserAttr($query, $value){
if($value){
$query->where(function($query) use($value){
$query->whereLike("goods.username", "%{$value}%", "or")->whereLike("goods.user_islicode", "%{$value}%", "or");
});
}
}
public function searchGoodsAttr($query, $value){
if($value){
$query->where(function($query) use($value){
$query->whereLike("goodsDetail.goods_name", "%{$value}%", "or")->whereLike("goods.goods_islicode", "%{$value}%", "or");
});
}
}
public function searchGoodsFieldAttr($query, $value){
if($value){
$query->whereLike("goodsDetail.join_field", "%{$value}%");
}
}
public function list(array $search = [], array $where = [], $type = null, array $with = [], $limit = null){
$this->fields = ['id', 'user_id', 'goods_detail_id', 'goods_islicode', 'username', 'createtime', 'updatetime', 'goods_status', 'is_deleted', 'user_islicode', 'sale_count', 'click_count', 'recommend_sort', 'is_recommend', 'entrust_status', "username"];
$result = $this->field($this->fields)->order('goods.recommend_sort', 'desc')->alias('goods');
$withData = [];
if($search){
$result = $result->withSearch($search[0], $search[1]);
}
if ($with){ // 自定义with数据
$with = $this->buildWith($with, $withData);
if ($this->withJoin){
$result = $result->withJoin($with, $this->withJoin === true ? 'INNER' : $this->withJoin);
}else{
$result = $result->with($with);
}
}
if($where){
$result = $result->where($where);
}
// 执行查询
if ($type == 'fetchSql' || $type == 'sql'){
$result = $result->fetchSql(true)->select();
}elseif (in_array($type, ['select', 'find', 'count'])){
if(!empty($limit)){
$result = $result->limit($limit)->$type();
}else{
$result = $result->$type();
}
}else{
$result = $result->paginate($type ?: 20);
}
return $result;
}
}