Commit 949d5a1ced8c8ef19911089e5b90cfefa99df0f4
1 parent
ca0a8d0f27
Exists in
master
and in
8 other branches
resyncdata
Showing 2 changed files with 277 additions and 0 deletions
platform-operate-api/src/main/java/com/lyms/platform/operate/web/controller/InitDataController.java
View file @
949d5a1
| 1 | +package com.lyms.platform.operate.web.controller; | |
| 2 | + | |
| 3 | +import com.lyms.platform.common.base.BaseController; | |
| 4 | +import com.lyms.platform.common.dao.operator.MongoCondition; | |
| 5 | +import com.lyms.platform.common.dao.operator.MongoOper; | |
| 6 | +import com.lyms.platform.common.dao.operator.MongoQuery; | |
| 7 | +import com.lyms.platform.common.result.BaseModel; | |
| 8 | +import com.lyms.platform.common.utils.*; | |
| 9 | +import com.lyms.platform.common.utils.ReflectionUtils; | |
| 10 | +import com.lyms.platform.common.utils.StringUtils; | |
| 11 | +import com.lyms.platform.pojo.*; | |
| 12 | +import org.apache.commons.codec.binary.Base64; | |
| 13 | +import org.apache.commons.httpclient.HttpClient; | |
| 14 | +import org.apache.commons.httpclient.NameValuePair; | |
| 15 | +import org.apache.commons.httpclient.methods.PostMethod; | |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 17 | +import org.springframework.data.mongodb.core.MongoTemplate; | |
| 18 | +import org.springframework.data.mongodb.core.query.Query; | |
| 19 | +import org.springframework.data.mongodb.core.query.Update; | |
| 20 | +import org.springframework.stereotype.Controller; | |
| 21 | +import org.springframework.util.*; | |
| 22 | +import org.springframework.web.bind.annotation.RequestMapping; | |
| 23 | +import org.springframework.web.bind.annotation.RequestMethod; | |
| 24 | +import org.springframework.web.bind.annotation.ResponseBody; | |
| 25 | + | |
| 26 | +import java.io.Serializable; | |
| 27 | +import java.util.ArrayList; | |
| 28 | +import java.util.List; | |
| 29 | + | |
| 30 | +/** | |
| 31 | + * Created by riecard on 2016/12/4. | |
| 32 | + */ | |
| 33 | +@Controller | |
| 34 | +@RequestMapping("/initdata") | |
| 35 | +public class InitDataController extends BaseController { | |
| 36 | + | |
| 37 | + @Autowired | |
| 38 | + protected MongoTemplate mongoTemplate; | |
| 39 | + | |
| 40 | + @ResponseBody | |
| 41 | + @RequestMapping(value = "/resyncdata", method = RequestMethod.POST) | |
| 42 | + public String resyncdata(String hospitalId) { | |
| 43 | + if (org.apache.commons.lang.StringUtils.isBlank(hospitalId)) { | |
| 44 | + return "hospitalId is null"; | |
| 45 | + } | |
| 46 | + StringBuffer sb = new StringBuffer(); | |
| 47 | + List<Class> classList = new ArrayList<>(); | |
| 48 | + classList.add(AntenatalExaminationModel.class); | |
| 49 | + classList.add(AntExChuModel.class); | |
| 50 | + classList.add(AntExRecordModel.class); | |
| 51 | + classList.add(BabyModel.class); | |
| 52 | + classList.add(BabyCheckModel.class); | |
| 53 | + classList.add(MaternalDeliverModel.class); | |
| 54 | + classList.add(Patients.class); | |
| 55 | + classList.add(PersonModel.class); | |
| 56 | + classList.add(PostReviewModel.class); | |
| 57 | + classList.add(PuerperaModel.class); | |
| 58 | + classList.add(ReferralApplyOrderModel.class); | |
| 59 | + classList.add(SieveModel.class); | |
| 60 | + classList.add(SieveApplyOrderModel.class); | |
| 61 | + classList.add(SieveResultModel.class); | |
| 62 | + classList.add(StopPregModel.class); | |
| 63 | + Query query = new MongoQuery(new MongoCondition("hospitalId", hospitalId, MongoOper.IS)).convertToMongoQuery(); | |
| 64 | + for (Class cla:classList) { | |
| 65 | + sb.append("sync start... "); | |
| 66 | + sb.append(cla.getName()); | |
| 67 | + sb.append(" query size:"); | |
| 68 | + int size = 0; | |
| 69 | + List<BaseModel> list = null; | |
| 70 | + if (cla.equals(PersonModel.class)) { | |
| 71 | + list = mongoTemplate.find(new Query(), cla); | |
| 72 | + } else { | |
| 73 | + list = mongoTemplate.find(query, cla); | |
| 74 | + } | |
| 75 | + sb.append(list.size()); | |
| 76 | + sb.append("\r\n<br>\r\n"); | |
| 77 | + int index = 0; | |
| 78 | + List<String> synclist = new ArrayList<>(); | |
| 79 | + for (BaseModel model:list) { | |
| 80 | + synclist.add(Base64.encodeBase64String(SerializUtils.objToByte(model))); | |
| 81 | + index++; | |
| 82 | + if (index%100 == 0) { | |
| 83 | + postser(synclist, cla.getName()); | |
| 84 | + synclist.clear(); | |
| 85 | + } | |
| 86 | + } | |
| 87 | + if (synclist.size() > 0) { | |
| 88 | + postser(synclist, cla.getName()); | |
| 89 | + } | |
| 90 | + } | |
| 91 | + return sb.toString(); | |
| 92 | + } | |
| 93 | + | |
| 94 | + private void postser(List<String> synclist, String claName) { | |
| 95 | + try { | |
| 96 | + HttpClient client = new HttpClient(); | |
| 97 | + client.getHttpConnectionManager().getParams().setConnectionTimeout(30000); | |
| 98 | + client.getHttpConnectionManager().getParams().setSoTimeout(30000); | |
| 99 | + PostMethod post = new MessageUtil.UTF8PostMethod("http://api.meishengbb.com/initdata/reser"); | |
| 100 | + NameValuePair[] pairs = { | |
| 101 | + new NameValuePair("className", claName), | |
| 102 | + new NameValuePair("json", JsonUtil.array2JsonString(synclist)), | |
| 103 | + }; | |
| 104 | + post.setRequestBody(pairs); | |
| 105 | + client.executeMethod(post); | |
| 106 | + int statusCode = post.getStatusCode(); | |
| 107 | + String json = post.getResponseBodyAsString(); | |
| 108 | + System.out.println(json); | |
| 109 | + post.releaseConnection(); | |
| 110 | + if (200 == statusCode) { | |
| 111 | + | |
| 112 | + } | |
| 113 | + } catch (Exception e) { | |
| 114 | + e.printStackTrace(); | |
| 115 | + } | |
| 116 | + } | |
| 117 | + | |
| 118 | + @ResponseBody | |
| 119 | + @RequestMapping(value = "/reser", method = RequestMethod.POST) | |
| 120 | + public String reser(String className, String json) { | |
| 121 | + if (StringUtils.isNotEmpty(className) && StringUtils.isNotEmpty(json)) { | |
| 122 | + try { | |
| 123 | + Class cla = Class.forName(className); | |
| 124 | + List<String> synclist = JsonUtil.toList(json, String.class); | |
| 125 | + for (String s:synclist) { | |
| 126 | + Object obj = SerializUtils.byteToObj(Base64.decodeBase64(s)); | |
| 127 | + String id = cla.getMethod("getId").invoke(obj).toString(); | |
| 128 | + if (StringUtils.isNotEmpty(id)) { | |
| 129 | + if (mongoTemplate.findById(id, cla) == null) { | |
| 130 | + mongoTemplate.insert(obj); | |
| 131 | + } else { | |
| 132 | + Query query = new MongoQuery(new MongoCondition("id", id, MongoOper.IS)).convertToMongoQuery(); | |
| 133 | + Update update = MongoConvertHelper | |
| 134 | + .convertToNativeUpdate(ReflectionUtils.getUpdateField(obj)); | |
| 135 | + mongoTemplate.updateMulti(query, update, cla); | |
| 136 | + } | |
| 137 | + } | |
| 138 | + } | |
| 139 | + return "success"; | |
| 140 | + } catch (Exception e) { | |
| 141 | + e.printStackTrace(); | |
| 142 | + return e.toString(); | |
| 143 | + } | |
| 144 | + } else { | |
| 145 | + return "param empty"; | |
| 146 | + } | |
| 147 | + } | |
| 148 | + | |
| 149 | +} |
platform-operate-api/src/main/java/com/lyms/platform/operate/web/controller/InitReverseDataController.java
View file @
949d5a1
| 1 | +package com.lyms.platform.operate.web.controller; | |
| 2 | + | |
| 3 | +import com.lyms.platform.common.base.BaseController; | |
| 4 | +import com.lyms.platform.common.dao.operator.MongoCondition; | |
| 5 | +import com.lyms.platform.common.dao.operator.MongoOper; | |
| 6 | +import com.lyms.platform.common.dao.operator.MongoQuery; | |
| 7 | +import com.lyms.platform.common.result.BaseModel; | |
| 8 | +import com.lyms.platform.common.utils.*; | |
| 9 | +import com.lyms.platform.pojo.*; | |
| 10 | +import org.apache.commons.codec.binary.Base64; | |
| 11 | +import org.apache.commons.httpclient.HttpClient; | |
| 12 | +import org.apache.commons.httpclient.NameValuePair; | |
| 13 | +import org.apache.commons.httpclient.methods.PostMethod; | |
| 14 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 15 | +import org.springframework.data.mongodb.core.MongoTemplate; | |
| 16 | +import org.springframework.data.mongodb.core.query.Query; | |
| 17 | +import org.springframework.data.mongodb.core.query.Update; | |
| 18 | +import org.springframework.stereotype.Controller; | |
| 19 | +import org.springframework.web.bind.annotation.RequestMapping; | |
| 20 | +import org.springframework.web.bind.annotation.RequestMethod; | |
| 21 | +import org.springframework.web.bind.annotation.ResponseBody; | |
| 22 | + | |
| 23 | +import java.io.Serializable; | |
| 24 | +import java.util.ArrayList; | |
| 25 | +import java.util.List; | |
| 26 | + | |
| 27 | +/** | |
| 28 | + * Created by riecard on 2016/12/4. | |
| 29 | + */ | |
| 30 | +@Controller | |
| 31 | +@RequestMapping("/initdata/reverse") | |
| 32 | +public class InitReverseDataController extends BaseController { | |
| 33 | + | |
| 34 | + @Autowired | |
| 35 | + protected MongoTemplate mongoTemplate; | |
| 36 | + | |
| 37 | + @ResponseBody | |
| 38 | + @RequestMapping(value = "/resyncdata", method = RequestMethod.POST) | |
| 39 | + public String resyncdata(String hospitalId) { | |
| 40 | + if (org.apache.commons.lang.StringUtils.isBlank(hospitalId)) { | |
| 41 | + return "hospitalId is null"; | |
| 42 | + } | |
| 43 | + StringBuffer sb = new StringBuffer(); | |
| 44 | + List<Class> classList = new ArrayList<>(); | |
| 45 | + classList.add(AntenatalExaminationModel.class); | |
| 46 | + classList.add(AntExChuModel.class); | |
| 47 | + classList.add(AntExRecordModel.class); | |
| 48 | + classList.add(BabyModel.class); | |
| 49 | + classList.add(BabyCheckModel.class); | |
| 50 | + classList.add(MaternalDeliverModel.class); | |
| 51 | + classList.add(Patients.class); | |
| 52 | + classList.add(PersonModel.class); | |
| 53 | + classList.add(PostReviewModel.class); | |
| 54 | + classList.add(PuerperaModel.class); | |
| 55 | + classList.add(ReferralApplyOrderModel.class); | |
| 56 | + classList.add(SieveModel.class); | |
| 57 | + classList.add(SieveApplyOrderModel.class); | |
| 58 | + classList.add(SieveResultModel.class); | |
| 59 | + classList.add(StopPregModel.class); | |
| 60 | + if ("216".equals(hospitalId)) { | |
| 61 | + for (Class cla:classList) { | |
| 62 | + try { | |
| 63 | + HttpClient client = new HttpClient(); | |
| 64 | + client.getHttpConnectionManager().getParams().setConnectionTimeout(600000); | |
| 65 | + client.getHttpConnectionManager().getParams().setSoTimeout(600000); | |
| 66 | + PostMethod post = new MessageUtil.UTF8PostMethod("http://121.22.16.246:18018/initdata/reverse/findsyncdata"); | |
| 67 | + NameValuePair[] pairs = { | |
| 68 | + new NameValuePair("className", cla.getName()), | |
| 69 | + new NameValuePair("hospitalId", hospitalId), | |
| 70 | + }; | |
| 71 | + post.setRequestBody(pairs); | |
| 72 | + client.executeMethod(post); | |
| 73 | + int statusCode = post.getStatusCode(); | |
| 74 | + post.releaseConnection(); | |
| 75 | + if (200 == statusCode) { | |
| 76 | + String json = post.getResponseBodyAsString(); | |
| 77 | + List<String> synclist = JsonUtil.toList(json, String.class); | |
| 78 | + sb.append(cla.getName()); | |
| 79 | + sb.append(":"); | |
| 80 | + sb.append(synclist.size()); | |
| 81 | + sb.append("\r\n<br>\r\n"); | |
| 82 | + for (String s:synclist) { | |
| 83 | + Object obj = SerializUtils.byteToObj(Base64.decodeBase64(s)); | |
| 84 | + String id = cla.getMethod("getId").invoke(obj).toString(); | |
| 85 | + if (StringUtils.isNotEmpty(id)) { | |
| 86 | + if (mongoTemplate.findById(id, cla) == null) { | |
| 87 | + mongoTemplate.insert(obj); | |
| 88 | + } else { | |
| 89 | + Query query = new MongoQuery(new MongoCondition("id", id, MongoOper.IS)).convertToMongoQuery(); | |
| 90 | + Update update = MongoConvertHelper | |
| 91 | + .convertToNativeUpdate(ReflectionUtils.getUpdateField(obj)); | |
| 92 | + mongoTemplate.updateMulti(query, update, cla); | |
| 93 | + } | |
| 94 | + } | |
| 95 | + } | |
| 96 | + } | |
| 97 | + } catch (Exception e) { | |
| 98 | + e.printStackTrace(); | |
| 99 | + } | |
| 100 | + } | |
| 101 | + } else { | |
| 102 | + sb.append("only hospital id is 216(qhdfy)"); | |
| 103 | + } | |
| 104 | + return sb.toString(); | |
| 105 | + } | |
| 106 | + | |
| 107 | + @ResponseBody | |
| 108 | + @RequestMapping(value = "/findsyncdata", method = RequestMethod.POST) | |
| 109 | + public String resyncdata(String hospitalId, String className) { | |
| 110 | + if (StringUtils.isNotEmpty(hospitalId) && StringUtils.isNotEmpty(className)) { | |
| 111 | + Query query = new MongoQuery(new MongoCondition("hospitalId", hospitalId, MongoOper.IS)).convertToMongoQuery(); | |
| 112 | + try { | |
| 113 | + List list = mongoTemplate.find(query, Class.forName(className)); | |
| 114 | + List<String> synclist = new ArrayList<>(); | |
| 115 | + for (Object obj:list) { | |
| 116 | + synclist.add(Base64.encodeBase64String(SerializUtils.objToByte((Serializable)obj))); | |
| 117 | + } | |
| 118 | + return JsonUtil.array2JsonString(synclist); | |
| 119 | + } catch (Exception e) { | |
| 120 | + e.printStackTrace(); | |
| 121 | + return e.toString(); | |
| 122 | + } | |
| 123 | + } else { | |
| 124 | + return "fail"; | |
| 125 | + } | |
| 126 | + } | |
| 127 | + | |
| 128 | +} |