WechatNewsRepository.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\wechat;
  12. use app\common\dao\wechat\WechatNewsDao;
  13. use app\common\repositories\article\ArticleRepository;
  14. use app\common\repositories\BaseRepository;
  15. use think\facade\Db;
  16. use app\common\dao\article\ArticleDao;
  17. class WechatNewsRepository extends BaseRepository
  18. {
  19. public function __construct(WechatNewsDao $dao)
  20. {
  21. $this->dao = $dao;
  22. }
  23. /**
  24. * 创建图文消息
  25. * @param array $data
  26. * @param int $merId
  27. * @param int $adminId
  28. * @author wuhaotian
  29. * @email 442384644@qq.com
  30. * @date 2024/7/25
  31. */
  32. public function create(array $data,int $merId,int $adminId)
  33. {
  34. Db::transaction(function () use($adminId,$merId,$data){
  35. $wechatNews = $this->dao->create([
  36. 'status' => $data['status'] ?? 1,
  37. 'mer_id' => $merId
  38. ]);
  39. $make = app()->make(ArticleRepository::class);
  40. $wechat_news_id = $wechatNews->wechat_news_id;
  41. foreach ($data['data'] as $v) {
  42. $result = [
  43. 'admin_id' => $adminId,
  44. 'mer_id' => $merId,
  45. 'wechat_news_id' => $wechat_news_id,
  46. 'title' => $v['title'],
  47. "author" => $v['author'],
  48. "synopsis" => $v['synopsis'],
  49. "image_input" => $v['image_input'],
  50. 'content' => $v['content'],
  51. 'status' => $data['status'] ?? 1,
  52. ];
  53. $make->create($result);
  54. }
  55. });
  56. }
  57. /**
  58. * 更新图文消息
  59. * @param int $id
  60. * @param array $data
  61. * @param int $merId
  62. * @param int $adminId
  63. * @author wuhaotian
  64. * @email 442384644@qq.com
  65. * @date 2024/7/25
  66. */
  67. public function update(int $id , array $data,int $merId,int $adminId)
  68. {
  69. Db::transaction(function () use($id,$adminId,$merId,$data){
  70. $this->dao->update($id,['status' => $data['status']]);
  71. $make = app()->make(ArticleRepository::class);
  72. $make->clearByNewId($id);
  73. foreach ($data['data'] as $v) {
  74. $result = [
  75. 'admin_id' => $adminId,
  76. 'mer_id' => $merId,
  77. 'wechat_news_id' => $id,
  78. 'title' => $v['title'],
  79. "author" => $v['author'],
  80. "synopsis" => $v['synopsis'],
  81. "image_input" => $v['image_input'],
  82. 'content' => $v['content']['content'] ?? $v['content'],
  83. ];
  84. $make->create($result);
  85. }
  86. });
  87. }
  88. /**
  89. * 删除
  90. * @param int $id
  91. * @param int $merId
  92. * @author Qinii
  93. */
  94. public function delete(int $id,int $merId)
  95. {
  96. Db::transaction(function()use($id,$merId){
  97. $make = app()->make(ArticleRepository::class);
  98. $ids = $make->getKey($id,'wechat_news_id');
  99. $this->dao->delete($id);
  100. $make->getSearch([])->where('article_id','in',$ids)->delete();
  101. });
  102. }
  103. /**
  104. * 查询
  105. * @param int $merId
  106. * @param $page
  107. * @param $limit
  108. * @return array
  109. * @author Qinii
  110. */
  111. public function search(array $where, $page, $limit)
  112. {
  113. $query = $this->dao->getAll($where);
  114. $count = $query->count();
  115. $list = $query->page($page, $limit)->select();
  116. return compact('count', 'list');
  117. }
  118. /**
  119. * 获取一条
  120. * @param int $id
  121. * @param int $merId
  122. * @return \think\Model|null
  123. * @author wuhaotian
  124. * @email 442384644@qq.com
  125. * @date 2024/7/25
  126. */
  127. public function git(int $id,int $merId)
  128. {
  129. return $this->dao->get($id,$merId);
  130. }
  131. }