Topic.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. // +---------------------------------------------------------------------+
  3. // | NiuCloud | [ WE CAN DO IT JUST NiuCloud ]  |
  4. // +---------------------------------------------------------------------+
  5. // | Copy right 2019-2029 www.niucloud.com  |
  6. // +---------------------------------------------------------------------+
  7. // | Author | NiuCloud <niucloud@outlook.com>  |
  8. // +---------------------------------------------------------------------+
  9. // | Repository | https://github.com/niucloud/framework.git  |
  10. // +---------------------------------------------------------------------+
  11. namespace addon\topic\model;
  12. use app\model\BaseModel;
  13. use think\facade\Cache;
  14. /**
  15. * 专题活动
  16. */
  17. class Topic extends BaseModel
  18. {
  19. /**
  20. * 添加专题活动
  21. * @param unknown $data
  22. */
  23. public function addTopic($data)
  24. {
  25. //时间段检测
  26. $topic_count = model('promotion_topic')->getCount([
  27. [ 'start_time|end_time', 'between', [ $data['start_time'], $data['end_time'] ] ]
  28. ]);
  29. if ($topic_count > 0) {
  30. return $this->error('', '专题时间段设置冲突');
  31. }
  32. $topic_count = model('promotion_topic')->getCount([
  33. [ 'start_time', '<=', $data['start_time'] ],
  34. [ 'end_time', '>=', $data['end_time'] ],
  35. ]);
  36. if ($topic_count > 0) {
  37. return $this->error('', '专题时间段设置冲突');
  38. }
  39. //添加数据
  40. $data['create_time'] = time();
  41. $topic_id = model('promotion_topic')->add($data);
  42. Cache::clear("promotion_topic");
  43. return $this->success($topic_id);
  44. }
  45. /**
  46. * 修改专题活动
  47. * @param unknown $data
  48. * @return multitype:string
  49. */
  50. public function editTopic($data)
  51. {
  52. //时间段检测
  53. $topic_count = model('promotion_topic')->getCount([
  54. [ 'start_time|end_time', 'between', [ $data['start_time'], $data['end_time'] ] ],
  55. [ 'topic_id', '<>', $data['topic_id'] ]
  56. ]);
  57. if ($topic_count > 0) {
  58. return $this->error('', '专题时间段设置冲突');
  59. }
  60. $topic_count = model('promotion_topic')->getCount([
  61. [ 'start_time', '<=', $data['start_time'] ],
  62. [ 'end_time', '>=', $data['end_time'] ],
  63. [ 'topic_id', '<>', $data['topic_id'] ]
  64. ]);
  65. if ($topic_count > 0) {
  66. return $this->error('', '专题时间段设置冲突');
  67. }
  68. //更新数据
  69. $res = model('promotion_topic')->update($data, [ [ 'topic_id', '=', $data['topic_id'] ] ]);
  70. $goods_data = [
  71. 'start_time' => $data['start_time'],
  72. 'end_time' => $data['end_time'],
  73. ];
  74. model('promotion_topic_goods')->update($goods_data, [ [ 'topic_id', '=', $data['topic_id'] ] ]);
  75. Cache::clear("promotion_topic");
  76. return $this->success($res);
  77. }
  78. /**
  79. * 删除专题活动
  80. * @param unknown $topic_id
  81. */
  82. public function deleteTopic($topic_id)
  83. {
  84. $res = model('promotion_topic')->delete([ [ 'topic_id', '=', $topic_id ] ]);
  85. if ($res) {
  86. model('promotion_topic_goods')->delete([ [ 'topic_id', '=', $topic_id ] ]);
  87. }
  88. Cache::clear("promotion_topic");
  89. return $this->success($res);
  90. }
  91. /**
  92. * 获取专题活动信息
  93. * @param array $condition
  94. * @param string $field
  95. */
  96. public function getTopicInfo($condition, $field = '*')
  97. {
  98. $data = json_encode([ $condition, $field ]);
  99. $cache = Cache::get("promotion_topic_gettopicInfo_" . $data);
  100. if (!empty($cache)) {
  101. return $this->success($cache);
  102. }
  103. $res = model('promotion_topic')->getInfo($condition, $field);
  104. Cache::tag("promotion_topic")->set("promotion_topic_gettopicInfo_" . $data, $res);
  105. return $this->success($res);
  106. }
  107. /**
  108. * 获取专题活动列表
  109. * @param array $condition
  110. * @param string $field
  111. * @param string $order
  112. * @param string $limit
  113. */
  114. public function getTopicList($condition = [], $field = '*', $order = '', $limit = null)
  115. {
  116. $data = json_encode([ $condition, $field, $order, $limit ]);
  117. $cache = Cache::get("promotion_topic_gettopicList_" . $data);
  118. if (!empty($cache)) {
  119. return $this->success($cache);
  120. }
  121. $list = model('promotion_topic')->getList($condition, $field, $order, '', '', '', $limit);
  122. Cache::tag("promotion_topic")->set("promotion_topic_gettopicList_" . $data, $list);
  123. return $this->success($list);
  124. }
  125. /**
  126. * 获取专题分页列表
  127. * @param array $condition
  128. * @param number $page
  129. * @param string $page_size
  130. * @param string $order
  131. * @param string $field
  132. */
  133. public function getTopicPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'create_time desc', $field = '*')
  134. {
  135. $data = json_encode([ $condition, $field, $order, $page, $page_size ]);
  136. $cache = Cache::get("promotion_topic_getTopicPageList_" . $data);
  137. if (!empty($cache)) {
  138. return $this->success($cache);
  139. }
  140. $list = model('promotion_topic')->pageList($condition, $field, $order, $page, $page_size);
  141. Cache::tag("promotion_topic")->set("promotion_topic_getTopicPageList_" . $data, $list);
  142. return $this->success($list);
  143. }
  144. }