package com.yc.service.user; import com.yc.action.grid.GridUtils; import com.yc.entity.LoginUserEntity; import com.yc.factory.FactoryBean; import com.yc.service.BaseService; import com.yc.service.impl.BaseDoIfc; import org.apache.commons.lang3.StringUtils; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.stereotype.Service; import java.util.List; import java.util.Map; @Service public class LoginEquipmentImpl extends BaseService implements LoginEquipmentIfc { @Override public EquipmentEntry checkSameEquipmentByLastLogin(String tel,String uniqueIdentifier) { //存在(上一次登录)都当作是true返回, // LastUniqueIdentifier 为null(启用新功能时第一次登录的情况)也需要验证 addNew by danaus 2023-11-04 11:56 if(StringUtils.isNotBlank(uniqueIdentifier)) { String sql=" set nocount on \n " + " declare @userid int, \n" + " @tel varchar(20)=" + GridUtils.prossSqlParm(tel) + ",@LastUniqueIdentifier varchar(50)=" + GridUtils.prossSqlParm(uniqueIdentifier) + " ,@UniqueIdentifier varchar(50),@currUniqueIdentifier varchar(50) \n" + " select @userId=userid,@currUniqueIdentifier=lastUniqueIdentifier from gProfile where Telephone=@tel \n" + " select top 1 @userId=a.userid,@UniqueIdentifier=a.uniqueIdentifier --首先判断是否存在这个设备\n" + " from gProfileEquipment a \n" + " where a.userid=@userid and a.uniqueIdentifier=@lastUniqueIdentifier\n" + " and a.Status = 1 order by a.lastLogonDate desc\n" + " if isnull(@UniqueIdentifier,'')='' --信任设备列表没有当前设备\n" + " begin\n" + " select @userId as userId, null as uniqueIdentifier --作为新设备,需要弹出验证\n" + " end\n" + " else\n" + " begin\n" + " if isnull(@UniqueIdentifier,'')=isnull(@currUniqueIdentifier,'')--是否为最近登录的设备\n" + " begin\n" + " select null --返回null表示直接跳过\n" + " end\n" + " else --否则输出最近登录设备信息\n" + " begin\n" + " select top 1 case when isnull(EquipmentName,'')='' \n" + " then EquipmentModel else EquipmentName end as EquipmentName ,convert(varchar(20),lastLogonDate,120) as lastLogonDate, uniqueIdentifier,@userid as userid\n" + " from gProfileEquipment a \n" + " where userid=@UserId and uniqueIdentifier=@currUniqueIdentifier and Status = 1 order by lastLogonDate desc\n" + " end\n" + " end"; // log.info("checkSameEquipmentByLastLogin:"+sql); return this.jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(EquipmentEntry.class)); }else { return null; } } @Override public void updateUserLoginEquipmentInfo(LoginUserEntity userEntity) { if(StringUtils.isNotBlank(userEntity.getUniqueIdentifier())) { //APP端才需要处理 //更新最近登录设备信息到demo数据库gProfile,gProfileEquipment表 BaseDoIfc baseDoIfc = (BaseDoIfc) FactoryBean.getBean("baseDoImpl"); baseDoIfc.doExecute(" set nocount on\n " + " declare @UserId int,@UniqueIdentifier varchar(50)=" + GridUtils.prossSqlParm(userEntity.getUniqueIdentifier()) + ",@tel varchar(20)=" + GridUtils.prossSqlParm(userEntity.getTelePhone()) + "\n" + " select top 1 @UserId = UserId from gProfile where Telephone = @tel \n" + " update a set a.LastUniqueIdentifier=@UniqueIdentifier from gProfile a where a.Telephone=@tel \n" + " update b set b.LastLogonDate=GETDATE() from gProfileEquipment b where b.UniqueIdentifier=@UniqueIdentifier and b.userid=@UserId\n" + " if @@rowcount=0 \n" + " begin \n" + " insert into gProfileEquipment(userid,UniqueIdentifier,EquipmentModel,OSType,RegisterDate,Status,EquipmentName,LastLogonDate) values(@UserId,@UniqueIdentifier," + GridUtils.prossSqlParm(userEntity.getEquipmentModel()) + "," + GridUtils.prossSqlParm(userEntity.getOSType()) + ",GETDATE(),1," + GridUtils.prossSqlParm(userEntity.getEquipmentModel()) + ",GETDATE()) \n" + " end \n"); } } @Override public List listLogonEquipmentInfo(String tel) { String sql=" declare @UserId int ,@Telephone varchar(50) ="+GridUtils.prossSqlParm(tel)+"\n" + " select top 1 @UserId = UserId from gProfile where Telephone = @Telephone\n" + " select 0 as isCurrentEquipment ,EquipmentModel,UniqueIdentifier,Status,case when isnull(EquipmentName,'')='' then EquipmentModel else EquipmentName end as EquipmentName ,userid,convert(varchar(20),lastLogonDate,120) as lastLogonDate \n" + " from gProfileEquipment a\n" + " where userid=@UserId \n" + " and Status = 1 \n" + " and not exists(select 1 from gProfile b \n" + " where b.UserId=@UserId and a.UserId = b.UserId and a.UniqueIdentifier = b.LastUniqueIdentifier)\n" + " union all \n" + " select 1 as isCurrentEquipment ,EquipmentModel,UniqueIdentifier,Status,case when isnull(EquipmentName,'')='' then EquipmentModel else EquipmentName end as EquipmentName ,userid ,convert(varchar(20),lastLogonDate,120) as lastLogonDate\n" + " from gProfileEquipment a \n" + " where userid=@UserId \n" + " and Status = 1 \n" + " and exists(select 1 from gProfile b \n" + " where b.UserId=@UserId and a.UserId = b.UserId and a.UniqueIdentifier = b.LastUniqueIdentifier)\n"; return this.jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(EquipmentEntry.class)); } @Override public void updateLogonEquipmentName(EquipmentEntry entry) { String sql=" set nocount on\n" + " declare @UserId int="+entry.getUserId()+",@UniqueIdentifier varchar(50)=" + GridUtils.prossSqlParm(entry.getUniqueIdentifier()) + ",@EquipmentName varchar(50)=" + GridUtils.prossSqlParm(entry.getEquipmentName()) +"\n"+ " update b set b.EquipmentName=@EquipmentName from gProfileEquipment b where b.UniqueIdentifier=@UniqueIdentifier and b.userid=@UserId"; this.doBaseExecute(sql); } @Override public void deleteLogonEquipment(EquipmentEntry entry) { String sql=" set nocount on\n" + " declare @UserId int="+entry.getUserId()+",@UniqueIdentifier varchar(50)=" + GridUtils.prossSqlParm(entry.getUniqueIdentifier()) +"\n"+ " delete b from gProfileEquipment b where b.UniqueIdentifier=@UniqueIdentifier and b.userid=@UserId"; this.doBaseExecute(sql); } @Override public Integer removeLoginUser(RemoveUserEntry entry) { String sql="set nocount on\n" + " declare @UserCode varchar(50) = '"+entry.getUserCode()+"',@ModifyCode varchar(50) = '"+entry.getUserCode()+"',@ModifyName varchar(80)='"+entry.getUserName()+"' ,@LogonType int = "+entry.getLogonType()+",@myCount int=0\n" + " if isnull(@LogonType,0) = 0\n" + " begin\n" + " update a set inActive = 1 , inActiveDate=getdate(),ModifyCode = @ModifyCode,ModifyName = @ModifyName\n" + " from _sysUser a \n" + " where UserCode = @UserCode \n" + " select @myCount=@@rowcount;\n"+ " end \n" + " if isnull(@LogonType,0) = 1\n" + " begin\n" + " update a set inActive = 1 , inActiveDate=getdate(),ModifyCode = @ModifyCode,ModifyName = @ModifyName\n" + " from t110203 a \n" + " where CltCode = @UserCode \n" + " select @myCount=@@rowcount;\n"+ " end \n" + " if isnull(@LogonType,0) = 3\n" + " begin\n" + " update a set inActive = 1, inActiveDate=getdate(),ModifyCode = @ModifyCode,ModifyName = @ModifyName\n" + " from t110302 a \n" + " where CltCode = @UserCode \n" + " select @myCount=@@rowcount;\n"+ " end \n" + " if @myCount=1\n" + " begin \n" + " delete from t9668h where UserCode = @UserCode\n" +//方便用户可以重新开通app " select @myCount=@@rowcount;\n" + " end\n" + " select @myCount;" ; return this.jdbcTemplate.queryForObject(sql,Integer.class); } @Override public void removeLoginUserByDemo(RemoveUserEntry entry) { String sql="set nocount on\n" + " declare @UserId int,@Telephone varchar(50) = '"+entry.getTelPhone()+"',@InvitationCode varchar(50)\n" + " declare @myrowcount int,@myerror int,@systemid varchar(100),@userName varchar(20)='"+entry.getUserName()+"'\n" + " --取用户已注册的所有系统\n" + " select @InvitationCode=InvitationCode,@systemid=systemid from gsystem where id="+entry.getDbid()+"\n" + " \n" + " select @UserId = UserId from gprofile where Telephone = @Telephone \n" + " select @myrowcount = @@rowcount,@myerror = @@error \n" + " if isnull(@myrowcount,0) = 0\n" + " begin\n" + " \traiserror('%s系统:用户[%s]不存在,注销失败',16,1,@systemid,@userName)\n" + " \treturn \n" + " end \n" + " \n" + " if isnull(@myrowcount,0) >1 \n" + " begin\n" + " \traiserror('%s系统:用户[%s]存在重复手机号[%s],注销失败',16,1,@systemid,@userName,@Telephone)\n" + " \treturn \n" + " end \n" + " delete a from gProfileInvitationCode a \n" + " where a.UserId = @UserId and InvitationCode=@InvitationCode\n" + " \n" + " --如果系统列表为空,则同时清掉用户表gprofile\n" + " if not exists(select top 1 1 from gProfileInvitationCode a \n" + " where a.UserId = @UserId )\n" + " begin\n" + " \tdelete a from gprofile a where UserId = @UserId \n" + " end "; this.doBaseExecute(sql); } @Override public List> listCompany(String tel) { String sql="set nocount on\n" + " declare @UserId int,@Telephone varchar(50) = '"+tel+"'" + " select @UserId=userid from gProfile where Telephone=@Telephone" + " select g.id,g.systemid as name,LEFT(g.domain, CHARINDEX(';', g.domain) - 1) as domain from gsystem g join gProfileInvitationCode p on g.InvitationCode = p.InvitationCode \n" + " where p.userid =@userid \n" + " and g.actived = 1 \n" + " and (g.expiredDate is null or DATEDIFF(day,g.expiredDate,getdate()) <=0 ) \n" + " order by g.systemid asc"; return this.jdbcTemplate.queryForList(sql); } }