ComplexChannelModel.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace app\common\model;
  3. use think\Cache;
  4. use think\Db;
  5. /**
  6. * 聚合渠道表
  7. * @author Administrator
  8. */
  9. class ComplexChannelModel extends \think\Model
  10. {
  11. protected $pk = 'id';
  12. protected $table = 'nw_complex_channel';
  13. protected static function init()
  14. {
  15. ComplexChannelModel::afterInsert(function () {
  16. Cache::clear('complex_channel_cache');
  17. });
  18. ComplexChannelModel::afterUpdate(function () {
  19. Cache::clear('complex_channel_cache');
  20. });
  21. ComplexChannelModel::afterWrite(function () {
  22. Cache::clear('complex_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', $condition = ['status' => 1],$order='id desc')
  36. {
  37. $prefix = 'clearable_complex_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('complex_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_complex_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('complex_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_complex_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')) {
  93. $result[ $v['id'] ] = $v;
  94. }
  95. }
  96. unset($list);
  97. Cache::tag('complex_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. $cacheKey = 'clearable_complex_channel_childIds:'.$pid;
  109. $result = Cache::get($cacheKey);
  110. if (empty($result)) {
  111. $result = $this->getIds($pid);
  112. Cache::tag('complex_channel_cache')->set($cacheKey, $result, 3600 * 24*30);
  113. }
  114. if(is_array($result)){
  115. return array_unique($result);
  116. }
  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. if(!$pid || $pid<=0) return false;
  129. $ids = $this->where(['parent_id'=>['in',$pid]])->column('id');
  130. //未找到,返回已经找到的
  131. if(empty($ids))
  132. return $childids;
  133. //添加到子渠道ID集合中
  134. $childids = array_merge($childids,$ids);
  135. //递归查找
  136. return $this->getIds($ids,$childids);
  137. }
  138. }