描述:资源管理>>驾驶员管理>>导入驾驶员信息导入
新需求要求:以前导入类似新增就是在数据库中增加记录,现在要进行判断了,
如果身份证已存在,再看驾驶证,如果驾驶证存在,则结束,不存在则用excel中的数据替换数据库中的那条记录;
如果身份证不存在,再看驾驶证,如果驾驶证存在,则结束,不存在则把excel这条记录新增进数据库。
Excel模板:
涉及的表:
驾驶员表 CLGL_DRIVER_INFO
用户表 USER_BASICINFO
修改的代码:
DriverManagerController
@RequestMapping(value = "/importRecords", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public BaseResp importDriverInfo(@RequestBody List<DriverInfoImport> req){
int rowNum = 1;
for(int i=0; i<req.size(); i++){
rowNum++;
DriverInfoImport curr = req.get(i);
if(!Util.isEmpty(curr.getBirthday())){
try{
DateUtil.stringToDate(curr.getBirthday(), "yyyy-MM-dd");
}catch(ParseException e){
throw new EcodeServiceException("导入失败:第"+rowNum+"行【出生年月】日期格式错误");
}
}
Asserts.notEmpty(curr.getEntryDate(), "导入失败:第"+rowNum+"行【本单位上岗日期】不能为空");
try{
DateUtil.stringToDate(curr.getEntryDate(), "yyyy-MM-dd");
}catch(ParseException e){
throw new EcodeServiceException("导入失败:第"+rowNum+"行【本单位上岗日期】日期格式错误");
}
}
AbstractRequestExecutor executorDriver = new AbstractRequestExecutor("请求访问批量导入驾驶员信息接口", "importSingleDriverInfo"){
@SuppressWarnings("unchecked")
protected <M> BaseResp batchOperate(List<M> records) throws Exception{
return driverManagerService.addNewImportDriverInfo((List<DriverInfoImport>)records);
}
};
return executorDriver.batchExecute(req);
}
IDriverManagerService
public BaseResp addNewImportDriverInfo(List<DriverInfoImport> records)throws Exception;
DriverManagerServiceImpl
@Override
public BaseResp addNewImportDriverInfo(List<DriverInfoImport> records) throws Exception{
BaseResp resp = new BaseResp();
StringBuffer sb = new StringBuffer();
for(DriverInfoImport record : records){
Integer lineNo = record.getLineNo();
String userName = record.getUserName().trim();
String idCardNo = record.getIdCardNo().trim();
String drivingLicenseId = record.getDrivingLicenseId().trim();
String sexDesc = record.getSexDesc();
String allowDriveVehTypeDesc = record.getAllowDriveVehTypeDesc();
String birthday = record.getBirthday().trim();
String entryDate = record.getEntryDate().trim();
Date bday = DateUtil.parseStrDate(birthday, "yyyy-MM-dd");
Date edate = DateUtil.parseStrDate(entryDate, "yyyy-MM-dd");
if(idCardNo.length()>18 || idCardNo.length()<15){
sb.append("第[").append(lineNo).append("]行,身份证长度不正确<br>\n\r");
continue;
}
if(Util.isEmpty(bday)){
sb.append("第[").append(lineNo).append("]行,出生日期不正确<br>\n\r");
continue;
}
if(Util.isEmpty(edate)){
sb.append("第[").append(lineNo).append("]行,入单位日期不正确<br>\n\r");
continue;
}
// 检查用户名不能重复
/*Boolean userNameUnique = checkUserNameUnique(userName, null);
if(!userNameUnique){
sb.append("第[").append(lineNo).append("]行,用户名信息已存在<br>\n\r");
continue;
}*/
boolean isUpdate=false;
boolean isAdd=false;
// 查询用户表身份证是否有值
UserBasicinfo ub = checkIdCardUniqueBean(idCardNo, null);
ClglDriverInfo driver2 = null;
if(!Util.isEmpty(ub)){ //存在,更新
// 存在该身份证号,检查驾照编号不能重复
Boolean drivingLicUnique = checkDrivingLicUnique(drivingLicenseId, null);
if(!drivingLicUnique){
sb.append("第[").append(lineNo).append("]行,驾驶证号码已经存在<br>\n\r");
continue;
}else{
driver2 = driverManagerDAO.queryDriverInfoByUserId(ub.getUserid());//根据userid查询出驾驶员信息
isUpdate=true;
}
}else{ // 用户表身份证不存在值,新增
//检查驾照编号不能重复
Boolean drivingLicUnique = checkDrivingLicUnique(drivingLicenseId, null);
if(!drivingLicUnique){
sb.append("第[").append(lineNo).append("]行,驾驶证号码已经存在<br>\n\r");
continue;
}else{
isAdd=true;
}
}
MemoryTable mem = MemoryTable.getInstance();
List<DictionaryItem> sexList = mem.getDictionaryItem(DicTypeConst.SEX, Constant.DICT_NO_PARENT_ID);
Boolean isTure = false;
for(DictionaryItem item : sexList){
if(item.getItemName().equals(sexDesc)){
record.setSex(Short.valueOf(item.getItemValue()));
isTure = true;
break;
}
}
if(!isTure){
sb.append("第[").append(lineNo).append("]行,输入的性别不正确<br>\n\r");
continue;
}
isTure = false;
List<DictionaryItem> allowDriveVehTypeList = mem.getDictionaryItem(DicTypeConst.DRIVER_ZJCX, Constant.DICT_NO_PARENT_ID);
for(DictionaryItem item : allowDriveVehTypeList){
if(item.getItemName().equals(allowDriveVehTypeDesc)){
record.setAllowDriveVehType(Short.valueOf(item.getItemValue()));
isTure = true;
break;
}
}
if(!isTure){
sb.append("第[").append(lineNo).append("]行,输入的准驾车型不正确<br>\n\r");
continue;
}
if(isAdd){//数据新增
Date crtDate = getOracleSysDate();
LoginInfo loginInfo = Common.getLoginInfo();
SysDeptInfo loginDept = mem.getSysDeptMap(loginInfo.getUserSysDept().getAppSysId(), loginInfo.getUserSysDept().getAppDeptId());
int loginUserId = loginInfo.getAppUserInfo().getIntAppUserId();
String loginUserName = loginInfo.getUserBasicInfo().getRealName();
UserBasicinfo user = new UserBasicinfo();
DriverEditReq driverEditReq = new DriverEditReq();
driverEditReq = driverInfoImportToDriverEditReq(driverEditReq, record);
setUserPo(user, false, driverEditReq, bday, loginUserId, loginUserName, crtDate, loginDept);
driverManagerDAO.save(user);
ClglDriverInfo driver = new ClglDriverInfo();
setDriverPo(driver, false, driverEditReq, user.getUserid(), loginUserId, loginUserName, crtDate, loginDept);
driver.setWorkStatus((short)0);
driverManagerDAO.save(driver);
}else if(isUpdate){// 更新
//用户名
ub.setRealname(userName);
//性别
ub.setSex(Short.toString(record.getSex()));
//身份证
ub.setIdcardno(idCardNo);
//驾驶证号
driver2.setDrivingLicenseId(drivingLicenseId);
//出生年月
ub.setBirthday(DateUtil.formatDate(bday, "yyyyMMddHHmmss"));
//手机号码
ub.setMobileCmpp(record.getMobileLong().trim());
//本岗位上岗日期
driver2.setEntryDate(DateUtil.parseStrDate(entryDate,"yyyy-MM-dd"));
//准驾车型
driver2.setAllowDriveVehType(record.getAllowDriveVehType());
driverManagerDAO.update(ub);
driverManagerDAO.update(driver2);
}
}
if(sb.length() > 0){
resp.setRetCode(Constant.RET_CODE_BATCH_DEL_PART_FAIL);
resp.setRetMsg(sb.toString());
}else{
resp.setRetMsg(Constant.RET_SUCC_STR);
}
return resp;
}
// 用户表信息
private UserBasicinfo checkIdCardUniqueBean(String idCardNo, String userId) throws Exception{
UserBasicinfo user = driverManagerDAO.queryUserInfoByIdCard(idCardNo, userId);
return user;
}
public UserBasicinfo queryUserInfoByIdCard(String idCard, String userId) throws Exception{
HibernateParams hParams = new HibernateParams();
hParams.addSqlStrBuffer("from UserBasicinfo where idcardno = ? ");
hParams.addParamObj(idCard);
if(!Util.isEmpty(userId)){
hParams.addSqlStrBuffer(" and userid <> ? ");
hParams.addParamObj(userId.trim());
}
List<?> list = this.findByHql(hParams.getSqlStr(), hParams.getParamObj());
return list.size() > 0?(UserBasicinfo)list.get(0):null;
}
// 检查驾照编号唯一
private Boolean checkDrivingLicUnique(String drivingLicenseId, String userId) throws Exception{
ClglDriverInfo driver = driverManagerDAO.queryDriverInfoByDrivingLic(drivingLicenseId, userId);
return driver == null;
}
public ClglDriverInfo queryDriverInfoByDrivingLic(String drivinglic, String userId) throws Exception{
HibernateParams hParams = new HibernateParams();
hParams.addSqlStrBuffer("from ClglDriverInfo where drivingLicenseId = ? ");
hParams.addParamObj(drivinglic);
if(!Util.isEmpty(userId)){
hParams.addSqlStrBuffer(" and userid <> ? ");
hParams.addParamObj(userId.trim());
}
List<?> list = this.findByHql(hParams.getSqlStr(), hParams.getParamObj());
return list.size() > 0?(ClglDriverInfo)list.get(0):null;
}
public ClglDriverInfo queryDriverInfoByUserId(String userId) throws Exception{
HibernateParams hParams = new HibernateParams();
hParams.addSqlStrBuffer("from ClglDriverInfo where userid = ? ");
hParams.addParamObj(userId.trim());
List<?> list = this.findByHql(hParams.getSqlStr(), hParams.getParamObj());
return list.size() > 0?(ClglDriverInfo)list.get(0):null;
}
public void update(UserBasicinfo po) throws Exception;
public void update(ClglDriverInfo po) throws Exception;
@Override
public void update(UserBasicinfo po) throws Exception{
super.update(po);
}
@Override
public void update(ClglDriverInfo po) throws Exception{
super.update(po);
}
重点代码:
主要这边有两张表,驾驶员表 CLGL_DRIVER_INFO,用户表 USER_BASICINFO
这两张表的userid字段是一样的,所以可以根据userid查询出驾驶员信息,然后导入excel的时候可以对驾驶员信息进行替换
----------------------------------------------------------------------------------------------------------------------------------------------------------------
开发过程还出现了以下问题:
数据库存的出生年月是这种格式:
我这样保存ub.setBirthday(birthday);在页面上却不显示出生年月,估计是格式错了
看了setUserPo(user,false,driverEditReq,才知道原来是这样的
于是改成这样即可:ub.setBirthday(DateUtil.formatDate(bday, "yyyyMMddHHmmss"));