WechatNewsRepository.php 3.2 KB

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