123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace app\common\model;
- use app\common\library\Redis;
- use think\Model;
- class GuideRelation extends Model
- {
- // 表名
- protected $table = 'guide_relation';
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- // 追加属性
- protected $append = [
- 'host_name'
- ];
- /**
- * redis key 前缀
- */
- const RP_GDH = 'GD-H:';
- /**
- * 初始化模型
- * @access protected
- * @return void
- */
- public function initialize()
- {
- parent::initialize(); // TODO: Change the autogenerated stub
- //$this->query('set SESSION sql_mode =\'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION \';'); //修改sql_mode only_full_group_by
- }
- /**
- * 初始化处理
- *
- * @author liuns@dianzhong.com
- * @date 2018-08-22 11:28:46
- * @access protected
- * @return void
- */
- protected static function init()
- {
- self::afterInsert(function($self){
- Redis::instance()->del(self::RP_GDH.$self->admin_id);
- });
- self::afterUpdate(function($self){
- Redis::instance()->del(self::RP_GDH.$self->admin_id);
- });
- }
- public function getHostName()
- {
- return model('GuideDomain')->select();
- }
- public function getHostNameAttr($value, $data)
- {
- $value = $value ? $value : $data['domain_id'];
- $list = $this->getHostName();
- return isset($list[$value]) ? $list[$value]['host'] : '';
- }
- /**
- * 检测是渠道否有关联
- * @param $channel_id
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function checkChannelRelation($channel_id){
- if($relation = $this->where(['admin_id'=>$channel_id])->find()){
- return true;
- }else{
- return false;
- }
- }
- public function getRandGuideHost($admin_id){
- if ($result = $this->alias('re')->join('guide_domain gu', 'gu.id = re.domain_id')->where(['re.admin_id' => $admin_id, 'gu.status' => '1'])->column('gu.host')) {
- $key = $result ? array_rand($result) : 0;
- return $result[$key];
- }
- return null;
- }
- /**
- * 获取渠道/代理商关联的域名数量
- * @param int $admin_id
- * @return int|string
- */
- public function getRelationCount($admin_id){
- return $this->alias('gr')
- ->join('guide_domain gd','gd.id=gr.domain_id', 'LEFT')
- ->where(['gd.status'=>'1','gr.admin_id'=>$admin_id])
- ->count();
- }
- }
|