//提交报销信息
@RequestMapping(value = {"/saveexpense","/process/saveexpense"},method = RequestMethod.POST)
@ResponseBody
public ResultMap saveexpense(@RequestParam(value = "ifdraft",required = false)Boolean ifdraft,@RequestParam(value = "formSign",required = false)String formSign,
@RequestParam(value = "officeid",required = false)String officeid,@RequestParam(value = "officeType",required = false)String officeType,
ExpenseTitle title, @RequestParam(value = "newfile",required = false) String newfile, @RequestParam(value = "json",required = false) String json,
@RequestParam(value = "json1",required = false) String json1,HttpSession session,HttpServletRequest request){
ResultMap resultMap=null;
JSONObject data=null;
String toppid="";
try {
if(title.getTitle()==null&&officeid==null&&formSign==null&&newfile==null){
//解析请求
String string = expenseApplyService.analyzeRequestParameter(request);
if(StringUtils.isNotBlank(string)) {
data = JSONObject.parseObject(string, JSONObject.class);
formSign=data.getString("formSign");
officeid=data.getString("officeid");
officeType=data.getString("officeType");
String titlejson=data.getString("title");
title=JSON.parseObject(titlejson,new ExpenseTitle().getClass());
newfile=data.getString("newfile");
json=data.getString("json");
json1=data.getString("json1");
}
}
title.setStatus(0);
title.setApplytime(new Date());
expenseApplyService.saveTitle(title);
Integer eid=title.getEid();
if(!StringUtils.isBlank(newfile)){
List<? extends ExpenseFile> expenseFiles= JSON.parseArray(newfile,new ExpenseFile().getClass());
for (ExpenseFile file:expenseFiles
) {
file.setEid(eid);
expenseApplyService.savefile(file);
}
}
if(!StringUtils.isEmpty(json)){
List<? extends ExpenseRel> expenseRelList= JSON.parseArray(json,new ExpenseRel().getClass());
for (ExpenseRel rel: expenseRelList
) {
rel.setEid(eid);
expenseApplyService.saveRel(rel);
}
}
//List<Integer> loanIdArray=new ArrayList<>();
if(!StringUtils.isEmpty(json1)) {
List<? extends ExpenseDetail> expenseDetailList = JSON.parseArray(json1, new ExpenseDetail().getClass());
for (ExpenseDetail detail : expenseDetailList
) {
/*if(title.getPaytype()==4){
loanIdArray.add(detail.getLoanid());
}*/
if(!toppid.equals("")&&!toppid.contains(detail.getToppid()+"")){
return new ResultMap(-1,"只能选择一个科目大项",null);
}
detail.setEid(eid);
expenseApplyService.saveDetail(detail);
if(toppid.equals(""))
toppid+=""+detail.getToppid();
}
}
/*if(title.getPaytype()==4){
expenseApplyService.updateLoanState(loanIdArray);
}*/
String draftid=request.getParameter("draftid");
if(draftid!=null){
expenseApplyService.deletedraftid(Integer.valueOf(draftid));
}
} catch (Exception e) {
e.printStackTrace();
return new ResultMap(-1,"表单保存失败",null);
}
}
/**
* 获取post请求中的参数
* @param request
* @return
* @
*/
@Override
public String analyzeRequestParameter(HttpServletRequest request) throws IOException{
String string ="";
//若data有值即为键值对String data=request.getParameter("data");
String data=null;
if(StringUtils.isNotBlank(data)){
string=data;
}else{
InputStream inputStream = request.getInputStream();
ByteArrayOutputStream reqDataByteArrayOutputStream = new ByteArrayOutputStream();
// 1、请求数据的获取
int length = request.getContentLength();
System.out.println("length:"+length);
if (length == 0) {
throw new RuntimeException();
}
byte[] b = new byte[1024];
int tempLength = -1;
while ((tempLength = inputStream.read(b)) != -1) {
reqDataByteArrayOutputStream.write(b, 0, tempLength);
}
if (reqDataByteArrayOutputStream.size() != request.getContentLength()) {// 读取失败
// throw new RuntimeException();
}
string = reqDataByteArrayOutputStream.toString();
}
// System.err.println("string:\n"+string);
return string;
}