IntegralGoods.php 5.5 KB
<?php

namespace app\api\model;

use think\Model;


class IntegralGoods extends Model
{
    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 selectPageData($where, $page, $limit,$lang)
    {
        $where['g.status'] = 1;

        if ($lang == 'ch') $field = 'g.id integral_goods_id,g.ch_name name,g.image,g.original_price,g.goods_price,g.integral,c.ch_name country_name,stock_num';
        else $field = 'g.id integral_goods_id,g.en_name name,g.image,g.original_price,g.goods_price,g.integral,c.en_name country_name,stock_num';

        $total = $this->alias('g')
            ->join('fa_country c', 'g.country_id=c.id')
            ->where($where)
            ->count();

        $list = $this->alias('g')
            ->join('fa_country c', 'g.country_id=c.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 integral_goods_id,g.ch_name name,g.image,g.status,g.original_price,g.goods_price,g.integral,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,c.ch_name country_name';
        else $field = 'g.id integral_goods_id,g.en_name name,g.image,g.status,g.original_price,g.goods_price,g.integral,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,c.en_name country_name';

        $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'] = '2';
        $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){
        $arr = [];//仓库对应需扣除库存信息
        //获取全部仓库库存
        $sum_stock_num = $this->alias('g')
            ->join('fa_integral_depot d','g.id=d.goods_id')
            ->where(['g.id' => $goodsId])
            ->field('d.id,d.stock_num')
            ->sum('d.stock_num');
        if($number > $sum_stock_num){
            return false; //库存不足
        }
        //获取当前省仓库库存
        $province_depot = $this->alias('g')
            ->join('fa_integral_depot d','g.id=d.goods_id')
            ->where(['g.id' => $goodsId, 'd.area_id' => $areaId])
            ->field('d.id,d.stock_num')->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){
            //需要其它仓库提供库存
            $arr1['depots_id'] = $province_depot['id'];
            $arr1['number'] = $province_stock_num;
            $arr[] = $arr1;
            $depots = $this->alias('g')
                ->join('fa_integral_depot d','g.id=d.goods_id')
                ->where(['g.id' => $goodsId, 'd.id' => ['not in',$province_depot['id']]])
                ->field('d.id,d.stock_num')->order('d.weigh desc')->select();
            foreach($depots as $key => $depot){
                $arr1['depots_id'] = $depot['id'];
                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['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',2)
            ->field('d.id,d.stock_num')
            ->sum('d.stock_num');
        if($number > $sum_stock_num){
            return false; //库存不足
        }else{
            return true;
        }
    }
}