123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace app\common\model;
- use app\common\library\Redis;
- use think\Model;
- class ShortRelation extends Model
- {
- // 表名
- protected $table = 'short_relation';
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- // 追加属性
- protected $append = [
- 'domain',
- 'admin'
- ];
- /**
- * 初始化模型
- * @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()
- {
- }
- /**
- * 关联短链接域名表
- *
- * @author liues@dianzhong.com
- * @date 2018-08-16 11:20:57
- * @return $this
- */
- public function domain(){
- return $this->hasOne(ShortDomain::class, 'id', 'domain_id', [], 'LEFT');
- }
- /**
- * 关联 渠道商/代理商信息
- *
- * @author liues@dianzhong.com
- * @date 2018-08-16 13:07:55
- * @return \think\model\relation\HasOne
- */
- public function admin(){
- return $this->hasOne(Admin::class, 'id', 'admin_id', [], 'LEFT');
- }
- /**
- * 检测是渠道否有关联
- * @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;
- }
- }
- /**
- * 随机读取渠道商绑定的短链域名
- *
- * @param $admin_id
- * @return mixed|string
- */
- public function getRandShort($admin_id){
- $ids = $this->alias('sr')->field('sd.*')
- ->join('admin_config ac', 'ac.admin_id=sr.admin_id')
- ->join('short_domain sd', 'sd.id=sr.domain_id')
- ->where(['ac.guide_domain'=>'1', 'sr.admin_id'=>$admin_id, 'sd.status'=>'1'])
- ->select();
- return $ids ? $ids[array_rand($ids)] : '';
- }
- /**
- * 获取渠道/代理商关联的域名数量
- * @param int $admin_id
- * @return int|string
- */
- public function getRelationCount($admin_id){
- return $this->alias('sr')
- ->join('short_domain sd','sd.id=sr.domain_id', 'LEFT')
- ->where(['sd.status'=>'1','sr.admin_id'=>$admin_id])
- ->count();
- }
- }
|