新增用户数据导入导出功能,支持导入数据更新
This commit is contained in:
@@ -13,7 +13,8 @@ import com.jeesite.common.mybatis.mapper.query.QueryType;
|
||||
import com.jeesite.common.utils.excel.annotation.ExcelField;
|
||||
import com.jeesite.common.utils.excel.annotation.ExcelField.Align;
|
||||
import com.jeesite.common.utils.excel.annotation.ExcelFields;
|
||||
import com.jeesite.common.utils.excel.fieldtype.RoleListType;
|
||||
import com.jeesite.common.utils.excel.fieldtype.CompanyType;
|
||||
import com.jeesite.common.utils.excel.fieldtype.OfficeType;
|
||||
|
||||
/**
|
||||
* 员工用户管理Entity
|
||||
@@ -25,7 +26,9 @@ import com.jeesite.common.utils.excel.fieldtype.RoleListType;
|
||||
}, joinTable={
|
||||
@JoinTable(type=Type.JOIN, entity=Employee.class, alias="e",
|
||||
on="e.emp_code=a.ref_code AND a.user_type=#{USER_TYPE_EMPLOYEE}",
|
||||
attrName="employee", columns={@Column(includeEntity=Employee.class)}),
|
||||
attrName="employee", columns={
|
||||
@Column(includeEntity=Employee.class)
|
||||
}),
|
||||
@JoinTable(type=Type.LEFT_JOIN, entity=Office.class, alias="o",
|
||||
on="o.office_code=e.office_code", attrName="employee.office",
|
||||
columns={
|
||||
@@ -74,17 +77,17 @@ public class EmpUser extends User {
|
||||
|
||||
@Valid
|
||||
@ExcelFields({
|
||||
@ExcelField(title="归属机构", attrName="office.officeName", align=Align.CENTER, sort=10),
|
||||
@ExcelField(title="归属公司", attrName="company.officeName", align = Align.CENTER, sort=20),
|
||||
@ExcelField(title="归属机构", attrName="employee.office", align=Align.CENTER, sort=10, fieldType=OfficeType.class),
|
||||
@ExcelField(title="归属公司", attrName="employee.company", align = Align.CENTER, sort=20, fieldType=CompanyType.class),
|
||||
@ExcelField(title="登录账号", attrName="loginCode", align=Align.CENTER, sort=30),
|
||||
@ExcelField(title="用户昵称", attrName="userName", align=Align.LEFT, sort=40),
|
||||
@ExcelField(title="用户昵称", attrName="userName", align=Align.CENTER, sort=40),
|
||||
@ExcelField(title="电子邮箱", attrName="email", align=Align.LEFT, sort=50),
|
||||
@ExcelField(title="手机号码", attrName="mobile", align=Align.CENTER, sort=60),
|
||||
@ExcelField(title="办公电话", attrName="phone", align=Align.CENTER, sort=70),
|
||||
@ExcelField(title="员工编码", attrName="employee.empCode", align=Align.CENTER, sort=80),
|
||||
@ExcelField(title="员工姓名", attrName="employee.empName", align=Align.CENTER, sort=95),
|
||||
@ExcelField(title="拥有角色", attrName="userRoleString", align=Align.LEFT, sort=800, fieldType=RoleListType.class),
|
||||
@ExcelField(title="最后登录日期", attrName="lastLoginDate", type=ExcelField.Type.EXPORT, dataFormat="yyyy-MM-dd HH:mm", align=Align.CENTER, sort=900),
|
||||
@ExcelField(title="拥有角色编号", attrName="userRoleString", align=Align.LEFT, sort=800, type=ExcelField.Type.IMPORT),
|
||||
@ExcelField(title="最后登录日期", attrName="lastLoginDate", align=Align.CENTER, sort=900, type=ExcelField.Type.EXPORT, dataFormat="yyyy-MM-dd HH:mm"),
|
||||
})
|
||||
public Employee getEmployee(){
|
||||
Employee employee = (Employee)super.getRefObj();
|
||||
|
||||
@@ -3,18 +3,28 @@
|
||||
*/
|
||||
package com.jeesite.modules.sys.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.validation.ConstraintViolationException;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.jeesite.common.entity.Page;
|
||||
import com.jeesite.common.idgen.IdGen;
|
||||
import com.jeesite.common.lang.StringUtils;
|
||||
import com.jeesite.common.service.CrudService;
|
||||
import com.jeesite.common.service.ServiceException;
|
||||
import com.jeesite.common.utils.excel.ExcelImport;
|
||||
import com.jeesite.common.validator.ValidatorUtils;
|
||||
import com.jeesite.modules.sys.dao.EmpUserDao;
|
||||
import com.jeesite.modules.sys.entity.EmpUser;
|
||||
import com.jeesite.modules.sys.entity.Employee;
|
||||
import com.jeesite.modules.sys.entity.User;
|
||||
import com.jeesite.modules.sys.utils.EmpUtils;
|
||||
import com.jeesite.modules.sys.utils.UserUtils;
|
||||
|
||||
/**
|
||||
* 员工管理Service
|
||||
@@ -93,6 +103,69 @@ public class EmpUserService extends CrudService<EmpUserDao, EmpUser> {
|
||||
employeeService.save(employee);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入用户数据
|
||||
* @param file 导入的用户数据文件
|
||||
* @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据
|
||||
*/
|
||||
@Transactional(readOnly=false)
|
||||
public String importData(MultipartFile file, Boolean isUpdateSupport) {
|
||||
if (file == null){
|
||||
throw new ServiceException("请选择导入的数据文件!");
|
||||
}
|
||||
int successNum = 0; int failureNum = 0;
|
||||
StringBuilder successMsg = new StringBuilder();
|
||||
StringBuilder failureMsg = new StringBuilder();
|
||||
try {
|
||||
ExcelImport ei = new ExcelImport(file, 2, 0);
|
||||
List<EmpUser> list = ei.getDataList(EmpUser.class);
|
||||
for (EmpUser user : list) {
|
||||
try{
|
||||
// 验证数据文件
|
||||
ValidatorUtils.validateWithException(user);
|
||||
// 验证是否存在这个用户
|
||||
User u = UserUtils.getByLoginCode(user.getLoginCode());
|
||||
if (u == null){
|
||||
this.save(user);
|
||||
successNum++;
|
||||
successMsg.append("<br/>" + successNum + "、账号 " + user.getLoginCode() + " 导入成功");
|
||||
} else if (isUpdateSupport){
|
||||
user.setUserCode(u.getUserCode());
|
||||
this.save(user);
|
||||
successNum++;
|
||||
successMsg.append("<br/>" + successNum + "、账号 " + user.getLoginCode() + " 更新成功");
|
||||
} else {
|
||||
failureNum++;
|
||||
failureMsg.append("<br/>" + failureNum + "、账号 " + user.getLoginCode() + " 已存在");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
failureNum++;
|
||||
String msg = "<br/>" + failureNum + "、账号 " + user.getLoginCode() + " 导入失败:";
|
||||
if (e instanceof ConstraintViolationException){
|
||||
List<String> messageList = ValidatorUtils.extractPropertyAndMessageAsList((ConstraintViolationException)e, ": ");
|
||||
for (String message : messageList) {
|
||||
msg += message + "; ";
|
||||
}
|
||||
}else{
|
||||
msg += e.getMessage();
|
||||
}
|
||||
failureMsg.append(msg);
|
||||
logger.error(msg, e);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
failureMsg.append(e.getMessage());
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
if (failureNum > 0) {
|
||||
failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
|
||||
throw new ServiceException(failureMsg.toString());
|
||||
}else{
|
||||
successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
|
||||
}
|
||||
return successMsg.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新状态
|
||||
*/
|
||||
|
||||
@@ -11,6 +11,7 @@ import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.shiro.authz.annotation.Logical;
|
||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
@@ -19,12 +20,16 @@ import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.jeesite.common.collect.ListUtils;
|
||||
import com.jeesite.common.collect.MapUtils;
|
||||
import com.jeesite.common.config.Global;
|
||||
import com.jeesite.common.entity.Page;
|
||||
import com.jeesite.common.lang.DateUtils;
|
||||
import com.jeesite.common.lang.StringUtils;
|
||||
import com.jeesite.common.utils.excel.ExcelExport;
|
||||
import com.jeesite.common.utils.excel.annotation.ExcelField.Type;
|
||||
import com.jeesite.common.web.BaseController;
|
||||
import com.jeesite.modules.sys.entity.EmpUser;
|
||||
import com.jeesite.modules.sys.entity.Employee;
|
||||
@@ -157,78 +162,57 @@ public class EmpUserController extends BaseController {
|
||||
return renderResult(Global.TRUE, text("保存用户''{0}''成功", empUser.getUserName()));
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 导出用户数据
|
||||
// */
|
||||
// @RequiresPermissions("sys:empUser:view")
|
||||
// @RequestMapping(value = "export", method = RequestMethod.POST)
|
||||
// public void exportFile(EmpUser empUser, HttpServletResponse response) {
|
||||
// List<EmpUser> list = empUserService.findList(empUser);
|
||||
// String fileName = "用户数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
|
||||
// new ExcelExport("用户数据", EmpUser.class).setDataList(list).write(response, fileName).dispose();
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 导入用户数据
|
||||
// */
|
||||
// @ResponseBody
|
||||
// @RequiresPermissions("sys:empUser:edit")
|
||||
// @RequestMapping(value = "import", method = RequestMethod.POST)
|
||||
// public String importFile(MultipartFile file) {
|
||||
// try {
|
||||
// int successNum = 0;
|
||||
// int failureNum = 0;
|
||||
// StringBuilder failureMsg = new StringBuilder();
|
||||
// ExcelImport ei = new ExcelImport(file, 1, 0);
|
||||
// List<EmpUser> list = ei.getDataList(EmpUser.class);
|
||||
// for (EmpUser user : list) {
|
||||
// try {
|
||||
// if (ObjectUtils.toBoolean(userService.checkLoginCode("", user.getUserCode(), null))){
|
||||
// ValidatorUtils.validateWithException(user);
|
||||
// empUserService.save(user);
|
||||
// successNum++;
|
||||
// } else {
|
||||
// failureMsg.append("<br/>登录账号 " + user.getUserCode() + " 已存在; ");
|
||||
// failureNum++;
|
||||
// }
|
||||
// } catch (ConstraintViolationException ex) {
|
||||
// failureMsg.append("<br/>登录账号 " + user.getUserCode() + " 导入失败:");
|
||||
// List<String> messageList = ValidatorUtils.extractPropertyAndMessageAsList(ex, ": ");
|
||||
// for (String message : messageList) {
|
||||
// failureMsg.append(message + "; ");
|
||||
// failureNum++;
|
||||
// }
|
||||
// } catch (Exception ex) {
|
||||
// failureMsg.append("<br/>登录名 " + user.getUserCode() + " 导入失败:" + ex.getMessage());
|
||||
// }
|
||||
// }
|
||||
// if (failureNum > 0) {
|
||||
// failureMsg.insert(0, ",失败 " + failureNum + " 条用户,导入信息如下:");
|
||||
// }
|
||||
// return renderResult(Global.TRUE, "已成功导入 " + successNum + " 条用户" + failureMsg);
|
||||
// } catch (Exception ex) {
|
||||
// return renderResult(Global.FALSE, "导入用户失败!失败信息:" + ex.getMessage());
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 下载导入用户数据模板
|
||||
// */
|
||||
// @RequiresPermissions("sys:empUser:view")
|
||||
// @RequestMapping(value = "import/template")
|
||||
// public String importFileTemplate(HttpServletRequest request, HttpServletResponse response) {
|
||||
// try {
|
||||
// String fileName = "用户数据导入模板.xlsx";
|
||||
// List<User> list = ListUtils.newArrayList();
|
||||
// list.add(UserUtils.getUser());
|
||||
// new ExcelExport("用户数据", User.class, Type.IMPORT).setDataList(list)
|
||||
// .write(response, fileName).dispose();
|
||||
// } catch (Exception e) {
|
||||
// request.setAttribute("message", "导入模板下载失败!失败信息:" + e.getMessage());
|
||||
// request.getRequestDispatcher("/error/404").forward(request, response);
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
/**
|
||||
* 导出用户数据
|
||||
*/
|
||||
@RequiresPermissions("sys:empUser:view")
|
||||
@RequestMapping(value = "exportData")
|
||||
public void exportData(EmpUser empUser, Boolean isAll, HttpServletResponse response) {
|
||||
empUser.getEmployee().getOffice().setIsQueryChildren(true);
|
||||
empUser.getEmployee().getCompany().setIsQueryChildren(true);
|
||||
if (!(isAll != null && isAll)){
|
||||
empUserService.addDataScopeFilter(empUser, UserDataScope.CTRL_PERMI_MANAGE);
|
||||
}
|
||||
List<EmpUser> list = empUserService.findList(empUser);
|
||||
String fileName = "用户数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
|
||||
new ExcelExport("用户数据", EmpUser.class).setDataList(list)
|
||||
.write(response, fileName).dispose();
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载导入用户数据模板
|
||||
*/
|
||||
@RequiresPermissions("sys:empUser:view")
|
||||
@RequestMapping(value = "importTemplate")
|
||||
public void importTemplate(HttpServletResponse response) {
|
||||
EmpUser empUser = new EmpUser();
|
||||
User user = UserUtils.getUser();
|
||||
if (User.USER_TYPE_EMPLOYEE.equals(user.getUserType())){
|
||||
empUser = empUserService.get(user.getUserCode());
|
||||
}else{
|
||||
BeanUtils.copyProperties(user, empUser);
|
||||
}
|
||||
List<EmpUser> list = ListUtils.newArrayList(empUser);
|
||||
String fileName = "用户数据模板.xlsx";
|
||||
new ExcelExport("用户数据", EmpUser.class, Type.IMPORT).setDataList(list)
|
||||
.write(response, fileName).dispose();
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入用户数据
|
||||
*/
|
||||
@ResponseBody
|
||||
@RequiresPermissions("sys:empUser:edit")
|
||||
@PostMapping(value = "importData")
|
||||
public String importData(MultipartFile file, String updateSupport) {
|
||||
try {
|
||||
boolean isUpdateSupport = Global.YES.equals(updateSupport);
|
||||
String message = empUserService.importData(file, isUpdateSupport);
|
||||
return renderResult(Global.TRUE, "posfull:"+message);
|
||||
} catch (Exception ex) {
|
||||
return renderResult(Global.FALSE, "posfull:"+ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 停用用户
|
||||
|
||||
@@ -10,6 +10,15 @@
|
||||
<% if(hasPermi('sys:empUser:edit')){ %>
|
||||
<a href="${ctx}/sys/empUser/form?op=add" class="btn btn-default btnTool" title="${text('新增用户')}"><i class="fa fa-plus"></i> ${text('新增')}</a>
|
||||
<% } %>
|
||||
<div class="btn-group">
|
||||
<a href="javascript:" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
|
||||
<i class="fa fa-navicon"></i> <span class="caret"></span>
|
||||
</a>
|
||||
<ul class="dropdown-menu">
|
||||
<li><a href="javascript:" id="btnExport"><i class="glyphicon glyphicon-export"></i> 导出</a></li>
|
||||
<li><a href="javascript:" id="btnImport"><i class="glyphicon glyphicon-import"></i> 导入</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="box-body">
|
||||
@@ -17,7 +26,7 @@
|
||||
data-page-no="${parameter.pageNo}" data-page-size="${parameter.pageSize}" data-order-by="${parameter.orderBy}">
|
||||
<div class="form-group">
|
||||
<label class="control-label">${text('账号')}:</label>
|
||||
<div class="control-inline">
|
||||
<div class="control-inline">
|
||||
<#form:input path="loginCode" maxlength="100" class="form-control width-90"/>
|
||||
</div>
|
||||
</div>
|
||||
@@ -85,7 +94,7 @@
|
||||
<button type="submit" class="btn btn-primary btn-sm">${text('查询')}</button>
|
||||
<button type="reset" class="btn btn-default btn-sm">${text('重置')}</button>
|
||||
</div>
|
||||
</#form:form>
|
||||
</#form:form>
|
||||
<table id="dataGrid"></table>
|
||||
<div id="dataGridPage"></div>
|
||||
</div>
|
||||
@@ -149,4 +158,61 @@ $('#dataGrid').dataGrid({
|
||||
|
||||
}
|
||||
});
|
||||
</script>
|
||||
$('#btnExport').click(function(){
|
||||
js.ajaxSubmitForm($('#searchForm'), {
|
||||
url:'${ctx}/sys/empUser/exportData',
|
||||
downloadFile:true
|
||||
});
|
||||
});
|
||||
$('#btnImport').click(function(){
|
||||
js.layer.open({
|
||||
type: 1,
|
||||
area: ['400px'],
|
||||
title: '导入用户数据',
|
||||
resize: false,
|
||||
scrollbar: true,
|
||||
content: js.template('importTpl'),
|
||||
success: function(layero, index){
|
||||
layero.find('input[type="checkbox"]').iCheck();
|
||||
},
|
||||
btn: ['<i class="fa fa-check"></i> 导入',
|
||||
'<i class="fa fa-remove"></i> 取消'],
|
||||
btn1: function(index, layero){
|
||||
var form = {
|
||||
inputForm: layero.find('#inputForm'),
|
||||
file: layero.find('#file').val()
|
||||
};
|
||||
if (form.file == '' || (!js.endWith(form.file, '.xls') && !js.endWith(form.file, '.xlsx'))){
|
||||
js.showMessage("${text('文件不正确,请选择后缀为 “xls”或“xlsx”的文件。')}", null, 'warning');
|
||||
return false;
|
||||
}
|
||||
js.ajaxSubmitForm(form.inputForm, function(data){
|
||||
js.showMessage(data.message);
|
||||
if(data.result == Global.TRUE){
|
||||
js.layer.closeAll();
|
||||
}
|
||||
page();
|
||||
}, "json");
|
||||
return true;
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
<script id="importTpl" type="text/template">//<!--
|
||||
<form id="inputForm" action="${ctx}/sys/empUser/importData" method="post" enctype="multipart/form-data"
|
||||
class="form-horizontal mt20 mb10" style="overflow:auto;max-height:200px;">
|
||||
<div class="row">
|
||||
<div class="col-xs-12 col-xs-offset-1">
|
||||
<input type="file" id="file" name="file" class="form-file"/>
|
||||
<div class="mt10 pt5">
|
||||
<#form:checkbox name="updateSupport" label="${text('否更新已经存在的用户数据')}" class="form-control"
|
||||
title="${text('如果用户编码已经存在,更新这条数据。')}"/>
|
||||
<a href="${ctx}/sys/empUser/importTemplate" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> ${text('下载模板')}</a>
|
||||
</div>
|
||||
<font color="red" class="pull-left mt10">
|
||||
${text('提示:仅允许导入“xls”或“xlsx”格式文件!')}
|
||||
</font>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
//--></script>
|
||||
Reference in New Issue
Block a user