| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace app\common\model;
- use think\Cache;
- use think\Db;
- /**
- * 聚合渠道表
- * @author Administrator
- */
- class ComplexChannelModel extends \think\Model
- {
- protected $pk = 'id';
- protected $table = 'nw_complex_channel';
- protected static function init()
- {
- ComplexChannelModel::afterInsert(function () {
- Cache::clear('complex_channel_cache');
- });
- ComplexChannelModel::afterUpdate(function () {
- Cache::clear('complex_channel_cache');
- });
- ComplexChannelModel::afterWrite(function () {
- Cache::clear('complex_channel_cache');
- });
- }
- /**
- * 根据条件查询渠道信息 (缓存)
- * @param string $field
- * @param array $condition
- * @param string $order 排序
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getAllByCondition($field = 'id,name,parent_id', $condition = ['status' => 1],$order='id desc')
- {
- $prefix = 'clearable_complex_channel_list_';
- $cacheKey = $prefix . md5(json_encode($condition) . $field.$order);
- $result = Cache::get($cacheKey);
- if (empty($result)) {
- $list = Db::table($this->table)->field($field)->where($condition)->order($order)->select();
- if (!empty($list)) {
- foreach ($list as $v) {
- $result[ $v['id'] ] = $v;
- }
- unset($list);
- Cache::tag('complex_channel_cache')->set($cacheKey, $result,3600 * 24);
- }
- }
- return $result;
- }
- /**
- * 获取渠道 id->name 列表
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getChannleList()
- {
- $prefix = 'clearable_complex_channel_list_data';
- $cacheKey = $prefix . md5(json_encode($prefix));
- $result = Cache::get($cacheKey);
- if (empty($result)) {
- $list = $this->getAllByCondition();
- $result = [];
- foreach ($list as $v) {
- $result[ $v['id'] ] = $v['name'];
- }
- unset($list);
- Cache::tag('complex_channel_cache')->set($cacheKey, $result, 3600 * 24);
- }
- unset($list);
- return $result;
- }
- /**
- * 获取所有父类渠道
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getParentChannel()
- {
- $prefix = 'clearable_complex_channel_list_parent';
- $cacheKey = $prefix . md5(json_encode($prefix));
- $result = Cache::get($cacheKey);
- if (empty($result)) {
- $list = $this->getAllByCondition();
- $result = [];
- foreach ($list as $v) {
- if ($v['parent_id'] == config('TOP_CHANNEL_ID')) {
- $result[ $v['id'] ] = $v;
- }
- }
- unset($list);
- Cache::tag('complex_channel_cache')->set($cacheKey, $result, 3600 * 24);
- }
- unset($list);
- return $result;
- }
-
-
- /**
- * 获取当前渠道下的所有子渠道ID
- * @param pid:当前渠道ID
- *
- */
- public function getChildIds($pid){
-
- $cacheKey = 'clearable_complex_channel_childIds:'.$pid;
- $result = Cache::get($cacheKey);
-
- if (empty($result)) {
- $result = $this->getIds($pid);
-
- Cache::tag('complex_channel_cache')->set($cacheKey, $result, 3600 * 24*30);
- }
-
- if(is_array($result)){
- return array_unique($result);
- }
- else
- return $result;
-
- }
-
-
- /**
- * 获取渠道下所有父/子渠道ID
- *
- * @param pid:多个父渠道ID集
- * @param childids:找到的子渠道集
- *
- */
- public function getIds($pid,$childids=[]){
-
- if(!$pid || $pid<=0) return false;
-
- $ids = $this->where(['parent_id'=>['in',$pid]])->column('id');
- //未找到,返回已经找到的
- if(empty($ids))
- return $childids;
-
- //添加到子渠道ID集合中
- $childids = array_merge($childids,$ids);
-
- //递归查找
- return $this->getIds($ids,$childids);
- }
- }
|