diff --git a/app/adminapi/logic/cost/CostBudgetLogic.php b/app/adminapi/logic/cost/CostBudgetLogic.php index fd890381a..c6e41311f 100644 --- a/app/adminapi/logic/cost/CostBudgetLogic.php +++ b/app/adminapi/logic/cost/CostBudgetLogic.php @@ -16,6 +16,7 @@ namespace app\adminapi\logic\cost; use app\common\model\cost\CostBudget; +use app\common\model\cost\CostBudgetDetail; use app\common\logic\BaseLogic; use think\facade\Db; @@ -40,7 +41,7 @@ class CostBudgetLogic extends BaseLogic { Db::startTrans(); try { - CostBudget::create([ + $costBudget = CostBudget::create([ 'approve_id' => $params['approve_id'] ?? 0, 'years' => $params['years'] ?? '', 'document_preparation_time' => $params['document_preparation_time'] ?? '', @@ -48,7 +49,27 @@ class CostBudgetLogic extends BaseLogic 'remark' => $params['remark'] ?? '', 'annex' => $params['annex'] ?? '', ]); - + foreach ($params['detail'] ?? [] as $item) + { + CostBudgetDetail::create([ + 'cost_budget_id' => $costBudget->id, + 'cost_subject_id' => $item['cost_subject_id'] ?? 0, + 'dept_id' => $item['dept_id'] ?? 0, + 'month1' => $item['month1'] ?? 0, + 'month2' => $item['month2'] ?? 0, + 'month3' => $item['month3'] ?? 0, + 'month4' => $item['month4'] ?? 0, + 'month5' => $item['month5'] ?? 0, + 'month6' => $item['month6'] ?? 0, + 'month7' => $item['month7'] ?? 0, + 'month8' => $item['month8'] ?? 0, + 'month9' => $item['month9'] ?? 0, + 'month10' => $item['month10'] ?? 0, + 'month11' => $item['month11'] ?? 0, + 'month12' => $item['month12'] ?? 0 + ]); + } + Db::commit(); return true; } catch (\Exception $e) { @@ -78,6 +99,26 @@ class CostBudgetLogic extends BaseLogic 'remark' => $params['remark'] ?? '', 'annex' => $params['annex'] ?? '', ]); + CostBudgetDetail::where('cost_budget_id', $params['id'])->update(['delete_time' => time()]); + foreach ($params['detail'] ?? [] as $item) + { + CostBudgetDetail::where('cost_budget_id', $params['id'])->update([ + 'cost_subject_id' => $item['cost_subject_id'] ?? 0, + 'dept_id' => $item['dept_id'] ?? 0, + 'month1' => $item['month1'] ?? 0, + 'month2' => $item['month2'] ?? 0, + 'month3' => $item['month3'] ?? 0, + 'month4' => $item['month4'] ?? 0, + 'month5' => $item['month5'] ?? 0, + 'month6' => $item['month6'] ?? 0, + 'month7' => $item['month7'] ?? 0, + 'month8' => $item['month8'] ?? 0, + 'month9' => $item['month9'] ?? 0, + 'month10' => $item['month10'] ?? 0, + 'month11' => $item['month11'] ?? 0, + 'month12' => $item['month12'] ?? 0 + ]); + } Db::commit(); return true; @@ -111,6 +152,8 @@ class CostBudgetLogic extends BaseLogic */ public static function detail($params): array { - return CostBudget::findOrEmpty($params['id'])->toArray(); + $costBudget = CostBudget::findOrEmpty($params['id']); + $costBudget->detail; + return $costBudget->toArray(); } } \ No newline at end of file diff --git a/app/adminapi/validate/cost/CostBudgetValidate.php b/app/adminapi/validate/cost/CostBudgetValidate.php index 0b001d96f..311b586af 100644 --- a/app/adminapi/validate/cost/CostBudgetValidate.php +++ b/app/adminapi/validate/cost/CostBudgetValidate.php @@ -31,9 +31,10 @@ class CostBudgetValidate extends BaseValidate * @var string[] */ protected $rule = [ - 'id' => 'require', - 'years' => 'require', - 'document_preparation_time' => 'require', + 'id' => 'require', + 'years' => 'require', + 'document_preparation_time' => 'require', + 'detail' => 'array|checkDetail', ]; @@ -42,9 +43,9 @@ class CostBudgetValidate extends BaseValidate * @var string[] */ protected $field = [ - 'id' => 'id', - 'years' => '年份', - 'document_preparation_time' => '制单时间', + 'id' => 'id', + 'years' => '年份', + 'document_preparation_time' => '制单时间', ]; @@ -56,7 +57,7 @@ class CostBudgetValidate extends BaseValidate */ public function sceneAdd() { - return $this->only(['years','document_preparation_time']); + return $this->only(['years','document_preparation_time', 'detail']); } @@ -68,7 +69,7 @@ class CostBudgetValidate extends BaseValidate */ public function sceneEdit() { - return $this->only(['id','years','document_preparation_time']); + return $this->only(['id','years','document_preparation_time', 'detail']); } @@ -95,4 +96,17 @@ class CostBudgetValidate extends BaseValidate return $this->only(['id']); } + public function checkDetail($value, $rule, $data) + { + $firstData = $value[0] ?? []; + if (empty($firstData)) { + return true; + } else { + if (empty($firstData['cost_subject_id'])) { + return '一级科目不能为空!'; + } + } + return true; + } + } \ No newline at end of file