| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- use think\Request;
- use function fast\e;
- /**
- * 商户产品
- */
- class Merchant extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- /**
- * 商户列表
- * @return \think\response\Json
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function merchantList()
- {
- // 验证请求方法
- if (!$this->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);
- }
- }
- }
|