SystemNoticeRepository.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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. class SystemNoticeRepository extends BaseRepository
  19. {
  20. public function __construct(SystemNoticeDao $dao)
  21. {
  22. $this->dao = $dao;
  23. }
  24. public function create(array $data, $admin_id)
  25. {
  26. $data['admin_id'] = $admin_id;
  27. $merchantRepository = app()->make(MerchantRepository::class);
  28. if ($data['type'] == 1) {
  29. $ids = (array)$data['mer_id'];
  30. $type_str = implode('/', $merchantRepository->names($ids));
  31. } else if ($data['type'] == 2) {
  32. $ids = $merchantRepository->search(['is_trader' => (int)$data['is_trader']])->column('mer_id');
  33. $type_str = $data['is_trader'] ? '自营' : '非自营';
  34. } else if ($data['type'] == 3) {
  35. $ids = $merchantRepository->search(['category_id' => (array)$data['category_id']])->column('mer_id');
  36. $type_str = implode('/', app()->make(MerchantCategoryRepository::class)->names((array)$data['category_id']));
  37. } else if ($data['type'] == 4) {
  38. $ids = $merchantRepository->search([])->column('mer_id');
  39. $type_str = '全部';
  40. } else {
  41. throw new ValidateException('商户类型有误');
  42. }
  43. if (!count($ids)) throw new ValidateException('没有有效的商户信息');
  44. $data['type_str'] = $type_str;
  45. unset($data['is_trader'], $data['category_id'], $data['mer_id']);
  46. return Db::transaction(function () use ($data, $ids) {
  47. $notice = $this->dao->create($data);
  48. $systemNoticeLogRepository = app()->make(SystemNoticeLogRepository::class);
  49. $inserts = [];
  50. foreach ($ids as $id) {
  51. if (!$id) continue;
  52. $inserts[] = [
  53. 'mer_id' => (int)$id,
  54. 'notice_id' => $notice->notice_id
  55. ];
  56. }
  57. $systemNoticeLogRepository->insertAll($inserts);
  58. return $notice;
  59. });
  60. }
  61. public function getList(array $where, $page, $limit)
  62. {
  63. $query = $this->dao->search($where);
  64. $count = $query->count();
  65. $list = $query->page($page, $limit)->order('notice_id DESC')->select();
  66. return compact('count', 'list');
  67. }
  68. }