Protector.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\admin\model;
  3. use think\Db;
  4. use think\Model;
  5. use think\Session;
  6. class Protector extends Model
  7. {
  8. // 表名
  9. protected $table = 'protector';
  10. // 自动写入时间戳字段
  11. protected $autoWriteTimestamp = false;
  12. // 定义时间戳字段名
  13. protected $createTime = false;
  14. protected $updateTime = false;
  15. // 追加属性
  16. protected $append = [
  17. ];
  18. /**
  19. * 判断单位名称是否存在,存在返回错误信息,不存在返回false
  20. * @param $unit_name
  21. * @return bool|string
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. * @throws \think\exception\DbException
  25. */
  26. public static function unitNameIsExist($unit_name)
  27. {
  28. $res = self::where('unit_name',$unit_name)
  29. ->field('status,unit_name,usr_nickname,again_date')
  30. ->find();
  31. if($res){
  32. switch ($res['status']){
  33. case 0:
  34. $msg = $unit_name.'已被"'.$res['usr_nickname'].'"保护,无法保护!';
  35. break;
  36. case 1:
  37. $msg = $unit_name.'已经过期,请到已过期列表再次保护!';
  38. break;
  39. case 2:
  40. if((time() - strtotime($res['again_date'])) >= 0){
  41. return false;
  42. }
  43. $msg = $unit_name.'已经进入签单冷却期,无法保护!';
  44. break;
  45. case 4:
  46. $msg = $unit_name.'已经放弃,请到已放弃列表再次保护!';
  47. break;
  48. }
  49. return $msg;
  50. }
  51. return false;
  52. }
  53. /**
  54. * 判断用户是否超出保护个数上限
  55. * @param $usr_id
  56. * @param $count
  57. * @return bool|string
  58. */
  59. public static function proCountIsExceed($usr_id, $count)
  60. {
  61. $exist_count = self::where('usr_id',$usr_id)
  62. ->where('status',0)
  63. ->count();
  64. if($exist_count >= $count){
  65. return true;
  66. }
  67. return false;
  68. }
  69. /**
  70. * 更新过期的保护客户
  71. */
  72. public static function updateOverdue()
  73. {
  74. $now = date('Y-m-d');
  75. $ids = self::where('status',0)
  76. ->where('ex_date','<=',$now)
  77. ->column('id');
  78. if(!empty($ids)){
  79. self::whereIn('id',$ids)
  80. ->update(['status'=>1]);
  81. }
  82. }
  83. /**
  84. * 根据usr_id获取已经保护的个数
  85. * @param $usr_id
  86. * @return int|string
  87. */
  88. public static function getProtectCount($usr_id)
  89. {
  90. $count = self::where('usr_id',$usr_id)
  91. ->where('status',0)
  92. ->count();
  93. return $count;
  94. }
  95. // 获取角色权限where条件
  96. public static function getGroupWhere()
  97. {
  98. $rules = AuthGroup::getRulesByGroupId(Session::get('admin')['depart_id']);
  99. if($rules == '*'){
  100. $where = false; // 管理员可以看到所有
  101. } else {
  102. $auth_group = Db::table('auth_group')->select(); // 所有组别
  103. $depart_id = Session::get('admin')['depart_id']; // 所属组别
  104. // 获取所有子级部门
  105. $arr = AuthGroup::getSubs($auth_group,$depart_id);
  106. array_push($arr,$depart_id);
  107. // 只能查看自己和自己子级的保护客户和签单客户
  108. $where = ['depart_id'=>['in',$arr]];
  109. }
  110. return $where;
  111. }
  112. // 格式化全系统搜索单位名称
  113. public static function formatUnitName($str,$search)
  114. {
  115. if($str == $search){
  116. $str = str_replace($search,"<span class='text-red'>$search</span>",$str);
  117. return $str;
  118. }
  119. $arr = explode($search,$str);
  120. foreach ($arr as $v){
  121. if(!empty($v)){
  122. $char = mb_strlen($v);
  123. $xchar = '';
  124. for ($i=0; $i<$char; $i++){
  125. $xchar .= '*';
  126. }
  127. $str = str_replace($v,$xchar,$str);
  128. }
  129. }
  130. $str = str_replace($search,"<span class='text-red'>$search</span>",$str);
  131. return $str;
  132. }
  133. }