'int',//主键,自动递增 'manager_id'=>'int',//导入数据记录id 'product_id'=>'int',//关联商品id 'type'=>'int', // 类型 1 增加 2减少 'num'=>'decimal',//关联数量 'status'=>'int',//状态 1 正常 0 删除 'apply_id'=>'int',//申请id 'apply_name'=>'string',//申请人 'create_time'=>'datetime',//时间 ]; protected $createTime='create_time'; protected $updateTime=false; public function Product(){ return $this->belongsTo(FinancialProducts::class,'product_id','id')->bind(['goodName','skuCode']); } public static function AddProduct($manager_id,$product){ $add=(new ManagerProduct)->saveAll(array_map(function ($iitem) use ($manager_id){ $iitem['manager_id']=$manager_id; return $iitem; },$product)); if($add->isEmpty()) throw new \Exception('添加失败'); } public static function onAfterWrite(Model $model) : void{ $original=$model->getOrigin(); $changed=$model->getChangedData(); if(($model->status==1 || $changed['status']==1) && ($original['status']==0|| isset($original['status']))){ $manager= FinancialManager::findOrEmpty($model->manager_id); if($manager->isEmpty()) throw new \Exception('数据不存在'); $product=FinancialProducts::with(['ProductsCombind'=>["Products"]])->findOrEmpty($model->product_id); if($product->isEmpty()) throw new \Exception('商品不存在'); $data[]=[ "type"=>$model['type'], 'order_item_id'=>$model->manager_id, 'product_id'=>$model->product_id, 'num'=>$model->num, "fz_date"=>$manager->fz_date, 'unit_price'=>$product->unit_price, 'rate'=>$manager->inv_tax, 'apply_id'=>$manager->apply_id, 'apply_name'=>$manager->apply_name, ]; if($product->is_combind==1 && !$product->ProductsCombind->isEmpty()){ $product->ProductsCombind->each(function ($item) use (&$data,$manager,$model){ $data[]=[ "type"=>$model['type'], 'order_item_id'=>$model->manager_id, 'product_id'=>$item->child_id, 'fz_date'=>$manager->fz_date, 'num'=>bcmul($item->child_num,$model->num,8), 'unit_price'=>$item->products->unit_price??0, 'rate'=>$item->products->cat_tax, 'apply_id'=>$manager->apply_id, 'apply_name'=>$manager->apply_name, ]; }); } (new ProductOnlog)->saveAll($data); } } }