request->isGet()) { $this->error('请求错误'); } // 获取并验证参数 $param = $this->request->param(); $page = max(1, intval($param['page'] ?? 1)); $limit = max(20, min(100, intval($param['limit'] ?? 20))); $where = ['deleteTime' => null]; // 添加搜索条件 if (!empty($param['merchant_name'])) { $where[] = ['merchant_name|merchant_code', 'like', '%' . trim($param['search']) . '%']; } // 查询总数 $count = \db('product_merchant') ->where($where) ->count(); // 查询分页数据 $list = \db('product_merchant') ->field([ 'id', 'merchant_name as 商户名', 'merchant_code as 商户编码', 'contact_person as 联系人', 'contact_phone as 联系电话', 'address as 地址', 'email as 邮箱', 'status', 'remark as 备注', 'createTime as 创建时间', 'createName as 创建人', 'updateTime as 更新时间' ]) ->where($where) ->page($page, $limit) ->order('id', 'desc') ->select(); // 处理状态显示转换 if ($list) { $statusMap = [ 0 => ['text' => '禁用', 'type' => 'danger'], 1 => ['text' => '正常', 'type' => 'success'] ]; foreach ($list as &$item) { // 转换状态显示 if (isset($item['status']) && array_key_exists($item['status'], $statusMap)) { $item['状态'] = $statusMap[$item['status']]['text']; $item['status_type'] = $statusMap[$item['status']]['type']; $item['status_value'] = $item['status']; } else { $item['状态'] = '未知'; $item['status_type'] = 'info'; $item['status_value'] = $item['status'] ?? -1; } if (!empty($item['创建时间'])) { $item['创建时间'] = date('Y-m-d H:i', strtotime($item['创建时间'])); } if (!empty($item['更新时间'])) { $item['更新时间'] = date('Y-m-d H:i', strtotime($item['更新时间'])); } } unset($item); } $data = [ 'count' => $count, 'data' => $list ]; $this->success('成功', $data); } /** * 商户详情 * @return \think\response\Json * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function merchantDetail() { if (!$this->request->isGet()) { $this->error('请求错误'); } $id = $this->request->param('id'); if (empty($id)) { $this->error('参数错误'); } $merchant = \db('product_merchant') ->field([ 'id', 'merchant_name as 商户名称', 'merchant_code as 商户编码', 'contact_person as 联系人', 'contact_phone as 联系号码', 'address as 地址', 'email as 邮箱', 'remark as 备注' ]) ->where([ 'id' => $id, 'deleteTime' => null ]) ->find(); if (!$merchant) { $this->error('商户不存在或已被删除'); } $this->success('success', $merchant); } /** * 商户添加 * @return \think\response\Json */ public function merchantAdd() { if (!$this->request->isPost()) { $this->error('请求错误'); } $param = $this->request->post(); // 移除参数两端的空格 $param = array_map(function ($value) { return is_string($value) ? trim($value) : $value; }, $param); // 定义验证规则 $validateRules = [ 'merchant_name' => 'require|max:100', 'merchant_code' => 'require|max:20|unique:product_merchant', 'contact_person' => 'max:50', 'contact_phone' => 'max:20', 'email' => 'email', 'createName' => 'require|max:50', ]; // 定义验证错误信息 $validateMessages = [ 'merchant_name.require' => '商户名称不能为空', 'merchant_name.max' => '商户名称最多100个字符', 'merchant_code.require' => '商户编码不能为空', 'merchant_code.max' => '商户编码最多20个字符', 'merchant_code.unique' => '商户编码已存在', 'contact_person.max' => '联系人最多50个字符', 'contact_phone.max' => '联系电话最多20个字符', 'email.email' => '邮箱格式不正确', 'createName.require' => '创建人不能为空', 'createName.max' => '创建人最多50个字符', ]; // 实例化验证器 $validate = new \think\Validate($validateRules, $validateMessages); // 执行验证 if (!$validate->check($param)) { $this->error($validate->getError()); } // 使用事务确保数据一致性 Db::startTrans(); try { // 准备商户数据 $merchantData = [ 'merchant_name' => $param['merchant_name'], 'merchant_code' => $param['merchant_code'], 'contact_person' => $param['contact_person'] ?? '', 'contact_phone' => $param['contact_phone'] ?? '', 'address' => $param['address'] ?? '', 'email' => $param['email'] ?? '', 'status' => isset($param['status']) ? intval($param['status']) : 1, 'remark' => $param['remark'] ?? '', 'createName' => $param['createName'], 'createTime' => date('Y-m-d H:i:s'), 'updateTime' => date('Y-m-d H:i:s') ]; // 插入商户数据 $merchantId = \db('product_merchant')->insertGetId($merchantData); if (!$merchantId) { throw new \Exception('商户信息插入失败'); } // 准备日志数据 $logData = [ 'ModifyUser' => $param['createName'], 'UserCode' => $param['createCode'], 'ModifyTime' => date('Y-m-d H:i:s'), 'MerchantName' => $param['merchant_name'], 'MerchantCode' => $param['merchant_code'], 'Type' => '新增商户', ]; // 插入日志数据 $logResult = \db('merchant_log')->insert($logData); if (!$logResult) { throw new \Exception('操作日志记录失败'); } // 提交事务 Db::commit(); $result = true; } catch (\Exception $e) { Db::rollback(); $errorMsg = $e->getMessage(); } if ($result) { $this->success('添加成功'); } else { $this->error('添加失败:' . $errorMsg); } } /** * 商户修改 * @return \think\response\Json */ public function merchantEdit() { if (!$this->request->isPost()) { $this->error('请求错误'); } $param = $this->request->post(); // 移除参数两端的空格 $param = array_map(function ($value) { return is_string($value) ? trim($value) : $value; }, $param); // 验证必填字段 if (empty($param['id'])) { $this->error('商户ID不能为空'); } $id = intval($param['id']); // 检查商户是否存在并获取原始数据 $originalData = \db('product_merchant') ->where([ 'id' => $id, 'deleteTime' => null ]) ->find(); if (!$originalData) { $this->error('商户不存在或已被删除'); } // 定义验证规则 $validateRules = [ 'merchant_name' => 'require|max:100', 'merchant_code' => 'require|max:20|unique:product_merchant,merchant_code,' . $id, 'contact_person' => 'max:50', 'contact_phone' => 'max:20', 'email' => 'email', 'status' => 'in:0,1', ]; // 定义验证错误信息 $validateMessages = [ 'merchant_name.require' => '商户名称不能为空', 'merchant_name.max' => '商户名称最多100个字符', 'merchant_code.require' => '商户编码不能为空', 'merchant_code.max' => '商户编码最多20个字符', 'merchant_code.unique' => '商户编码已存在', 'contact_person.max' => '联系人最多50个字符', 'contact_phone.max' => '联系电话最多20个字符', 'email.email' => '邮箱格式不正确', ]; // 实例化验证器 $validate = new \think\Validate($validateRules, $validateMessages); // 执行验证 if (!$validate->check($param)) { $this->error($validate->getError()); } // 使用事务确保数据一致性 Db::startTrans(); try { // 准备更新数据 $updateData = [ 'merchant_name' => $param['merchant_name'], 'merchant_code' => $param['merchant_code'], 'contact_person' => $param['contact_person'] ?? '', 'contact_phone' => $param['contact_phone'] ?? '', 'address' => $param['address'] ?? '', 'email' => $param['email'] ?? '', 'remark' => $param['remark'] ?? '', 'updateTime' => date('Y-m-d H:i:s') ]; // 检查是否有实际修改(排除更新时间字段) $checkUpdateData = $updateData; unset($checkUpdateData['updateTime']); $hasChanges = false; foreach ($checkUpdateData as $key => $newValue) { $oldValue = $originalData[$key] ?? ''; if ($newValue != $oldValue) { $hasChanges = true; break; } } // 如果没有实际修改,直接返回成功 if (!$hasChanges) { Db::rollback(); // 不需要提交事务 $this->success('数据未发生变化'); } // 执行更新操作 $result = \db('product_merchant') ->where('id', $id) ->update($updateData); if ($result === false) { throw new \Exception('商户信息更新失败'); } // 获取操作人信息(可以根据业务需要调整) $operator = $param['updateName'] ?? $param['createName'] ?? '系统'; // 准备日志数据 $logData = [ 'ModifyUser' => $operator, 'UserCode' => $param['createCode'], 'ModifyTime' => date('Y-m-d H:i:s'), 'MerchantName' => $param['merchant_name'], 'MerchantCode' => $param['merchant_code'], 'Type' => '修改商户', 'OldValue' => $this->prepareOldValueForLog($originalData, $checkUpdateData), 'NewValue' => $this->prepareNewValueForLog($checkUpdateData, $originalData), ]; // 插入日志数据 $logResult = \db('merchant_log')->insert($logData); if (!$logResult) { throw new \Exception('操作日志记录失败'); } // 提交事务 Db::commit(); $result = true; } catch (\Exception $e) { Db::rollback(); $errorMsg = $e->getMessage(); } // 事务结束后再返回结果 if ($result) { $this->success('修改成功'); } else { $this->error('修改失败:' . $errorMsg); } } private function codeList($key) { $data = [ 'merchant_name' => '商户名称', 'merchant_code' => '商户编码', 'contact_person' => '联系人', 'contact_phone' => '联系电话', 'address' => '地址', 'email' => '邮箱', 'status' => '状态', 'remark' => '备注', ]; return $data[$key] ?? ''; } /** * 准备日志中的旧值(只记录有变化的字段) * @param array $originalData 原始数据 * @param array $newData 新数据 * @return string JSON格式的旧值 */ private function prepareOldValueForLog($originalData, $newData) { $changedFields = []; foreach ($newData as $key => $newValue) { $oldValue = $originalData[$key] ?? ''; if ($newValue != $oldValue) { $item = $this->codeList($key); $changedFields[$item] = $oldValue; } } return !empty($changedFields) ? json_encode($changedFields, JSON_UNESCAPED_UNICODE) : ''; } /** * 准备日志中的新值(只记录有变化的字段) * @param array $newData 新数据 * @param array $originalData 原始数据 * @return string JSON格式的新值 */ private function prepareNewValueForLog($newData, $originalData) { $changedFields = []; foreach ($newData as $key => $newValue) { $oldValue = $originalData[$key] ?? ''; if ($newValue != $oldValue) { $item = $this->codeList($key); $changedFields[$item] = $newValue; } } return !empty($changedFields) ? json_encode($changedFields, JSON_UNESCAPED_UNICODE) : ''; } /** * 商户删除 * @return \think\response\Json */ public function merchantDelete() { if (!$this->request->isGet()) { $this->error('请求错误'); } $param = $this->request->param(); // 移除参数两端的空格 $param = array_map(function ($value) { return is_string($value) ? trim($value) : $value; }, $param); if (empty($param['id'])) { $this->error('请选择要删除的商户'); } // 获取操作人信息(可以根据业务需要调整) $operator = $param['operator'] ?? $param['updateName'] ?? $param['createName'] ?? '系统'; // 支持单个ID或逗号分隔的多个ID if (is_string($param['id'])) { $idList = explode(',', $param['id']); $idList = array_filter(array_map('intval', $idList)); } else { $idList = [intval($param['id'])]; } if (empty($idList)) { $this->error('参数错误:商户ID无效'); } // 使用事务确保数据一致性 Db::startTrans(); try { // 查询要删除的商户信息(用于日志记录) $merchantList = \db('product_merchant') ->whereIn('id', $idList) ->whereNull('deleteTime') ->field(['id', 'merchant_name', 'merchant_code', 'contact_person', 'status']) ->select(); if (empty($merchantList)) { Db::rollback(); $this->error('未找到要删除的商户记录或商户已被删除'); } $deleteTime = date('Y-m-d H:i:s'); // 执行软删除操作 $result = \db('product_merchant') ->whereIn('id', $idList) ->whereNull('deleteTime') ->update([ 'deleteTime' => $deleteTime, 'updateTime' => $deleteTime ]); if ($result === false) { throw new \Exception('商户信息删除失败'); } // 批量记录删除日志 $logData = []; foreach ($merchantList as $merchant) { $logData[] = [ 'ModifyUser' => $operator, 'UserCode' => $param['createCode'], 'ModifyTime' => $deleteTime, 'MerchantName' => $merchant['merchant_name'] ?? '未知商户', 'MerchantCode' => $merchant['merchant_code'] ?? '', 'Type' => '删除商户' ]; } // 批量插入日志数据 if (!empty($logData)) { $logResult = \db('merchant_log')->insertAll($logData); if (!$logResult) { throw new \Exception('操作日志记录失败'); } } // 提交事务 Db::commit(); // 返回成功信息 $successCount = count($merchantList); $res = true; } catch (\Exception $e) { Db::rollback(); $errorMsg = $e->getMessage(); } // 事务结束后再返回结果 if ($res) { $this->success('删除成功','成功'.$successCount.'条'); } else { $this->error('删除失败:' . $errorMsg); } } /** * 商户状态修改 * 用于单独修改商户的启用/禁用状态 * * @return \think\response\Json */ public function merchantChangeStatus() { if (!$this->request->isGet()) { $this->error('请求错误'); } $param = $this->request->param(); // 移除参数两端的空格 $param = array_map(function ($value) { return is_string($value) ? trim($value) : $value; }, $param); // 验证必填字段 if (empty($param['id'])) { $this->error('商户ID不能为空'); } if (!isset($param['status'])) { $this->error('状态值不能为空'); } $id = intval($param['id']); $status = intval($param['status']); // 验证状态值合法性 if (!in_array($status, [0, 1])) { $this->error('状态值无效,只能为0(禁用)或1(启用)'); } // 检查商户是否存在 $originalData = \db('product_merchant') ->where([ 'id' => $id, 'deleteTime' => null]) ->field(['id', 'merchant_name', 'merchant_code', 'status', 'contact_person']) ->find(); if (!$originalData) { $this->error('商户不存在或已被删除'); } // 检查状态是否有变化 $oldStatus = intval($originalData['status']); if ($oldStatus === $status) { $this->error('状态未发生变化'); } // 获取操作人信息 $operator = $param['operator'] ?? $param['updateName'] ?? $param['createName'] ?? '系统'; // 获取状态描述 $statusMap = [ 0 => '禁用', 1 => '正常' ]; $oldStatusText = $statusMap[$oldStatus] ?? '未知'; $newStatusText = $statusMap[$status] ?? '未知'; // 使用事务确保数据一致性 Db::startTrans(); try { $updateTime = date('Y-m-d H:i:s'); // 执行状态更新 $result = \db('product_merchant') ->where('id', $id) ->whereNull('deleteTime') ->update([ 'status' => $status, 'updateTime' => $updateTime ]); if ($result === false) { throw new \Exception('状态更新失败'); } // 准备日志数据 $logData = [ 'ModifyUser' => $operator, 'UserCode' => $param['code'], 'ModifyTime' => $updateTime, 'MerchantName' => $originalData['merchant_name'], 'MerchantCode' => $originalData['merchant_code'], 'Type' => '修改状态', 'OldValue' => json_encode([ '状态' => $oldStatusText, '商户名称' => $originalData['merchant_name'] ?? '', '商户编码' => $originalData['merchant_code'] ?? '' ], JSON_UNESCAPED_UNICODE), 'NewValue' => json_encode([ '状态' => $newStatusText, '商户名称' => $originalData['merchant_name'] ?? '', '商户编码' => $originalData['merchant_code'] ?? '', '修改时间' => $updateTime ], JSON_UNESCAPED_UNICODE), ]; // 插入日志数据 $logResult = \db('merchant_log')->insert($logData); if (!$logResult) { throw new \Exception('操作日志记录失败'); } // 提交事务 Db::commit(); $result = true; } catch (\Exception $e) { Db::rollback(); $errorMsg = $e->getMessage(); } // 事务结束后再返回结果 if ($result) { $this->success('更新状态成功'); } else { $this->error('更新状态失败:' . $errorMsg); } } /** * 批量修改商户状态 * 用于同时修改多个商户的状态 * * @return \think\response\Json */ public function merchantBatchChangeStatus() { if (!$this->request->isGet()) { $this->error('请求错误'); } $param = $this->request->param(); // 移除参数两端的空格 $param = array_map(function ($value) { return is_string($value) ? trim($value) : $value; }, $param); // 验证必填字段 if (empty($param['ids'])) { $this->error('请选择要修改的商户'); } if (!isset($param['status'])) { $this->error('状态值不能为空'); } // 解析商户ID列表 if (is_string($param['ids'])) { $idList = explode(',', $param['ids']); $idList = array_filter(array_map('intval', $idList)); } elseif (is_array($param['ids'])) { $idList = array_filter(array_map('intval', $param['ids'])); } else { $this->error('商户ID参数格式错误'); } if (empty($idList)) { $this->error('请选择有效的商户'); } $status = intval($param['status']); // 验证状态值合法性 if (!in_array($status, [0, 1])) { $this->error('状态值无效,只能为0(禁用)或1(启用)'); } // 获取操作人信息 $operator = $param['operator'] ?? $param['updateName'] ?? $param['createName'] ?? '系统'; // 获取状态描述 $statusMap = [ 0 => '禁用', 1 => '正常' ]; $newStatusText = $statusMap[$status] ?? '未知'; // 使用事务确保数据一致性 Db::startTrans(); try { // 查询要修改的商户信息 $merchantList = \db('product_merchant') ->whereIn('id', $idList) ->whereNull('deleteTime') ->field(['id', 'merchant_name', 'merchant_code', 'status', 'contact_person']) ->select(); if (empty($merchantList)) { Db::rollback(); $this->error('未找到有效的商户记录或商户已被删除'); } $updateTime = date('Y-m-d H:i:s'); // 执行批量状态更新 $result = \db('product_merchant') ->whereIn('id', $idList) ->whereNull('deleteTime') ->update([ 'status' => $status, 'updateTime' => $updateTime ]); if ($result === false) { throw new \Exception('批量状态更新失败'); } // 批量记录操作日志 $logData = []; $successCount = 0; $statusChangedCount = 0; foreach ($merchantList as $merchant) { $oldStatus = intval($merchant['status']); // 如果状态没有变化,不记录日志 if ($oldStatus === $status) { $successCount++; continue; } $oldStatusText = $statusMap[$oldStatus] ?? '未知'; $logData[] = [ 'ModifyUser' => $operator, 'UserCode' => $param['code'], 'ModifyTime' => $updateTime, 'MerchantName' => $merchant['merchant_name'], 'MerchantCode' => $merchant['merchant_code'], 'Type' => '批量修改状态', 'OldValue' => json_encode([ '状态' => $oldStatusText, '商户名称' => $merchant['merchant_name'] ?? '', '商户编码' => $merchant['merchant_code'] ?? '' ], JSON_UNESCAPED_UNICODE), 'NewValue' => json_encode([ '状态' => $newStatusText, '商户名称' => $merchant['merchant_name'] ?? '', '商户编码' => $merchant['merchant_code'] ?? '', '修改时间' => $updateTime ], JSON_UNESCAPED_UNICODE) ]; $successCount++; $statusChangedCount++; } // 如果有状态变化的记录,插入日志 if (!empty($logData)) { $logResult = \db('merchant_log')->insertAll($logData); if (!$logResult) { throw new \Exception('操作日志记录失败'); } } // 提交事务 Db::commit(); // 返回操作结果 $message = "操作完成,共处理 {$successCount} 个商户"; if ($statusChangedCount > 0) { $message .= ",其中 {$statusChangedCount} 个商户状态已修改为 {$newStatusText}"; } else { $message .= ",状态均未发生变化"; } $result = true; } catch (\Exception $e) { Db::rollback(); $errorMsg = $e->getMessage(); } // 事务结束后再返回结果 if ($result) { $this->success('更新状态成功', $message); } else { $this->error('更新状态失败:' . $errorMsg); } } /** * 操作日志数据列表 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function LogList() { if (!$this->request->isGet()) { $this->error('请求错误'); } $param = $this->request->param(); if (!isset($param['code'])) { $this->error('参数错误'); } $logList = \db('merchant_log') ->where('UserCode',$param['code']) ->field([ 'UserCode as 操作用户账号', 'ModifyUser as 操作用户名', 'MerchantName as 被操作商户', 'OldValue as 原数据', 'NewValue as 操作后数据', 'Type as 操作类型', 'ModifyTime as 操作时间', ]) ->order('ModifyTime DESC') ->select(); if (empty($logList)) { $this->error('没有商户操作日志'); }else{ $this->success('成功', $logList); } } }