184 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			PHP
		
	
	
			
		
		
	
	
			184 lines
		
	
	
		
			5.5 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);
 | 
						||
        }
 | 
						||
    }
 | 
						||
 | 
						||
    public static function exportFields()
 | 
						||
    {
 | 
						||
        return '';
 | 
						||
    }
 | 
						||
 | 
						||
    public static function fieldName()
 | 
						||
    {
 | 
						||
        return [
 | 
						||
            'id' => 'ID',
 | 
						||
            'type' => '商品类型',
 | 
						||
            'name' => '商品名称',
 | 
						||
            'code' => '商品编码',
 | 
						||
            'shop_id' => '商家',
 | 
						||
            'shop_cate_id' => '商家商品分类',
 | 
						||
            'first_cate_id' => '平台商品一级分类',
 | 
						||
            'second_cate_id' => '平台商品二级分类',
 | 
						||
            'third_cate_id' => '平台商品三级分类',
 | 
						||
            'brand_id' => '品牌',
 | 
						||
            'unit_id' => '商品单位',
 | 
						||
            'image' => '商品主图',
 | 
						||
            'content' => '商品详情',
 | 
						||
            'sales_actual' => '商品实际销量',
 | 
						||
            'max_price' => '最高价格',
 | 
						||
            'min_price' => '最低价格',
 | 
						||
            'market_price' => '市场价',
 | 
						||
            'stock' => '总库存',
 | 
						||
            'custom_params' => '自定义参数',
 | 
						||
        ];
 | 
						||
    }
 | 
						||
} |