154 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			PHP
		
	
	
			
		
		
	
	
			154 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			PHP
		
	
	
| <?php
 | ||
| // +----------------------------------------------------------------------
 | ||
| // | likeshop开源商城系统
 | ||
| // +----------------------------------------------------------------------
 | ||
| // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
 | ||
| // | gitee下载:https://gitee.com/likeshop_gitee
 | ||
| // | github下载:https://github.com/likeshop-github
 | ||
| // | 访问官网:https://www.likeshop.cn
 | ||
| // | 访问社区:https://home.likeshop.cn
 | ||
| // | 访问手册:http://doc.likeshop.cn
 | ||
| // | 微信公众号:likeshop技术社区
 | ||
| // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
 | ||
| // |  likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
 | ||
| // | 禁止对系统程序代码以任何目的,任何形式的再发布
 | ||
| // | likeshop团队版权所有并拥有最终解释权
 | ||
| // +----------------------------------------------------------------------
 | ||
| // | author: likeshop.cn.team
 | ||
| // +----------------------------------------------------------------------
 | ||
| 
 | ||
| 
 | ||
| namespace app\common\model\goods;
 | ||
| 
 | ||
| use app\common\basics\Models;
 | ||
| use app\common\model\distribution\DistributionGoods;
 | ||
| 
 | ||
| 
 | ||
| /**
 | ||
|  * 商品-模型
 | ||
|  * Class Goods
 | ||
|  * @package app\common\model\goods
 | ||
|  */
 | ||
| class Goods extends Models
 | ||
| {
 | ||
|     /**
 | ||
|      * 商品轮播图 关联模型
 | ||
|      */
 | ||
|     public function GoodsImage()
 | ||
|     {
 | ||
|         return $this->hasMany('GoodsImage', 'goods_id', 'id')->field('goods_id, uri');
 | ||
|     }
 | ||
|     /**
 | ||
|      * 商品SKU 关联模型
 | ||
|      */
 | ||
|     public function GoodsItem()
 | ||
|     {
 | ||
|         return $this->hasMany('GoodsItem', 'goods_id', 'id')
 | ||
|             ->field('id, goods_id, image, spec_value_ids, spec_value_str, market_price, price, stock, chengben_price');
 | ||
|     }
 | ||
| 
 | ||
|     /**
 | ||
|      * 店铺 关联模型
 | ||
|      */
 | ||
|     public function Shop()
 | ||
|     {
 | ||
|         return $this->hasOne(\app\common\model\shop\Shop::class, 'id', 'shop_id')
 | ||
|             ->field('id, name, logo, type, star, score, intro,is_pay, mobile');
 | ||
|     }
 | ||
| 
 | ||
|     public function getIsDistributionDescAttr($value, $data)
 | ||
|     {
 | ||
|         return $data['is_distribution'] ? '是': '否';
 | ||
|     }
 | ||
| 
 | ||
|     /**
 | ||
|      * 根据商品id获取商品名称
 | ||
|      */
 | ||
|     public function getGoodsNameById($goods_id)
 | ||
|     {
 | ||
|         return $this->where('id',$goods_id)->value('name');
 | ||
| 
 | ||
|     }
 | ||
| 
 | ||
|     /**
 | ||
|      * 根据商品id查询商品是否上架
 | ||
|      */
 | ||
|     public function checkStatusById($goods_id)
 | ||
|     {
 | ||
|         $status = $this
 | ||
|             ->where([
 | ||
|                 ['id','=',$goods_id],
 | ||
|                 ['del','=',0],
 | ||
|             ])
 | ||
|             ->value('status');
 | ||
|         if ($status){
 | ||
|             if ($status == 1){
 | ||
|                 return true;
 | ||
|             }
 | ||
|             if (empty($status) || $status ===0){
 | ||
|                 return  false;
 | ||
|             }
 | ||
|         }
 | ||
|         return false;
 | ||
|     }
 | ||
| 
 | ||
| 
 | ||
|     /**
 | ||
|      * 根据goods_id查询商品配送方式及所需信息
 | ||
|      */
 | ||
|     public function getExpressType($goods_id)
 | ||
|     {
 | ||
|         return $this->where('id',$goods_id)->column('express_type,express_money,express_template_id')[0];
 | ||
|     }
 | ||
| 
 | ||
|     /**
 | ||
|      * 最小值与最大值范围
 | ||
|      */
 | ||
|     public function getMinMaxPriceAttr($value, $data)
 | ||
|     {
 | ||
|         return '¥ ' . $data['min_price'] . '~ ¥ '. $data['max_price'];
 | ||
|     }
 | ||
| 
 | ||
|     /**
 | ||
|      * @notes 商品是否参与分销
 | ||
|      * @param $value
 | ||
|      * @return string
 | ||
|      * @author Tab
 | ||
|      * @date 2021/9/1 17:29
 | ||
|      */
 | ||
|     public function getDistributionFlagAttr($value)
 | ||
|     {
 | ||
|         $data = DistributionGoods::where('goods_id', $value)->findOrEmpty()->toArray();
 | ||
|         if (!empty($data) && $data['is_distribution'] == 1) {
 | ||
|             return true;
 | ||
|         }
 | ||
|         return false;
 | ||
|     }
 | ||
| 
 | ||
|     /**
 | ||
|      * @notes 分销状态搜索器
 | ||
|      * @param $query
 | ||
|      * @param $value
 | ||
|      * @param $data
 | ||
|      * @author Tab
 | ||
|      * @date 2021/9/2 9:55
 | ||
|      */
 | ||
|     public function searchIsDistributionAttr($query, $value, $data)
 | ||
|     {
 | ||
|         // 不参与分销
 | ||
|         if (isset($data['is_distribution']) && $data['is_distribution'] == '0') {
 | ||
|             // 先找出参与分销的商品id
 | ||
|             $ids = DistributionGoods::where('is_distribution', 1)->column('goods_id');
 | ||
|             // 在搜索条件中将它们排除掉
 | ||
|             $query->where('id', 'not in', $ids);
 | ||
| 
 | ||
|         }
 | ||
|         // 参与分销
 | ||
|         if (isset($data['is_distribution']) && $data['is_distribution'] == '1') {
 | ||
|             // 先找出参与分销的商品id
 | ||
|             $ids = DistributionGoods::where('is_distribution', 1)->column('goods_id');
 | ||
|             // 在搜索条件中使用它们来进行过滤
 | ||
|             $query->where('id', 'in', $ids);
 | ||
|         }
 | ||
|     }
 | ||
| } |