klxedu / blog

2 stars 0 forks source link

产品通用导入功能实现方案 #43

Open songhuiqing88 opened 6 years ago

songhuiqing88 commented 6 years ago

1、创建具体的业务ExcelBean继承ExcelBean,以资讯为实例。明确每列对应的具体业务属性,根据每列的具体要求,确定其ExcelFieldMeta对应的属性。nullAble指代是否允许为空,默认为true(允许为空),maxLength指代该字段最大长度,日期类型若不指定具体的dateFormat,默认为"yyyy/MM/dd"。

public class InformationExcelBean extends ExcelBean<InformationExcelBean>{

    public class ImportResult {
        private boolean result;
        private String message;

        public boolean isResult() {
            return result;
        }

        public void setResult(boolean result) {
            this.result = result;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

    }

    public ImportResult instanceImportResult() {
        return new ImportResult();
    }

    @ExcelFieldMeta(cell = 1, nullAble = false, maxLength = 180)
    private String title;       // 资源标题

    @ExcelFieldMeta(cell = 2, maxLength = 40)
    private String source;      // 来源

    @ExcelFieldMeta(cell = 3, nullAble = false, maxLength = 40)
    private String author;      //作者

    @ExcelFieldMeta(cell = 4, maxLength = 180)
    private String keyWord;     // 关键字

    private String classifyId;  //分类ID

    @ExcelFieldMeta(cell = 5, nullAble = false, maxLength = 40)
    private String classifyName;// 分类名称

    @ExcelFieldMeta(cell = 6, maxLength = 200)
    private String description; // 简介

    private String content;     //资源详情
    @ExcelFieldMeta(cell = 7, nullAble = false, maxLength = 100)
    private String contentPath; // 详情文件对应路径

    @ExcelFieldMeta(cell = 8)
    private String picPath;     // 图片对应路径

    @ExcelFieldMeta(cell = 9)
    private String videoPaths;  // 视频文件路径

    @ExcelFieldMeta(cell = 10)
    private String attPaths;    // 附件文件路径

    @ExcelFieldMeta(cell = 11, dateFormat="yyyy/MM/dd HH:mm:ss")
    private Date publishTime;   // 发布时间

    @ExcelFieldMeta(cell = 12, maxLength = 50)
    private String scopeCode;   // Scopecode

    private int isTop;
    @ExcelFieldMeta(cell = 13, maxLength = 2)
    private String isTopStr;        // 是否置顶

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * @return the source
     */
    public String getSource() {
        return source;
    }

    /**
     * @param source the source to set
     */
    public void setSource(String source) {
        this.source = source;
    }

    /**
     * @return the author
     */
    public String getAuthor() {
        return author;
    }

    /**
     * @param author the author to set
     */
    public void setAuthor(String author) {
        this.author = author;
    }

    /**
     * @return the keyWord
     */
    public String getKeyWord() {
        return keyWord;
    }

    /**
     * @param keyWord the keyWord to set
     */
    public void setKeyWord(String keyWord) {
        this.keyWord = keyWord;
    }

    /**
     * @return the classifyId
     */
    public String getClassifyId() {
        return classifyId;
    }

    /**
     * @param classifyId the classifyId to set
     */
    public void setClassifyId(String classifyId) {
        this.classifyId = classifyId;
    }

    /**
     * @return the classifyName
     */
    public String getClassifyName() {
        return classifyName;
    }

