Goods.php 10.4 KB
<?php

namespace app\api\model;

use think\Db;
use think\Model;


class Goods extends Model
{
    // 表名
    protected $name = 'goods';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }


    public function getIsGroupList()
    {
        return ['1' => __('Is_group 1'), '2' => __('Is_group 2')];
    }

    public function getIsVipPriceList()
    {
        return ['1' => __('Is_vip_price 1'), '2' => __('Is_vip_price 2')];
    }

    public function getIsRecommendList()
    {
        return ['1' => __('Is_recommend 1'), '2' => __('Is_recommend 2')];
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getIsGroupTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_group']) ? $data['is_group'] : '');
        $list = $this->getIsGroupList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsVipPriceTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_vip_price']) ? $data['is_vip_price'] : '');
        $list = $this->getIsVipPriceList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsRecommendTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_recommend']) ? $data['is_recommend'] : '');
        $list = $this->getIsRecommendList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getImageAttr($value)
    {
        if ($value) $value = cdnurl($value);
        return $value;
    }

    public function getImagesAttr($value)
    {
        $arr = explode(',', $value);
        $data = [];
        foreach ($arr as $k => $v) {
            $data[$k] = cdnurl($v);
        }
        return $data;
    }
    public function selectHotData($where, $limit,$lang)
    {
        $where['g.status'] = 1;
        if ($lang == 'ch') $field = 'g.id goods_id,g.ch_name name,g.image,g.sales_actual+g.sales_initial sales';
        else $field = 'g.id goods_id,g.en_name name,g.image,g.sales_actual+g.sales_initial sales';
        $data = $this->alias('g')
            ->join('fa_country c', 'g.country_id=c.id')
            ->where($where)
            ->field($field)
            ->limit($limit)
            ->order('sales desc')
            ->select();
        return $data;
    }
    public function selectData($where, $limit,$lang)
    {
        $where['g.status'] = 1;

        if ($lang == 'ch') $field = 'g.id goods_id,g.ch_name name,g.image,g.goods_price,is_group,c.ch_name country_name,is_vip_price,group_price,stock_num,g.vip1_price,g.vip1_price,g.one_agency_price,g.two_agency_price,g.three_agency_price';
        else $field = 'g.id goods_id,g.en_name name,g.image,g.goods_price,is_group,c.en_name country_name,is_vip_price,group_price,stock_num,g.vip1_price,g.vip1_price,g.one_agency_price,g.two_agency_price,g.three_agency_price';

        $data = $this->alias('g')
            ->join('fa_country c', 'g.country_id=c.id')
            ->where($where)
            ->field($field)
            ->limit($limit)
            ->order('g.weigh desc')
            ->select();
        return $data;
    }

    public function selectPageData($where,$page,$limit,$lang)
    {
        $where['g.status'] = 1;

        if ($lang == 'ch') $field = 'g.id goods_id,g.ch_name name,g.image,g.goods_price,c.ch_name country_name,is_vip_price,is_group,group_price,vip_price,stock_num,g.vip1_price,g.vip1_price,g.one_agency_price,g.two_agency_price,g.three_agency_price';
        else $field = 'g.id goods_id,g.en_name name,g.image,g.goods_price,c.en_name country_name,is_vip_price,is_group,group_price,vip_price,stock_num,g.vip1_price,g.vip1_price,g.one_agency_price,g.two_agency_price,g.three_agency_price';

        $total = $this->alias('g')
            ->join('fa_country c', 'g.country_id=c.id')
            ->join('fa_category cate1', 'g.category_one_id=cate1.id')
            ->join('fa_category cate2', 'g.category_two_id=cate2.id')
            ->join('fa_category cate3', 'g.category_three_id=cate3.id')
            ->where($where)
            ->count();

        $list = $this->alias('g')
            ->join('fa_country c', 'g.country_id=c.id')
            ->join('fa_category cate1', 'g.category_one_id=cate1.id')
            ->join('fa_category cate2', 'g.category_two_id=cate2.id')
            ->join('fa_category cate3', 'g.category_three_id=cate3.id')
            ->where($where)
            ->field($field)
            ->order('g.weigh desc')
            ->page($page,$limit)
            ->select();
        return ['total' => $total, 'list' => $list];
    }

    public function getInfo($where,$lang)
    {
        $where['g.status'] = 1;

        if ($lang == 'ch') $field = 'g.id goods_id,g.ch_name name,g.image,g.status,g.goods_price,g.images,g.goods_no,g.ch_specification specification,g.ch_save_where save_where,g.ch_period period,g.ch_content content,g.stock_num,g.sales_actual,g.group_price,g.vip_price,g.is_vip_price,g.is_group,c.ch_name country_name,g.vip1_price,g.vip1_price,g.one_agency_price,g.two_agency_price,g.three_agency_price';
        else $field = 'g.id goods_id,g.en_name name,g.image,g.status,g.goods_price,g.images,g.goods_no,g.en_specification specification,g.en_save_where save_where,g.en_period period,g.en_content content,g.stock_num,g.sales_actual,g.group_price,g.vip_price,g.is_vip_price,g.is_group,c.en_name country_name,g.vip1_price,g.vip1_price,g.one_agency_price,g.two_agency_price,g.three_agency_price';

        $data = $this->alias('g')
            ->join('fa_country c', 'g.country_id=c.id')
            ->where($where)
            ->field($field)
            ->find();
        return $data;
    }

    public function selectGoodsStatus($where)
    {
        $list = $this->alias('g')
//            ->join('fa_depot d', 'g.id=d.goods_id')
            ->where($where)
            ->field('g.id,g.ch_name,g.status,g.stock_num')
            ->select();
        return $list;
    }

    /**
     * 获取仓库库存
     * @param $where
     * @return mixed
     */
    public function getAreaStockNum($where){
        $where['type'] = '1';
        $stockNum = $this->alias('g')
            ->join('fa_depot d','g.id=d.goods_id')
            ->where($where)
            ->field('d.stock_num,g.ch_name')
            ->find();
        return $stockNum;
    }

    /**
     * 获取仓库id
     * @param $goodsId
     * @param $areaId
     * @return mixed
     */
    public function getDepot($goodsId,$areaId,$number,$type){
        $arr = [];//仓库对应需扣除库存信息
        //获取全部仓库库存
        $sum_stock_num = $this->alias('g')
            ->join('fa_depot d','g.id=d.goods_id')
            ->where(['g.id' => $goodsId,'d.type' => $type])
            ->field('d.id,d.stock_num')
            ->sum('d.stock_num');
        if($number > $sum_stock_num){
            return false; //库存不足
        }
        //获取当前省仓库库存
        $province_depot = $this->alias('g')
            ->join('fa_depot d','g.id=d.goods_id')
            ->where(['g.id' => $goodsId, 'd.area_id' => $areaId, 'd.type' => $type])
            ->field('d.id,d.stock_num,d.area_id,d.type,g.id goods_id')->order('d.weigh desc')
            ->find();
        $province_stock_num = !empty($province_depot['stock_num']) ? $province_depot['stock_num'] : 0;
        $residue_number = $number - $province_stock_num;//还需多少库存
        if($residue_number > 0){
            //需要其它仓库提供库存
            if ($province_stock_num){
                $arr1['depots_id'] = $province_depot['id'];
                $arr1['area_id'] = $province_depot['area_id'];
                $arr1['goods_id'] = $province_depot['goods_id'];
                $arr1['type'] = $province_depot['type'];
                $arr1['number'] = $province_stock_num;
                if ($province_depot) $arr[] = $arr1;
            }


            $depots = $this->alias('g')
                ->join('fa_depot d','g.id=d.goods_id')
                ->where(['g.id' => $goodsId, 'd.id' => ['not in',$province_depot['id']], 'd.type' => $type])
                ->field('d.id,d.stock_num,d.area_id,d.type,g.id goods_id')->order('d.weigh desc')->select();
            foreach($depots as $key => $depot){
                $arr1['depots_id'] = $depot['id'];
                $arr1['area_id'] = $depot['area_id'];
                $arr1['goods_id'] = $depot['goods_id'];
                $arr1['type'] = $depot['type'];
                if($depot['stock_num'] < $residue_number){
                    //还是不够
                    $arr1['number'] = $depot['stock_num'];
                    $arr[] = $arr1;
                    $residue_number = $residue_number - $depot['stock_num'];
                }else{
                    //足够
                    $arr1['number'] = $residue_number;
                    $arr[] = $arr1;
                    break;
                }
            }
        }else{
            $arr1['depots_id'] = $province_depot['id'];
            $arr1['area_id'] = $province_depot['area_id'];
            $arr1['goods_id'] = $province_depot['goods_id'];
            $arr1['type'] = $province_depot['type'];
            $arr1['number'] = $number;
            $arr[] = $arr1;
        }
        return $arr;
    }

    /**
     * 检查商品是否有库存
     * @param $goodsId
     * @param $number
     * @return boolean
     */
    public function checkStockNum($goodsId,$number){
        $sum_stock_num = $this->alias('g')
            ->join('fa_depot d','g.id=d.goods_id')
            ->where(['g.id' => $goodsId])
            ->where('type',1)
            ->field('d.id,d.stock_num')
            ->sum('d.stock_num');
        if($number > $sum_stock_num){
            return false; //库存不足
        }else{
            return true;
        }
    }
}