ShortRelation.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Redis;
  4. use think\Model;
  5. class ShortRelation extends Model
  6. {
  7. // 表名
  8. protected $table = 'short_relation';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. // 追加属性
  15. protected $append = [
  16. 'domain',
  17. 'admin'
  18. ];
  19. /**
  20. * 初始化模型
  21. * @access protected
  22. * @return void
  23. */
  24. public function initialize()
  25. {
  26. parent::initialize(); // TODO: Change the autogenerated stub
  27. //$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
  28. }
  29. /**
  30. * 初始化处理
  31. *
  32. * @author liuns@dianzhong.com
  33. * @date 2018-08-22 11:28:46
  34. * @access protected
  35. * @return void
  36. */
  37. protected static function init()
  38. {
  39. }
  40. /**
  41. * 关联短链接域名表
  42. *
  43. * @author liues@dianzhong.com
  44. * @date 2018-08-16 11:20:57
  45. * @return $this
  46. */
  47. public function domain(){
  48. return $this->hasOne(ShortDomain::class, 'id', 'domain_id', [], 'LEFT');
  49. }
  50. /**
  51. * 关联 渠道商/代理商信息
  52. *
  53. * @author liues@dianzhong.com
  54. * @date 2018-08-16 13:07:55
  55. * @return \think\model\relation\HasOne
  56. */
  57. public function admin(){
  58. return $this->hasOne(Admin::class, 'id', 'admin_id', [], 'LEFT');
  59. }
  60. /**
  61. * 检测是渠道否有关联
  62. * @param $channel_id
  63. * @return bool
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. * @throws \think\exception\DbException
  67. */
  68. public function checkChannelRelation($channel_id){
  69. if($relation = $this->where(['admin_id'=>$channel_id])->find()){
  70. return true;
  71. }else{
  72. return false;
  73. }
  74. }
  75. /**
  76. * 随机读取渠道商绑定的短链域名
  77. *
  78. * @param $admin_id
  79. * @return mixed|string
  80. */
  81. public function getRandShort($admin_id){
  82. $ids = $this->alias('sr')->field('sd.*')
  83. ->join('admin_config ac', 'ac.admin_id=sr.admin_id')
  84. ->join('short_domain sd', 'sd.id=sr.domain_id')
  85. ->where(['ac.guide_domain'=>'1', 'sr.admin_id'=>$admin_id, 'sd.status'=>'1'])
  86. ->select();
  87. return $ids ? $ids[array_rand($ids)] : '';
  88. }
  89. /**
  90. * 获取渠道/代理商关联的域名数量
  91. * @param int $admin_id
  92. * @return int|string
  93. */
  94. public function getRelationCount($admin_id){
  95. return $this->alias('sr')
  96. ->join('short_domain sd','sd.id=sr.domain_id', 'LEFT')
  97. ->where(['sd.status'=>'1','sr.admin_id'=>$admin_id])
  98. ->count();
  99. }
  100. }