SystemNoticeRepository.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\repositories\system\notice;
  12. use app\common\dao\system\notice\SystemNoticeDao;
  13. use app\common\repositories\BaseRepository;
  14. use app\common\repositories\system\merchant\MerchantCategoryRepository;
  15. use app\common\repositories\system\merchant\MerchantRepository;
  16. use think\exception\ValidateException;
  17. use think\facade\Db;
  18. /**
  19. * 通知公告
  20. */
  21. class SystemNoticeRepository extends BaseRepository
  22. {
  23. public function __construct(SystemNoticeDao $dao)
  24. {
  25. $this->dao = $dao;
  26. }
  27. /**
  28. * 创建通知
  29. * 根据传入的数据和管理员ID,创建相应的通知,并根据不同的条件筛选商户,将通知与商户关联。
  30. *
  31. * @param array $data 包含通知信息和筛选条件的数据数组
  32. * @param int $admin_id 管理员ID,用于标识通知的创建者
  33. * @return object 创建的通知对象
  34. * @throws ValidateException 如果商户类型错误或没有有效的商户信息,则抛出验证异常
  35. */
  36. public function create(array $data, $admin_id)
  37. {
  38. // 为通知数据添加管理员ID
  39. $data['admin_id'] = $admin_id;
  40. // 获取商户仓库实例
  41. $merchantRepository = app()->make(MerchantRepository::class);
  42. // 根据不同的商户类型,筛选商户ID并生成相应的type_str
  43. if ($data['type'] == 1) {
  44. // 多个指定商户
  45. $ids = (array)$data['mer_id'];
  46. $type_str = implode('/', $merchantRepository->names($ids));
  47. } else if ($data['type'] == 2) {
  48. // 按自营或非自营筛选
  49. $ids = $merchantRepository->search(['is_trader' => (int)$data['is_trader']])->column('mer_id');
  50. $type_str = $data['is_trader'] ? '自营' : '非自营';
  51. } else if ($data['type'] == 3) {
  52. // 按类别筛选
  53. $ids = $merchantRepository->search(['category_id' => (array)$data['category_id']])->column('mer_id');
  54. $type_str = implode('/', app()->make(MerchantCategoryRepository::class)->names((array)$data['category_id']));
  55. } else if ($data['type'] == 4) {
  56. // 所有商户
  57. $ids = $merchantRepository->search([])->column('mer_id');
  58. $type_str = '全部';
  59. } else {
  60. // 商户类型错误,抛出异常
  61. throw new ValidateException('商户类型有误');
  62. }
  63. // 如果没有有效的商户ID,抛出异常
  64. if (!count($ids)) throw new ValidateException('没有有效的商户信息');
  65. // 为通知数据添加type_str,并移除不需要的字段
  66. $data['type_str'] = $type_str;
  67. unset($data['is_trader'], $data['category_id'], $data['mer_id']);
  68. // 在事务中执行通知的创建和商户通知关联的插入
  69. return Db::transaction(function () use ($data, $ids) {
  70. // 创建通知
  71. $notice = $this->dao->create($data);
  72. // 获取系统通知日志仓库实例
  73. $systemNoticeLogRepository = app()->make(SystemNoticeLogRepository::class);
  74. // 准备商户通知关联的插入数据
  75. $inserts = [];
  76. foreach ($ids as $id) {
  77. if (!$id) continue;
  78. $inserts[] = [
  79. 'mer_id' => (int)$id,
  80. 'notice_id' => $notice->notice_id
  81. ];
  82. }
  83. // 批量插入商户通知关联数据
  84. $systemNoticeLogRepository->insertAll($inserts);
  85. // 返回创建的通知对象
  86. return $notice;
  87. });
  88. }
  89. /**
  90. * 获取通知列表
  91. *
  92. * 根据给定的条件和分页信息,从数据库中检索通知列表。
  93. * 这个方法用于处理数据的查询逻辑,包括条件查询、分页和排序。
  94. *
  95. * @param array $where 查询条件,以键值对形式提供,用于构建SQL查询的WHERE子句。
  96. * @param int $page 当前的页码,用于实现分页查询。
  97. * @param int $limit 每页的记录数,用于控制分页查询的结果数量。
  98. * @return array 返回包含通知列表和总数的数组,方便前端进行分页显示。
  99. */
  100. public function getList(array $where, $page, $limit)
  101. {
  102. // 根据提供的条件进行查询
  103. $query = $this->dao->search($where);
  104. // 统计满足条件的通知总数
  105. $count = $query->count();
  106. // 进行分页查询,并按通知ID降序排序
  107. $list = $query->page($page, $limit)->order('notice_id DESC')->select();
  108. // 将总数和列表一起返回
  109. return compact('count', 'list');
  110. }
  111. }