ManagerQuestionnaireService.java 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  1. package com.yihu.wlyy.service.survey;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.yihu.wlyy.entity.dict.SystemDict;
  4. import com.yihu.wlyy.entity.doctor.profile.Doctor;
  5. import com.yihu.wlyy.entity.doctor.survey.*;
  6. import com.yihu.wlyy.entity.patient.Patient;
  7. import com.yihu.wlyy.entity.wechat.WechatTemplate;
  8. import com.yihu.wlyy.entity.wechat.WechatTemplateData;
  9. import com.yihu.wlyy.repository.dict.SystemDictDao;
  10. import com.yihu.wlyy.repository.doctor.*;
  11. import com.yihu.wlyy.repository.patient.PatientDao;
  12. import com.yihu.wlyy.service.BaseService;
  13. import com.yihu.wlyy.service.system.SystemDictService;
  14. import com.yihu.wlyy.task.PushMsgTask;
  15. import com.yihu.wlyy.util.HttpUtil;
  16. import com.yihu.wlyy.util.SystemConf;
  17. import com.yihu.wlyy.web.doctor.survey.ManagerQuestionnaireController;
  18. import com.yihu.wlyy.wechat.util.WeiXinTempMsgSendUtils;
  19. import io.swagger.models.auth.In;
  20. import org.apache.commons.lang3.StringUtils;
  21. import org.apache.poi.util.StringUtil;
  22. import org.bouncycastle.jce.provider.symmetric.HC128;
  23. import org.json.JSONArray;
  24. import org.json.JSONObject;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.data.domain.Page;
  29. import org.springframework.data.domain.PageRequest;
  30. import org.springframework.data.domain.Pageable;
  31. import org.springframework.data.domain.Sort;
  32. import org.springframework.jdbc.core.JdbcTemplate;
  33. import org.springframework.stereotype.Service;
  34. import org.springframework.transaction.annotation.Transactional;
  35. import java.text.SimpleDateFormat;
  36. import java.util.*;
  37. /**
  38. * Created by Reece on 2017/3/9.
  39. */
  40. @Service
  41. @Transactional
  42. public class ManagerQuestionnaireService extends BaseService {
  43. @Autowired
  44. private SurveyDao surveyDao;
  45. @Autowired
  46. private SurveyQuestionsDao surveyQuestionsDao;
  47. @Autowired
  48. private SurveyLabelInfoDao surveyLabelInfoDao;
  49. @Autowired
  50. private SurveyQuestionsOptionsDao surveyQuestionsOptionsDao;
  51. @Autowired
  52. private SurveyUserDao surveyUserDao;
  53. @Autowired
  54. private SurveyFilterDao surveyFilterDao;
  55. @Autowired
  56. private SurveyQuestionnaireOptionsDao surveyQuestionnaireOptionsDao;
  57. @Autowired
  58. private SurveyQuestionnaireDao surveyQuestionnaireDao;
  59. @Autowired
  60. private SurveyTemplateOptionsDao surveyTemplateOptionsDao;
  61. @Autowired
  62. private SurveyTemplateQuestionsDao surveyTemplateQuestionsDao;
  63. @Autowired
  64. private SurveyOptionAnswersDao surveyOptionAnswersDao;
  65. @Autowired
  66. private SurveyAnswersDao surveyAnswersDao;
  67. @Autowired
  68. private SurveyTemplatesDao surveyTemplatesDao;
  69. @Autowired
  70. private SurveyStatisticsDao surveyStatisticsDao;
  71. @Autowired
  72. private JdbcTemplate jdbcTemplate;
  73. @Autowired
  74. private SystemDictDao systemDictDao;
  75. @Autowired
  76. private SurveyDimensionDao surveyDimensionDao;
  77. @Autowired
  78. private SurveyDimensionDetailDao surveyDimensionDetailDao;
  79. @Autowired
  80. private DoctorDao doctorDao;
  81. @Autowired
  82. private PatientDao patientDao;
  83. public String createSurvey(String doctor, JSONObject jsonStr, String accessToken) throws Exception {
  84. System.out.println("=============>"+jsonStr);
  85. // 问卷信息
  86. String surveyCode = getCode();
  87. String surveyName = jsonStr.getString("surveyName");
  88. String surveyComment = jsonStr.getString("comment");
  89. String templateCode = null;
  90. String send = jsonStr.get("send").toString();
  91. int status = 1;
  92. // 自主创建
  93. int useType = 1;
  94. if (!StringUtils.isEmpty(jsonStr.getString("templateCode"))) {
  95. // 模板创建
  96. useType = 0;
  97. templateCode = jsonStr.getString("templateCode");
  98. }
  99. // 获取调查对象
  100. JSONObject surveyTarget = jsonStr.getJSONObject("surveyTarget");
  101. // 0不限 1男 2女
  102. int sex = Integer.parseInt(surveyTarget.get("sex").toString());
  103. JSONArray disease = surveyTarget.getJSONArray("disease");
  104. JSONArray service = surveyTarget.getJSONArray("service");
  105. JSONArray healthCondition = surveyTarget.getJSONArray("healthCondition");
  106. /*String str = "SELECT DISTINCT t.patient " +
  107. "FROM `wlyy_sign_patient_label_info` t,wlyy_patient p,`wlyy_sign_family` f " +
  108. "WHERE t.`patient` = p.`code` AND f.`patient`=t.`patient` AND f.`status` IN (1,2,3) AND p.`openid` IS NOT NULL AND p.`openid`!='' ";*/
  109. // 保存到维度详情表(不限不保存) 维度表(1性别 2疾病类型 3服务类型 4健康状况)
  110. if (sex != 0) {
  111. // str += "AND(p.sex=" + sex + ")";
  112. String dimensionCode = surveyDimensionDao.findByName("性别");
  113. String dimensionDetailCode = getCode();
  114. SurveyDimensionDetail surveyDimensionDetail = new SurveyDimensionDetail(dimensionDetailCode, dimensionCode, sex == 1 ? "男" : "女", sex, "性别", 1, surveyCode);
  115. surveyDimensionDetailDao.save(surveyDimensionDetail);
  116. // 保存到筛选表
  117. SurveyFilter surveyFilter = new SurveyFilter(getCode(), surveyCode, dimensionDetailCode);
  118. surveyFilterDao.save(surveyFilter);
  119. }
  120. // String ser = "";
  121. // String servi = "";
  122. for (Object serv : service) {
  123. if ("0".equals(serv.toString())) {
  124. // ser += ",";
  125. break;
  126. } else {
  127. // ser += serv.toString() + ",";
  128. // ===================================
  129. String dimensionCode = surveyDimensionDao.findByName("服务类型");
  130. String dimensionDetailCode = getCode();
  131. SurveyDimensionDetail surveyDimensionDetail = null;
  132. switch (Integer.parseInt(serv.toString())) {
  133. // 1.普通人群 2.慢病人群 3.65岁以上人群
  134. case 1:
  135. surveyDimensionDetail = new SurveyDimensionDetail(dimensionDetailCode, dimensionCode, "普通人群", 1, "服务类型", 3, surveyCode);
  136. break;
  137. case 2:
  138. surveyDimensionDetail = new SurveyDimensionDetail(dimensionDetailCode, dimensionCode, "慢病人群", 2, "服务类型", 3, surveyCode);
  139. break;
  140. case 3:
  141. surveyDimensionDetail = new SurveyDimensionDetail(dimensionDetailCode, dimensionCode, "65岁以上人群", 3, "服务类型", 3, surveyCode);
  142. break;
  143. }
  144. surveyDimensionDetailDao.save(surveyDimensionDetail);
  145. SurveyFilter surveyFilter = new SurveyFilter(getCode(), surveyCode, dimensionDetailCode);
  146. surveyFilterDao.save(surveyFilter);
  147. }
  148. }
  149. /*ser = ser.substring(0, ser.length() - 1);
  150. if (ser.length() != 0) {
  151. servi = "(t.`label_type`=1 AND t.`label`IN(" + ser + "))";
  152. }*/
  153. // String heal = "";
  154. // String healt = "";
  155. for (Object health : healthCondition) {
  156. if ("0".equals(health.toString())) {
  157. // heal += ",";
  158. break;
  159. } else {
  160. // heal += health.toString() + ",";
  161. String dimensionCode = surveyDimensionDao.findByName("健康状况");
  162. String dimensionDetailCode = getCode();
  163. String surveyFilterCode = getCode();
  164. SurveyDimensionDetail surveyDimensionDetail = null;
  165. switch (Integer.parseInt(health.toString())) {
  166. // 1.健康人群 2.患病人群 3.高危人群 4.恢复期人群
  167. case 1:
  168. surveyDimensionDetail = new SurveyDimensionDetail(dimensionDetailCode, dimensionCode, "健康人群", 1, "健康状况", 4, surveyCode);
  169. break;
  170. case 2:
  171. surveyDimensionDetail = new SurveyDimensionDetail(dimensionDetailCode, dimensionCode, "患病人群", 2, "健康状况", 4, surveyCode);
  172. break;
  173. case 3:
  174. surveyDimensionDetail = new SurveyDimensionDetail(dimensionDetailCode, dimensionCode, "高危人群", 3, "健康状况", 4, surveyCode);
  175. break;
  176. case 4:
  177. surveyDimensionDetail = new SurveyDimensionDetail(dimensionDetailCode, dimensionCode, "恢复期人群", 4, "健康状况", 4, surveyCode);
  178. break;
  179. }
  180. surveyDimensionDetailDao.save(surveyDimensionDetail);
  181. SurveyFilter surveyFilter = new SurveyFilter(surveyFilterCode, surveyCode, dimensionDetailCode);
  182. surveyFilterDao.save(surveyFilter);
  183. }
  184. }
  185. /* heal = heal.substring(0, heal.length() - 1);
  186. if (heal.length() != 0) {
  187. healt = "(t.`label_type`=2 AND t.`label`IN(" + heal + "))";
  188. }*/
  189. // String dise = "";
  190. // String diseas = "";
  191. for (Object disea : disease) {
  192. if ("0".equals(disea.toString())) {
  193. // dise += ",";
  194. break;
  195. } else {
  196. // dise += disea.toString() + ",";
  197. // ================================================================
  198. String dimensionCode = surveyDimensionDao.findByName("疾病类型");
  199. String dimensionDetailCode = getCode();
  200. SurveyDimensionDetail surveyDimensionDetail = null;
  201. switch (Integer.parseInt(disea.toString())) {
  202. // 1.高血压 2糖尿病 3.高血压+糖尿病
  203. case 1:
  204. surveyDimensionDetail = new SurveyDimensionDetail(dimensionDetailCode, dimensionCode, "高血压", 1, "疾病类型", 2, surveyCode);
  205. break;
  206. case 2:
  207. surveyDimensionDetail = new SurveyDimensionDetail(dimensionDetailCode, dimensionCode, "糖尿病", 2, "疾病类型", 2, surveyCode);
  208. break;
  209. }
  210. surveyDimensionDetailDao.save(surveyDimensionDetail);
  211. SurveyFilter surveyFilter = new SurveyFilter(getCode(), surveyCode, dimensionDetailCode);
  212. surveyFilterDao.save(surveyFilter);
  213. }
  214. }
  215. /*dise = dise.substring(0, dise.length() - 1);
  216. if (dise.length() != 0) {
  217. diseas = "(t.`label_type`=3 AND t.`label`IN(" + dise + "))";
  218. }
  219. if (servi.length() != 0 & healt.length() != 0 & diseas.length() != 0) {
  220. str += " AND ( " + servi + " OR " + healt + " OR " + diseas + " )";
  221. } else if (servi.length() != 0 & healt.length() != 0 & diseas.length() == 0) {
  222. str += " AND ( " + servi + " OR " + healt + " )";
  223. } else if (servi.length() != 0 & healt.length() == 0 & diseas.length() != 0) {
  224. str += " AND ( " + servi + " OR " + diseas + " )";
  225. } else if (servi.length() == 0 & healt.length() != 0 & diseas.length() != 0) {
  226. str += " AND ( " + healt + " OR " + diseas + " )";
  227. } else if (servi.length() != 0 & healt.length() == 0 & diseas.length() == 0) {
  228. str += " AND " + servi;
  229. } else if (servi.length() == 0 & healt.length() != 0 & diseas.length() == 0) {
  230. str += " AND " + healt;
  231. } else if (servi.length() == 0 & healt.length() == 0 & diseas.length() != 0) {
  232. str += " AND " + diseas;
  233. }*/
  234. /* System.out.println("*****************str***********************" + str);
  235. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  236. sdf.format(new Date());
  237. String jsonData = "{\"remark\":\"我们将按调查最终结果,优化健康服务。\",\"keyword1\":\"调查问卷\"}";
  238. JSONObject json = new JSONObject(jsonData);
  239. json.put("survey_id", surveyCode);*/
  240. // List<Map<String, Object>> patients = jdbcTemplate.queryForList(str);
  241. // Map<String, Object> map = jdbcTemplate.queryForMap(str);
  242. // List<String> codes = jdbcTemplate.queryForList(str, String.class);
  243. List<String> codes = getCount(surveyTarget.toString());
  244. int amount = codes.size();
  245. for (String code : codes) {
  246. // String code = patient.get("patient").toString();
  247. SurveyUser user = new SurveyUser(surveyCode, code, 0, 1, new Date(), new Date());
  248. surveyUserDao.save(user);
  249. // 发送居民模板 send=1发送
  250. /* if ("1".equals(send)) {
  251. String name = patient.get("name").toString();
  252. String openId = patient.get("openid").toString();
  253. // String openId = "o7NFZw1QM4YR1O19mLjwfX1Hh11A";
  254. json.put("keyword2", sdf.format(new Date()));
  255. json.put("toUser", code);
  256. json.put("first", name + ",您好!为了给您提供更好的家庭医生健康服务,现有一份问卷需要您填写。");
  257. System.out.println("*************************" + json + "******************");
  258. System.out.println("********name*********************" + name + "openId" + openId);
  259. PushMsgTask.getInstance().sendWeixinMessage(accessToken, 11, openId, name, json);
  260. }*/
  261. }
  262. // 发送居民模板 send=1发送
  263. // ====================================================
  264. if ("1".equals(send)) {
  265. SendMessage sendMessage = new SendMessage(surveyCode, codes, accessToken);
  266. new Thread(sendMessage).start();
  267. }
  268. // =====================================================
  269. // 获取标签数组
  270. JSONArray lables = jsonStr.getJSONArray("label");
  271. // 标签保存到wlyy_survey_label_info
  272. for (Object label : lables) {
  273. SurveyLabelInfo surveyLabelInfo = new SurveyLabelInfo();
  274. surveyLabelInfo.setCode(getCode());
  275. surveyLabelInfo.setRelationCode(surveyCode);
  276. surveyLabelInfo.setUseType(useType);
  277. surveyLabelInfo.setLabel(Integer.parseInt(label.toString()));
  278. SurveyLabelInfo ss = surveyLabelInfoDao.save(surveyLabelInfo);
  279. }
  280. // 获取问题数组
  281. JSONArray questions = jsonStr.getJSONArray("questions");
  282. for (Object question : questions) {
  283. JSONObject tempQestion = new JSONObject(question.toString());
  284. // 不分题型都有的属性
  285. String questionnaireCode = tempQestion.get("qstCode").toString();
  286. String title = tempQestion.get("title").toString();
  287. String comment = tempQestion.get("comment").toString();
  288. int type = Integer.parseInt(tempQestion.get("type").toString());
  289. int isRequired = Integer.parseInt(tempQestion.get("isRequired").toString());
  290. int sort = Integer.parseInt(tempQestion.get("sort").toString());
  291. int del = 1;
  292. switch (type) {
  293. case 0:
  294. // 单选
  295. SurveyQuestionnaire surveyQuestionnaire = new SurveyQuestionnaire();
  296. surveyQuestionnaire.setCode(questionnaireCode);
  297. surveyQuestionnaire.setTitle(title);
  298. surveyQuestionnaire.setQuestionComment(comment);
  299. surveyQuestionnaire.setQuestionType(type);
  300. surveyQuestionnaire.setSurveyCode(surveyCode);
  301. surveyQuestionnaire.setIsRequired(isRequired);
  302. surveyQuestionnaire.setDel(del);
  303. surveyQuestionnaire.setSort(sort);
  304. // 单选没有最大最小选项数及题目逻辑跳转
  305. surveyQuestionnaire.setMinNum(null);
  306. surveyQuestionnaire.setMaxNum(null);
  307. surveyQuestionnaire.setQuestionCodeNext(null);
  308. surveyQuestionnaireDao.save(surveyQuestionnaire);
  309. // =======================选项保存=========================
  310. // 选择题
  311. JSONArray options = new JSONArray(tempQestion.get("options").toString());
  312. for (Object option : options) {
  313. String optionCode = getCode();
  314. JSONObject tempOption = new JSONObject(option.toString());
  315. int haveComment = Integer.parseInt(tempOption.get("haveComment").toString());
  316. String optionComment = tempOption.get("content").toString();
  317. int required = Integer.parseInt(tempOption.get("isRequired").toString());
  318. int optionSort = Integer.parseInt(tempOption.get("sort").toString());
  319. int optionDel = 1;
  320. String optionNextQuestion = null;
  321. if (!StringUtils.isEmpty(tempOption.get("nextQuestion").toString())) {
  322. optionNextQuestion = tempOption.get("nextQuestion").toString();
  323. }
  324. // 自主创建,插入表
  325. SurveyQuestionnaireOptions surveyQuestionnaireOptions = new SurveyQuestionnaireOptions();
  326. surveyQuestionnaireOptions.setCode(optionCode);
  327. surveyQuestionnaireOptions.setHaveComment(haveComment);
  328. surveyQuestionnaireOptions.setQuestionnaireCode(questionnaireCode);
  329. surveyQuestionnaireOptions.setContent(optionComment);
  330. surveyQuestionnaireOptions.setSurveyCode(surveyCode);
  331. surveyQuestionnaireOptions.setQuestionCodeNext(optionNextQuestion);
  332. surveyQuestionnaireOptions.setIsRequired(required);
  333. surveyQuestionnaireOptions.setSort(optionSort);
  334. surveyQuestionnaireOptions.setDel(optionDel);
  335. surveyQuestionnaireOptionsDao.save(surveyQuestionnaireOptions);
  336. // 单选题保存到统计表
  337. String statisticsCode = getCode();
  338. String questionTitle = surveyQuestionnaireDao.findByIdAndQstId(surveyCode, questionnaireCode).getTitle();
  339. String optcontent = surveyQuestionnaireOptionsDao.findByRelationCode(surveyCode, questionnaireCode, optionCode).getContent();
  340. SurveyStatistics statistics = new SurveyStatistics(statisticsCode, surveyCode, questionnaireCode, questionTitle, optionCode, 0, optcontent, haveComment, type, sort, optionSort);
  341. surveyStatisticsDao.save(statistics);
  342. }
  343. // ===============================================================
  344. break;
  345. case 1:
  346. // 多选特有属性
  347. Integer maxOptions = null;
  348. Integer minOptions = null;
  349. if (!StringUtils.isEmpty(tempQestion.get("minOptions").toString())) {
  350. minOptions = Integer.parseInt(tempQestion.get("minOptions").toString());
  351. }
  352. if (!StringUtils.isEmpty(tempQestion.get("maxOptions").toString())) {
  353. maxOptions = Integer.parseInt(tempQestion.get("maxOptions").toString());
  354. }
  355. SurveyQuestionnaire surveyQuestionnaire1 = new SurveyQuestionnaire();
  356. surveyQuestionnaire1.setCode(questionnaireCode);
  357. surveyQuestionnaire1.setTitle(title);
  358. surveyQuestionnaire1.setQuestionComment(comment);
  359. surveyQuestionnaire1.setQuestionType(type);
  360. surveyQuestionnaire1.setSurveyCode(surveyCode);
  361. surveyQuestionnaire1.setIsRequired(isRequired);
  362. surveyQuestionnaire1.setMinNum(minOptions);
  363. surveyQuestionnaire1.setMaxNum(maxOptions);
  364. surveyQuestionnaire1.setSort(sort);
  365. surveyQuestionnaire1.setQuestionCodeNext(null);
  366. surveyQuestionnaire1.setDel(del);
  367. surveyQuestionnaireDao.save(surveyQuestionnaire1);
  368. // =======================选项保存=========================
  369. // 选择题
  370. JSONArray options1 = new JSONArray(tempQestion.get("options").toString());
  371. for (Object option : options1) {
  372. String optionCode = getCode();
  373. JSONObject tempOption = new JSONObject(option.toString());
  374. int haveComment = Integer.parseInt(tempOption.get("haveComment").toString());
  375. String optionComment = tempOption.get("content").toString();
  376. int required = Integer.parseInt(tempOption.get("isRequired").toString());
  377. int optionSort = Integer.parseInt(tempOption.get("sort").toString());
  378. int optionDel = 1;
  379. String optionNextQuestion = null;
  380. if (!StringUtils.isEmpty(tempOption.get("nextQuestion").toString())) {
  381. optionNextQuestion = tempOption.get("nextQuestion").toString();
  382. }
  383. // 自主创建,插入表
  384. SurveyQuestionnaireOptions surveyQuestionnaireOptions = new SurveyQuestionnaireOptions();
  385. surveyQuestionnaireOptions.setCode(optionCode);
  386. surveyQuestionnaireOptions.setHaveComment(haveComment);
  387. surveyQuestionnaireOptions.setQuestionnaireCode(questionnaireCode);
  388. surveyQuestionnaireOptions.setContent(optionComment);
  389. surveyQuestionnaireOptions.setSurveyCode(surveyCode);
  390. surveyQuestionnaireOptions.setQuestionCodeNext(optionNextQuestion);
  391. surveyQuestionnaireOptions.setIsRequired(required);
  392. surveyQuestionnaireOptions.setSort(optionSort);
  393. surveyQuestionnaireOptions.setDel(optionDel);
  394. surveyQuestionnaireOptionsDao.save(surveyQuestionnaireOptions);
  395. // 多选题保存到统计表
  396. String statisticsCode = getCode();
  397. String questionTitle = surveyQuestionnaireDao.findByIdAndQstId(surveyCode, questionnaireCode).getTitle();
  398. String optcontent = surveyQuestionnaireOptionsDao.findByRelationCode(surveyCode, questionnaireCode, optionCode).getContent();
  399. SurveyStatistics statistics = new SurveyStatistics(statisticsCode, surveyCode, questionnaireCode, questionTitle, optionCode, 0, optcontent, haveComment, type, sort, optionSort);
  400. surveyStatisticsDao.save(statistics);
  401. }
  402. // ===============================================================
  403. break;
  404. case 2:
  405. // 问答特有属性
  406. String nextQuestion = null;
  407. if (tempQestion.has("nextQuestion")) {
  408. nextQuestion = tempQestion.get("nextQuestion").toString();
  409. }
  410. SurveyQuestionnaire surveyQuestionnaire2 = new SurveyQuestionnaire();
  411. surveyQuestionnaire2.setCode(questionnaireCode);
  412. surveyQuestionnaire2.setTitle(title);
  413. surveyQuestionnaire2.setQuestionComment(comment);
  414. surveyQuestionnaire2.setQuestionType(type);
  415. surveyQuestionnaire2.setSurveyCode(surveyCode);
  416. surveyQuestionnaire2.setIsRequired(isRequired);
  417. // 单选没有最大最小选项数
  418. surveyQuestionnaire2.setMinNum(null);
  419. surveyQuestionnaire2.setMaxNum(null);
  420. surveyQuestionnaire2.setSort(sort);
  421. surveyQuestionnaire2.setQuestionCodeNext(nextQuestion);
  422. surveyQuestionnaire2.setDel(del);
  423. surveyQuestionnaireDao.save(surveyQuestionnaire2);
  424. // 问答题保存到统计表(只负责更改数量不负责创建)
  425. String statisticsCode = getCode();
  426. String questionTitle = surveyQuestionnaireDao.findByIdAndQstId(surveyCode, questionnaireCode).getTitle();
  427. SurveyStatistics statistics = new SurveyStatistics(statisticsCode, surveyCode, questionnaireCode, questionTitle, null, 0, null, null, type, sort, null);
  428. surveyStatisticsDao.save(statistics);
  429. break;
  430. }
  431. }
  432. // 保存到问卷
  433. Survey survey = new Survey();
  434. survey.setCode(surveyCode);
  435. survey.setTitle(surveyName);
  436. survey.setSurveyComment(surveyComment);
  437. survey.setCreater(doctor);
  438. survey.setTemplateCode(templateCode);
  439. survey.setStatus(status);
  440. survey.setAmount(amount);
  441. surveyDao.save(survey);
  442. return surveyCode;
  443. }
  444. public JSONArray getQuestionnaireList(int pageNo, int pageSize) throws Exception {
  445. JSONArray jsonArray = new JSONArray();
  446. // 排序
  447. Sort sort = new Sort(Sort.Direction.DESC, "createTime");
  448. PageRequest pageRequest = new PageRequest(pageNo - 1, pageSize, sort);
  449. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  450. Page<Survey> surveys = surveyDao.findAll(pageRequest);
  451. List<Survey> arrays = surveys.getContent();
  452. for (Survey arr : arrays) {
  453. Map<String, Object> sur = new HashMap<>();
  454. String code = arr.getCode();
  455. String title = arr.getTitle();
  456. String doctor = arr.getCreater();
  457. String comment = arr.getSurveyComment();
  458. String createTime = sdf.format(arr.getCreateTime());
  459. // String updateTime = sdf.format(arr.getUpdateTime());
  460. // 查询医生信息
  461. Doctor doc = doctorDao.findByCode(doctor);
  462. String name = doc.getName();
  463. String photo = doc.getPhoto();
  464. String dept = doc.getDeptName();
  465. int amount = arr.getAmount();
  466. //查询居民回答的反馈率
  467. int complete = surveyUserDao.findBySurveyCode(code);
  468. sur.put("code", code);
  469. sur.put("name", name);
  470. sur.put("photo", photo);
  471. sur.put("deptName", dept);
  472. sur.put("percent", (Math.round(((float) complete / amount) * 100)) + "%");
  473. sur.put("title", title);
  474. sur.put("comment", comment);
  475. sur.put("createTime", createTime);
  476. // sur.put("updateTime", updateTime);
  477. sur.put("complete", complete);
  478. sur.put("amount", amount);
  479. jsonArray.put(sur);
  480. }
  481. return jsonArray;
  482. }
  483. public JSONObject getQuestionnaireSummary(String id) throws Exception {
  484. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  485. Survey survey = surveyDao.findById(id);
  486. String creater = survey.getCreater();
  487. Date createTime = survey.getCreateTime();
  488. int amount = survey.getAmount();
  489. String comment = survey.getSurveyComment();
  490. String title = survey.getTitle();
  491. int status = survey.getStatus();
  492. List<String> targets = surveyDimensionDetailDao.findByCode(id);
  493. Map<String, Object> surveyTarget = new HashMap<>();
  494. for (String target : targets) {
  495. List<String> labels = surveyDimensionDetailDao.findByCode(id, target);
  496. List tags = new ArrayList();
  497. for (String label : labels) {
  498. tags.add(label);
  499. }
  500. switch (target) {
  501. case "疾病类型":
  502. target = "disease";
  503. break;
  504. case "服务类型":
  505. target = "service";
  506. break;
  507. case "健康状况":
  508. target = "healthCondition";
  509. break;
  510. case "性别":
  511. target = "sex";
  512. break;
  513. }
  514. surveyTarget.put(target, tags);
  515. }
  516. int[] label = surveyLabelInfoDao.findByRelationCode(id);
  517. String[] labels = new String[label.length];
  518. for (int i = 0; i < label.length; i++) {
  519. switch (label[i]) {
  520. case 1:
  521. labels[i] = "满意度";
  522. break;
  523. case 2:
  524. labels[i] = "糖尿病";
  525. break;
  526. case 3:
  527. labels[i] = "高血压";
  528. break;
  529. case 4:
  530. labels[i] = "生活日常";
  531. break;
  532. }
  533. }
  534. // 查询该问卷回答人数
  535. // int complete = surveyUserDao.findBySurveyCode(id);
  536. int complete = surveyUserDao.findBySurveyCode(id);
  537. Map<String, Object> map = new HashMap<>();
  538. // 获取创建者信息
  539. Doctor doctor = doctorDao.findByCode(creater);
  540. String name = doctor.getName();
  541. String deptName = doctor.getDeptName();
  542. String photo = doctor.getPhoto();
  543. map.put("name", name);
  544. map.put("deptName", deptName);
  545. map.put("photo", photo);
  546. map.put("createTime", sdf.format(createTime));
  547. map.put("amount", amount);
  548. map.put("complete", complete);
  549. map.put("percent", (Math.round(((float) complete / amount) * 100)) + "%");
  550. map.put("comment", comment);
  551. map.put("title", title);
  552. map.put("status", status);
  553. map.put("label", labels);
  554. map.put("surveyTarget", surveyTarget);
  555. JSONObject jsonObject = new JSONObject(map);
  556. return jsonObject;
  557. }
  558. public JSONObject getQuestionnaireDetail(String id) throws Exception {
  559. JSONObject jsonObject = new JSONObject();
  560. //问卷信息
  561. Survey survey = surveyDao.findById(id);
  562. int[] label = surveyLabelInfoDao.findByRelationCode(id);
  563. String[] labels = new String[label.length];
  564. for (int i = 0; i < label.length; i++) {
  565. switch (label[i]) {
  566. case 1:
  567. labels[i] = "满意度";
  568. break;
  569. case 2:
  570. labels[i] = "糖尿病";
  571. break;
  572. case 3:
  573. labels[i] = "高血压";
  574. break;
  575. case 4:
  576. labels[i] = "生活日常";
  577. break;
  578. }
  579. }
  580. //问题信息
  581. List<SurveyQuestionnaire> surveyQuestionnair = surveyQuestionnaireDao.findById(id);
  582. List questions = new ArrayList();
  583. // 问题信息
  584. for (SurveyQuestionnaire questionnair : surveyQuestionnair) {
  585. Map<String, Object> question = new HashMap<>();
  586. String qstcode = questionnair.getCode();
  587. String title = questionnair.getTitle();
  588. int isRequired = questionnair.getIsRequired();
  589. int sort = questionnair.getSort();
  590. int type = questionnair.getQuestionType();
  591. if (questionnair.getQuestionComment() != null) {
  592. String comment = questionnair.getQuestionComment();
  593. question.put("comment", comment);
  594. }
  595. if (questionnair.getQuestionCodeNext() != null) {
  596. String next = questionnair.getQuestionCodeNext();
  597. SurveyQuestionnaire sq = surveyQuestionnaireDao.findByIdAndQstId(id, next);
  598. question.put("nextQuestion", sq.getSort());
  599. }
  600. // List<SurveyQuestionnaireOptions> options = surveyQuestionnaireOptionsDao.findById(qstcode);
  601. List<SurveyQuestionnaireOptions> options = surveyQuestionnaireOptionsDao.findByIdAndQstCode(id, qstcode);
  602. List optList = new ArrayList();
  603. for (SurveyQuestionnaireOptions option : options) {
  604. Map<String, Object> opt = new HashMap<>();
  605. String conetent = option.getContent();
  606. int haveComment = option.getHaveComment();
  607. int required = option.getIsRequired();
  608. int st = option.getSort();
  609. if (option.getQuestionCodeNext() != null) {
  610. String next = option.getQuestionCodeNext();
  611. SurveyQuestionnaire sq = surveyQuestionnaireDao.findByIdAndQstId(id, next);
  612. System.out.println("=============sq============== " + sq);
  613. System.out.println("=============next============== " + next);
  614. opt.put("nextQuestion", sq.getSort());
  615. }
  616. opt.put("content", conetent);
  617. opt.put("sort", st);
  618. opt.put("haveComment", haveComment);
  619. opt.put("isRequired", required);
  620. optList.add(opt);
  621. }
  622. question.put("title", title);
  623. question.put("type", type);
  624. question.put("isRequired", isRequired);
  625. question.put("sort", sort);
  626. question.put("options", optList);
  627. questions.add(question);
  628. }
  629. String surveyComment = survey.getSurveyComment();
  630. String surveyTitle = survey.getTitle();
  631. jsonObject.put("title", surveyTitle);
  632. jsonObject.put("comment", surveyComment);
  633. jsonObject.put("questions", questions);
  634. jsonObject.put("label", labels);
  635. return jsonObject;
  636. }
  637. public int modifyQuestionnaireStatus(String id) throws Exception {
  638. Survey survey = surveyDao.findById(id);
  639. int status = survey.getStatus();
  640. if (status > 1) {
  641. status = 1;
  642. surveyDao.modifyStatus(status, id);
  643. } else {
  644. status = 2;
  645. surveyDao.modifyStatus(status, id);
  646. }
  647. return status;
  648. }
  649. public JSONObject getAnswers(String id) throws Exception {
  650. JSONObject jsonObject = new JSONObject();
  651. //一个问卷内的所有问题
  652. List<SurveyQuestionnaire> questions = surveyQuestionnaireDao.findById(id);
  653. if (questions.size() == 0) {
  654. return jsonObject;
  655. }
  656. List qstList = new ArrayList();
  657. for (SurveyQuestionnaire question : questions) {
  658. // 新造对象
  659. Map<String, Object> qst = new HashMap<>();
  660. String qstcode = question.getCode();
  661. String title = question.getTitle();
  662. int type = question.getQuestionType();
  663. // 有效答了该题的人数
  664. /*int amount = 0;
  665. int[] sum = surveyStatisticsDao.findByIdAndQstId(id, qstcode);
  666. for (int count : sum) {
  667. amount += count;
  668. }*/
  669. int amount = 0;
  670. if (type == 2) {
  671. // 问卷答案表有效(去重)答题人数
  672. amount = surveyAnswersDao.countByRelationCode(id, qstcode);
  673. } else {
  674. // 问卷选项答案表有效(去重)答题人数
  675. amount = surveyOptionAnswersDao.countByRelationCode(id, qstcode);
  676. }
  677. qst.put("amount", amount);
  678. qst.put("qstcode", qstcode);
  679. qst.put("title", title);
  680. qst.put("type", type);
  681. // 一个问题内的所有选项
  682. // List<SurveyQuestionnaireOptions> options = surveyQuestionnaireOptionsDao.findById(qstcode);
  683. List<SurveyQuestionnaireOptions> options = surveyQuestionnaireOptionsDao.findByIdAndQstCode(id, qstcode);
  684. List optList = new ArrayList();
  685. for (SurveyQuestionnaireOptions option : options) {
  686. Map<String, Object> opt = new HashMap<>();
  687. String optcode = option.getCode();
  688. String optTitle = option.getContent();
  689. int haveComment = option.getHaveComment();
  690. int count = surveyStatisticsDao.findByAllId(id, qstcode, optcode);
  691. opt.put("optcode", optcode);
  692. opt.put("option", optTitle);
  693. opt.put("count", count);
  694. opt.put("percent", (Math.round(((float) count / amount) * 100)) + "%");
  695. opt.put("haveComment", haveComment);
  696. optList.add(opt);
  697. }
  698. qst.put("options", optList);
  699. qstList.add(qst);
  700. }
  701. jsonObject.put("questions", qstList);
  702. return jsonObject;
  703. }
  704. public JSONObject getOptionsComment(String id, String questionId, String optionId, int pageNo, int pageSize) throws Exception {
  705. JSONObject jsonObject = new JSONObject();
  706. SurveyQuestionnaire question = surveyQuestionnaireDao.findByIdAndQstId(id, questionId);
  707. Sort sort = new Sort(Sort.Direction.ASC, "createTime");
  708. PageRequest request = new PageRequest(pageNo - 1, pageSize, sort);
  709. if (!StringUtils.isEmpty(optionId)) {
  710. // 查看选择题详细答案
  711. SurveyQuestionnaireOptions opt = surveyQuestionnaireOptionsDao.findByRelationCode(id, questionId, optionId);
  712. String option = opt.getContent();
  713. jsonObject.put("option", option);
  714. // 获取同一个选项的所有回答的说明(分页?)
  715. Page<String> optionComment = surveyOptionAnswersDao.findByRelationCode(id, questionId, optionId, request);
  716. jsonObject.put("content", optionComment.getContent());
  717. int amount = surveyOptionAnswersDao.findByRelationCode(id, questionId, optionId);
  718. jsonObject.put("amount", amount);
  719. } else {
  720. // 查看问答题答案(分页?)
  721. Page<String> answer = surveyAnswersDao.findByRelationCode(id, questionId, request);
  722. jsonObject.put("content", answer.getContent());
  723. int amount = surveyAnswersDao.findByRelationCode(id, questionId);
  724. jsonObject.put("amount", amount);
  725. }
  726. String title = question.getTitle();
  727. jsonObject.put("title", title);
  728. return jsonObject;
  729. }
  730. public List getTemplateList(int pageNo, int pageSize) throws Exception {
  731. Sort sort = new Sort(Sort.Direction.DESC, "createTime");
  732. PageRequest request = new PageRequest(pageNo - 1, pageSize, sort);
  733. List<SurveyTemplates> templates = surveyTemplatesDao.findAlls(request);
  734. List list = new ArrayList();
  735. for (SurveyTemplates template : templates) {
  736. Map<String, Object> tempMap = new HashMap();
  737. String title = template.getTitle();
  738. String templateId = template.getCode();
  739. String comment = template.getTemplateComment();
  740. int[] label = surveyLabelInfoDao.findByRelationCode(templateId);
  741. String[] labels = new String[label.length];
  742. for (int i = 0; i < label.length; i++) {
  743. switch (label[i]) {
  744. case 1:
  745. labels[i] = "满意度";
  746. break;
  747. case 2:
  748. labels[i] = "糖尿病";
  749. break;
  750. case 3:
  751. labels[i] = "高血压";
  752. break;
  753. case 4:
  754. labels[i] = "生活日常";
  755. break;
  756. }
  757. }
  758. tempMap.put("templateId", templateId);
  759. tempMap.put("title", title);
  760. if (comment.length() > 50) {
  761. comment = comment.substring(0, 49);
  762. }
  763. tempMap.put("comment", comment);
  764. tempMap.put("label", labels);
  765. list.add(tempMap);
  766. }
  767. return list;
  768. }
  769. public List getTemplateLabel() throws Exception {
  770. List list = new ArrayList();
  771. List<SystemDict> labels = systemDictDao.findByDictName("SURVEY_LABEL");
  772. for (SystemDict label : labels) {
  773. Map<String, Object> map = new HashMap();
  774. String code = label.getCode();
  775. String value = label.getValue();
  776. map.put("code", code);
  777. map.put("value", value);
  778. list.add(map);
  779. }
  780. return list;
  781. }
  782. public JSONObject getQuestionList(String title, int pageNo, int pageSize) throws Exception {
  783. JSONObject jsonObject = new JSONObject();
  784. Sort sort = new Sort(Sort.Direction.DESC, "createTime");
  785. PageRequest request = new PageRequest(pageNo - 1, pageSize, sort);
  786. List<SurveyQuestions> questions = new ArrayList<>();
  787. if (StringUtils.isEmpty(title)) {
  788. questions = surveyQuestionsDao.findByTitle(request);
  789. } else {
  790. questions = surveyQuestionsDao.findByTitle("%" + title + "%", request);
  791. }
  792. List list = new ArrayList();
  793. for (SurveyQuestions question : questions) {
  794. Map<String, Object> map = new HashMap<>();
  795. String code = question.getCode();
  796. String qstTitle = question.getTitle();
  797. if (question.getQuestionComment() != null) {
  798. String comment = question.getQuestionComment();
  799. map.put("comment", comment);
  800. }
  801. int isRequired = question.getIsRequired();
  802. int questionType = question.getQuestionType();
  803. if (questionType != 2) {
  804. // 获取选择题选项值
  805. List<SurveyQuestionsOptions> options = surveyQuestionsOptionsDao.findById(code);
  806. List opts = new ArrayList();
  807. for (SurveyQuestionsOptions option : options) {
  808. Map<String, Object> opt = new HashMap<>();
  809. opt.put("optCode", option.getCode());
  810. opt.put("content", option.getContent());
  811. opt.put("haveComment", option.getHaveComment());
  812. opt.put("isRequired", option.getIsRequired());
  813. opts.add(opt);
  814. }
  815. map.put("options", opts);
  816. }
  817. map.put("code", code);
  818. map.put("title", qstTitle);
  819. map.put("isRequired", isRequired);
  820. map.put("questionType", questionType);
  821. list.add(map);
  822. }
  823. jsonObject.put("questions", list);
  824. return jsonObject;
  825. }
  826. public List getTemplateByLabelOrTitle(String name, int pageNo, int pageSize) throws Exception {
  827. Sort sort = new Sort(Sort.Direction.DESC, "createTime");
  828. PageRequest request = new PageRequest(pageNo - 1, pageSize, sort);
  829. String sql = "SELECT DISTINCT s.code " +
  830. " FROM System_dict s WHERE s.dict_name ='SURVEY_LABEL' and s.value LIKE '%" + name +
  831. "%'";
  832. List<Map<String, Object>> labels = jdbcTemplate.queryForList(sql);
  833. List<SurveyTemplates> surveyTemplates = new ArrayList<>();
  834. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  835. if (labels.size() == 0) {
  836. // 模板标题
  837. String cql = "select distinct t.* from wlyy_survey_templates t where t.del = 1 and t.title like '%" + name + "%' ";
  838. List<Map<String, Object>> surveyTemp = jdbcTemplate.queryForList(cql);
  839. for (Map<String, Object> temp : surveyTemp) {
  840. String create = temp.get("create_time").toString();
  841. String update = temp.get("update_time").toString();
  842. Date createTime = sdf.parse(create);
  843. Date updateTime = sdf.parse(update);
  844. SurveyTemplates tem = new SurveyTemplates(temp.get("code").toString(),
  845. temp.get("title").toString(), temp.get("template_comment").toString(), temp.get("creater").toString(),
  846. Integer.parseInt(temp.get("del").toString()), createTime, updateTime);
  847. surveyTemplates.add(tem);
  848. }
  849. } else {
  850. for (Map<String, Object> label : labels) {
  851. // if ("SURVEY_LABEL".equals(label.get("dict_name").toString())) {
  852. surveyTemplates = surveyLabelInfoDao.findByLabel(Integer.parseInt(label.get("code").toString()));
  853. // }
  854. }
  855. }
  856. List list = new ArrayList();
  857. for (SurveyTemplates template : surveyTemplates) {
  858. Map<String, Object> map = new HashMap<>();
  859. String tempCode = template.getCode();
  860. String title = template.getTitle();
  861. String comment = template.getTemplateComment();
  862. Date createTime = template.getCreateTime();
  863. int[] label = surveyLabelInfoDao.findByRelationCode(tempCode);
  864. String[] tags = new String[label.length];
  865. for (int i = 0; i < label.length; i++) {
  866. switch (label[i]) {
  867. case 1:
  868. tags[i] = "满意度";
  869. break;
  870. case 2:
  871. tags[i] = "糖尿病";
  872. break;
  873. case 3:
  874. tags[i] = "高血压";
  875. break;
  876. case 4:
  877. tags[i] = "生活日常";
  878. break;
  879. }
  880. }
  881. map.put("tempCode", tempCode);
  882. map.put("title", title);
  883. map.put("createTime", sdf.format(createTime));
  884. map.put("label", tags);
  885. map.put("comment", comment);
  886. list.add(map);
  887. }
  888. return list;
  889. }
  890. public List getTemplateByLabel(int pageNo, int pageSize, String labels) throws Exception {
  891. if (StringUtils.isEmpty(labels)) {
  892. Sort sort = new Sort(Sort.Direction.DESC, "createTime");
  893. PageRequest request = new PageRequest(pageNo - 1, pageSize, sort);
  894. List<SurveyTemplates> templates = surveyTemplatesDao.findAlls(request);
  895. List list = new ArrayList();
  896. for (SurveyTemplates template : templates) {
  897. Map<String, Object> tempMap = new HashMap();
  898. String title = template.getTitle();
  899. String templateId = template.getCode();
  900. String comment = template.getTemplateComment();
  901. int[] label = surveyLabelInfoDao.findByRelationCode(templateId);
  902. String[] tags = new String[label.length];
  903. for (int i = 0; i < label.length; i++) {
  904. switch (label[i]) {
  905. case 1:
  906. tags[i] = "满意度";
  907. break;
  908. case 2:
  909. tags[i] = "糖尿病";
  910. break;
  911. case 3:
  912. tags[i] = "高血压";
  913. break;
  914. case 4:
  915. tags[i] = "生活日常";
  916. break;
  917. }
  918. }
  919. tempMap.put("templateId", templateId);
  920. tempMap.put("title", title);
  921. if (comment.length() > 50) {
  922. comment = comment.substring(0, 49);
  923. }
  924. tempMap.put("comment", comment);
  925. tempMap.put("label", tags);
  926. list.add(tempMap);
  927. }
  928. return list;
  929. } else {
  930. List list = new ArrayList();
  931. pageNo -= 1;
  932. String sql = "SELECT DISTINCT t.* " +
  933. " FROM wlyy_survey_templates t,wlyy_survey_label_info s " +
  934. " WHERE t.code = s.relation_code " +
  935. " AND s.use_type= 0 " +
  936. " AND t.del = 1 " +
  937. " AND s.label IN (" + labels +
  938. ") " +
  939. " ORDER by t.create_time DESC " +
  940. " limit " + pageNo +
  941. "," + pageSize;
  942. List<Map<String, Object>> listMap = jdbcTemplate.queryForList(sql);
  943. for (Map<String, Object> template : listMap) {
  944. Map<String, Object> map = new HashMap();
  945. String code = template.get("code").toString();
  946. String title = template.get("title").toString();
  947. String comment = template.get("template_comment").toString();
  948. if (comment.length() > 50) {
  949. comment = comment.substring(0, 49);
  950. }
  951. // 查询标签
  952. int[] label = surveyLabelInfoDao.findByRelationCode(code);
  953. String[] tags = new String[label.length];
  954. for (int i = 0; i < label.length; i++) {
  955. switch (label[i]) {
  956. case 1:
  957. tags[i] = "满意度";
  958. break;
  959. case 2:
  960. tags[i] = "糖尿病";
  961. break;
  962. case 3:
  963. tags[i] = "高血压";
  964. break;
  965. case 4:
  966. tags[i] = "生活日常";
  967. break;
  968. }
  969. }
  970. map.put("title", title);
  971. map.put("templateId", code);
  972. map.put("comment", comment);
  973. map.put("label", tags);
  974. list.add(map);
  975. }
  976. return list;
  977. }
  978. }
  979. public Map<String, Object> getTemplateById(String code) throws Exception {
  980. Map<String, Object> tempMap = new HashMap();
  981. SurveyTemplates template = surveyTemplatesDao.findById(code);
  982. String title = template.getTitle();
  983. String comment = template.getTemplateComment();
  984. int[] label = surveyLabelInfoDao.findByRelationCode(code);
  985. String[] labels = new String[label.length];
  986. for (int i = 0; i < label.length; i++) {
  987. switch (label[i]) {
  988. case 1:
  989. labels[i] = "满意度";
  990. break;
  991. case 2:
  992. labels[i] = "糖尿病";
  993. break;
  994. case 3:
  995. labels[i] = "高血压";
  996. break;
  997. case 4:
  998. labels[i] = "生活日常";
  999. break;
  1000. }
  1001. }
  1002. tempMap.put("templateId", code);
  1003. tempMap.put("title", title);
  1004. tempMap.put("comment", comment);
  1005. tempMap.put("label", labels);
  1006. return tempMap;
  1007. }
  1008. public JSONObject getTemplateDetail(String id) throws Exception {
  1009. JSONObject jsonObject = new JSONObject();
  1010. //问卷信息
  1011. SurveyTemplates template = surveyTemplatesDao.findById(id);
  1012. int[] label = surveyLabelInfoDao.findByRelationCode(id);
  1013. String[] labels = new String[label.length];
  1014. for (int i = 0; i < label.length; i++) {
  1015. switch (label[i]) {
  1016. case 1:
  1017. labels[i] = "满意度";
  1018. break;
  1019. case 2:
  1020. labels[i] = "糖尿病";
  1021. break;
  1022. case 3:
  1023. labels[i] = "高血压";
  1024. break;
  1025. case 4:
  1026. labels[i] = "生活日常";
  1027. break;
  1028. }
  1029. }
  1030. //问题信息
  1031. List<SurveyTemplateQuestions> surveyQuestionnair = surveyTemplateQuestionsDao.findById(id);
  1032. List questions = new ArrayList();
  1033. // 问题信息
  1034. for (SurveyTemplateQuestions questionnair : surveyQuestionnair) {
  1035. Map<String, Object> question = new HashMap<>();
  1036. String qstcode = questionnair.getCode();
  1037. String title = questionnair.getTitle();
  1038. int isRequired = questionnair.getIsRequired();
  1039. int sort = questionnair.getSort();
  1040. String comment = questionnair.getQuestionComment();
  1041. int type = questionnair.getQuestionType();
  1042. List<SurveyTemplateOptions> options = surveyTemplateOptionsDao.findById(qstcode, id);
  1043. List optList = new ArrayList();
  1044. for (SurveyTemplateOptions option : options) {
  1045. Map<String, Object> opt = new HashMap<>();
  1046. String conetent = option.getContent();
  1047. int haveComment = option.getHaveComment();
  1048. int required = option.getIsRequired();
  1049. int optsort = option.getSort();
  1050. if (!StringUtils.isEmpty(option.getQuestionCodeNext())) {
  1051. String next = option.getQuestionCodeNext();
  1052. SurveyTemplateQuestions tempQst = surveyTemplateQuestionsDao.findById(id, next);
  1053. // 跳题结束怎么办?
  1054. opt.put("nextQuestion", tempQst == null ? 0 : tempQst.getSort());
  1055. }
  1056. opt.put("isRequired", required);
  1057. opt.put("content", conetent);
  1058. opt.put("sort", optsort);
  1059. opt.put("haveComment", haveComment);
  1060. optList.add(opt);
  1061. }
  1062. question.put("title", title);
  1063. question.put("isRequired", isRequired);
  1064. question.put("comment", comment);
  1065. question.put("type", type);
  1066. question.put("options", optList);
  1067. question.put("sort", sort);
  1068. question.put("maxNum", questionnair.getMaxNum());
  1069. question.put("minNum", questionnair.getMinNum());
  1070. if (!StringUtils.isEmpty(questionnair.getQuestionCodeNext())) {
  1071. String next = questionnair.getQuestionCodeNext();
  1072. SurveyTemplateQuestions tempQst = surveyTemplateQuestionsDao.findById(id, next);
  1073. question.put("nextQuestion", tempQst == null ? 0 : tempQst.getSort());
  1074. }
  1075. questions.add(question);
  1076. }
  1077. String tempComment = template.getTemplateComment();
  1078. String tempTitle = template.getTitle();
  1079. jsonObject.put("title", tempTitle);
  1080. jsonObject.put("comment", tempComment);
  1081. jsonObject.put("label", labels);
  1082. jsonObject.put("questions", questions);
  1083. return jsonObject;
  1084. }
  1085. public int savaSurveyAsTemplate(String code) throws Exception {
  1086. Survey survey = surveyDao.findById(code);
  1087. String tempCode = getCode();
  1088. String title = survey.getTitle();
  1089. String templateComment = survey.getSurveyComment();
  1090. String creater = survey.getCreater();
  1091. Date createTime = survey.getCreateTime();
  1092. Date updateTime = survey.getUpdateTime();
  1093. // 查询问卷标题重复情况
  1094. int number = surveyTemplatesDao.distinctByTitle(title);
  1095. if (number!=0){
  1096. int likeNum = surveyTemplatesDao.distinctLikeTitle(title+"%");
  1097. return likeNum;
  1098. }
  1099. SurveyTemplates surveyTemplates = new SurveyTemplates(tempCode, title, templateComment, creater, 1, createTime, updateTime);
  1100. // 保存模板信息
  1101. surveyTemplatesDao.save(surveyTemplates);
  1102. List<SurveyLabelInfo> surveyLabelInfos = surveyLabelInfoDao.findByCode(code);
  1103. for (SurveyLabelInfo surveyLabelInfo : surveyLabelInfos) {
  1104. // 保存模板标签信息
  1105. SurveyLabelInfo surveyLabelInfo1 = new SurveyLabelInfo(getCode(), tempCode, 0, surveyLabelInfo.getLabel());
  1106. surveyLabelInfoDao.save(surveyLabelInfo1);
  1107. }
  1108. List<SurveyQuestionnaire> surveyQuestionnaires = surveyQuestionnaireDao.findById(survey.getCode());
  1109. for (SurveyQuestionnaire question : surveyQuestionnaires) {
  1110. String qstCode = getCode();
  1111. String qstTitle = question.getTitle();
  1112. String qstComment = question.getQuestionComment();
  1113. Integer qstType = question.getQuestionType();
  1114. Integer isRequired = question.getIsRequired();
  1115. Integer minNum = question.getMinNum();
  1116. Integer maxNum = question.getMaxNum();
  1117. Date createTiem = question.getCreateTime();
  1118. Date updateTiem = question.getUpdateTime();
  1119. Integer sort = question.getSort();
  1120. Integer de = question.getDel();
  1121. String next = question.getQuestionCodeNext();
  1122. SurveyTemplateQuestions surveyTemplateQuestions = new SurveyTemplateQuestions(qstCode, qstTitle, qstComment, qstType, tempCode, isRequired, minNum, maxNum, sort, next, null, de, createTiem, updateTiem);
  1123. // 保存问题到模板问题表
  1124. surveyTemplateQuestionsDao.save(surveyTemplateQuestions);
  1125. if (qstType != 2) {
  1126. List<SurveyQuestionnaireOptions> surveyQuestionnaireOptions = surveyQuestionnaireOptionsDao.findByIdAndQstCode(code, question.getCode());
  1127. for (SurveyQuestionnaireOptions option : surveyQuestionnaireOptions) {
  1128. SurveyTemplateOptions surveyTemplateOptions = new SurveyTemplateOptions(getCode(), option.getHaveComment(), qstCode, option.getContent(),
  1129. tempCode, option.getQuestionCodeNext(), null, option.getIsRequired(), option.getSort(), option.getDel());
  1130. // 保存到模板选项表 survey_template_options
  1131. surveyTemplateOptionsDao.save(surveyTemplateOptions);
  1132. }
  1133. }
  1134. }
  1135. return 0;
  1136. }
  1137. public int modifyTemplate(String code, String templateCode) throws Exception {
  1138. Survey survey = surveyDao.findById(code);
  1139. String title = survey.getTitle();
  1140. String templateComment = survey.getSurveyComment();
  1141. // 查询问卷标题重复情况
  1142. int number = surveyTemplatesDao.distinctByTitle(title);
  1143. if (number!=0){
  1144. int likeNum = surveyTemplatesDao.distinctLikeTitle(title+"%");
  1145. return likeNum;
  1146. }
  1147. // SurveyTemplates surveyTemplates = new SurveyTemplates(templateCode, title, templateComment, creater, 1, creater, updateTime);
  1148. // 更改模板信息
  1149. // surveyTemplatesDao.save(surveyTemplates);
  1150. surveyTemplatesDao.modifyTemplate(templateCode, title, templateComment);
  1151. List<SurveyLabelInfo> surveyLabelInfos = surveyLabelInfoDao.findByCode(code);
  1152. for (SurveyLabelInfo surveyLabelInfo : surveyLabelInfos) {
  1153. // 更改模板标签信息
  1154. // SurveyLabelInfo surveyLabelInfo1 = new SurveyLabelInfo(getCode(), templateCode, 0, surveyLabelInfo.getLabel());
  1155. // surveyLabelInfoDao.save(surveyLabelInfo1);
  1156. surveyLabelInfoDao.modifyLabel(templateCode, surveyLabelInfo.getLabel());
  1157. }
  1158. List<SurveyQuestionnaire> surveyQuestionnaires = surveyQuestionnaireDao.findById(survey.getCode());
  1159. for (SurveyQuestionnaire question : surveyQuestionnaires) {
  1160. String qstCode = getCode();
  1161. String qstTitle = question.getTitle();
  1162. String qstComment = question.getQuestionComment();
  1163. Integer qstType = question.getQuestionType();
  1164. Integer isRequired = question.getIsRequired();
  1165. Integer minNum = question.getMinNum();
  1166. Integer maxNum = question.getMaxNum();
  1167. // Date createTiem = question.getCreateTime();
  1168. // Date updateTiem = question.getUpdateTime();
  1169. Integer sort = question.getSort();
  1170. // Integer de = question.getDel();
  1171. String next = question.getQuestionCodeNext();
  1172. // SurveyTemplateQuestions surveyTemplateQuestions = new SurveyTemplateQuestions(qstCode, qstTitle, qstComment, qstType, templateCode, isRequired, minNum, maxNum, sort, next, null, de, createTiem, updateTiem);
  1173. // 更改问题到模板问题表
  1174. // surveyTemplateQuestionsDao.save(surveyTemplateQuestions);
  1175. surveyTemplateQuestionsDao.modifyQuestion(templateCode, qstCode, qstTitle, qstComment, qstType, isRequired, minNum, maxNum, sort, next);
  1176. if (qstType != 2) {
  1177. List<SurveyQuestionnaireOptions> surveyQuestionnaireOptions = surveyQuestionnaireOptionsDao.findByIdAndQstCode(code, question.getCode());
  1178. for (SurveyQuestionnaireOptions option : surveyQuestionnaireOptions) {
  1179. SurveyTemplateOptions surveyTemplateOptions = new SurveyTemplateOptions(getCode(), option.getHaveComment(), qstCode, option.getContent(),
  1180. templateCode, option.getQuestionCodeNext(), null, option.getIsRequired(), option.getSort(), option.getDel());
  1181. // 更改到模板选项表 survey_template_options
  1182. // surveyTemplateOptionsDao.save(surveyTemplateOptions);
  1183. surveyTemplateOptionsDao.modifyOption(templateCode, qstCode, option.getCode(),
  1184. option.getHaveComment(), option.getContent(), option.getQuestionCodeNext(), option.getIsRequired(), option.getSort());
  1185. }
  1186. }
  1187. }
  1188. return 0;
  1189. }
  1190. public Map getFilter() {
  1191. JSONObject jsonObject = new JSONObject();
  1192. // List lis = new ArrayList();
  1193. Iterable<SurveyDimension> surveyDimension = surveyDimensionDao.findAll();
  1194. Map<String, Object> map = new HashMap<>();
  1195. for (SurveyDimension dimension : surveyDimension) {
  1196. // Map<String, Object> map = new HashMap<>();
  1197. String name = dimension.getName();
  1198. int value = dimension.getValue();
  1199. switch (value) {
  1200. case 1:
  1201. name = "sex";
  1202. break;
  1203. case 2:
  1204. name = "disease";
  1205. break;
  1206. case 3:
  1207. name = "service";
  1208. break;
  1209. case 4:
  1210. name = "healthCondition";
  1211. break;
  1212. }
  1213. String sql = "SELECT t.value,t.`name` " +
  1214. " FROM wlyy_survey_dimension_option t\n" +
  1215. " WHERE t.`dimension_value`= " + value;
  1216. List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
  1217. List subLabel = new ArrayList();
  1218. for (Map<String, Object> li : list) {
  1219. Map<String, Object> map1 = new HashMap<>();
  1220. int var = Integer.parseInt(li.get("value").toString());
  1221. String nm = li.get("name").toString();
  1222. map1.put("key", var);
  1223. map1.put("value", nm);
  1224. subLabel.add(map1);
  1225. }
  1226. map.put(name, subLabel);
  1227. // lis.add(map);
  1228. }
  1229. return map;
  1230. }
  1231. public int getAmount(String json) {
  1232. return getCount(json).size();
  1233. }
  1234. public List<String> getCount(String json) {
  1235. // 1.服务 2.健康 3疾病
  1236. JSONObject jsonObject = new JSONObject(json);
  1237. String sex = jsonObject.get("sex").toString();
  1238. String str = "SELECT s.patient FROM ( " +
  1239. "SELECT t.patient,GROUP_CONCAT(t.label_name) label_name FROM wlyy_sign_patient_label_info t " +
  1240. "WHERE t.patient IN (SELECT a.patient FROM wlyy_sign_family a WHERE a.status IN(1,2,3)) AND t.patient IN (SELECT b.`code` FROM wlyy_patient b WHERE b.`openid` IS NOT NULL AND b.`openid`!='' ";
  1241. if (!"0".equals(sex)) {
  1242. str += " AND b.sex= " + sex + " ) GROUP BY t.patient ) s WHERE 1=1 ";
  1243. } else {
  1244. str += " ) GROUP BY t.patient ) s WHERE 1=1 ";
  1245. }
  1246. JSONArray service = jsonObject.getJSONArray("service");
  1247. String ser = "";
  1248. service:
  1249. for (Object serv : service) {
  1250. String type = serv.toString();
  1251. switch (type) {
  1252. // AND (s.label_name LIKE '%普通人群%'OR s.label_name LIKE '%慢病人群%'OR s.label_name LIKE '%65岁以上人群%')
  1253. case "0":
  1254. break service;
  1255. case "1":
  1256. ser += " s.label_name LIKE '%普通人群%' ";
  1257. break;
  1258. case "2":
  1259. ser += " s.label_name LIKE '%慢病人群%' ";
  1260. break;
  1261. case "3":
  1262. ser += " s.label_name LIKE '%65岁以上人群%' ";
  1263. break;
  1264. }
  1265. ser += " OR ";
  1266. }
  1267. if (!StringUtils.isEmpty(ser)) {
  1268. str += " AND ( " + ser.substring(0, ser.lastIndexOf("OR")) + " ) ";
  1269. }
  1270. JSONArray healthCondition = jsonObject.getJSONArray("healthCondition");
  1271. String heal = "";
  1272. health:
  1273. for (Object health : healthCondition) {
  1274. String type = health.toString();
  1275. switch (type) {
  1276. // AND (s.label_name LIKE '%普通人群%'OR s.label_name LIKE '%慢病人群%'OR s.label_name LIKE '%65岁以上人群%')
  1277. case "0":
  1278. break health;
  1279. case "1":
  1280. heal += " s.label_name LIKE '%健康人群%' ";
  1281. break;
  1282. case "2":
  1283. heal += " s.label_name LIKE '%患病人群%' ";
  1284. break;
  1285. case "3":
  1286. heal += " s.label_name LIKE '%高危人群%' ";
  1287. break;
  1288. case "4":
  1289. heal += " s.label_name LIKE '%恢复期人群%' ";
  1290. break;
  1291. }
  1292. heal += " OR ";
  1293. }
  1294. if (!StringUtils.isEmpty(heal)) {
  1295. str += " AND ( " + heal.substring(0, heal.lastIndexOf("OR")) + " ) ";
  1296. }
  1297. JSONArray disease = jsonObject.getJSONArray("disease");
  1298. String dis = "";
  1299. disea:
  1300. for (Object disea : disease) {
  1301. String type = disea.toString();
  1302. switch (type) {
  1303. // AND (s.label_name LIKE '%普通人群%'OR s.label_name LIKE '%慢病人群%'OR s.label_name LIKE '%65岁以上人群%')
  1304. case "0":
  1305. break disea;
  1306. case "1":
  1307. dis += " s.label_name LIKE '%糖尿病%' ";
  1308. break;
  1309. case "2":
  1310. dis += " s.label_name LIKE '%高血压%' ";
  1311. break;
  1312. }
  1313. dis += " OR ";
  1314. }
  1315. if (!StringUtils.isEmpty(dis)) {
  1316. str += " AND ( " + dis.substring(0, dis.lastIndexOf("OR")) + " ) ";
  1317. }
  1318. /* if (Integer.parseInt(sex) != 0) {
  1319. str += "and t.sex = " + sex;
  1320. }
  1321. JSONArray service = jsonObject.getJSONArray("service");
  1322. String ser = "";
  1323. String servi = "";
  1324. for (Object serv : service) {
  1325. if ("0".equals(serv.toString())) {
  1326. ser += ",";
  1327. break;
  1328. } else {
  1329. ser += serv.toString() + ",";
  1330. }
  1331. }
  1332. ser = ser.substring(0, ser.length() - 1);
  1333. if (ser.length() != 0) {
  1334. servi = "(t.`label_type`=1 AND t.`label`IN(" + ser + "))";
  1335. }
  1336. JSONArray healthCondition = jsonObject.getJSONArray("healthCondition");
  1337. String heal = "";
  1338. String healt = "";
  1339. for (Object health : healthCondition) {
  1340. if ("0".equals(health.toString())) {
  1341. heal += ",";
  1342. break;
  1343. } else {
  1344. heal += health.toString() + ",";
  1345. }
  1346. }
  1347. heal = heal.substring(0, heal.length() - 1);
  1348. if (heal.length() != 0) {
  1349. healt = "(t.`label_type`=2 AND t.`label`IN(" + heal + "))";
  1350. }
  1351. JSONArray disease = jsonObject.getJSONArray("disease");
  1352. String dise = "";
  1353. String diseas = "";
  1354. for (Object disea : disease) {
  1355. if ("0".equals(disea.toString())) {
  1356. dise += ",";
  1357. break;
  1358. } else {
  1359. dise += disea.toString() + ",";
  1360. }
  1361. }
  1362. dise = dise.substring(0, dise.length() - 1);
  1363. if (dise.length() != 0) {
  1364. diseas = "(t.`label_type`=3 AND t.`label`IN(" + dise + "))";
  1365. }
  1366. if (servi.length() != 0 & healt.length() != 0 & diseas.length() != 0) {
  1367. str += "AND ( " + servi + "OR" + healt + "OR" + diseas + " )";
  1368. } else if (servi.length() != 0 & healt.length() != 0 & diseas.length() == 0) {
  1369. str += "AND ( " + servi + "OR" + healt + " )";
  1370. } else if (servi.length() != 0 & healt.length() == 0 & diseas.length() != 0) {
  1371. str += "AND ( " + servi + "OR" + diseas + " )";
  1372. } else if (servi.length() == 0 & healt.length() != 0 & diseas.length() != 0) {
  1373. str += "AND ( " + healt + "OR" + diseas + " )";
  1374. } else if (servi.length() != 0 & healt.length() == 0 & diseas.length() == 0) {
  1375. str += "AND " + servi;
  1376. } else if (servi.length() == 0 & healt.length() != 0 & diseas.length() == 0) {
  1377. str += "AND " + healt;
  1378. } else if (servi.length() == 0 & healt.length() == 0 & diseas.length() != 0) {
  1379. str += "AND " + diseas;
  1380. }*/
  1381. // Map<String, Object> patients = jdbcTemplate.queryForMap(str);
  1382. List<String> patients = jdbcTemplate.queryForList(str, String.class);
  1383. return patients;
  1384. }
  1385. /**
  1386. * 线程发送模板消息
  1387. */
  1388. class SendMessage implements Runnable {
  1389. private String code;
  1390. private List<String> codes;
  1391. private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  1392. private String str = "{\"remark\":\"我们将按调查最终结果,优化健康服务。\",\"keyword1\":\"调查问卷\"}";
  1393. private String accessToken;
  1394. private JSONObject json = new JSONObject(str);
  1395. public SendMessage(String code, List<String> codes, String accessToken) {
  1396. this.code = code;
  1397. this.codes = codes;
  1398. this.accessToken = accessToken;
  1399. }
  1400. @Override
  1401. public void run() {
  1402. synchronized (this) {
  1403. json.put("survey_id", code);
  1404. for (String cod : codes) {
  1405. Patient p = patientDao.findByCode(cod);
  1406. String name = p.getName();
  1407. String openId = p.getOpenid();
  1408. /*String name = "吴俊杰";
  1409. String openId = "o7NFZw1QM4YR1O19mLjwfX1Hh11A";
  1410. cod = "jojojojpkpokpko";*/
  1411. json.put("keyword2", sdf.format(new Date()));
  1412. json.put("toUser", cod);
  1413. json.put("first", name + ",您好!为了给您提供更好的家庭医生健康服务,现有一份问卷需要您填写。");
  1414. PushMsgTask.getInstance().sendWeixinMessage(accessToken, 11, openId, name, json);
  1415. }
  1416. }
  1417. }
  1418. }
  1419. }