123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558 |
- <?php
- // +----------------------------------------------------------------------
- // | Niucloud-admin 企业快速开发的多应用管理平台
- // +----------------------------------------------------------------------
- // | 官方网址:https://www.niucloud.com
- // +----------------------------------------------------------------------
- // | niucloud团队 版权所有 开源版本可自由商用
- // +----------------------------------------------------------------------
- // | Author: Niucloud Team
- // +----------------------------------------------------------------------
- namespace addon\mall\app\model\goods;
- use addon\mall\app\dict\coupon\CouponDict;
- use addon\mall\app\dict\goods\GoodsDict;
- use addon\mall\app\model\coupon\Coupon;
- use addon\mall\app\model\coupon\CouponGoods;
- use app\dict\sys\FileDict;
- use app\model\shop\Shop;
- use app\model\site\Site;
- use core\base\BaseModel;
- use think\model\concern\SoftDelete;
- use think\model\relation\HasMany;
- use think\model\relation\HasOne;
- /**
- * 商品模型
- * Class Goods
- * @package addon\mall\app\model\goods
- */
- class Goods extends BaseModel
- {
- use SoftDelete;
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'goods_id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'mall_goods';
- /**
- * 定义软删除标记字段.
- * @var string
- */
- protected $deleteTime = 'delete_time';
- /**
- * 定义软删除字段的默认值
- * @var int
- */
- protected $defaultSoftDelete = 0;
- // 设置json类型字段
- protected $json = [ 'goods_category', 'label_ids', 'service_ids', 'delivery_type', 'audit_info', 'attr_format' ];
- // 设置JSON数据返回数组
- protected $jsonAssoc = true;
- /**
- * 状态字段转化
- * @param $value
- * @param $data
- * @return mixed
- */
- public function getGoodsTypeNameAttr($value, $data)
- {
- if (!empty($data[ 'goods_type' ])) {
- return GoodsDict::getType($data[ 'goods_type' ])[ 'name' ] ?? '';
- }
- return '';
- }
- /**
- * 状态字段转化
- * @param $value
- * @param $data
- * @return mixed
- */
- public function getGoodsEditPathAttr($value, $data)
- {
- if (!empty($data[ 'goods_type' ])) {
- return GoodsDict::getType($data[ 'goods_type' ])[ 'path' ] ?? '';
- }
- return '';
- }
- /**
- * 获取封面缩略图(小)
- */
- public function getGoodsCoverThumbSmallAttr($value, $data)
- {
- if (isset($data[ 'goods_cover' ]) && $data[ 'goods_cover' ] != '') {
- return get_thumb_images($data[ 'site_id' ], $data[ 'goods_cover' ], FileDict::SMALL);
- }
- return [];
- }
- /**
- * 获取封面缩略图(中)
- */
- public function getGoodsCoverThumbMidAttr($value, $data)
- {
- if (isset($data[ 'goods_cover' ]) && $data[ 'goods_cover' ] != '') {
- return get_thumb_images($data[ 'site_id' ], $data[ 'goods_cover' ], FileDict::MID);
- }
- return [];
- }
- /**
- * 获取封面缩略图(大)
- */
- public function getGoodsCoverThumbBigAttr($value, $data)
- {
- if (isset($data[ 'goods_cover' ]) && $data[ 'goods_cover' ] != '') {
- return get_thumb_images($data[ 'site_id' ], $data[ 'goods_cover' ], FileDict::BIG);
- }
- return [];
- }
- /**
- * 获取商品图片缩略图(小)
- */
- public function getGoodsImageThumbSmallAttr($value, $data)
- {
- if (isset($data[ 'goods_image' ]) && $data[ 'goods_image' ] != '') {
- $goods_image = explode(',', $data[ 'goods_image' ]);
- $img_arr = [];
- foreach ($goods_image as $k => $v) {
- $img = get_thumb_images($data[ 'site_id' ], $v, FileDict::SMALL);
- if (!empty($img)) {
- $img_arr[] = $img;
- }
- }
- return $img_arr;
- }
- return [];
- }
- /**
- * 获取商品图片缩略图(中)
- */
- public function getGoodsImageThumbMidAttr($value, $data)
- {
- if (isset($data[ 'goods_image' ]) && $data[ 'goods_image' ] != '') {
- $goods_image = explode(',', $data[ 'goods_image' ]);
- $img_arr = [];
- foreach ($goods_image as $k => $v) {
- $img = get_thumb_images($data[ 'site_id' ], $v, FileDict::MID);
- if (!empty($img)) {
- $img_arr[] = $img;
- }
- }
- return $img_arr;
- }
- return [];
- }
- /**
- * 获取商品图片缩略图(大)
- */
- public function getGoodsImageThumbBigAttr($value, $data)
- {
- if (isset($data[ 'goods_image' ]) && $data[ 'goods_image' ] != '') {
- $goods_image = explode(',', $data[ 'goods_image' ]);
- $img_arr = [];
- foreach ($goods_image as $k => $v) {
- $img = get_thumb_images($data[ 'site_id' ], $v, FileDict::BIG);
- if (!empty($img)) {
- $img_arr[] = $img;
- }
- }
- return $img_arr;
- }
- return [];
- }
- /**
- * 获取商品分类
- */
- public function getGoodsCategoryAttr($value, $data)
- {
- if (!is_array($value)) {
- $value = json_decode($value, true);
- }
- if (!empty($value)) {
- return array_map(function($item) {
- return (int) $item;
- }, $value);
- }
- return [];
- }
- /**
- * 搜索器:商品商品id
- * @param $value
- * @param $data
- */
- public function searchGoodsIdAttr($query, $value, $data)
- {
- if ($value) {
- $query->where('goods_id', $value);
- }
- }
- /**
- * 搜索器:商品商品名称
- * @param $value
- * @param $data
- */
- public function searchGoodsNameAttr($query, $value, $data)
- {
- if ($value) {
- $query->where('goods_name', 'like', '%' . $value . '%');
- }
- }
- /**
- * 搜索器:商品副标题
- * @param $value
- * @param $data
- */
- public function searchSubTitleAttr($query, $value, $data)
- {
- if ($value) {
- $query->where('sub_title', 'like', '%' . $value . '%');
- }
- }
- /**
- * 搜索器:关键词
- * @param $value
- * @param $data
- */
- public function searchKeywordAttr($query, $value, $data)
- {
- if ($value) {
- $query->whereLike('goods_name|sub_title', '%' . $value . '%');
- }
- }
- /**
- * 搜索器:商品商品类型
- * @param $value
- * @param $data
- */
- public function searchGoodsTypeAttr($query, $value, $data)
- {
- if ($value) {
- $query->where('goods_type', $value);
- }
- }
- /**
- * 搜索器:商品商品品牌id
- * @param $value
- * @param $data
- */
- public function searchBrandIdAttr($query, $value, $data)
- {
- if ($value) {
- $query->where('brand_id', $value);
- }
- }
- /**
- * 搜索器:商品商品品牌id(关联查询使用)
- * @param $value
- * @param $data
- */
- public function searchSiteIdAttr($query, $value, $data)
- {
- if ($value) {
- $query->where('goods.site_id', $value);
- }
- }
- /**
- * 平台商品分类查询
- * @param $query
- * @param $value
- * @param $data
- */
- public function searchGoodsMallCategoryAttr($query, $value, $data)
- {
- if ($value) {
- if (is_array($value)) {
- $value = end($value);
- }
- //查询分类信息
- $goods_category_model = new Category();
- $category_info = $goods_category_model->where([ [ 'category_id', '=', $value ] ])->field('category_id,level,pid')->findOrEmpty()->toArray();
- if ($category_info[ 'level' ] == 3) {
- $query->where('goods_mall_category', $value);
- } elseif ($category_info[ 'level' ] == 2) {
- $category_ids = $value;
- $category_list = $goods_category_model->where([ [ 'pid', '=', $value ] ])->field('category_id,level,pid')->select()->toArray();
- if (!empty($category_list)) {
- foreach ($category_list as $k => $v) {
- $category_ids = $category_ids . ',' . $v[ 'category_id' ];
- }
- }
- $query->where('goods_mall_category', 'in', $category_ids);
- } else {
- // 一级
- $category_ids = $value;
- $child_list = $goods_category_model->where([ [ 'pid', '=', $value ] ])->field('category_id,level,pid')->select()->toArray();
- if (!empty($child_list)) {
- $child_ids = '';
- foreach ($child_list as $k => $v) {
- $category_ids = $category_ids . ',' . $v[ 'category_id' ];
- $child_ids = $child_ids == '' ? $v[ 'category_id' ] : $child_ids . ',' . $v[ 'category_id' ];
- }
- $grand_child_list = $goods_category_model->where([ [ 'pid', 'in', $child_ids ] ])->field('category_id,level')->select()->toArray();
- if (!empty($grand_child_list)) {
- foreach ($grand_child_list as $k_child => $v_child) {
- $category_ids = $category_ids . ',' . $v_child[ 'category_id' ];
- }
- }
- $query->where('goods_mall_category', 'in', $category_ids);
- } else {
- $query->where('goods_mall_category', '=', $category_ids);
- }
- }
- }
- }
- /**
- * 搜索器:商品商品分类
- * @param $value
- * @param $data
- */
- public function searchGoodsCategoryAttr($query, $value, $data)
- {
- if ($value) {
- if (is_array($value)) {
- $temp_where = array_map(function($item) { return '%"' . $item . '"%'; }, $value);
- } else {
- $temp_where = [ '%"' . $value . '"%' ];
- }
- $query->where('goods_category', 'like', $temp_where, 'or');
- }
- }
- /**
- * 搜索器:商品标签组
- * @param $value
- * @param $data
- */
- public function searchLabelIdsAttr($query, $value, $data)
- {
- if ($value) {
- if (is_array($value)) {
- $temp_where = array_map(function($item) { return '%"' . $item . '"%'; }, $value);
- } else {
- $temp_where = [ '%"' . $value . '"%' ];
- }
- $query->where('label_ids', 'like', $temp_where, 'or');
- }
- }
- /**
- * 搜索器:商品服务
- * @param $value
- * @param $data
- */
- public function searchServiceIdsAttr($query, $value, $data)
- {
- if ($value) {
- if (is_array($value)) {
- $temp_where = array_map(function($item) { return '%"' . $item . '"%'; }, $value);
- } else {
- $temp_where = [ '%"' . $value . '"%' ];
- }
- $query->where('service_ids', 'like', $temp_where, 'or');
- }
- }
- /**
- * 搜索器:商品销量
- * @param $value
- * @param $data
- */
- public function searchSaleNumAttr($query, $value, $data)
- {
- if (!empty($data[ 'start_sale_num' ]) && !empty($data[ 'end_sale_num' ])) {
- $money = [ $data[ 'start_sale_num' ], $data[ 'end_sale_num' ] ];
- sort($money);
- $query->where('goods.sale_num', 'between', $money);
- } elseif (!empty($data[ 'start_sale_num' ])) {
- $query->where('goods.sale_num', '>=', $data[ 'start_sale_num' ]);
- } elseif (!empty($data[ 'end_sale_num' ])) {
- $query->where('goods.sale_num', '<=', $data[ 'end_sale_num' ]);
- }
- }
- /**
- * 搜索器:商品sku是否默认
- * @param $value
- * @param $data
- */
- public function searchGoodsSkuIsDefaultAttr($query, $value, $data)
- {
- if ($value) {
- $query->where('goodsSku.is_default', $value);
- }
- }
- /**
- * 搜索器:商品sku价格
- * @param $value
- * @param $data
- */
- public function searchGoodsSkuPriceAttr($query, $value, $data)
- {
- if (!empty($data[ 'start_price' ]) && !empty($data[ 'end_price' ])) {
- $money = [ $data[ 'start_price' ], $data[ 'end_price' ] ];
- sort($money);
- $query->where('goodsSku.price', 'between', $money);
- } elseif (!empty($data[ 'start_price' ])) {
- $query->where('goodsSku.price', '>=', $data[ 'start_price' ]);
- } elseif (!empty($data[ 'end_price' ])) {
- $query->where('goodsSku.price', '<=', $data[ 'end_price' ]);
- }
- }
- /**
- * 搜索器:优惠券id(查询适用优惠券的商品列表)
- * @param $value
- * @param $data
- */
- public function searchCouponIdAttr($query, $value, $data)
- {
- if ($value) {
- $coupon_goods_model = new CouponGoods();
- $coupon = (new Coupon())->where([ [ 'id', '=', $value ] ])->field('site_id, is_all_site_join, join_site_ids')->findOrEmpty()->toArray();
- $coupon_list = $coupon_goods_model->where([ [ 'coupon_id', '=', $value ] ])->field('goods_id,category_id,mall_category_id,brand_id')->select()->toArray();
- if (!empty($coupon_list)) {
- $goods_ids = array_values(array_filter(array_column($coupon_list, 'goods_id')));
- $category_ids = array_values(array_filter(array_column($coupon_list, 'category_id')));
- $mall_category_ids = array_values(array_filter(array_column($coupon_list, 'mall_category_id')));
- $brand_ids = array_values(array_filter(array_column($coupon_list, 'brand_id')));
- if (!empty($goods_ids)) {
- $query->where('goods.goods_id', 'in', $goods_ids);
- } elseif (!empty($category_ids)) {
- $like_arr = [];
- foreach ($category_ids as $v) {
- $like_arr[] = '%' . $v . '%';
- }
- $query->where('goods_category', 'like', $like_arr, 'or');
- } elseif (!empty($mall_category_ids)) {
- $query->where('goods_mall_category', 'in', $mall_category_ids);
- } elseif (!empty($brand_ids)) {
- $query->where('brand_id', 'in', $brand_ids);
- }
- }
- if (!empty($coupon)) {
- if ($coupon['site_id']) {
- $query->where(function ($query) use ($coupon){
- $query->where('goods.site_id', '=', $coupon['site_id']);
- });
- } else {
- if ($coupon['is_all_site_join'] == CouponDict::NOT_ALL_SITE_JOIN) {
- $query->where(function ($query) use ($coupon){
- $query->where('goods.site_id', 'in', $coupon['join_site_ids']);
- });
- }
- }
- }
- }
- }
- /**
- * 搜索器:商品商品状态(1.正常0下架)
- * @param $value
- * @param $data
- */
- public function searchStatusAttr($query, $value, $data)
- {
- if ($value !== '') {
- $query->where('goods.status', $value);
- }
- }
- /**
- * 搜索器:供应商id
- * @param $value
- * @param $data
- */
- public function searchSupplierIdAttr($query, $value, $data)
- {
- if ($value) {
- $query->where('supplier_id', $value);
- }
- }
- /**
- * 关联默认商品规格
- * @return HasOne
- */
- public function goodsSku()
- {
- return $this->hasOne(GoodsSku::class, 'goods_id', 'goods_id');
- }
- /**
- * 关联商品规格列表
- * @return HasMany
- */
- public function skuList()
- {
- return $this->hasMany(GoodsSku::class, 'goods_id', 'goods_id');
- }
- /**
- * 关联站点信息
- * @return HasOne
- */
- public function site()
- {
- return $this->hasOne(Site::class, 'site_id', 'site_id');
- }
- /**
- * 关联店铺信息
- * @return HasOne
- */
- public function shop()
- {
- return $this->hasOne(Shop::class, 'site_id', 'site_id');
- }
- /**
- * 关联商品规格列表
- * @return HasMany
- */
- public function goodsSpec()
- {
- return $this->hasMany(GoodsSpec::class, 'goods_id', 'goods_id');
- }
- /**
- * 关联默认商品规格
- * @return HasOne
- */
- public function brand()
- {
- return $this->hasOne(Brand::class, 'brand_id', 'brand_id')
- ->joinType('left')
- ->withField('brand_id, brand_name, logo, desc');
- }
- }
|