BaseGoods.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use think\Db;
  5. // 商品
  6. class BaseGoods extends Model
  7. {
  8. // 商品关联仓库
  9. public static function depotGoods($goods_id)
  10. {
  11. $goods_info = static::where('id',$goods_id)->find()->toArray();
  12. if(!$goods_info['depot']) return true;
  13. $goods_item = BaseGoodsItem::field('id,goods_spec')->where(['goods_id'=>$goods_id])->select()->toArray();
  14. $depot_arr = explode(',',$goods_info['depot']);
  15. foreach ($depot_arr as $v)
  16. {
  17. $depot_goods = DepotGoods::where(['depot_id'=>$v,'goods_id'=>$goods_id])->find();
  18. if($depot_goods) continue;
  19. // 商品加入仓库
  20. $depot_info = DepotGoods::create(['depot_id'=>$v,'goods_id'=>$goods_id]);
  21. // 规格添加
  22. $item = [];
  23. foreach ($goods_item as $iv)
  24. {
  25. $item[]= [
  26. 'depot_id'=>$v,
  27. 'goods_id'=>$depot_info->id,
  28. 'spec_id'=>$iv['id'],
  29. 'goods_spec'=>$iv['goods_spec'],
  30. 'base_goods'=>$goods_id,
  31. ];
  32. }
  33. (new DepotGoodsItem())->insertAll($item);
  34. }
  35. return true;
  36. }
  37. public static function getBaseGoods()
  38. {
  39. $goods_list = self::with('itemList')->where(['is_deleted'=>0])->order('sort desc ,id desc')->column('id,name,depot','id');
  40. return $goods_list;
  41. /*$goods_list = self::where(['is_deleted'=>0])->order('sort desc ,id desc')->column('id,name,depot','id');
  42. array_walk($goods_list,function(&$v,$k){
  43. $v['list'] = BaseGoodsItem::field('id,goods_spec,goods_no,stock,original_price,sell_price')
  44. ->where(['goods_id'=>$v['id']])->order('i')->select()->toArray();
  45. });*/
  46. return $goods_list;
  47. }
  48. public function itemList()
  49. {
  50. return $this->hasMany('BaseGoodsItem','goods_id','id')->field('id,goods_spec,goods_no,stock,original_price,sell_price')->where(['is_deleted'=>0,'status'=>1]);
  51. }
  52. }