使用http request输入流方式获取接口传递的数据

package cn.abchinalife.om.controller.payment;

import cn.abchinalife.om.repository.om.LySendToFSUniqeNoDao;
import cn.abchinalife.om.repository.om.LyTransStatusDao;
import cn.abchinalife.om.repository.om.LyfslogDao;
import cn.abchinalife.om.repository.om.entity.*;
import cn.abchinalife.om.repository.om.entity.batchUUCP.ATS;
import cn.abchinalife.om.repository.om.entity.batchUUCP.IN;
import cn.abchinalife.om.repository.om.entity.batchUUCP.PUB;
import cn.abchinalife.om.repository.om.entity.batchUUCP.RD;
import cn.abchinalife.om.service.CheckFinshReturnService;
import cn.abchinalife.om.service.myservices.BatchRepeatedCheckService;
import cn.abchinalife.om.util.PubFun;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.TypeReference;
//import com.alibaba.fastjson.JSONReader;


/**
* Created by Ontheway on 2019/5/31.
*/
@Slf4j
@Controller
public class BachControllerReauest {


@Autowired
private BatchRepeatedCheckService batchRepeatedCheckService;
//记录交易状态与业务状态
@Autowired
private LyTransStatusDao lyTransStatusDao;
@Autowired
private LyfslogDao lyfslogDao;

@Autowired LySendToFSUniqeNoDao lySendToFSUniqeNoDao;

private int port=0;
private int serverPort=0;


@ResponseBody
@RequestMapping(value = "/ws/bachController.do")
public String submitData(HttpServletResponse response, HttpServletRequest request) throws IOException {
log.info("-----------submitData----------");
//String json = request.getParameter("param"); //这是通过get方式去url 拼接的键值对,post方式取不到值。
request.setCharacterEncoding("UTF-8"); //返回页面防止出现中文乱码
StringBuffer requestURL = request.getRequestURL();
String remoteAddr = request.getRemoteAddr();
int serverPort = request.getServerPort();
log.info("serverPort------------"+serverPort);
port = request.getRemotePort();
log.info("requestURL:"+requestURL.toString()+" remoteAddr:"+remoteAddr+" port:"+port);

BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));//post方式传递读取字符流
String jsonStr = null;
StringBuilder result = new StringBuilder();
try {
while ((jsonStr = reader.readLine()) != null) {
result.append(jsonStr);
}
} catch (IOException e) {
e.printStackTrace();
}
reader.close();// 关闭输入流

log.info("result===="+result.toString());
//ArrayList<ATS> atsList = JSON.parseObject(result.toString(), new TypeReference<ArrayList<ATS>>(){}); //转换为集合对象
ATS ATSInfo = JSON.parseObject(result.toString(), new TypeReference<ATS>(){});
log.info("ATS:"+ATSInfo.toString());
PUB pub = ATSInfo.getPUB();
log.info(" pub.getAppId()"+pub.getAppId()+" pub.getTransSeq():"+pub.getTransSeq());
IN in = ATSInfo.getIN();
log.info("ATS:"+in.toString());
log.info("批次号:"+in.getBatchNo()+" 总金额:"+in.getTotalAmount()+" 总笔数:"+in.getTotalCount());
List<RD> rdList = in.getRD();
log.info("rdList.size():" + rdList.size());
for (int j=0;j<rdList.size();j++) {
RD rd = rdList.get(j);
//单条数据落地
dataLandingLyTransStatus(pub,in,rd);
dataLandingLyfslog(pub,in,rd);
dataLandingLySendToFSUniqeNoInfo(pub,in,rd);
//封装成对象 进行数据传递
LySendToFSInfo lySendToFSInfo = lySendToFSInfo(pub, in, rd);
log.info("lySendToFSInfo:"+j+" 次"+lySendToFSInfo);
//校验数据
//batchRepeatedCheckService.doService(lySendToFSInfo);
}
/*
//19 位账号 尾号为0
//转换为集合对象
for(int i=0; i<atsList.size();i++){
ATS ATSInfo = atsList.get(i);
log.info("ATS:"+ATSInfo);
PUB pub = ATSInfo.getPUB();
log.info(" pub.getAppId()"+pub.getAppId()+" pub.getTransSeq():"+pub.getTransSeq());

IN in = ATSInfo.getIN();
log.info("ATS:"+in);
log.info("批次号:"+i+"次 "+in.getBatchNo()+" 总金额:"+in.getTotalAmount()+" 总笔数:"+in.getTotalCount());

List<RD> rdList = in.getRD();
for (int j=0;j<rdList.size();j++){
log.info("rdList.size():"+rdList.size());
RD rd = rdList.get(j);
log.info("rd:"+rd);

rd.getAccName();
rd.getAccNo();
rd.getAccType();
rd.getAgentCode();
rd.getAgentCode();
rd.getBankCode();
rd.getBankCode();
rd.getBusinessType();
rd.getCliamNo();
rd.getCliamNo();
rd.getComCode();
rd.getContactCode();
rd.getContactCode();
rd.getContactName();
rd.getContactNo();
rd.getContNo();
rd.getConvertFlag();
rd.getDataType();

log.info("bankCode:"+j+"次"+rd.getBankCode());

log.info("UniqueNo:"+j+"次"+rd.getSerialNo());
//单条数据落地
dataLandingLyTransStatus(pub,in,rd);
dataLandingLyfslog(pub,in,rd);
dataLandingLySendToFSUniqeNoInfo(pub,in,rd);

//封装成对象 进行数据传递
LySendToFSInfo lySendToFSInfo = lySendToFSInfo(pub, in, rd);
log.info("lySendToFSInfo:"+j+" 次"+lySendToFSInfo);
//校验数据
//batchRepeatedCheckService.doService(lySendToFSInfo);
}
}*/
//dealData();
CheckFinshReturnService batchReturn =new CheckFinshReturnService();
//batchReturn.sendMessage();
log.info("-------批次信息接收成功!-----end");
return "G88";

}

