Channel.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace app\common\model;
  3. use think\Cache;
  4. use think\Db;
  5. /**
  6. * 渠道表
  7. * @author Administrator
  8. */
  9. class Channel extends \think\Model
  10. {
  11. protected $pk = 'id';
  12. protected $table = 'nw_channel';
  13. protected static function init()
  14. {
  15. Channel::afterInsert(function () {
  16. Cache::clear('channel_cache');
  17. });
  18. Channel::afterUpdate(function () {
  19. Cache::clear('channel_cache');
  20. });
  21. Channel::afterWrite(function () {
  22. Cache::clear('channel_cache');
  23. });
  24. }
  25. /**
  26. * 根据条件查询渠道信息 (缓存)
  27. * @param string $field
  28. * @param array $condition
  29. * @param string $order 排序
  30. * @return mixed
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. * @throws \think\exception\DbException
  34. */
  35. public function getAllByCondition($field = 'id,name,parent_id,level', $condition = ['status' => 1], $order = 'id desc')
  36. {
  37. $prefix = 'clearable_channel_list_';
  38. $cacheKey = $prefix . md5(json_encode($condition) . $field . $order);
  39. $result = Cache::get($cacheKey);
  40. if (empty($result)) {
  41. $list = Db::table($this->table)->field($field)->where($condition)->order($order)->select();
  42. if (!empty($list)) {
  43. foreach ($list as $v) {
  44. $result[$v['id']] = $v;
  45. }
  46. unset($list);
  47. Cache::tag('channel_cache')->set($cacheKey, $result, 3600 * 24);
  48. }
  49. }
  50. return $result;
  51. }
  52. /**
  53. * 获取渠道 id->name 列表
  54. * @return array|mixed
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. * @throws \think\exception\DbException
  58. */
  59. public function getChannleList()
  60. {
  61. $prefix = 'clearable_channel_list_data';
  62. $cacheKey = $prefix . md5(json_encode($prefix));
  63. $result = Cache::get($cacheKey);
  64. if (empty($result)) {
  65. $list = $this->getAllByCondition();
  66. $result = [];
  67. foreach ($list as $v) {
  68. $result[$v['id']] = $v['name'];
  69. }
  70. unset($list);
  71. Cache::tag('channel_cache')->set($cacheKey, $result, 3600 * 24);
  72. }
  73. unset($list);
  74. return $result;
  75. }
  76. /**
  77. * 获取所有父类渠道
  78. * @return array|mixed
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\ModelNotFoundException
  81. * @throws \think\exception\DbException
  82. */
  83. public function getParentChannel()
  84. {
  85. $prefix = 'clearable_channel_list_parent';
  86. $cacheKey = $prefix . md5(json_encode($prefix));
  87. $result = Cache::get($cacheKey);
  88. if (empty($result)) {
  89. $list = $this->getAllByCondition();
  90. $result = [];
  91. foreach ($list as $v) {
  92. if ($v['parent_id'] == config('TOP_CHANNEL_ID') || $v['level'] == 1) {
  93. $result[$v['id']] = $v;
  94. }
  95. }
  96. unset($list);
  97. Cache::tag('channel_cache')->set($cacheKey, $result, 3600 * 24);
  98. }
  99. unset($list);
  100. return $result;
  101. }
  102. /**
  103. * 获取当前渠道下的所有子渠道ID
  104. * @param pid:当前渠道ID
  105. *
  106. */
  107. public function getChildIds($pid)
  108. {
  109. $cacheKey = 'clearable_channel_childIds:' . $pid;
  110. $result = Cache::get($cacheKey);
  111. if (empty($result)) {
  112. $result = $this->getIds($pid);
  113. Cache::tag('channel_cache')->set($cacheKey, $result, 3600 * 24 * 30);
  114. }
  115. if (is_array($result)) {
  116. return array_unique($result);
  117. } else
  118. return $result;
  119. }
  120. /**
  121. * 获取渠道下所有父/子渠道ID
  122. *
  123. * @param pid:多个父渠道ID集
  124. * @param childids:找到的子渠道集
  125. *
  126. */
  127. public function getIds($pid, $childids = [])
  128. {
  129. if (!$pid || $pid <= 0) return false;
  130. $ids = $this->where(['parent_id' => ['in', $pid]])->column('id');
  131. //未找到,返回已经找到的
  132. if (empty($ids))
  133. return $childids;
  134. //添加到子渠道ID集合中
  135. $childids = array_merge($childids, $ids);
  136. //递归查找
  137. return $this->getIds($ids, $childids);
  138. }
  139. public function channelList($channelIdPath)
  140. {
  141. $cacheKey = 'clearable_channel_getChannelList:' . md5($channelIdPath);
  142. $result = Cache::get($cacheKey);
  143. if (empty($result)) {
  144. $result = $this->field('id,name as value')->where(['level' => 3, 'id_path' => ['LIKE', $channelIdPath . '%']])->select();
  145. Cache::tag('channel_cache')->set($cacheKey, $result, 3600 * 24 * 30);
  146. }
  147. if (is_array($result)) {
  148. return array_unique($result);
  149. } else
  150. return $result;
  151. }
  152. public function channelListById($id)
  153. {
  154. $cacheKey = 'clearable_channel_getChannelListById:' . md5($id);
  155. $result = Cache::get($cacheKey);
  156. if (empty($result)) {
  157. $result = $this->field('id,name as value')->where(['level' => 3, 'id' => $id])->select();
  158. Cache::tag('channel_cache')->set($cacheKey, $result, 3600 * 24 * 30);
  159. }
  160. if (is_array($result)) {
  161. return array_unique($result);
  162. } else
  163. return $result;
  164. }
  165. public function getChannelIds($channel_id, $level = 3)
  166. {
  167. if ($level == 1) { // 会长
  168. $channel = model('Channel')->cache('business:getChannelIds:level_B' . $channel_id, 60)->whereIn('id', $channel_id)->column('id');
  169. if ($channel) {
  170. return $channel;
  171. } else {
  172. return -2;
  173. }
  174. } else if ($level == 2) { // 子会长
  175. $where = sprintf(' id_path like "%%,%s', $channel_id) . '%" ';
  176. $channel = model('Channel')->cache('business:getChannelIds:level_B_' . $channel_id,60)->where('level', $level)->whereRaw($where)->column('id');
  177. if ($channel) {
  178. return $channel;
  179. } else {
  180. return -2;
  181. }
  182. } else {//推广员
  183. $where = sprintf(' id_path like "%%,%s', $channel_id) . '%" ';
  184. $channel = model('Channel')->cache('business:getChannelIds:level_C' . $channel_id,60)->where('level', $level)->whereRaw($where)->column('id');
  185. if ($channel) {
  186. return $channel;
  187. } else {
  188. return -2;
  189. }
  190. }
  191. }
  192. }