Commit 087443c6 by liuyingkang

refactor: 优化合同管理模块的日志记录和事务处理

在Covenant.php中添加SQL查询日志记录,并在CovenantController中优化了合同删除和更新的逻辑,增加了事务处理以确保数据一致性。同时,移除了部分注释代码以提高代码可读性。
parent 97056f91
......@@ -20,6 +20,7 @@
use Dcat\Admin\Widgets\Card;
use App\Admin\Forms\ImportExcel;
use App\Admin\Actions\DownloadTemplateExcel;
use App\Command\Log;
class CovenantController extends AdminController
{
......@@ -159,14 +160,93 @@ protected function detail($id)
});
}
// /**
// * Make a form builder.
// *
// * @return Form
// */
// protected function form()
// {
// $list = ModelLawyer::select(['id', 'number', 'name'])->limit(1000)->get()->toArray();
// $lawyers = [];
// if ($list) {
// foreach ($list as $item) {
// $lawyers[$item['id']] = $item['number'] . ' ' . $item['name'];
// }
// }
// $form = Form::make(new Covenant(), function (Form $form) use ($lawyers) {
// $form->display('id');
// $form->text('number')->required();
// $form->select('ctype')->options(Covenant::CTYPE)->default(1)->required();
// $form->text('cname')->required();
// $form->date('sign_at')->format('YYYY-MM-DD')->default(date("Y-m-d"))->required();
// $form->select('principal_type')->options([1 => '单位', 2 => '个人'])->default(1)->load('principal_id', '/principal-list');;
// $form->select('principal_id')->required();
// $form->text('case_reason')->required();
// $form->select('lawyer_id')->options($lawyers)->required();
// $form->text('amount')->required();
// $form->select('payment_method')->options(Covenant::PAYMENT_METHOD)->default(1)->required();
// $form->text('avoid', '回避信息' );
// $form->text('remark', '备注');
// $form->disableCreatingCheck();
// $form->disableEditingCheck();
// $form->disableViewCheck();
// $form->disableDeleteButton();
// $form->disableViewButton();
// $form->display('created_at');
// $form->display('updated_at');
// });
// $form->saved(
// function (Form $form, $result) {
// $cid = $form->getKey();
// if (isset($_POST['principal_id']) && $_POST['principal_id']) {
// $pObj = ModelPrincipal::find($_POST['principal_id']);
// $pname = $pObj->name ? $pObj->name : $pObj->company;
// DB::table('covenant')->where("id", $cid)->update(['principal' => $pname]);
// }
// }
// );
// return $form;
// }
/**
* Make a form builder.
*
* @return Form
* 删除合同及关联记录
* @param int $id 合同ID
* @return mixed
*/
protected function form()
public function destroy($id)
{
DB::beginTransaction();
try {
// 软删除关联的收款、开票记录
ModelCovenantReceivePayment::where('cid', $id)->update(['deleted_at' => now()]);
// 删除合同
$res = $this->form()->destroy($id);
DB::commit();
return $res;
} catch (\Exception $e) {
DB::rollBack();
Log::Add('debug', '删除合同失败:'.$e->getMessage());
return false;
}
}
/**
* 更新合同及关联记录
* @param Form $form 表单对象
* @param mixed $result 保存结果
*/
protected function form()
{
$list = ModelLawyer::select(['id', 'number', 'name'])->limit(1000)->get()->toArray();
$lawyers = [];
if ($list) {
......@@ -204,25 +284,28 @@ protected function form()
$form->saved(
function (Form $form, $result) {
$cid = $form->getKey();
if (isset($_POST['principal_id']) && $_POST['principal_id']) {
$pObj = ModelPrincipal::find($_POST['principal_id']);
$pname = $pObj->name ? $pObj->name : $pObj->company;
DB::table('covenant')->where("id", $cid)->update(['principal' => $pname]);
DB::beginTransaction();
try {
// 更新委托人信息
if (isset($_POST['principal_id']) && $_POST['principal_id']) {
$pObj = ModelPrincipal::find($_POST['principal_id']);
$pname = $pObj->name ? $pObj->name : $pObj->company;
DB::table('covenant')->where("id", $cid)->update(['principal' => $pname]);
}
// 更新律师信息时同步更新关联记录
if (isset($_POST['lawyer_id']) && $_POST['lawyer_id']) {
ModelCovenantReceivePayment::where('cid', $cid)
->update(['lawyer_id' => $_POST['lawyer_id']]);
}
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
Log::Add('debug', '更新合同关联记录失败:'.$e->getMessage());
}
}
);
return $form;
}
public function destroy($id)
{
$res = $this->form()->destroy($id);
if ($res) {
//收款开票信息
ModelCovenantReceivePayment::where('cid', $id)->delete();
}
return $res;
}
}
......@@ -159,6 +159,8 @@ public function getList(array $param)
}
}
}
Log::add("debug:note_monitor ",$note_monitor->toSql());
// 计算列表总数
$count = 0; //$note_monitor->count();
......@@ -170,9 +172,8 @@ public function getList(array $param)
$data = [];
$list = $note_monitor->limit($prePage)->offset($stepstart)->orderBy('c.id', 'desc')->get()->toArray();
$queries = DB::getQueryLog();
// echo "<pre>";
// print_r($queries);
// die;
Log::add("debug:queries ",$queries);
foreach ($list as $key => $item) {
$tmp = [];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment