SystemNoticeRepository.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace app\common\repositories\system\notice;
  3. use app\common\dao\system\notice\SystemNoticeDao;
  4. use app\common\repositories\BaseRepository;
  5. use app\common\repositories\system\merchant\MerchantCategoryRepository;
  6. use app\common\repositories\system\merchant\MerchantRepository;
  7. use think\exception\ValidateException;
  8. use think\facade\Db;
  9. class SystemNoticeRepository extends BaseRepository
  10. {
  11. public function __construct(SystemNoticeDao $dao)
  12. {
  13. $this->dao = $dao;
  14. }
  15. public function create(array $data, $admin_id)
  16. {
  17. $data['admin_id'] = $admin_id;
  18. $merchantRepository = app()->make(MerchantRepository::class);
  19. if ($data['type'] == 1) {
  20. $ids = (array)$data['mer_id'];
  21. $type_str = implode('/', $merchantRepository->names($ids));
  22. } else if ($data['type'] == 2) {
  23. $ids = $merchantRepository->search(['is_trader' => (int)$data['is_trader']])->column('mer_id');
  24. $type_str = $data['is_trader'] ? '自营' : '非自营';
  25. } else if ($data['type'] == 3) {
  26. $ids = $merchantRepository->search(['category_id' => (array)$data['category_id']])->column('mer_id');
  27. $type_str = implode('/', app()->make(MerchantCategoryRepository::class)->names((array)$data['category_id']));
  28. } else if ($data['type'] == 4) {
  29. $ids = $merchantRepository->search([])->column('mer_id');
  30. $type_str = '全部';
  31. } else {
  32. throw new ValidateException('商户类型有误');
  33. }
  34. if (!count($ids)) throw new ValidateException('没有有效的商户信息');
  35. $data['type_str'] = $type_str;
  36. unset($data['is_trader'], $data['category_id'], $data['mer_id']);
  37. return Db::transaction(function () use ($data, $ids) {
  38. $notice = $this->dao->create($data);
  39. $systemNoticeLogRepository = app()->make(SystemNoticeLogRepository::class);
  40. $inserts = [];
  41. foreach ($ids as $id) {
  42. if (!$id) continue;
  43. $inserts[] = [
  44. 'mer_id' => (int)$id,
  45. 'notice_id' => $notice->notice_id
  46. ];
  47. }
  48. $systemNoticeLogRepository->insertAll($inserts);
  49. return $notice;
  50. });
  51. }
  52. public function getList(array $where, $page, $limit)
  53. {
  54. $query = $this->dao->search($where);
  55. $count = $query->count();
  56. $list = $query->page($page, $limit)->order('notice_id DESC')->select();
  57. return compact('count', 'list');
  58. }
  59. }