WechatNewsRepository.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\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. public function create(array $data,int $merId,int $adminId)
  24. {
  25. Db::transaction(function () use($adminId,$merId,$data){
  26. $wechatNews = $this->dao->create([
  27. 'status' => $data['status'] ?? 1,
  28. 'mer_id' => $merId
  29. ]);
  30. $make = app()->make(ArticleRepository::class);
  31. $wechat_news_id = $wechatNews->wechat_news_id;
  32. foreach ($data['data'] as $v) {
  33. $result = [
  34. 'admin_id' => $adminId,
  35. 'mer_id' => $merId,
  36. 'wechat_news_id' => $wechat_news_id,
  37. 'title' => $v['title'],
  38. "author" => $v['author'],
  39. "synopsis" => $v['synopsis'],
  40. "image_input" => $v['image_input'],
  41. 'content' => $v['content'],
  42. ];
  43. $make->create($result);
  44. }
  45. });
  46. }
  47. public function update(int $id , array $data,int $merId,int $adminId)
  48. {
  49. Db::transaction(function () use($id,$adminId,$merId,$data){
  50. $this->dao->update($id,['status' => $data['status']]);
  51. $make = app()->make(ArticleRepository::class);
  52. $make->clearByNewId($id);
  53. foreach ($data['data'] as $v) {
  54. $result = [
  55. 'admin_id' => $adminId,
  56. 'mer_id' => $merId,
  57. 'wechat_news_id' => $id,
  58. 'title' => $v['title'],
  59. "author" => $v['author'],
  60. "synopsis" => $v['synopsis'],
  61. "image_input" => $v['image_input'],
  62. 'content' => $v['content']['content'] ?? $v['content'],
  63. ];
  64. $make->create($result);
  65. }
  66. });
  67. }
  68. /**
  69. * @param int $id
  70. * @param int $merId
  71. * @author Qinii
  72. */
  73. public function delete(int $id,int $merId)
  74. {
  75. Db::transaction(function()use($id,$merId){
  76. $make = app()->make(ArticleRepository::class);
  77. $ids = $make->getKey($id,'wechat_news_id');
  78. foreach ($ids as $id) {$make->delete($id,$merId);}
  79. $this->dao->delete($id);
  80. });
  81. }
  82. /**
  83. * @param int $merId
  84. * @param $page
  85. * @param $limit
  86. * @return array
  87. * @author Qinii
  88. */
  89. public function search(array $where, $page, $limit)
  90. {
  91. $query = $this->dao->getAll($where);
  92. $count = $query->count();
  93. $list = $query->page($page, $limit)->select();
  94. return compact('count', 'list');
  95. }
  96. public function git(int $id,int $merId)
  97. {
  98. return $this->dao->get($id,$merId);
  99. }
  100. }