1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <?php
- namespace app\common\model;
- use think\Model;
- use think\Db;
- // 商品
- class BaseGoods extends Model
- {
- // 商品关联仓库
- public static function depotGoods($goods_id)
- {
- $goods_info = static::where('id',$goods_id)->find()->toArray();
- if(!$goods_info['depot']) return true;
- $goods_item = BaseGoodsItem::field('id,goods_spec')->where(['goods_id'=>$goods_id])->select()->toArray();
- $depot_arr = explode(',',$goods_info['depot']);
- foreach ($depot_arr as $v)
- {
- $depot_goods = DepotGoods::where(['depot_id'=>$v,'goods_id'=>$goods_id])->find();
- if($depot_goods) continue;
- // 商品加入仓库
- $depot_info = DepotGoods::create(['depot_id'=>$v,'goods_id'=>$goods_id]);
- // 规格添加
- $item = [];
- foreach ($goods_item as $iv)
- {
- $item[]= [
- 'depot_id'=>$v,
- 'goods_id'=>$depot_info->id,
- 'spec_id'=>$iv['id'],
- 'goods_spec'=>$iv['goods_spec'],
- 'base_goods'=>$goods_id,
- ];
- }
- (new DepotGoodsItem())->insertAll($item);
- }
- return true;
- }
- public static function getBaseGoods()
- {
- $goods_list = self::with('itemList')->where(['is_deleted'=>0])->order('sort desc ,id desc')->column('id,name,depot','id');
- return $goods_list;
- /*$goods_list = self::where(['is_deleted'=>0])->order('sort desc ,id desc')->column('id,name,depot','id');
- array_walk($goods_list,function(&$v,$k){
- $v['list'] = BaseGoodsItem::field('id,goods_spec,goods_no,stock,original_price,sell_price')
- ->where(['goods_id'=>$v['id']])->order('i')->select()->toArray();
- });*/
- return $goods_list;
- }
- public function itemList()
- {
- return $this->hasMany('BaseGoodsItem','goods_id','id')->field('id,goods_spec,goods_no,stock,original_price,sell_price')->where(['is_deleted'=>0,'status'=>1]);
- }
- }
|