package com.lyms.hospital.job;
import java.util.List;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lyms.base.common.entity.sys.SyncDataBasic;
import com.lyms.base.common.service.sys.SyncDataBasicService;
import com.lyms.sync.channel.ChannelData;
import com.lyms.util.HttpUtils;
import com.lyms.util.JsonUtils;
/**
* <li>@ClassName: SyncTableJob
* <li>@Description: 同步任务执行器,获取SYNC_DATA_BASIC表格的数据进行推送到中心
* <li>@author 方承
* <li>@date 2017年5月5日
* <li>
*/
public class SyncPushToCenterJob {
@Value("${center.syncpush.url}")
private String CENTER_SYNCPUSH_URL;
private static final int ERROR_COUNT_MAX = 4;
private static Logger log = LoggerFactory.getLogger(SyncPushToCenterJob.class);
@Autowired
private SyncDataBasicService syncDataBasicService;
@SuppressWarnings("unused")
public void excute() {
if(null != CENTER_SYNCPUSH_URL && CENTER_SYNCPUSH_URL.startsWith("http")){
List<SyncDataBasic> dataList = syncDataBasicService.selectList(new EntityWrapper<SyncDataBasic>().where("IFSUC=0").and("ERROR_COUNT < {0}", ERROR_COUNT_MAX));
if(dataList != null ){
log.debug("=======开始进行 SYNC_DATA_BASIC 表格数据的推送到中心=======");
for(SyncDataBasic entity : dataList){
try{
ChannelData data = JsonUtils.jsonToBean(entity.getData(), ChannelData.class);
data.setRemote(CENTER_SYNCPUSH_URL);
log.debug("节点推送数据:"+ToStringBuilder.reflectionToString(data));
String result = HttpUtils.REMOTE.post(data);
if(result == null){//远程无法访问等情况
syncDataBasicService.errorCountAdd(entity.getId());
return;
}
ChannelData model = JsonUtils.jsonToBean(result, ChannelData.class);
entity.setIfsuc(1);
syncDataBasicService.updateById(entity);
log.debug("中心处后数据:"+result);
}catch (Exception e) {
syncDataBasicService.errorCountAdd(entity.getId());
log.error("【SYNC_DATA_BASIC】表格数据的的推送到中心错误:SyncDataBasic = {} {}",JSONObject.toJSON(entity),e);
}
}
}
}
}
}