    /**
     * @param classifyName the classifyName to set
     */
    public void setClassifyName(String classifyName) {
        this.classifyName = classifyName;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the contentPath
     */
    public String getContentPath() {
        return contentPath;
    }

    /**
     * @param contentPath the contentPath to set
     */
    public void setContentPath(String contentPath) {
        this.contentPath = contentPath;
    }

    /**
     * @return the picPath
     */
    public String getPicPath() {
        return picPath;
    }

    /**
     * @param picPath the picPath to set
     */
    public void setPicPath(String picPath) {
        this.picPath = picPath;
    }

    /**
     * @return the videoPaths
     */
    public String getVideoPaths() {
        return videoPaths;
    }

    /**
     * @param videoPaths the videoPaths to set
     */
    public void setVideoPaths(String videoPaths) {
        this.videoPaths = videoPaths;
    }

    /**
     * @return the attPaths
     */
    public String getAttPaths() {
        return attPaths;
    }

    /**
     * @param attPaths the attPaths to set
     */
    public void setAttPaths(String attPaths) {
        this.attPaths = attPaths;
    }

    /**
     * @return the publishTime
     */
    public Date getPublishTime() {
        return publishTime;
    }

    /**
     * @param publishTime the publishTime to set
     */
    public void setPublishTime(Date publishTime) {
        this.publishTime = publishTime;
    }

    /**
     * @return the scopeCode
     */
    public String getScopeCode() {
        return scopeCode;
    }

    /**
     * @param scopeCode the scopeCode to set
     */
    public void setScopeCode(String scopeCode) {
        this.scopeCode = scopeCode;
    }

    /**
     * @return the isTop
     */
    public int getIsTop() {
        return isTop;
    }

    /**
     * @param isTop the isTop to set
     */
    public void setIsTop(int isTop) {
        this.isTop = isTop;
    }

    /**
     * @return the isTopStr
     */
    public String getIsTopStr() {
        return isTopStr;
    }

    /**
     * @param isTopStr the isTopStr to set
     */
    public void setIsTopStr(String isTopStr) {
        this.isTopStr = isTopStr;
    }

    /**
     * @return the content
     */
    public String getContent() {
        return content;
    }

    /**
     * @param content the content to set
     */
    public void setContent(String content) {
        this.content = content;
    }

2、创建继承DealDataAbstract抽象类的service,且需加上service注解

    @Override 
    public boolean checkData(FileImportDataBean fileImportDataBean) {
        // 根据具体业务逻辑校验上传数据,基本的非空及字段长度校验,已经处理,不需要在这里继续处理
        return false;
    }

    @Override
    public void doProcess(FileImportDataBean fileImportDataBean) {
        //执行具体的业务,如:数据入库等

    }

    @Override
    public ExcelBean findRealExcelBean() {
        // 返回具体的ExcelBean,已使得可解析对应的Excel
        return null;
    }

    //可直接使用的方法有
    /**
     * 校验文件是否存在
     * 前提:filePath不为空
     * 支持单个与多个文件的校验,多文件使用逗号分隔
     * @param fileImportDataBean
     * @param fileTitle 当前Excel的唯一标识列
     * @param filePath  具体文件路径
     */
    protected void verifyFileExist(FileImportDataBean fileImportDataBean, String fileTitle,String filePath) {

    }

    /**
     * 获取字典表内指定value对应的key
     * @param fileImportDataBean
     * @param dictList              字典列表
     * @param currentValue          指定值
     * @return 有值,返回具体的字典key,否则返回null
     */
    protected String findRealValueByDict(FileImportDataBean fileImportDataBean, List<DictDto> dictList,String currentValue){

    }
    /**
     * 
     * @param fileImportDataBean
     * @param fileTitle     Excel唯一表示列名称
     * @param contentPath   内容文件路径
     * @return  成功时返回内容的详细信息,失败时返回null
     */
    protected String fillRealContent(FileImportDataBean fileImportDataBean, String fileTitle, String contentPath) {

    }

在FileImportDataBean中通过如下方法,获取当前Excel的所有对象

List<InformationExcelBean> excelBeanList = (List<InformationExcelBean>) fileImportDataBean.getExcelImportResult().getResultList();

3.在页面引入导入的模板:

3.1在具体的页面引入导入的三个按钮,导入模板下载、导入、导入结果查看

<span class="btn btn-primary pull-right btn-sm add" ng-if="isShowFunction('informationmng-manage')" ng-click="preAdd()">
   <span class="glyphicon glyphicon-list">导入结果查看</span>
 </span>
<span class="btn btn-primary pull-right btn-sm add" ng-if="isShowFunction('informationmng-manage')" ng-click="openImportModel()">
   <span class="glyphicon glyphicon-cloud-upload" >导入</span>
</span>
<span class="btn btn-primary pull-right btn-sm add" ng-if="isShowFunction('informationmng-manage')" ng-click="preAdd()">
   <span class="glyphicon glyphicon-cloud-download" >导入模板下载</span>
</span>

3.2在uiState中引入如下js

+ ",../../securityworkbench/workbench/importmodel/importfile.html,../../securityworkbench/workbench/importmodel/importfileservice.js"       //导入需要js

注意:若指定的页面内,无上传相关js,还需引入

+",asserts/js/plugin/fileupload/ng-file-upload-shim.min.js,asserts/js/plugin/fileupload/ng-file-upload.min.js"// 图片上传

3.3在对应页面的service的angular.module中引入"Module.importfileservice",若当前模板未引入上传,还需添加"ngFileUpload"

3.4在对应页面的ctrl中引入importfileservice 且在Ctrl中实现openImportModel方法 注意:根据具体业务调整内部参数。

//导入弹窗
$scope.openImportModel = function(){
    var items = new Object();
    items.orgCode = "informationInfo";  //该参数与具体service的注解保持一致
    items.beginRow = 3; //指定入口excel的开始行
    items.needExtractor = true; //是否需要解压缩
    items.uploadTempFileName = "informationimportmodel.xlsx";  //指定的Excel入口文件
    items.callbackFunction = $scope.refreshsystem; //刷新当前页面的方法
    importfileservice.openExcelExportModal(items).result.then(function(data){
        console.log(data);
    });
}

若传入的参数不在框架提供范围内可使用otherParams传递相关参数

    var obj = {};
    obj['demo'] = "demo";
    items.otherParMap = obj;