//lyfslog 落库
public void dataLandingLyfslog(PUB pub,IN in,RD rd){
Lyfslog lyfslog=new Lyfslog();
lyfslog.setTotalMoney(Double.parseDouble(in.getTotalAmount()));
lyfslog.setTotalNum(Integer.valueOf(in.getTotalCount()));
lyfslog.setModifyDate(Date.valueOf(PubFun.getCurrentDate()));
lyfslog.setModifyTime(PubFun.getCurrentTime());
log.info("pub.getTransDate():"+pub.getTransDate());
//lyfslog.setTransDate(Date.valueOf( pub.getTransDate()));
lyfslog.setBatchNo(in.getBatchNo());
lyfslog.setComCode(rd.getComCode());
lyfslog.setOperationType(rd.getDoType());//业务类型
/* lyfslog.setSuccMoney();
lyfslog.setSuccNum();//成功金额
lyfslog.setCheckMoney();
lyfslog.setCheckNum();
lyfslog.setCheckNum();
lyfslog.setDealState(); //处理状态
lyfslog.setTransOperator(); //交易人*/
try {
batchRepeatedCheckService.insertLyfslog(lyfslog);
}catch (Exception ex){
log.info("添加Lyfslog数据异常");
ex.printStackTrace();
}
}

//LyTransStatus 落库
public void dataLandingLyTransStatus(PUB pub,IN in,RD rd){
LyTransStatus lyTransStatus =new LyTransStatus();
LySendToFSInfo lysend = new LySendToFSInfo();
//封装传递的数据
lysend.setBatchNo(in.getBatchNo());
lysend.setPolNo(rd.getPolNo());
lysend.setContNo(rd.getContNo());
lysend.setUniqueNo(rd.getSerialNo());
lysend.setPayMoney(Double.valueOf(rd.getPayMoney()));
lysend.setAccName(rd.getAccName());
lysend.setAccNo(rd.getAccNo());
lysend.setAgentCode(rd.getBusinessType());
lysend.setBusinessType(rd.getBusinessType());
lysend.setBankCode(rd.getBankCode());
String uniqueSerialNo = batchRepeatedCheckService.getUniqueComposition(rd.getBusinessType(),lysend);
if(!uniqueSerialNo.equals("")&&uniqueSerialNo !=null){
lyTransStatus.setSerialNo(uniqueSerialNo); //全局唯一流水
}
lyTransStatus.setBatchNo(in.getBatchNo());
//lyTransStatus.setSerialNo();
lyTransStatus.setSysFlag(pub.getSYSFlag());
lyTransStatus.setClientPort(String.valueOf(port));
lyTransStatus.setClientIP(String.valueOf(serverPort));
//lyTransStatus.setTransStatus();
lyTransStatus.setTransTime(pub.getTransTime());
String transSeq=pub.getTransSeq()+(int)(Math.random()*100000+(int)(Math.random()*9999)+(int)(Math.random()*999));
log.info("transSeq:"+transSeq);
lyTransStatus.setTransSeq(transSeq); //交易流水号 暂时这样写
lyTransStatus.setMakeDate(Date.valueOf(PubFun.getCurrentDate()));
lyTransStatus.setMakeTime(PubFun.getCurrentTime());
//lyTransStatus.setDescr();
lyTransStatus.setModifyDate(Date.valueOf(PubFun.getCurrentDate()));
lyTransStatus.setModifyTime(PubFun.getCurrentTime());
//lyTransStatus.setServiceCostTime();//交易耗时
lyTransStatus.setServiceStartTime(PubFun.getCurrentDate());//服务处理开始时间
//lyTransStatus.setServiceEndTime();
//lyTransStatus.setStatus(); //业务状态
lyTransStatus.setSysCode(pub.getSYSCode());
//lyTransStatus.setTransDate(Date.valueOf(pub.getTransDate()));

//新增数据
try {
batchRepeatedCheckService.insertLyTransStatus(lyTransStatus);
}catch (Exception ex){
log.info("添加lyTransStatus数据异常");
ex.printStackTrace();
}
}
//LySendToFSUniqeNoInfo 落库
public void dataLandingLySendToFSUniqeNoInfo(PUB pub,IN in,RD rd){
LySendToFSUniqeNoInfo lySendToFSUniqeNo = new LySendToFSUniqeNoInfo();

if(!rd.getXpayTimes().equals("")&&rd.getXpayTimes()!=null){
lySendToFSUniqeNo.setXpayTimes(Integer.parseInt(rd.getXpayTimes()));
}
log.info("UniqueNo:"+rd.getSerialNo());
lySendToFSUniqeNo.setUniqueNo(rd.getSerialNo());
lySendToFSUniqeNo.setTxID(rd.getTxID());
lySendToFSUniqeNo.setTempFeeNo(rd.getTempFeeNo());
lySendToFSUniqeNo.setSyscode(pub.getSYSCode());
//lySendToFSUniqeNo.setState();
log.info("rd.getShouldDate():"+rd.getShouldDate()+" rd.getSendDate():"+rd.getSendDate());
if(!rd.getShouldDate().equals("")&&rd.getShouldDate()!=null){
lySendToFSUniqeNo.setShouldDate( Date.valueOf(rd.getShouldDate()));//应付日期
}
lySendToFSUniqeNo.setSfflag(rd.getSFFlag());
//lySendToFSUniqeNo.setSendTimes();//发盘次数
lySendToFSUniqeNo.setSendDate(Date.valueOf(rd.getSendDate()));
lySendToFSUniqeNo.setPayType(rd.getPayType());
lySendToFSUniqeNo.setPayMoney(Double.valueOf(rd.getPayMoney()));
lySendToFSUniqeNo.setPayCode(rd.getPayCode());
lySendToFSUniqeNo.setName(rd.getName());
lySendToFSUniqeNo.setModifyTime(PubFun.getCurrentTime());
lySendToFSUniqeNo.setModifyDate(Date.valueOf(PubFun.getCurrentDate()));
lySendToFSUniqeNo.setInBankCode(rd.getInBankCode());
lySendToFSUniqeNo.setInAccType(rd.getInAccType());
lySendToFSUniqeNo.setInAccNo(rd.getInAccNo());
lySendToFSUniqeNo.setInAccName(rd.getInAccName());
lySendToFSUniqeNo.setEdorNo(rd.getEdorNo());
lySendToFSUniqeNo.setDoType(rd.getDoType());
lySendToFSUniqeNo.setDealType("1"); //0-实时 1-批量
lySendToFSUniqeNo.setConvertFlag(rd.getConvertFlag());
lySendToFSUniqeNo.setContNo(rd.getContNo());
lySendToFSUniqeNo.setClaimNo(rd.getCliamNo());
lySendToFSUniqeNo.setBusinessType(rd.getBusinessType());
lySendToFSUniqeNo.setBatchNo(in.getBatchNo());
//lySendToFSUniqeNo.setBankDealTime(); //银行处理时间
//lySendToFSUniqeNo.setBankDealDate();
//lySendToFSUniqeNo.setBankSuccFlag();
lySendToFSUniqeNo.setBankCode(rd.getBankCode());
lySendToFSUniqeNo.setAccType(rd.getAccType());
lySendToFSUniqeNo.setAccNo(rd.getAccNo());
lySendToFSUniqeNo.setAccName(rd.getAccName());
//lySendToFSUniqeNo.setAbstract(); //对账码

/*in.getBatchNo();
in.getTotalAmount();
in.getTotalCount();
pub.getAppId();
pub.getSign();
pub.getSYSCode();
pub.getSYSFlag();
pub.getTimestamp();
pub.getTimestamp();
pub.getTransDate();
pub.getTransSeq();
pub.getTransTime();*/

//新增数据
try {
batchRepeatedCheckService.insertLySendToFSUniqeNoInfo(lySendToFSUniqeNo);
}catch (Exception ex){
log.info("添加LySendToFSUniqeNo数据异常");
ex.printStackTrace();
}
}

