初始化table列
/**
* 初始化表格的列
*/
Haraidashiresult.initColumn = function () {
return [
{field: 'selectItem', radio: true, visible: false},
{
title: 'NO', align: 'center', valign: 'middle',
formatter: function (value, row, index) {
row.visible = false;
var pageSize = $('#' + Haraidashiresult.id).bootstrapTable('getOptions').pageSize; //通过table的#id 得到每页多少条
var pageNumber = $('#' + Haraidashiresult.id).bootstrapTable('getOptions').pageNumber; //通过table的#id 得到当前第几页
return pageSize * (pageNumber - 1) + index + 1; // 返回每条的序号: 每页条数 *(当前页 - 1 )+ 序号
}
},
{title: '出庫NO', field: 'data_no', visible: true, align: 'center', valign: 'middle', sortable: true},
{title: '品番/反番', field: 'partNumReCd', visible: true, align: 'center', valign: 'middle', sortable: true},
{title: '品名', field: 'machineNamePartsName', align: 'center', valign: 'middle'},
{title: '枝番', field: 'data_no_branch_nu', align: 'center', valign: 'middle', sortable: true},
{title: 'シリアルNO', field: 'serialNo', align: 'center', valign: 'middle', sortable: true},
{title: 'ステータス', field: 'status', align: 'center', valign: 'middle'},
{title: 'メッセージ', field: 'msg', align: 'center', valign: 'middle'}
];
};
js
layui.use(['upload', 'table'], function(){
var $ = layui.jquery,upload = layui.upload,table = layui.table;
upload.render({
elem: '#selectFile'
,url: zcm.ctxPath + '/t-haraidashi-result/importCsv'
,auto: false
,accept:'file'
,exts:'csv'
,bindAction: '#startUpload'
,done: function(res){
var list =new Array();
list = res.data;
for(var i= 0;i<list.length;i++){
var rows = { //要插入的数据,这里要和table列名一致
data_no: res.data[i].dataNo,
partNumReCd: res.data[i].partNumReCd,
machineNamePartsName: res.data[i].machineNamePartsName,
data_no_branch_nu: res.data[i].dataNoBranchNu,
serialNo: res.data[i].serialNo,
status: res.data[i].status,
msg: res.data[i].msg
}
$('#HaraidashiResultTable').bootstrapTable('insertRow', {
index: 0,
row: rows
});
}
},
});
});
后台逻辑
将各种错误情况 设置到前端需要显示的 VO 中,然后js遍历 insert 到table 中
@Override
@Transactional
public Object insertCsvInfo(MultipartFile file)throws Exception {
Page page = LayuiPageFactory.defaultPage();
if (file != null) {
InputStream inputStream = null;
List<THaraidashiResult> list = null;
try {
inputStream = file.getInputStream();
list = readCSVFile(inputStream);
} catch (IOException e) {
return new ServiceException(600, "正しいフォーマットのファイルで取込んでください。");
}
List<THaraidashiResult> importList = new ArrayList<THaraidashiResult>();
if (list.size() > 0) {
List<THaraidashiResultVo> tHaraidashiResultVo = new ArrayList<THaraidashiResultVo>(); //前台显示vo
//拼装前台显示vo
for (int i = 0; i < list.size(); i++) {
String epc = itHaraidashiResultService.selectEPCByEPCAndDataNoAndDataNoBranchNu(list.get(i).getSerialNo(),list.get(i).getDataNo(), list.get(i).getDataNoBranchNu());
String detail_part_num_re_cd = itHaraidashiDetailService.selectPartNumReNumByDataNoAndDataNoBranchNu
(list.get(i).getDataNo(), list.get(i).getDataNoBranchNu());
String stock_part_num_re_cd = itStockService.selectPartNumReCdBySerialNo(list.get(i).getSerialNo());
String name = imMechanicalLedgerService.selectNameByPartNumRdCd(stock_part_num_re_cd);//用t_stock表的品番反番 b 查机械章台name
BigDecimal stock_price = itStockService.selectUnitPriceBySerialNo(list.get(i).getSerialNo());
BigDecimal detail_price = itHaraidashiDetailService.selectUnitPriceByDataNoAndDataNoBranchNu(list.get(i).getDataNo(), list.get(i).getDataNoBranchNu());
String stock_yard_code = itStockService.selectYardCdByEPC(list.get(i).getSerialNo());
String yard_code = itHaraidashiService.selectYardCdByDataNo(list.get(i).getDataNo());
//发生错误的情况
//在库表没有该epc
if(stock_part_num_re_cd == null || stock_part_num_re_cd.equals("")){
THaraidashiResultVo haraidashiResultVo = new THaraidashiResultVo();
haraidashiResultVo.setSerialNo(GetAfterUtil.getAfter8(list.get(i).getSerialNo()));
haraidashiResultVo.setDataNo(list.get(i).getDataNo());
haraidashiResultVo.setDataNoBranchNu(list.get(i).getDataNoBranchNu());
haraidashiResultVo.setStatus(ImportState.FAIL.getMsg());
//haraidashiResultVo.setPartNumReCd("");
//haraidashiResultVo.setMachineNamePartsName("");
haraidashiResultVo.setMsg(ImportStateMsg.MSG3.getMsg());
tHaraidashiResultVo.add(haraidashiResultVo);
} else if (list.get(i).getSerialNo().equals(epc)) {//1.出库result已经存在该epc
THaraidashiResultVo haraidashiResultVo = new THaraidashiResultVo();
haraidashiResultVo.setSerialNo(GetAfterUtil.getAfter8(list.get(i).getSerialNo()));//后8位
haraidashiResultVo.setDataNo(list.get(i).getDataNo());
haraidashiResultVo.setDataNoBranchNu(list.get(i).getDataNoBranchNu());
haraidashiResultVo.setStatus(ImportState.FAIL.getMsg());
haraidashiResultVo.setPartNumReCd(stock_part_num_re_cd);//通过serial_no找到t_stock的part_num_re_cd
haraidashiResultVo.setMachineNamePartsName(name);//machine_name+"_"+parts_name
haraidashiResultVo.setMsg(ImportStateMsg.MSG4.getMsg());
tHaraidashiResultVo.add(haraidashiResultVo);
} else if (!stock_part_num_re_cd.equals(detail_part_num_re_cd)) {//品番不一致
THaraidashiResultVo haraidashiResultVo = new THaraidashiResultVo();
haraidashiResultVo.setSerialNo(GetAfterUtil.getAfter8(list.get(i).getSerialNo()));//后8位
haraidashiResultVo.setDataNo(list.get(i).getDataNo());
haraidashiResultVo.setDataNoBranchNu(list.get(i).getDataNoBranchNu());
haraidashiResultVo.setStatus(ImportState.FAIL.getMsg());
haraidashiResultVo.setPartNumReCd(stock_part_num_re_cd);//通过serial_no找到t_stock的part_num_re_cd
haraidashiResultVo.setMachineNamePartsName(name);//machine_name+"_"+parts_name
haraidashiResultVo.setMsg(ImportStateMsg.MSG3.getMsg());
tHaraidashiResultVo.add(haraidashiResultVo);
} else if (stock_part_num_re_cd.equals(detail_part_num_re_cd) && (stock_price.compareTo(detail_price) != 0)) {//单价不一致
THaraidashiResultVo haraidashiResultVo = new THaraidashiResultVo();
haraidashiResultVo.setSerialNo(GetAfterUtil.getAfter8(list.get(i).getSerialNo()));//后8位
haraidashiResultVo.setDataNo(list.get(i).getDataNo());
haraidashiResultVo.setDataNoBranchNu(list.get(i).getDataNoBranchNu());
haraidashiResultVo.setStatus(ImportState.FAIL.getMsg());
haraidashiResultVo.setPartNumReCd(stock_part_num_re_cd);//通过serial_no找到t_stock的part_num_re_cd
haraidashiResultVo.setMachineNamePartsName(name);//machine_name+"_"+parts_name
haraidashiResultVo.setMsg(ImportStateMsg.MSG2.getMsg());
tHaraidashiResultVo.add(haraidashiResultVo);
} else if (stock_part_num_re_cd.equals(detail_part_num_re_cd) && (!stock_yard_code.equals(yard_code))) {//yard_code不一致
THaraidashiResultVo haraidashiResultVo = new THaraidashiResultVo();
haraidashiResultVo.setSerialNo(GetAfterUtil.getAfter8(list.get(i).getSerialNo()));//后8位
haraidashiResultVo.setDataNo(list.get(i).getDataNo());
haraidashiResultVo.setDataNoBranchNu(list.get(i).getDataNoBranchNu());
haraidashiResultVo.setStatus(ImportState.FAIL.getMsg());
haraidashiResultVo.setPartNumReCd(stock_part_num_re_cd);//通过serial_no找到t_stock的part_num_re_cd
haraidashiResultVo.setMachineNamePartsName(name);//machine_name+"_"+parts_name
haraidashiResultVo.setMsg(ImportStateMsg.MSG1.getMsg());
tHaraidashiResultVo.add(haraidashiResultVo);
} else {
THaraidashiResultVo haraidashiResultVo = new THaraidashiResultVo();
haraidashiResultVo.setSerialNo(GetAfterUtil.getAfter8(list.get(i).getSerialNo()));//后8位
haraidashiResultVo.setDataNo(list.get(i).getDataNo());
haraidashiResultVo.setDataNoBranchNu(list.get(i).getDataNoBranchNu());
haraidashiResultVo.setStatus(ImportState.SUCCESS.getMsg());
haraidashiResultVo.setPartNumReCd(stock_part_num_re_cd);//通过serial_no找到t_stock的part_num_re_cd
haraidashiResultVo.setMachineNamePartsName(name);//machine_name+"_"+parts_name
tHaraidashiResultVo.add(haraidashiResultVo);
//序列号取epc后8位
list.get(i).setSerialNo(GetAfterUtil.getAfter8(list.get(i).getSerialNo()));
importList.add(list.get(i));
}
}
//①将数据保存到出庫(result)中 //成功的数据
if (importList.size() > 0) {
itHaraidashiResultService.saveBatches(importList);
//②将出库数量反应到出庫(detail)中
for (int i = 0; i < importList.size(); i++) {
//data_no 和 支号 对应的总数
BigDecimal quantity = itHaraidashiResultService.selectQuantityByDataNoAndDataNoBranchNu(importList.get(i).getDataNo(), importList.get(i).getDataNoBranchNu());
itHaraidashiDetailService.updateIssueQuantityByDataNoAndDataNoBranchNu(importList.get(i).getDataNo(), importList.get(i).getDataNoBranchNu(),quantity);
}
}
page.setRecords(tHaraidashiResultVo);
}
return LayuiPageFactory.createPageInfo(page);
} else {
return page;
}
}
读取csv 文件方法
public static List<THaraidashiResult> readCSVFile(InputStream inputStream) throws IOException {
List<THaraidashiResult> csvList = new ArrayList<THaraidashiResult>();
CsvReader reader = null;
THaraidashiResult tHaraidashiResult = null;
Timestamp ts = null;
Date date = null;
reader = new CsvReader(inputStream, ',', Charset.forName("UTF-8"));
reader.readHeaders(); // 跳过表头 如果需要表头的话,不要写这句。
while (reader.readRecord()) { // 逐行读入除表头的数据
tHaraidashiResult = new THaraidashiResult();
tHaraidashiResult.setEpc(reader.get("EPC"));
tHaraidashiResult.setSerialNo(reader.get("EPC"));//epc 后8位GetAfterUtil.getAfter8(
tHaraidashiResult.setDataNo(reader.get("data_no"));
tHaraidashiResult.setDataNoBranchNu(new BigDecimal(reader.get("data_no_branch_nu")));
tHaraidashiResult.setReversalClass(new BigDecimal(0));
tHaraidashiResult.setQuantity(new BigDecimal(1));
csvList.add(tHaraidashiResult);
}
return csvList;
}