# 模型逻辑层之实战代码 这节 其实没有什么好说的 当你把每个复杂的逻辑处理过程封装到逻辑层中,你前段的控制器代码就会优雅简洁很多 >[warning] 不要纠结你给你的逻辑处理代码起的名称 和存放位置 * 代码一 ~~~ /** * 创建项目预算书 * User: Mikkle * Q Q:776329498 * @param $data * @param $id * @param $project_id * @return array */ public function buildBudgetBook($data,$id,$project_id){ try{ $this->startTrans(); //修改审核记录 if(!$this->data($data)->isUpdate(true,['guid'=>$id])->save()){ throw new Exception("审核信息失败"); } if($project_id!=$this->where(['guid'=>$id])->value("project_id")){ throw new Exception("项目ID获取出错!"); } //审核结果 1审核通过 $m=isset($data['examine_status']) ? $data['examine_status'] : 0 ; if($m==1){ $model_book_number= new BudgetBookNumber(); Db::table("mk_budget_default_book_number")->where(["project_id"=>$project_id,"status"=>1])->update(["status"=>2]); $model_book_number->save(["project_id"=>$project_id]); $book_number_id = $model_book_number->id; if(!is_numeric($book_number_id)){ throw new Exception("获取预算书ID失败"); } //预算书编号 $book_number = $book_number_id+100000; $model_book_number->save(["book_number"=>$book_number],["id"=>$book_number_id]); $list_default_book = Db::table("mk_budget_default_book")->where(["project_id"=>$project_id,"status"=>1])->select(); if(count($list_default_book)!=1){ throw new Exception("项目书数量不是唯一"); } $list_default_book=$list_default_book[0]; unset($list_default_book["id"]); $list_default_book["book_number"]=$book_number; if(!isset(DefaultBookCopy::create($list_default_book)["id"])){ throw new Exception("出错了"); } //插入装修项目信息 $list_default_project = Db::table("mk_budget_default_project")->where(["project_id"=>$project_id,"status"=>1])->select(); $append=["book_number"=>$book_number]; $unset=["id"]; //重整数组 追加 book_number 删除 id $list_default_project=$this->changeArrayColumnData($list_default_project,$append,$unset); $insert_count= count($list_default_project); $model_project =new DefaultProjectCopy(); //超过100条 分批插入 if ($insert_count>0 && $insert_count<100){ $model_project->insertAll($list_default_project); }elseif($insert_count>100){ $insert_batch= ceil($insert_count/100); for( $i=0 ;$i<$insert_batch;$i++ ){ $model_project->insertAll(array_slice($list_default_project, $i*100,100)); } } //插入装修费率信息 $list_default_rate = Db::table("mk_budget_default_rate")->where(["project_id"=>$project_id,"status"=>1])->select(); $append=["book_number"=>$book_number]; $unset=["id"]; //重整数组 追加 book_number 删除 id $list_default_rate=$this->changeArrayColumnData($list_default_rate,$append,$unset); $model_rate =new DefaultRateCopy(); $model_rate->insertAll($list_default_rate) ; } $this->commit(); return self::showReturnCodeWithOutData(1001); }catch (Exception $e){ $this->rollback(); return self::showReturnCodeWithOutData(1008,$e->getMessage()); } } ~~~ * 实战代码二 以前写的代码 其实他更像是控制器的逻辑层 我现在也看着头大 ~~~ /** * 订单排产 * User: Mikkle * Q Q:776329498 * @param string $batch_no */ public function createFactoryProductBomByBatchNo($batch_no='20170303-16575510'){ @ob_end_clean(); @ob_implicit_flush(0); try{ if (!$batch_no){ throw new \Exception("生产批号[{$batch_no}]数据不存在"); } $model = new FactoryBatch(); $batch_info=$model -> where(['batch_no'=>$batch_no])->find(); if($batch_info['batch_status']!= 1){ throw new \Exception("生产批号[{$batch_no}]排产状态异常,请确认是否已经安排排产"); } $this->flush_str("生产批号[{$batch_no}]排产状态验证通过!"); $this->batch_no=$batch_no; $order_list = $model -> table('my_factory_orders_batch_access') -> where(['batch_no'=>$batch_no,'factory_state'=>0,'status'=>1]) -> column('order_no'); if($batch_info['order_num']!== count($order_list)){ throw new \Exception("生产批号[{$batch_no}]订单数量和流水线数量不符 请联系系统管理员Mikkle"); } $this->flush_str("生产批号[{$batch_no}]订单数量和流水线数量验证相符!"); $this->flush_str("生产批号[{$batch_no}]共有订单{$batch_info['order_num']}份"); $model->startTrans(); $this->flush_str("生产批号[{$batch_no}]验证通过,进入BOM单分解流程开始!"); $count_package=[]; $count_product=[]; foreach ($order_list as $item=>$order) { $no = $item +1; $order_info = $model ->table('my_orders')->where(['order_no' => $order,'status' => 1,'is_pay' => 1,'factory_batch'=>$batch_no,'factory_state'=>1])->find(); if (!$order_info) { throw new \Exception("订单[{$order}]状态异常或不存在,请核对订单号再排产"); } $this->flush_str("<-------------------拆解订单第{$no}个[{$order}]数据开始------------------->"); $this->flush_str("已经验证订单[{$order}]状态正常"); $save_data=$this->splitOrder($order); if (!$save_data) { throw new \Exception("订单[{$order}]BOM单分解数据不存在,请联系管理员."); } $count_package = $this->guidCountAdd($count_package,$this->count_package); $count_product = $this->guidCountAdd($count_product,$this->count_product); $data_num = count($save_data); $this->flush_str("拆解订单第{$no}个[{$order}]数据,共计[$data_num]份BOM数据"); foreach($save_data as $key=>$value){ $num=$key+1; $this->flush_str("拆解订单[{$order}]数据第[$num]份BOM数据,{$value['product_bom_name']}"); } if($model->table('my_factory_orders_bom_access')->insertAll($save_data)==0){ throw new \Exception("写入订单[{$order}]Bom单信息数据失败!"); }; $this->flush_str("写入订单[{$order}]Bom单信息数据成功!"); $this->flush_str("<-------------------拆解订单第{$no}个[{$order}]数据结束------------------->"); } $save_count_data=array_merge($this->buildCountDataArray($count_package,'Package'), $this->buildCountDataArray($count_product,'Product')); if($model->table('my_factory_batch_plan')->insertAll($save_count_data)==0){ throw new \Exception("写入生产批次[{$batch_no}]商品统计数据失败!"); }; $this->flush_str("写入生产批次[{$batch_no}]商品统计数据成功!"); $this->flush_str("生产批号[{$batch_no}]全部BOM单分解成功!"); //更新订单表信息 if (!$model->table('my_orders')->where(['factory_batch' => $batch_no])->update([ 'factory_state' => 2])) { throw new \Exception("更新[$batch_no]订单表数据失败"); } $this->flush_str("更新[$batch_no]订单表数据成功."); //更新订单表信息 if($batch_info->where('batch_no',$batch_no)->update(['batch_status'=>2])==0){ throw new \Exception("生产批号[{$batch_no}]排产状态修改失败!"); }; $this->flush_str("生产批号[{$batch_no}]状态修改成功!"); $model->commit(); $this->flush_str("生产批号[{$batch_no}]BOM单分解成功!"); $this->flush_str("<a onclick='history.go(-1)'>返回上一页</a>"); } catch (\Exception $e) { // / dump($e); $model->rollback(); $this->batch_no = Null; $str = "出错了,[{$e->getMessage()}],系统关联操作被取消!"; $this->flush_str($str); } } public function batchNoFactoryProductBomRollback($batch_no){ @ob_end_clean(); @ob_implicit_flush(0); $model_order = new Orders(); try { if (!$batch_no) { throw new \Exception('要回滚的生产批号不存在'); } $batch_info=$model_order->table('my_factory_batch')->where(['batch_no'=>$batch_no,'status'=>1])->find(); if (!$batch_info) { throw new \Exception("要回滚的生产批号[{$batch_no}]数据不存在"); } if ($batch_info['batch_status']!=2) { throw new \Exception("该生产批号[{$batch_no}]状态已经改变,不允许回滚."); } $order_num=$model_order->where(['is_pay' => 1, 'status' => 1, 'factory_batch' => $batch_no])->count('id'); if ($batch_info['order_num']!=$order_num) { throw new \Exception("该生产批号[{$batch_no}]要回滚的数量异常,请联系Mikkle."); } $this->flush_str("生产批号{$batch_no}回滚的订单号数量[{$order_num}]条,验证正确!!"); $model_order->startTrans(); $this->flush_str("生产批号{$batch_no}回滚验证已经通过,开始执行生成批号撤销!"); //更新订单表信息 if (!$model_order->update([ 'factory_state' => 1], ['factory_batch' => $batch_no,'is_pay' => 1, 'status' => 1,])) { throw new \Exception("回滚生成批次[$batch_no]数据失败"); } $this->flush_str("生产批号{$batch_no}相关订单数据回滚成功!"); //回滚生产BOM表 if (!$model_order->table('my_factory_orders_bom_access')->where(['batch_no'=>$batch_no,'status' => 1])->update(['status'=>0,'factory_desc'=>'该生产批号已经撤销'])) { throw new \Exception("回滚生产批次[$batch_no]BOM数据失败"); } $this->flush_str("回滚生产批次[$batch_no]流水数据成功!"); //回滚生产统计数据 if (!$model_order->table('my_factory_batch_plan')->where(['batch_no'=>$batch_no,'status' => 1])->update(['status'=>0,'factory_desc'=>'该生产批号已经撤销'])) { throw new \Exception("回滚生产批次[$batch_no]统计数据失败"); } $this->flush_str("回滚生产批次[$batch_no]统计数据成功!"); //回滚生产批号状态 if (!$model_order->table('my_factory_batch')->where(['batch_no'=>$batch_no])->update(['batch_status'=>1,])) { throw new \Exception("回滚生产批号[{$batch_no}]数据失败"); } $this->flush_str("回滚生产表批号[{$batch_no}]数据成功"); $model_order->commit(); $this->flush_str("回滚操作成功"); } catch (\Exception $e) { $model_order->rollback(); $str = "出错了,[{$e->getMessage()}],系统关联操作被取消!"; $this->flush_str($str); } } ~~~