LogDao.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\dao\system\admin;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\system\admin\Log;
  15. use think\db\BaseQuery;
  16. use app\common\model\system\admin\Admin;
  17. use app\common\repositories\system\admin\AdminRepository;
  18. /**
  19. * Class LogDao
  20. * @package app\common\dao\system\admin
  21. * @author xaboy
  22. * @day 2020-04-16
  23. */
  24. class LogDao extends BaseDao
  25. {
  26. /**
  27. * @return BaseModel
  28. * @author xaboy
  29. * @day 2020-03-30
  30. */
  31. protected function getModel(): string
  32. {
  33. return Log::class;
  34. }
  35. /**
  36. * @param array $where
  37. * @param $merId
  38. * @return BaseQuery
  39. * @author xaboy
  40. * @day 2020-04-16
  41. */
  42. /**
  43. * 根据条件搜索日志记录
  44. *
  45. * 本函数用于查询特定条件下的日志记录。它支持多个查询条件,包括商家ID、日期、操作方法、管理员ID以及创建时间范围。
  46. * 通过灵活组合这些条件,可以精确地定位到特定的日志数据。
  47. *
  48. * @param array $where 查询条件数组,包含各种可能的搜索参数。
  49. * @param int $merId 商家ID,用于限定查询的日志所属商家。
  50. * @return \Illuminate\Database\Query\Builder|BaseQuery
  51. */
  52. public function search(array $where, $merId)
  53. {
  54. // 初始化查询,指定查询商家ID为$merId的日志数据
  55. $query = Log::getDB()->where('mer_id', $merId);
  56. // 如果指定了查询日期,则进一步限定查询日期范围
  57. $query->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  58. // 调用辅助函数处理日期查询条件
  59. getModelTime($query, $where['date']);
  60. });
  61. // 如果指定了操作方法,则进一步限定查询特定操作方法的日志
  62. if (isset($where['method']) && $where['method'] !== '') {
  63. $query->where('method', $where['method']);
  64. }
  65. // 如果指定了管理员ID,则进一步限定查询特定管理员操作的日志
  66. if (isset($where['admin_id']) && $where['admin_id'] !== '') {
  67. $query->where('admin_id', $where['admin_id']);
  68. }
  69. // 如果指定了管理员ID,则进一步限定查询特定管理员操作的日志
  70. if (isset($where['keyword']) && $where['keyword'] !== '') {
  71. $id = Admin::whereLike('admin_id|real_name', "%{$where['keyword']}%")->column('admin_id');
  72. $query->whereIn('admin_id', $id);
  73. }
  74. // 如果同时指定了开始时间和结束时间,则进一步限定查询在指定时间范围内的日志
  75. if (isset($where['section_startTime']) && $where['section_startTime'] && isset($where['section_endTime']) && $where['section_endTime']) {
  76. $query->where('create_time', '>', $where['section_startTime'])->where('create_time', '<', $where['section_endTime']);
  77. }
  78. // 返回构建好的查询条件,可用于进一步的查询操作或数据获取
  79. return $query;
  80. }
  81. }