123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace app\admin\model;
- use think\Db;
- use think\Model;
- use think\Session;
- class Protector extends Model
- {
- // 表名
- protected $table = 'protector';
-
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = false;
- // 定义时间戳字段名
- protected $createTime = false;
- protected $updateTime = false;
-
- // 追加属性
- protected $append = [
- ];
- /**
- * 判断单位名称是否存在,存在返回错误信息,不存在返回false
- * @param $unit_name
- * @return bool|string
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public static function unitNameIsExist($unit_name)
- {
- $res = self::where('unit_name',$unit_name)
- ->field('status,unit_name,usr_nickname,again_date')
- ->find();
- if($res){
- switch ($res['status']){
- case 0:
- $msg = $unit_name.'已被"'.$res['usr_nickname'].'"保护,无法保护!';
- break;
- case 1:
- $msg = $unit_name.'已经过期,请到已过期列表再次保护!';
- break;
- case 2:
- if((time() - strtotime($res['again_date'])) >= 0){
- return false;
- }
- $msg = $unit_name.'已经进入签单冷却期,无法保护!';
- break;
- case 4:
- $msg = $unit_name.'已经放弃,请到已放弃列表再次保护!';
- break;
- }
- return $msg;
- }
- return false;
- }
- /**
- * 判断用户是否超出保护个数上限
- * @param $usr_id
- * @param $count
- * @return bool|string
- */
- public static function proCountIsExceed($usr_id, $count)
- {
- $exist_count = self::where('usr_id',$usr_id)
- ->where('status',0)
- ->count();
- if($exist_count >= $count){
- return true;
- }
- return false;
- }
- /**
- * 更新过期的保护客户
- */
- public static function updateOverdue()
- {
- $now = date('Y-m-d');
- $ids = self::where('status',0)
- ->where('ex_date','<=',$now)
- ->column('id');
- if(!empty($ids)){
- self::whereIn('id',$ids)
- ->update(['status'=>1]);
- }
- }
- /**
- * 根据usr_id获取已经保护的个数
- * @param $usr_id
- * @return int|string
- */
- public static function getProtectCount($usr_id)
- {
- $count = self::where('usr_id',$usr_id)
- ->where('status',0)
- ->count();
- return $count;
- }
- // 获取角色权限where条件
- public static function getGroupWhere()
- {
- $rules = AuthGroup::getRulesByGroupId(Session::get('admin')['depart_id']);
- if($rules == '*'){
- $where = false; // 管理员可以看到所有
- } else {
- $auth_group = Db::table('auth_group')->select(); // 所有组别
- $depart_id = Session::get('admin')['depart_id']; // 所属组别
- // 获取所有子级部门
- $arr = AuthGroup::getSubs($auth_group,$depart_id);
- array_push($arr,$depart_id);
- // 只能查看自己和自己子级的保护客户和签单客户
- $where = ['depart_id'=>['in',$arr]];
- }
- return $where;
- }
- // 格式化全系统搜索单位名称
- public static function formatUnitName($str,$search)
- {
- if($str == $search){
- $str = str_replace($search,"<span class='text-red'>$search</span>",$str);
- return $str;
- }
- $arr = explode($search,$str);
- foreach ($arr as $v){
- if(!empty($v)){
- $char = mb_strlen($v);
- $xchar = '';
- for ($i=0; $i<$char; $i++){
- $xchar .= '*';
- }
- $str = str_replace($v,$xchar,$str);
- }
- }
- $str = str_replace($search,"<span class='text-red'>$search</span>",$str);
- return $str;
- }
- }
|