导入Excel将数据更新到数据库

前台layui

html代码

<button class="layui-btn layui-btn-primary" id="uploadCredit" data-kercode="400002" style="display: none"><i class="layui-icon"></i>导入
        </button>

js代码

 //上传授信额度
    upload.render({
        elem: '#uploadCredit'
        , url: url_scm_basic + 'api/SupplierManage/uploadCredit?token=' + getToken()
        , accept: 'file' //普通文件
        , exts: 'xls|xlsx'
        , field: 'file'
        , before: function (obj) {
            showProcess();
            $('#uploadTemplate').attr("disabled", "disabled");
        },
        // 上传完毕后的回调
        done: function (res, index, upload) {
            hiddenProcess();
            $('#uploadCredit').removeAttr("disabled");
            if (res.code === 0) {
                layer.msg(res.msg, {icon: 6});
                location.reload();
            } else {
                layer.alert(res.msg, {icon: 5, time: 5000, title: false, closeBtn: 0, shadeClose: true});
            }
        },
        // 上传失败时的回调
        error: function (res, index, upload) {
            hiddenProcess();
            $('#uploadCredit').removeAttr("disabled");
        }
    });

controller代码

controller层的配置

serviceImpl代码

@Override
	public Map<String, Object> uploadCredit(List<UploadCreditModel> list) throws IllegalAccessException {
		BeanUtils.trimThisListObjectStringFields(list);

        Map<String, String> noPassInf = inspectionPassedForUploadCreditInf(list);
        if (ObjectUtils.isEmpty(noPassInf)) {
        	saveOrUpdateCredit(list);
            return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "更新成功", "");
        } else {
            return ResultUtil.put(ConstantUtil.UPLOAD_FAIL, noPassInf.values().iterator().next(), "");
        }
	}
	
    private Map<String, String> inspectionPassedForUploadCreditInf(List<UploadCreditModel> list) {
        Map<String, String> noPassInf = new LinkedHashMap<>();
        for (UploadCreditModel uploadCreditModel : list) {
        	//经销商编码
            String supplierCode = uploadCreditModel.getSupplierCode();
            //经销商名称
            //String supplierName = uploadCreditModel.getSupplierName();
            // 经销商编码不能为空
            if (!StringUtils.isEmpty(supplierCode)) {
	            SupplierManage supplierManage = supplierManageDao.get(new WherePrams().where("supplier_code", supplierCode));
	            if(null != supplierManage){
	            	continue;
	            }else{
	            	// 经销商编码不能为空
	                    noPassInf.put(supplierCode , "错误 : 不存在编码为" + supplierCode + " 的经销商记录,请核对后再导入");
	            }
            }else{
            	continue;
            }
            
        }
        return noPassInf;
    }

    private void saveOrUpdateCredit(List<UploadCreditModel> list) {
        for (UploadCreditModel uploadCreditModel : list) {
            String supplierCode = uploadCreditModel.getSupplierCode();
            SupplierManage supplierManage = supplierManageDao.get(new WherePrams().where("supplier_code", supplierCode));
            //如果授信额度为空,则默认是0
            if(StringUtils.isEmpty(uploadCreditModel.getCreditLine())){
            	supplierManage.setCreditLine("0");
            //如果授信额度写的不变,则不改原值
            }else if (UPDATA_STATIC_CONSTANT.equals(uploadCreditModel.getCreditLine())) {
            	supplierManage.setCreditLine(supplierManage.getCreditLine());
            }else{
            	supplierManage.setCreditLine(uploadCreditModel.getCreditLine());
            }
            supplierManageDao.updateLocal(supplierManage);
           
        } 
    }

猜你喜欢

转载自blog.csdn.net/Sun_of_Rainy/article/details/89102714
今日推荐