@IocBean
@At("/front/userInfo")
public class UserInfoController {
@Inject
private UserInfoService userInfoService;
@At("/getUserInfoByIDNum")
@Ok("json")
@AdaptBy(type = JsonAdaptor.class)
public Object getUserInfoByIDNum(@Param("idNum") String idNum) {
if(Strings.isBlank(idNum)) {
return Result.error("参数缺失");
}
return userInfoService.getUserInfoByIDNum(idNum);
}
@At("/saveUserInfo")
@Ok("json")
@AdaptBy(type = JsonAdaptor.class)
public Object saveUserInfo(@Param("idNum") String idNum, @Param("name") String name,
@Param("phoneNum") String phoneNum) {
if (Strings.isBlank(idNum) || Strings.isBlank(name) || Strings.isBlank(phoneNum)) {
return Result.error("参数缺失");
}
return userInfoService.saveUserInfo(idNum, name, phoneNum);
}
}
@IocBean(args = { "refer:dao" })
public class UserInfoServiceImpl extends BaseServiceImpl<UserInfo> implements UserInfoService {
private static final Log log = Logs.get();
@Inject
private UserInfoService userInfoService;
public UserInfoServiceImpl(Dao dao) {
super(dao);
}
@Override
public Result getUserInfoByIDNum(String idNum) {
UserInfo userInfo = this.fetch(idNum);
if (userInfo == null) {
return Result.error("查询不到相关数据");
}
return Result.success("ok", userInfo);
}
@Override
public Result saveUserInfo(String idNum, String name, String phoneNum) {
try {
UserInfo dbInfo = this.fetch(idNum);
if (dbInfo == null) {
UserInfo userInfo = new UserInfo();
userInfo.setId(idNum);
userInfo.setName(name);
userInfo.setPhoneNum(phoneNum);
userInfo.setCreateDate(new Date());
userInfo=this.insert(userInfo);
return Result.success("ok", userInfo);
} else {
dbInfo.setId(idNum);
dbInfo.setName(name);
dbInfo.setPhoneNum(phoneNum);
this.updateIgnoreNull(dbInfo);
return Result.success("ok", dbInfo);
}
} catch (Exception e) {
log.error(e.getMessage());
return Result.error("插入或更新用户信息时出现异常");
}
}
}