Notice.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace app\admin\model;
  3. use think\Log;
  4. use think\Model;
  5. class Notice extends Model
  6. {
  7. // 表名
  8. protected $table = 'notice';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. // 追加属性
  15. protected $append = [
  16. 'show_level_text',
  17. 'dialog_type_text',
  18. 'notice_type_text',
  19. 'status_text'
  20. ];
  21. public function getShowLevelList()
  22. {
  23. return ['1' => '超管可见', '2' => '超管、普管、客服、运营可见', '3' => '超管、普管、渠道商、客服、运营可见', '4' => '超管、普管、渠道商、代理商、客服、运营可见','5' => '代理商、客服、运营可见'];
  24. }
  25. public function getDialogTypeList()
  26. {
  27. return ['1' => '只弹一次', '2' => '每次登陆', '3' => '每天一次', '4' => '不弹窗','5'=>'自定义'];
  28. }
  29. public function getNoticeTypeList()
  30. {
  31. return ['1' => '常规公告', '2' => '新书推荐', '3' => '活动通知'];
  32. }
  33. public function getStatusList()
  34. {
  35. return ['normal' => '显示', 'hidden' => '隐藏'];
  36. }
  37. public function getImportantTypeList(){
  38. return ['0' => '普通', '1' => '标题标红'];
  39. }
  40. public function getShowLevelTextAttr($value, $data)
  41. {
  42. $value = $value ? $value : $data['show_level'];
  43. $list = $this->getShowLevelList();
  44. return isset($list[$value]) ? $list[$value] : '';
  45. }
  46. public function getDialogTypeTextAttr($value, $data)
  47. {
  48. $value = $value ? $value : $data['dialog_type'];
  49. $list = $this->getDialogTypeList();
  50. return isset($list[$value]) ? $list[$value] : '';
  51. }
  52. public function getNoticeTypeTextAttr($value, $data)
  53. {
  54. $value = $value ? $value : $data['notice_type'];
  55. $list = $this->getNoticeTypeList();
  56. return isset($list[$value]) ? $list[$value] : '';
  57. }
  58. public function getStatusTextAttr($value, $data)
  59. {
  60. $value = $value ? $value : $data['status'];
  61. $list = $this->getStatusList();
  62. return isset($list[$value]) ? $list[$value] : '';
  63. }
  64. /**
  65. * 获取需要弹窗的公告ID
  66. * @return int
  67. */
  68. public function dialog($admin_id)
  69. {
  70. //级别过滤
  71. $group = model("AuthGroupAccess")->getGroupId($admin_id);
  72. // $showlevel = [];
  73. // switch ($group){
  74. // case 1://超管
  75. // $showlevel = [1,2,3,4];
  76. // break;
  77. // case 2://普管
  78. // $showlevel = [2,3,4];
  79. // break;
  80. // case 3://渠道商
  81. // $showlevel = [3,4];
  82. // break;
  83. // case 4://代理商
  84. // $showlevel = [4,5];
  85. // break;
  86. // case 5://客服
  87. // $showlevel = [2,3,4,5];
  88. // break;
  89. // case 6://运营
  90. // $showlevel = [2,3,4,5];
  91. // break;
  92. // }
  93. $n2Sql = $this
  94. ->alias('n')
  95. ->join('notice_log l', "n.dialog_type='1' and n.id=l.notice_id and l.admin_id='{$admin_id}'", 'left')
  96. ->field('n.id,l.admin_id')
  97. ->buildSql();
  98. $n3Sql = $this
  99. ->alias('n')
  100. ->join('notice_log l',
  101. "n.dialog_type='3' and n.id=l.notice_id and l.admin_id='{$admin_id}' and l.createtime>" . strtotime(date('Y-m-d 00:00:00')),
  102. 'left')
  103. ->field('n.id,l.admin_id')
  104. ->buildSql();
  105. $result = $this
  106. ->alias('n1')
  107. ->join($n2Sql . ' n2', "n1.id=n2.id and n2.admin_id IS NULL", 'inner')
  108. ->join($n3Sql . ' n3', "n1.id=n3.id and n3.admin_id IS NULL", 'inner')
  109. ->where('status', 'normal')
  110. ->where('dialog_type',"neq", '4')
  111. // ->where("show_level","in",$showlevel)
  112. ->field('n1.id,n1.groups,n1.pop_num,n1.dialog_type')
  113. ->order('id', 'desc')
  114. // ->limit(1)
  115. ->select();
  116. //计算自定义公告当天弹窗的次数
  117. $dialog5 = $this
  118. ->alias('n2')
  119. ->join("notice_log l2","n2.id=l2.notice_id and l2.admin_id='{$admin_id}' and l2.createtime>" . strtotime(date('Y-m-d 00:00:00')),'inner')
  120. ->where('n2.dialog_type=5')
  121. ->field('l2.notice_id,count(1) num')
  122. ->group('l2.notice_id')
  123. ->select();
  124. if (!empty($dialog5)){
  125. $dialog5 = array_column($dialog5,null,'notice_id');
  126. }
  127. foreach ($result as $v){
  128. if(strpos($v['groups'],(string)$group)!==false){
  129. if ($v['dialog_type'] == 5 && !empty($dialog5) && isset($dialog5[$v['id']]) && $dialog5[$v['id']]['num']>=$v['pop_num']){
  130. continue;
  131. }
  132. return $v['id'];
  133. }else{
  134. continue;
  135. }
  136. }
  137. return null;
  138. }
  139. }