WechatNewsRepository.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace app\common\repositories\wechat;
  3. use app\common\dao\wechat\WechatNewsDao;
  4. use app\common\repositories\article\ArticleRepository;
  5. use app\common\repositories\BaseRepository;
  6. use think\facade\Db;
  7. use app\common\dao\article\ArticleDao;
  8. class WechatNewsRepository extends BaseRepository
  9. {
  10. public function __construct(WechatNewsDao $dao)
  11. {
  12. $this->dao = $dao;
  13. }
  14. public function create(array $data,int $merId,int $adminId)
  15. {
  16. Db::transaction(function () use($adminId,$merId,$data){
  17. $wechatNews = $this->dao->create([
  18. 'status' => $data['status'] ?? 1,
  19. 'mer_id' => $merId
  20. ]);
  21. $make = app()->make(ArticleRepository::class);
  22. $wechat_news_id = $wechatNews->wechat_news_id;
  23. foreach ($data['data'] as $v) {
  24. $result = [
  25. 'admin_id' => $adminId,
  26. 'mer_id' => $merId,
  27. 'wechat_news_id' => $wechat_news_id,
  28. 'title' => $v['title'],
  29. "author" => $v['author'],
  30. "synopsis" => $v['synopsis'],
  31. "image_input" => $v['image_input'],
  32. 'content' => $v['content'],
  33. ];
  34. $make->create($result);
  35. }
  36. });
  37. }
  38. public function update(int $id , array $data,int $merId,int $adminId)
  39. {
  40. Db::transaction(function () use($id,$adminId,$merId,$data){
  41. $this->dao->update($id,['status' => $data['status']]);
  42. $make = app()->make(ArticleRepository::class);
  43. $make->clearByNewId($id);
  44. foreach ($data['data'] as $v) {
  45. $result = [
  46. 'admin_id' => $adminId,
  47. 'mer_id' => $merId,
  48. 'wechat_news_id' => $id,
  49. 'title' => $v['title'],
  50. "author" => $v['author'],
  51. "synopsis" => $v['synopsis'],
  52. "image_input" => $v['image_input'],
  53. 'content' => $v['content']['content'] ?? $v['content'],
  54. ];
  55. $make->create($result);
  56. }
  57. });
  58. }
  59. /**
  60. * @param int $id
  61. * @param int $merId
  62. * @author Qinii
  63. */
  64. public function delete(int $id,int $merId)
  65. {
  66. Db::transaction(function()use($id,$merId){
  67. $make = app()->make(ArticleRepository::class);
  68. $ids = $make->getKey($id,'wechat_news_id');
  69. foreach ($ids as $id) {$make->delete($id,$merId);}
  70. $this->dao->delete($id);
  71. });
  72. }
  73. /**
  74. * @param int $merId
  75. * @param $page
  76. * @param $limit
  77. * @return array
  78. * @author Qinii
  79. */
  80. public function search(array $where, $page, $limit)
  81. {
  82. $query = $this->dao->getAll($where);
  83. $count = $query->count();
  84. $list = $query->page($page, $limit)->select();
  85. return compact('count', 'list');
  86. }
  87. public function git(int $id,int $merId)
  88. {
  89. return $this->dao->get($id,$merId);
  90. }
  91. }