package com.dxp.springbootnetty.netty.handler;
import com.dxp.springbootnetty.domain.Contact;
import com.dxp.springbootnetty.domain.Endowment;
import com.dxp.springbootnetty.domain.Sobase;
import com.dxp.springbootnetty.domain.Yiliaodangan;
import com.dxp.springbootnetty.domain.Yiliaoyushou;
import com.dxp.springbootnetty.domain.Yliao;
import com.dxp.springbootnetty.domain.Yliaochaxu;
import com.dxp.springbootnetty.netty.ChannelRepository;
import com.dxp.springbootnetty.repository.ContactMapper;
import com.dxp.springbootnetty.repository.YlfMapper;
import com.dxp.springbootnetty.service.ContactService;
import com.dxp.springbootnetty.service.EndowmentService;
import com.dxp.springbootnetty.service.MedicalBaseService;
import com.dxp.springbootnetty.service.MedicalBaseServiceImp;
import com.dxp.springbootnetty.service.SobaseService;
import com.dxp.springbootnetty.service.YiliaodanganService;
import com.dxp.springbootnetty.service.YiliaoyushouService;
import com.dxp.springbootnetty.service.YliaoService;
import com.dxp.springbootnetty.service.YliaochaxuService;
import com.dxp.springbootnetty.service.vo.StudentInfo;
import com.google.gson.Gson;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.concurrent.Future;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @author carzy
* @date 2018/08/28
*/
@Component
@ChannelHandler.Sharable
public class StringHandler extends SimpleChannelInboundHandler<String> {
private Logger logger = LoggerFactory.getLogger(StringHandler.class);
private final ChannelRepository channelRepository;
private final Gson gson;
private final ContactService contactService;
private final SobaseService sobaseService;
private final EndowmentService endowmentService;
private final YlfMapper ylfMapper;
private final YliaoService yliaoService;
private final YiliaodanganService yiliaodanganService;
private final MedicalBaseService medicalBaseService;
private final YiliaoyushouService yiliaoyushouService;
private final YliaochaxuService yliaochaxuService;
private final MedicalBaseServiceImp medicalBaseServiceImp;
@Autowired
public StringHandler(ChannelRepository channelRepository,
Gson gson,
ContactService contactService,
SobaseService sobaseService,
EndowmentService endowmentService,
YlfMapper ylfMapper,
MedicalBaseServiceImp medicalBaseServiceImp,
YiliaodanganService yiliaodanganService,
YliaoService yliaoService,
MedicalBaseService medicalBaseService,
YiliaoyushouService yiliaoyushouService,
YliaochaxuService yliaochaxuService
)
{
this.channelRepository = channelRepository;
this.gson = gson;
this.contactService = contactService;
this.sobaseService=sobaseService;
this.endowmentService=endowmentService;
this.ylfMapper=ylfMapper;
this.medicalBaseServiceImp=medicalBaseServiceImp;
this.yiliaodanganService=yiliaodanganService;
this.yliaoService=yliaoService;
this.medicalBaseService=medicalBaseService;
this.yiliaoyushouService=yiliaoyushouService;
this.yliaochaxuService=yliaochaxuService;
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception
{
String[] strarray = msg.trim().split("\\|;");
String retWire="";
String type="00";
if(strarray.length>0)
{
for (String temstr : strarray)
{
System.out.println(temstr);
String[] sonarray=temstr.split("\\|");
type=sonarray[0].trim();
//信息
if(type.equals("01"))
{
String sfzh=sonarray[4].trim();
retWire= contactService.findSfzh(sfzh);
}
//查询
if(type.equals("02"))
{
System.out.println("type="+type);
String sfzh=sonarray[4].trim();
retWire=sobaseService.findJs(sfzh);
}
//缴费
//03|10|123456|01|330724197311131137|李伟荣|WD2084|010302225176|Y2018090123314500140|2018|2247.77|0|5394.60|;
if(type.equals("03"))
{
Endowment endowment=getEndowment(sonarray);
Endowment ylfEndowment =endowmentService.seachEndowment(endowment);
if(ylfEndowment!=null&&ylfEndowment.getId()>0)
{
String rettemString="";
String typeString="03|";
String chkString="1|";
String sfzhString="|";
String sbidString="|";
String xtbmString="|";
String stateString="|";
Endowment tem=ylfEndowment;
if(tem.getSfzh()!=null&&tem.getSfzh().length()>0)
{
sfzhString=tem.getSfzh()+"|";
sbidString=tem.getSbid()+"|";
xtbmString=String.valueOf(tem.getId())+"|";
stateString="成功"+"|";
}
rettemString=typeString+chkString+stateString+sfzhString+sbidString+xtbmString+";";
ctx.writeAndFlush(Unpooled.copiedBuffer(rettemString.getBytes("GBK")));
}
else
{
endowmentService.Endowmentadd(endowment,ctx);
}
}
if(type.equals("04"))
{
String sfzh=sonarray[4].trim();
String yltype=sonarray[3].trim();
if(yltype.equals("02"))
{
Yiliaodangan yl=yiliaodanganService.Dangan(sfzh);
if(yl!=null)
{
System.out.println("contact="+yl.getXm());
retWire=medicalBaseServiceImp.getMedicalmsg(sfzh,yl);
}
else {
retWire=type+"|-1|无效用户|"+sfzh+"|||||;";
}
}
}
//yljf 05|20|654321|02|610104195804245116|朱振星|010039|010300023673|15929951626|1001275518|Y2019012110055308545|2019|4850.00|0.00|4850.00|;
if(type.equals("05"))
{
String yltype=sonarray[3].trim();
if(yltype.equals("02"))
{
String sfzhString=sonarray[4].trim();
Yiliaodangan yl=yiliaodanganService.Dangan(sfzhString);
if(yl!=null)
{
String sbbj=sonarray[10];
Yiliaoyushou yliao=getYliao(sonarray,yl);
if(yliao!=null)
{
yiliaoyushouService.yushouSave(yliao);
}
else
{