//获取json解析数据 封装成对象 进行数据传递
public LySendToFSInfo lySendToFSInfo(PUB pub,IN in,RD rd){
LySendToFSInfo lysend = new LySendToFSInfo();
//封装传递的数据
lysend.setBatchNo(in.getBatchNo());
lysend.setPolNo(rd.getPolNo());
lysend.setContNo(rd.getContNo());
lysend.setUniqueNo(rd.getSerialNo());
lysend.setPayMoney(Double.valueOf(rd.getPayMoney()));
lysend.setAccName(rd.getAccName());
lysend.setAccNo(rd.getAccNo());
lysend.setAccType(rd.getAccType());
lysend.setAgentCode(rd.getBusinessType());
lysend.setBankCode(rd.getBankCode());
//lysend.setBankDealDate();
//lysend.setAbstract();
if(!rd.getXpayTimes().equals("")&&rd.getXpayTimes()!=null){
lysend.setXpayTimes(Integer.valueOf(rd.getXpayTimes()));
}
lysend.setUniqueNo(rd.getSerialNo());
lysend.setTxID(rd.getTxID());
lysend.setTempFeeNo(rd.getTempFeeNo());
lysend.setSysCode(pub.getSYSCode());
//lySendToFSUniqeNo.setState();
if(!rd.getShouldDate().equals("")&&rd.getShouldDate()!=null){
lysend.setShouldDate( Date.valueOf(rd.getShouldDate()));//应付日期
}
lysend.setSfFlag(rd.getSFFlag());
//lySendToFSUniqeNo.setSendTimes();//发盘次数
lysend.setSendDate(Date.valueOf(rd.getSendDate()));
lysend.setPayType(rd.getPayType());
lysend.setPayMoney(Double.valueOf(rd.getPayMoney()));
lysend.setPayCode(rd.getPayCode());
lysend.setName(rd.getName());
lysend.setModifyTime(PubFun.getCurrentTime());
lysend.setModifyDate(Date.valueOf(PubFun.getCurrentDate()));
lysend.setInBankCode(rd.getInBankCode());
lysend.setInAccType(rd.getInAccType());
lysend.setInAccNo(rd.getInAccNo());
lysend.setInAccName(rd.getInAccName());
lysend.setEdorNo(rd.getEdorNo());
lysend.setDoType(rd.getDoType());
lysend.setDealType("1"); //0-实时 1-批量
lysend.setConvertFlag(rd.getConvertFlag());
lysend.setContNo(rd.getContNo());
lysend.setClaimNo(rd.getCliamNo());
lysend.setBusinessType(rd.getBusinessType());
lysend.setBatchNo(in.getBatchNo());
/* lysend.setBankDealTime(); //银行处理时间
lysend.setBankDealDate();
lysend.setBankSuccFlag();*/
lysend.setBankCode(rd.getBankCode());
lysend.setAccType(rd.getAccType());
lysend.setAccNo(rd.getAccNo());
lysend.setAccName(rd.getAccName());
//lysend.setAbstract(); //对账码

return lysend;
}


}

posted @ 2019-06-20 09:16  徒手攀岩  阅读(3653)  评论(0编辑  收藏  举报