CombindBninfo.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\model;
  4. use think\Model;
  5. /**
  6. * @mixin \think\Model
  7. */
  8. class CombindBninfo extends Model
  9. {
  10. protected $createTime="addtime";
  11. public function AaddBNLog($spuCode='',$bnCode='',$bnArr=[]){
  12. if(empty($bnArr)) return ;
  13. $list=[];
  14. foreach ($bnArr as $item){
  15. $tmp=[];
  16. $tmp["bnCode"]=$bnCode;
  17. $tmp["spuCode"]=$spuCode;
  18. $tmp["child_bnCode"]=$item['bnCode'];
  19. $tmp["stock_id"]=$item['stockid'];
  20. $tmp["childCode"]=$item['spuCode'];
  21. $tmp["childBnNum"]=$item['num'];
  22. $tmp["origin_price"]=$item['origin_price'];
  23. $list[]=$tmp;
  24. }
  25. $this->saveAll($list);
  26. }
  27. /**
  28. * @param string $spuCode
  29. * @param string $bnCode
  30. * @param string $num
  31. */
  32. public function subBN($spuCode='',$bnCode='',$childCode='',$num=''){
  33. $stockinfo = $this->where(["spuCode"=>$spuCode,"bnCode"=>$bnCode,"childCode"=>$childCode])->where("childBnNum",">",0)->order("id desc")
  34. ->select()->toArray();
  35. if(empty($stockinfo)) throw new \Exception("未找到关联得bn库存数据");
  36. foreach ($stockinfo as $item){
  37. $tempnum=0;
  38. if($num > $item['childBnNum']){
  39. $tempnum = $item['childBnNum'];
  40. $num= $num -$item['childBnNum'];
  41. $item['childBnNum']=0;
  42. }else{
  43. $tempnum = $num;
  44. $item['childBnNum']=$item['childBnNum'] -$num;
  45. $num =0;
  46. }
  47. $this->save($item);
  48. $up=GoodStockInfo::AddBn($item['stock_id'],$item['child_bnCode'],$tempnum,$item['origin_price']);
  49. if($up==false) throw new \Exception('bn库存数据更新失败');
  50. $save= (new GoodStock())->where(["id"=>$item['stock_id']])->inc("usable_stock",$tempnum)->inc("total_stock",$tempnum)->save();
  51. if($save==false) throw new \Exception('bn库存数据更新失败');
  52. }
  53. return true;
  54. }
  55. }