package com.ss.apitask;
import java.io.IOException;
import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;
import org.apache.axis.AxisFault;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.ss.data.ISubsDomain;
import com.ss.notice.NotifyService;
import com.inspur.paas.StartSubscribe;
import com.inspur.paas.util.*;
import javax.xml.namespace.QName;
/**
*思路:定时抓取数据(线程睡眠机制),首先查询哪些用户订阅了数据,然后根据这些用户的社保账号信息,
* 取社保详情,之后比对数据,上次抓取的数据是否记录在案,否则插入数据(抓取数据完成)
*
**/
public class ShebaoApiWorker extends ApiWorker {
private String[] columns = {"cardID","password"};
private static Log log = LogFactory.getLog(ShebaoApiWorker.class);
private static String send_id=ConfUtil.getValue("mc_subscribe_sendid").toString();
private static String send_name=ConfUtil.getValue("site_name").toString();
private static String msg_type=ConfUtil.getValue("mc_shebao_msgtype").toString();
private static ISubsDomain subsDomain;
private List subsList = new ArrayList();
public static ISubsDomain getSubsDomain() {
if (subsDomain != null) {
return subsDomain;
} else {
subsDomain = (ISubsDomain) StartSubscribe.getApplicationContext()
.getBean("SubsDomain", ISubsDomain.class);
return subsDomain;
}
}
public void run() {
Map shebaoParam = new HashMap();
String cache_time = (String) this.apiInfo.get("cache_time");
Map param = new HashMap();
// 订阅用户
subsList = getSubsList();
try {
if(subsList.size()>0){
List<Map> shebaoData = new ArrayList();
for(int k=0, len = subsList.size();k<len;k++){
Map temp = (Map) subsList.get(k);
//社保账号
String note = temp.get("note").toString();
//解密
Map shebaoParamInfo = getDecode(JsonUtil.readToObject(note, Map.class));
shebaoParamInfo.put("uid", temp.get("uid"));
//抓数据
Map shebaoInfo = getSheBaoInfo(shebaoParamInfo);
//数据List
shebaoData.add(shebaoInfo);
}
if(shebaoData.size()>0){
//存数据和发消息
parseData(shebaoData);
}
}
subsList.clear();
long start = System.currentTimeMillis();
//睡眠
Calendar cl = Calendar.getInstance();
int day = Integer.parseInt(cache_time.replace("d", ""));
cl.add(Calendar.DATE, day);
Date d = new Date();
long sleepTime = cl.getTimeInMillis()-d.getTime();
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
long end = System.currentTimeMillis();
log.error("社保抓取数据时sleep时间在:"+(end-start)+"毫秒时出现异常", e);
}
} catch (IOException e) {
long start = System.currentTimeMillis();
//睡眠
Calendar cl = Calendar.getInstance();
int day = Integer.parseInt(cache_time.replace("d", ""));
cl.add(Calendar.DATE, day);
Date d = new Date();
long sleepTime = cl.getTimeInMillis()-d.getTime();
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e1) {
long end = System.currentTimeMillis();
log.error("社保抓取数据时sleep时间在:"+(end-start)+"毫秒时出现异常", e1);
}
log.error("社保抓取数据出错"+(List)subsList, e);
}
}
//数据量比较大时,查询会对数据库造成大压力,可以每次查询5000次
private synchronized List getSubsList(){
if(subsList == null){
subsList = new ArrayList();
}
if(subsList.size()>0){
return subsList;
}
subsList.clear();
int num = 5000;
int nowCount = 0;
int page = 1;
Map sub = new HashMap();
sub.put("uc_api_id", this.apiId);
sub.put("num", num);
do{
sub.put("page", page);
page++;
Map subMap = getSubsDomain().getSubss(sub);
List subList = (List) subMap.get("data");
nowCount = subList.size();
for(int k=0, len = subList.size();k<len;k++){
subsList.add(subList.get(k));
}
}while(nowCount == num);
System.out.println("subsMap 次数"+subsList.size());
return subsList;
}
private synchronized int parseData(List<Map> dataList){
if(dataList.size()<0){
return 0;
}
for(Map temp : dataList){
int uid = Integer.parseInt(temp.get("uid").toString());
Map payInfo = new HashMap();
//yanglao
boolean yangl = false;
String yanglDay = "";
if(temp.get("yanglao") != null){
Map yanglao = (Map)temp.get("yanglao");
Map yanglaoParam = new HashMap();
yanglaoParam.put("uid", uid);
yanglaoParam.put("type", "shebao");
yanglaoParam.put("title", "yanglao");
try {
yanglaoParam.put("content", JsonUtil.convertToString(yanglao));
} catch (IOException e) {
log.error("社保-养老转json出错:"+yanglao, e);
}
String yanglaoHash = CodeUtil.md5(yanglao.get("jfdate").toString()+yanglao.get("ysdate").toString()+String.valueOf(uid)+"yanglao");
yanglaoParam.put("hash", yanglaoHash);
Map perYanglao = getDataDomain().getDataMap(yanglaoParam);
if(perYanglao.size() == 0){
getDataDomain().addData(yanglaoParam);
yangl = true;
yanglDay = yanglao.get("jfdate").toString();
}
}
payInfo.put("yangl", yangl);
payInfo.put("yanglDay", yanglDay);
//yiliao
boolean yil = false;
String yilDay = "";
if(temp.get("yiliao") != null){
Map yiliao= (Map)temp.get("yiliao");
Map yiliaoParam = new HashMap();
yiliaoParam.put("uid", uid);
yiliaoParam.put("type", "shebao");
yiliaoParam.put("title", "yiliao");
try {
yiliaoParam.put("content", JsonUtil.convertToString(yiliao));
} catch (IOException e) {
log.error("社保-医疗转json出错:"+yiliao, e);
}
String yiliaoHash = CodeUtil.md5(yiliao.get("jfdate").toString()+yiliao.get("ysdate").toString()+String.valueOf(uid)+"yiliao");
yiliaoParam.put("hash", yiliaoHash);
Map perYiliao = getDataDomain().getDataMap(yiliaoParam);
if(perYiliao.size() == 0){
getDataDomain().addData(yiliaoParam);
yil = true;
yilDay = yiliao.get("jfdate").toString();
}
}
payInfo.put("yil", yil);
payInfo.put("yilDay", yilDay);
//shiy
boolean shiy = false;
String shiyDay = "";
if(temp.get("shiye") != null){
Map shiye = (Map)temp.get("shiye");
Map shiyeParam = new HashMap();
shiyeParam.put("uid", uid);
shiyeParam.put("type", "shebao");
shiyeParam.put("title", "shiye");
//内容shiye
//============================ToDO
try {
shiyeParam.put("content", JsonUtil.convertToString(shiye));
} catch (IOException e) {
log.error("社保-失业转json出错:"+shiye, e);
}
String shiyeHash = CodeUtil.md5(shiye.get("jfdate").toString()+shiye.get("ysdate").toString()+String.valueOf(uid)+"shiye");
shiyeParam.put("hash", shiyeHash);
Map perShiye = getDataDomain().getDataMap(shiyeParam);
if(perShiye.size() == 0){
getDataDomain().addData(shiyeParam);
shiy = true;
shiyDay = shiye.get("jfdate").toString();
}
}
payInfo.put("shiy", shiy);
payInfo.put("shiyDay", shiyDay);
sendMsg(uid, payInfo);
}
return 1;
}
private Map getDecode(Map map){
if (map != null) {
for (int i = 0; i < columns.length; i++) {
String column = columns[i];
if(map.get(column) != null){
map.put(column,AESUtil.decode(String.valueOf(map.get(column))));
}
}
}
return map;
}
private void sendMsg(int uid, Map payInfo){
if( uid <= 0 ){//update 数据
return;
}
String str = "";
if(Boolean.parseBoolean(payInfo.get("yangl").toString())){
str += "养老保险("+payInfo.get("yanglDay").toString()+")";
}
if(Boolean.parseBoolean(payInfo.get("yil").toString())){
str += "、医疗保险("+payInfo.get("yilDay").toString()+")";
}
if(Boolean.parseBoolean(payInfo.get("shiy").toString())){
str += "、失业保险("+payInfo.get("shiyDay").toString()+")";
}
if(!("".equals(str))){
Map notifyMap = new HashMap();
notifyMap.put("send_id", send_id);
notifyMap.put("user_id", uid);
notifyMap.put("send_name", send_name);
notifyMap.put("msg_type", msg_type);
notifyMap.put("param1", str);
NotifyService.addNotification(notifyMap);
}
}
private Map getSheBaoInfo(Map shebaoParam){
Map res = new HashMap();
res.put("uid", shebaoParam.get("uid").toString());
String url = ConfUtil.getValue("shebao_getInfo").toString();
String cardID = shebaoParam.get("cardID").toString();
String password = shebaoParam.get("password").toString();
Calendar cal = Calendar.getInstance(Locale.CHINA);
String nowYear = new SimpleDateFormat( "yyyy").format(cal.getTime());
String nowMonth = new SimpleDateFormat( "MM").format(cal.getTime());
// 登录CheckLogin(string cardID, string password, string userCode)
String soapaction = "http://tempuri.org/";
String funcnameLogin = "CheckLogin";
String paramsLogin[] = {"cardID","password","userCode"};
String runParamsLogin[] = {cardID,password,"lc"};
Boolean isLogin = false;
String contentLogin = "";
try {
contentLogin = callWsApiForAxis(url, soapaction, funcnameLogin, paramsLogin, runParamsLogin);
// System.out.println("LoginResXml===="+contentLogin);
} catch (Exception e1) {
log.error("调登录出错"+funcnameLogin, e1);
}
if(contentLogin != null && !"".equals(contentLogin) && !"1".equals(contentLogin) && !"2".equals(contentLogin) && !"0".equals(contentLogin)){
Map loginRes = xmlLogin(contentLogin);
if(loginRes != null){
isLogin = true;
}
System.out.println("loginRes=="+loginRes);
}
if(isLogin){
// 养老GetSocialInsuranceInfo(string cardID, string year, string month, string userCode)
String funcnameYanglao = "GetSocialInsuranceInfo";
String paramsYanglao[] = {"cardID","year","month","userCode"};
String runParamsYanglao[] = {cardID,nowYear,nowMonth,"lc"};
String contentYanglao = "";
try {
contentYanglao = callWsApiForAxis(url, soapaction, funcnameYanglao, paramsYanglao, runParamsYanglao);
// System.out.println("YanglaoResXml===="+contentYanglao);
} catch (Exception e2) {
log.error("调养老出错"+funcnameYanglao, e2);
}
if(contentYanglao != null && !"".equals(contentYanglao) && !"1".equals(contentYanglao) && !"2".equals(contentYanglao) && !"3".equals(contentYanglao)){
Map yanglaoRes = xmlShebao(contentYanglao);
if(yanglaoRes != null){
res.put("yanglao", yanglaoRes);
}
System.out.println("YanglaoRes===" +yanglaoRes);
}
// 医疗GetMedicalInsuranceInfo (string cardID, string year, string month, string userCode)
String funcnameYiliao = "GetMedicalInsuranceInfo";
String paramsYiliao[] = {"cardID","year","month","userCode"};
String runParamsYiliao[] = {cardID,nowYear,nowMonth,"lc"};
String contentYiliao = "";
try {
contentYiliao = callWsApiForAxis(url, soapaction, funcnameYiliao, paramsYiliao, runParamsYiliao);
// System.out.println("YiliaoResXml===="+contentYiliao);
} catch (Exception e2) {
log.error("调医疗出错"+funcnameYiliao, e2);
}
if(contentYiliao != null && !"".equals(contentYiliao) && !"1".equals(contentYiliao) && !"2".equals(contentYiliao) && !"3".equals(contentYiliao)){
Map yiliaoRes = xmlShebao(contentYiliao);
if(yiliaoRes != null){
res.put("yiliao", yiliaoRes);
}
System.out.println("YiliaoRes===" +yiliaoRes);
}
// 失业GetIdlenessInfo (string cardID, string year, string month, string userCode)
String funcnameShiye = "GetIdlenessInfo";
String paramsShiye[] = {"cardID","year","month","userCode"};
String runParamsShiye[] = {cardID,nowYear,nowMonth,"lc"};
String contentShiye = "";
try {
contentShiye = callWsApiForAxis(url, soapaction, funcnameShiye, paramsShiye, runParamsShiye);
// System.out.println("ShiyeResXml===="+contentShiye);
} catch (Exception e3) {
log.error("调失业出错"+funcnameShiye, e3);
}
if(contentShiye != null && !"".equals(contentShiye) && !"1".equals(contentShiye) && !"2".equals(contentShiye) && !"3".equals(contentShiye)){
Map shiyeRes = xmlShebao(contentShiye);
if(shiyeRes != null){
res.put("shiye", shiyeRes);
}
System.out.println("ShiyeRes===" +shiyeRes);
}
// 退出LogOut()
String funcnameLogout = "LogOut";
String paramsLogout[] = null;
String runParamsLogout[] = null;
String contentLogout = "";
try {
contentLogout = callWsApiForAxis(url, soapaction, funcnameLogout, paramsLogout, runParamsLogout);
System.out.println("LogoutResXml===="+contentLogout);
} catch (Exception e1) {
log.error("调退出出错"+funcnameLogin, e1);
}
}
System.out.println("调取接口的所有Res================"+res);
return res;
}
public String callWsApiForAxis(String url,String soapaction,String funcname,String params[],String runParams[])throws Exception{
String resXmlStr = "";
try {
Service service = new Service();
Call call;
call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
call.setOperationName(new QName(soapaction, funcname)); // 设置要调用哪个方法
if(params != null){
for(int i = 0 ; i < params.length ; i++){
call.addParameter(
new QName(soapaction, params[i]), // 设置要传递的参数
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
}
}
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 返回类型
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapaction + funcname);
resXmlStr = (String) call.invoke(runParams);
} catch (AxisFault e) {
log.error("调用webservice数据出错"+funcname, e);
}
return resXmlStr;
}
public Map xmlShebao(String resXmlStr){
if(resXmlStr == null && "".equals(resXmlStr)){
return null;
}
Map res = new HashMap();
try{
StringReader read = new StringReader(resXmlStr);
InputSource source = new InputSource(read);
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(source);
Element root = doc.getRootElement();
List jiedian = root.getChildren();
if(jiedian.size()>0){
root = (Element) jiedian.get(0);
jiedian = root.getChildren();
Element et = null;
for (int i = 0; i < jiedian.size(); i++) {
et = (Element) jiedian.get(i);
//et.getAttributeValue("id") 获取xml里id的属性值
if(et.getAttributeValue("id")!= null && "jfdate".equals(et.getAttributeValue("id").toString())){
res.put("jfdate", et.getText());
}
if(et.getAttributeValue("id")!= null && "ysdate".equals(et.getAttributeValue("id").toString())){
res.put("ysdate", et.getText());
}
if(et.getAttributeValue("id")!= null && "dwname".equals(et.getAttributeValue("id").toString())){
res.put("dwname", et.getText());
}
if(et.getAttributeValue("id")!= null && "jftype".equals(et.getAttributeValue("id").toString())){
res.put("jftype", et.getText());
}
if(et.getAttributeValue("id")!= null && "grjs".equals(et.getAttributeValue("id").toString())){
res.put("grjs", et.getText());
}
if(et.getAttributeValue("id")!= null && "dwjf".equals(et.getAttributeValue("id").toString())){
res.put("dwjf", et.getText());
}
if(et.getAttributeValue("id")!= null && "spjf".equals(et.getAttributeValue("id").toString())){
res.put("spjf", et.getText());
}
if(et.getAttributeValue("id")!= null && "jfbz".equals(et.getAttributeValue("id").toString())){
res.put("jfbz", et.getText());
}
}
}
}catch (Exception e) {
log.error("解析webservice返回数据出错"+resXmlStr, e);
}
return res;
}
public Map xmlLogin(String resXmlStr){
if(resXmlStr == null && "".equals(resXmlStr)){
return null;
}
Map res = new HashMap();
try{
StringReader read = new StringReader(resXmlStr);
InputSource source = new InputSource(read);
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(source);
Element root = doc.getRootElement();
List jiedian = root.getChildren();
if(jiedian.size()>0){
root = (Element) jiedian.get(0);
jiedian = root.getChildren();
Element et = null;
Map yanglao = new HashMap();
for (int i = 0; i < jiedian.size(); i++) {
et = (Element) jiedian.get(i);
if(et.getAttributeValue("id")!= null && "cardID".equals(et.getAttributeValue("id").toString())){
res.put("cardID", et.getText());
}
if(et.getAttributeValue("id")!= null && "name".equals(et.getAttributeValue("id").toString())){
res.put("name", et.getText());
}
}
}
}catch (Exception e) {
log.error("解析登录返回数据出错"+resXmlStr, e);
}
return res;
}
}