AccountService.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. package com.yihu.jw.service;/**
  2. * Created by nature of king on 2018/5/10.
  3. */
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.yihu.jw.dao.AccountDao;
  6. import com.yihu.jw.dao.TaskRuleDao;
  7. import com.yihu.jw.entity.health.bank.AccountDO;
  8. import com.yihu.jw.entity.health.bank.TaskDO;
  9. import com.yihu.jw.entity.health.bank.TaskPatientDetailDO;
  10. import com.yihu.jw.entity.health.bank.TaskRuleDO;
  11. import com.yihu.jw.restmodel.web.MixEnvelop;
  12. import com.yihu.jw.rm.health.bank.HealthBankMapping;
  13. import com.yihu.jw.util.DateUtils;
  14. import com.yihu.jw.util.ISqlUtils;
  15. import com.yihu.jw.util.TransforSqlUtl;
  16. import com.yihu.mysql.query.BaseJpaService;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  19. import org.springframework.jdbc.core.JdbcTemplate;
  20. import org.springframework.stereotype.Service;
  21. import javax.transaction.Transactional;
  22. import java.text.ParseException;
  23. import java.text.SimpleDateFormat;
  24. import java.util.ArrayList;
  25. import java.util.Date;
  26. import java.util.List;
  27. import java.util.Map;
  28. /**
  29. * @author wangzhinan
  30. * @create 2018-05-10 11:26
  31. * @desc account service
  32. **/
  33. @Service
  34. @Transactional
  35. public class AccountService extends BaseJpaService<AccountDO,AccountDao> {
  36. @Autowired
  37. private AccountDao accountDao;
  38. @Autowired
  39. private JdbcTemplate jdbcTemplate;
  40. @Autowired
  41. private TaskRuleDao taskRuleDao;
  42. /**
  43. * 添加银行账户信息
  44. *
  45. * @param accountDO 银行账户对象
  46. * @return
  47. */
  48. public MixEnvelop<Boolean, Boolean> insert(AccountDO accountDO){
  49. accountDao.save(accountDO);
  50. MixEnvelop<Boolean, Boolean> envelop = new MixEnvelop<>();
  51. envelop.setObj(true);
  52. return envelop;
  53. }
  54. /**
  55. * 获取银行账户信息
  56. *
  57. * @param accountDO 银行账户对象
  58. * @param page 页码
  59. * @param size 每页大小
  60. * @return
  61. * @throws ParseException
  62. */
  63. public MixEnvelop<AccountDO, AccountDO> findByCondition(AccountDO accountDO, Integer page, Integer size) throws ParseException {
  64. String sql = new ISqlUtils().getSql(accountDO,page,size,"*");
  65. List<AccountDO> accountDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(AccountDO.class));
  66. for (AccountDO accountDO1:accountDOS){
  67. String sql1 = "select COALESCE(sum(bcd.integrate),0) as total from wlyy_health_bank_credits_detail bcd where bcd.trade_direction = 1" +
  68. " AND bcd.create_time > '"+ DateUtils.getDayBegin()+"' AND bcd.create_time < '"+DateUtils.getDayEnd()+"' AND bcd.patient_id = '" + accountDO1.getPatientId() +"'"
  69. +"AND transaction_id = '"+accountDO.getTaskId()+"'";
  70. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sql1);
  71. Long count = 0L;
  72. if(rstotal!=null&&rstotal.size()>0){
  73. Object object = rstotal.get(0).get("total");
  74. if (object != null){
  75. count = Long.parseLong(object.toString());
  76. }
  77. }
  78. accountDO1.setNowTotal(count);
  79. String activitySql1 =" SELECT " +
  80. " SUM(ptpd.total) AS total FROM wlyy_health_bank_task_patient_detail ptpd " +
  81. " WHERE " +" task_id = '" + accountDO.getTaskId() + "' " +
  82. "and patient_id = '"+accountDO1.getPatientId()+"'";
  83. List<Map<String,Object>> rstotal6 = jdbcTemplate.queryForList(activitySql1);
  84. Long activityIntegrate = 0L;
  85. if(rstotal6!=null&&rstotal6.size()>0){
  86. Object object = rstotal6.get(0).get("total");
  87. if (object!=null){
  88. activityIntegrate = Long.parseLong(object.toString());
  89. }
  90. }
  91. accountDO1.setActivityIntegrate(activityIntegrate);
  92. String activitySql = "SELECT COUNT(*) AS total FROM ( SELECT * FROM " +
  93. "wlyy_health_bank_task_patient_detail " +
  94. " WHERE patient_id = '" + accountDO1.getPatientId()+
  95. "'AND activity_id != '' GROUP BY activity_id ) btpd1";
  96. List<Map<String,Object>> rstotal1 = jdbcTemplate.queryForList(activitySql);
  97. Long activityCount = 0L;
  98. if(rstotal1!=null&&rstotal1.size()>0){
  99. Object object = rstotal1.get(0).get("total");
  100. if (object != null){
  101. activityCount = Long.parseLong(object.toString());
  102. }
  103. }
  104. accountDO1.setActivityTotal(activityCount);
  105. String taskSql = "SELECT COUNT(*) AS total FROM ( SELECT * FROM " +
  106. "wlyy_health_bank_task_patient_detail " +
  107. " WHERE patient_id = '" + accountDO1.getPatientId()+
  108. "' GROUP BY task_id ) btpd1";
  109. List<Map<String,Object>> rstotal2 = jdbcTemplate.queryForList(taskSql);
  110. Long taskCount = 0L;
  111. if(rstotal2!=null&&rstotal2.size()>0){
  112. Object object = rstotal2.get(0).get("total");
  113. if (object != null ){
  114. taskCount = Long.parseLong(object.toString());
  115. }
  116. }
  117. accountDO1.setTaskTotal(taskCount);
  118. if (accountDO.getPatientIds() != null && accountDO.getPatientIds().size() != 0){
  119. StringBuffer buffer = new StringBuffer();
  120. buffer.append(" (");
  121. for (int i=0;i<accountDO.getPatientIds().size();i++){
  122. buffer.append("'"+accountDO.getPatientIds().get(i)+"'").append(",");
  123. }
  124. buffer.deleteCharAt(buffer.length()-1);
  125. buffer.append(") ");
  126. String accountSql = "select * from wlyy_health_bank_account where patient_id in "+buffer;
  127. accountSql = TransforSqlUtl.wlyyHealthBankAccountAll(accountSql);
  128. List<AccountDO> accountDOSList = jdbcTemplate.query(accountSql,new BeanPropertyRowMapper<>(AccountDO.class));
  129. if (accountDOSList != null && accountDOSList.size() !=0){
  130. String sql2 = " SELECT " +
  131. " ba.total + COALESCE (bacd1.sum, 0) AS total " +
  132. " FROM " +
  133. " wlyy_health_bank_account ba " +
  134. " LEFT JOIN ( " +
  135. " SELECT " +
  136. " COALESCE (SUM(bacd.integrate), 0) AS sum, " +
  137. " bacd.account_id " +
  138. " FROM " +
  139. " wlyy_health_bank_credits_detail bacd " +
  140. " WHERE " +
  141. " bacd.trade_direction = - 1 " +
  142. " GROUP BY " +
  143. " bacd.account_id ) bacd1 ON ba.id = bacd1.account_id " +
  144. " WHERE " +
  145. " ba.id = '"+accountDO1.getId()+"'";
  146. List<Map<String,Object>> rstotal3 = jdbcTemplate.queryForList(sql2);
  147. Long Count = 0L;
  148. if(rstotal3!=null&&rstotal3.size()>0){
  149. Object object = rstotal3.get(0).get("total");
  150. if (object!=null){
  151. Count = Long.parseLong(object.toString());
  152. }
  153. }
  154. accountDO1.setSum(Count);
  155. StringBuffer buffer1 = new StringBuffer();
  156. buffer1.append(" (");
  157. for (int i=0;i<accountDOSList.size();i++){
  158. buffer1.append("'"+accountDOSList.get(i).getId()+"'").append(",");
  159. }
  160. buffer1.deleteCharAt(buffer1.length()-1);
  161. buffer1.append(") ");
  162. String sql3 = "SELECT " +
  163. " COUNT(1) + 1 AS total " +
  164. "FROM " +
  165. " ( " +
  166. " SELECT " +
  167. " ba.id AS id, " +
  168. " ba.total AS sum " +
  169. " FROM " +
  170. " wlyy_health_bank_account ba " +
  171. " WHERE " +
  172. " ba.id IN " +buffer1+
  173. " )ba1 WHERE ba1.sum > "+accountDO1.getSum();
  174. List<Map<String,Object>> rstotal4 = jdbcTemplate.queryForList(sql3);
  175. Integer Count1 = 0;
  176. if(rstotal4!=null&&rstotal4.size()>0){
  177. Object object = rstotal4.get(0).get("total");
  178. if (object != null){
  179. Count1 = Integer.parseInt(object.toString());
  180. }
  181. }
  182. accountDO1.setTeamRanking(Count1);
  183. String sql4 = "SELECT " +
  184. " COUNT(1) + 1 AS total " +
  185. "FROM " +
  186. " ( " +
  187. " SELECT " +
  188. " ba.id AS id, " +
  189. " ba.total AS sum " +
  190. " FROM " +
  191. " wlyy_health_bank_account ba " +
  192. " )ba1 WHERE ba1.sum > "+accountDO1.getSum();
  193. List<Map<String,Object>> rstotal5 = jdbcTemplate.queryForList(sql4);
  194. Integer Count2 = 0;
  195. if(rstotal5!=null&&rstotal5.size()>0){
  196. Object object = rstotal5.get(0).get("total");
  197. if (object != null){
  198. Count2 = Integer.parseInt(object.toString());
  199. }
  200. }
  201. accountDO1.setCityRanking(Count2);
  202. }
  203. }
  204. }
  205. String sqlcount = new ISqlUtils().getSql(accountDO,0,0,"count");
  206. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlcount);
  207. Long count = 0L;
  208. if(rstotal!=null&&rstotal.size()>0){
  209. count = (Long) rstotal.get(0).get("total");
  210. }
  211. return MixEnvelop.getSuccessListWithPage(HealthBankMapping.api_success,accountDOS,page,size,count);
  212. }
  213. /**
  214. * 根据条件活动用户信息
  215. *
  216. * @param page 页码
  217. *
  218. * @param size 分页大小
  219. * @return
  220. */
  221. public MixEnvelop<AccountDO, AccountDO> findByCondition1(JSONArray patients, String ruleId, Integer page, Integer size){
  222. for (int i=0;i<patients.size();i++){
  223. String patientId = patients.getJSONObject(i).getString("code");
  224. // String sql = "select * from wlyy_health_bank_account where patient_id = '"+patientId+"'";
  225. // List<AccountDO> accountDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(AccountDO.class));
  226. List<AccountDO> accountDOS = accountDao.findByPatientId(patientId);
  227. if (accountDOS == null || accountDOS.size() == 0) {
  228. AccountDO accountDO1 = new AccountDO();
  229. accountDO1.setPatientId(patientId);
  230. accountDO1.setTotal(0);
  231. accountDO1.setAccountName(patients.getJSONObject(i).getString("name"));
  232. accountDO1.setCardNumber(patients.getJSONObject(i).getString("idcard"));
  233. accountDO1.setHospital("350205");
  234. accountDO1.setPassword("321321312321");
  235. accountDO1.setHospitalName("海沧区");
  236. accountDO1.setCreateTime(new Date());
  237. accountDO1.setUpdateTime(new Date());
  238. accountDao.save(accountDO1);
  239. }
  240. }
  241. TaskRuleDO taskRuleDO = taskRuleDao.findOne(ruleId);
  242. String taskSql = "select * from wlyy_health_bank_task where transaction_id = '"+taskRuleDO.getId()+"'";
  243. List<TaskDO> taskDOList = jdbcTemplate.query(taskSql,new BeanPropertyRowMapper(TaskDO.class));
  244. List<String> ids = new ArrayList<>();
  245. if(taskDOList != null && taskDOList.size() != 0){
  246. if (taskRuleDO.getPeriod() == 1){
  247. for (int i = 0;i<patients.size();i++){
  248. String patientId = patients.getJSONObject(i).getString("code");
  249. String sql = "select * from wlyy_health_bank_task_patient_detail where patient_id = '"+patientId+"' AND task_id = '"+taskDOList.get(0).getId()+"'";
  250. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  251. if (taskPatientDetailDOS ==null || taskPatientDetailDOS.size() == 0){
  252. ids.add(patientId);
  253. }
  254. }
  255. }else if (taskRuleDO.getPeriod() == 0){
  256. for (int i = 0;i<patients.size();i++){
  257. String patientId = patients.getJSONObject(i).getString("code");
  258. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  259. Date date = new Date();
  260. String date1 = dateFormat.format(date);
  261. String begin = DateUtils.getMinMonthDate(date1);
  262. String end = DateUtils.getMaxMonthDate(date1);
  263. String sql = "select * from wlyy_health_bank_task_patient_detail where patient_id = '"+patientId+"' AND task_id = '"+taskDOList.get(0).getId()+"' AND create_time > '"+begin+"' AND create_time < '"+end+"'";
  264. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  265. if (taskPatientDetailDOS ==null || taskPatientDetailDOS.size() == 0){
  266. ids.add(patientId);
  267. }
  268. }
  269. }
  270. }else {
  271. for (int i = 0;i<patients.size();i++){
  272. String patientId = patients.getJSONObject(i).getString("code");
  273. ids.add(patientId);
  274. }
  275. }
  276. StringBuffer buffer = new StringBuffer();
  277. buffer.append("(");
  278. if (ids == null || ids.size() == 0){
  279. buffer.append("''");
  280. }else {
  281. for (int i=0;i<ids.size();i++){
  282. buffer.append("'"+ids.get(i)+"'").append(",");
  283. }
  284. buffer.deleteCharAt(buffer.length()-1);
  285. }
  286. buffer.append(") ");
  287. String sql =
  288. "SELECT ba1.patient_id AS patient_id," +
  289. "ba1.account_name AS account_name," +
  290. "ba1.hospital AS hospital," +
  291. "ba1.total AS total," +
  292. "ba1.create_time AS create_time," +
  293. "ba1.sum AS sum" +
  294. " FROM" +
  295. "( SELECT " +
  296. "ba.patient_id AS patient_id," +
  297. "ba.account_name AS account_name," +
  298. "ba.hospital AS hospital," +
  299. "ba.total AS total," +
  300. "ba.create_time AS create_time," +
  301. "(ba.total +COALESCE((cd1.total),0)) AS sum" +
  302. " FROM" +
  303. " wlyy_health_bank_account ba" +
  304. " LEFT JOIN ( " +
  305. "SELECT" +
  306. " SUM(cd.integrate) AS total," +
  307. " cd.patient_id AS patient_id" +
  308. " FROM" +
  309. " wlyy_health_bank_credits_detail cd" +
  310. " WHERE " +
  311. "cd.trade_direction = - 1" +
  312. " GROUP BY " +
  313. " cd.patient_id ) cd1 ON cd1.patient_id = ba.patient_id " +
  314. " WHERE ba.patient_id IN " + buffer +
  315. " ORDER BY" +
  316. " ba.create_time DESC " +
  317. "LIMIT "+(page-1)*size+","+size +")ba1" +
  318. " ORDER BY " +
  319. " ba1.sum DESC";
  320. sql = TransforSqlUtl.accountName(sql);
  321. List<AccountDO> accountDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(AccountDO.class));
  322. String sqlCount = "SELECT count(1) AS total"+
  323. " FROM " +
  324. " wlyy_health_bank_account ba LEFT JOIN " +
  325. " ( " +
  326. " SELECT " +
  327. " SUM(cd.integrate) AS total, " +
  328. " cd.patient_id AS patient_id " +
  329. " FROM " +
  330. " wlyy_health_bank_credits_detail cd " +
  331. " WHERE " +
  332. " cd.trade_direction = - 1 " +
  333. " GROUP BY " +
  334. " cd.patient_id " +
  335. " ) cd1 ON cd1.patient_id = ba.patient_id " +
  336. "WHERE ba.patient_id IN " + buffer +
  337. " ORDER BY " +
  338. " ba.create_time, " +
  339. " (ba.total + COALESCE(cd1.total,0)) DESC ";
  340. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlCount);
  341. Long count = 0L;
  342. if(rstotal!=null&&rstotal.size()>0){
  343. count = (Long) rstotal.get(0).get("total");
  344. }
  345. return MixEnvelop.getSuccessListWithPage(HealthBankMapping.api_success, accountDOS,page,size,count);
  346. }
  347. /**
  348. * 根据居民code获取积分
  349. *
  350. * @param patient
  351. * @return
  352. */
  353. public MixEnvelop<AccountDO, AccountDO> selectByPatient(String patient,String name,String hospital,String hospitalName,String idcard){
  354. MixEnvelop<AccountDO,AccountDO> envelop = new MixEnvelop<>();
  355. // String sql = "select * from wlyy_health_bank_account where status = 1 and patient_id = '"+patient+"'";
  356. // List<AccountDO> accountDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(AccountDO.class));
  357. List<AccountDO> accountDOS = accountDao.selectsByPatientId(patient);
  358. if (accountDOS == null || accountDOS.size()==0){
  359. AccountDO accountDO = new AccountDO();
  360. accountDO.setTotal(0);
  361. accountDO.setId(getCode());
  362. accountDO.setPatientId(patient);
  363. accountDO.setAccountName(name);
  364. accountDO.setHospital(hospital);
  365. accountDO.setHospitalName(hospitalName);
  366. if(idcard.length()>=4){// 判断是否长度大于等于4
  367. String cardNumber=idcard.substring(idcard.length()- 4,idcard.length());//截取两个数字之间的部分
  368. int random = (int)((Math.random()*9+1)*100000);
  369. accountDO.setCardNumber(cardNumber+Integer.toString(random));
  370. }
  371. accountDO.setPassword("123456");
  372. accountDO.setSaasId("dev");
  373. accountDO.setStatus(1);
  374. accountDO.setCreateTime(new Date());
  375. accountDO.setUpdateTime(new Date());
  376. accountDO = accountDao.save(accountDO);
  377. envelop.setObj(accountDO);
  378. }else {
  379. envelop.setObj(accountDOS.get(0));
  380. }
  381. return envelop;
  382. }
  383. }