ArticleServices.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\article;
  12. use app\dao\article\ArticleDao;
  13. use app\services\BaseServices;
  14. use app\services\wechat\WechatNewsCategoryServices;
  15. use crmeb\exceptions\AdminException;
  16. /**
  17. * Class ArticleServices
  18. * @package app\services\article
  19. * @method cidByArticleList(array $where, int $page, int $limit, string $field)
  20. */
  21. class ArticleServices extends BaseServices
  22. {
  23. /**
  24. * ArticleServices constructor.
  25. * @param ArticleDao $dao
  26. */
  27. public function __construct(ArticleDao $dao)
  28. {
  29. $this->dao = $dao;
  30. }
  31. /**
  32. * 获取列表
  33. * @param array $where
  34. * @param int $page
  35. * @param int $limit
  36. * @return array
  37. */
  38. public function getList(array $where, int $page = 0, int $limit = 0)
  39. {
  40. if (!$page && !$limit) {
  41. [$page, $limit] = $this->getPageValue();
  42. }
  43. /** @var WechatNewsCategoryServices $services */
  44. $services = app()->make(WechatNewsCategoryServices::class);
  45. $where['ids'] = $services->getNewIds();
  46. $list = $this->dao->getList($where, $page, $limit);
  47. foreach ($list as &$item) {
  48. $item['store_name'] = $item['storeInfo']['store_name'] ?? '';
  49. }
  50. $count = $this->dao->count($where);
  51. return compact('list', 'count');
  52. }
  53. /**
  54. * 新增编辑文章
  55. * @param array $data
  56. * @return mixed
  57. */
  58. public function save(array $data)
  59. {
  60. /** @var ArticleContentServices $articleContentService */
  61. $articleContentService = app()->make(ArticleContentServices::class);
  62. $content['content'] = $data['content'];
  63. $id = $data['id'];
  64. unset($data['content'], $data['id']);
  65. $info = $this->transaction(function () use ($id, $data, $articleContentService, $content) {
  66. if ($id) {
  67. $info = $this->dao->update($id, $data);
  68. $content['nid'] = $id;
  69. $res = $info && $articleContentService->update($id, $content, 'nid');
  70. } else {
  71. unset($data['id']);
  72. $data['add_time'] = time();
  73. $info = $this->dao->save($data);
  74. $content['nid'] = $info->id;
  75. $res = $info && $articleContentService->save($content);
  76. }
  77. if (!$res) {
  78. throw new AdminException(100006);
  79. } else {
  80. return $info;
  81. }
  82. });
  83. return $info;
  84. }
  85. /**
  86. * 获取商品详情
  87. * @param $id
  88. * @return array
  89. */
  90. public function read(int $id)
  91. {
  92. $info = $this->dao->read($id);
  93. $info['cid'] = (int)$info['cid'];
  94. return compact('info');
  95. }
  96. /**
  97. * 删除商品
  98. * @param int $id
  99. */
  100. public function del(int $id)
  101. {
  102. /** @var ArticleContentServices $articleContentService */
  103. $articleContentService = app()->make(ArticleContentServices::class);
  104. $this->transaction(function () use ($id, $articleContentService) {
  105. $res = $this->dao->delete($id);
  106. $res = $res && $articleContentService->del($id);
  107. if (!$res) {
  108. throw new AdminException(100008);
  109. }
  110. });
  111. }
  112. /**
  113. * 文章关联商品
  114. * @param int $id
  115. * @param int $product_id
  116. * @return mixed
  117. */
  118. public function bindProduct(int $id, int $product_id = 0)
  119. {
  120. return $this->dao->update($id, ['product_id' => $product_id]);
  121. }
  122. /**
  123. * 获取数量
  124. * @param array $where
  125. * @param bool $search
  126. * @return int
  127. */
  128. public function count(array $where = [], bool $search = true): int
  129. {
  130. return $this->search($where, $search)->count();
  131. }
  132. /**获取一条数据
  133. * @param int $id
  134. * @return mixed
  135. */
  136. public function getInfo(int $id)
  137. {
  138. $info = $this->dao->read($id);
  139. $info->visit = intval($info['visit']) + 1;
  140. if (!$info->save())
  141. throw new AdminException(400456);
  142. if ($info) {
  143. $info = $info->toArray();
  144. $info['visit'] = (int)$info['visit'];
  145. $info['add_time'] = date('Y-m-d', $info['add_time']);
  146. }
  147. return $info;
  148. }
  149. /**
  150. * 获取文章列表
  151. * @param $new_id
  152. * @return int
  153. */
  154. public function articleList($new_id)
  155. {
  156. return $this->dao->articleLists($new_id);
  157. }
  158. /**图文详情
  159. * @param $new_id
  160. * @return mixed
  161. */
  162. public function articlesList($new_id)
  163. {
  164. return $this->dao->articleContentList($new_id);
  165. }
  166. }