GuideRelation.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Redis;
  4. use think\Model;
  5. class GuideRelation extends Model
  6. {
  7. // 表名
  8. protected $table = 'guide_relation';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. // 追加属性
  15. protected $append = [
  16. 'host_name'
  17. ];
  18. /**
  19. * redis key 前缀
  20. */
  21. const RP_GDH = 'GD-H:';
  22. /**
  23. * 初始化模型
  24. * @access protected
  25. * @return void
  26. */
  27. public function initialize()
  28. {
  29. parent::initialize(); // TODO: Change the autogenerated stub
  30. //$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
  31. }
  32. /**
  33. * 初始化处理
  34. *
  35. * @author liuns@dianzhong.com
  36. * @date 2018-08-22 11:28:46
  37. * @access protected
  38. * @return void
  39. */
  40. protected static function init()
  41. {
  42. self::afterInsert(function($self){
  43. Redis::instance()->del(self::RP_GDH.$self->admin_id);
  44. });
  45. self::afterUpdate(function($self){
  46. Redis::instance()->del(self::RP_GDH.$self->admin_id);
  47. });
  48. }
  49. public function getHostName()
  50. {
  51. return model('GuideDomain')->select();
  52. }
  53. public function getHostNameAttr($value, $data)
  54. {
  55. $value = $value ? $value : $data['domain_id'];
  56. $list = $this->getHostName();
  57. return isset($list[$value]) ? $list[$value]['host'] : '';
  58. }
  59. /**
  60. * 检测是渠道否有关联
  61. * @param $channel_id
  62. * @return bool
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. * @throws \think\exception\DbException
  66. */
  67. public function checkChannelRelation($channel_id){
  68. if($relation = $this->where(['admin_id'=>$channel_id])->find()){
  69. return true;
  70. }else{
  71. return false;
  72. }
  73. }
  74. public function getRandGuideHost($admin_id){
  75. 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')) {
  76. $key = $result ? array_rand($result) : 0;
  77. return $result[$key];
  78. }
  79. return null;
  80. }
  81. /**
  82. * 获取渠道/代理商关联的域名数量
  83. * @param int $admin_id
  84. * @return int|string
  85. */
  86. public function getRelationCount($admin_id){
  87. return $this->alias('gr')
  88. ->join('guide_domain gd','gd.id=gr.domain_id', 'LEFT')
  89. ->where(['gd.status'=>'1','gr.admin_id'=>$admin_id])
  90. ->count();
  91. }
  92. }