package com.yihu.jw.service;/** * Created by nature of king on 2018/4/27. */ import com.yihu.base.mysql.query.BaseJpaService; import com.yihu.jw.dao.*; import com.yihu.jw.entity.health.bank.*; import com.yihu.jw.restmodel.common.Envelop; import com.yihu.jw.rm.health.bank.HealthBankMapping; import com.yihu.jw.util.ISqlUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import javax.transaction.Transactional; import java.text.ParseException; import java.util.List; import java.util.Map; /** * @author wangzhinan * @create 2018-04-27 16:53 * @desc credits log info Service **/ @Service @Transactional public class CreditsDetailService extends BaseJpaService { @Autowired private CredittsLogDetailDao credittsLogDetailDao; @Autowired private TaskDao taskDao; @Autowired private ActivityDao activityDao; @Autowired private JdbcTemplate jdbcTemplate; @Autowired private AccountDao accountDao; @Autowired private TaskDetailDao taskDetailDao; /** * find creditsLogInfo * * @return * @throws ParseException */ public Envelop findByCondition(CreditsDetailDO creditsDetailDO, Integer page, Integer size) throws ParseException { String sql = new ISqlUtils().getSql(creditsDetailDO,page,size,"*"); List creditsDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(CreditsDetailDO.class)); for (CreditsDetailDO creditsDetailDO1 : creditsDetailDOS){ if (creditsDetailDO1.getTradeType().equalsIgnoreCase("HEALTH_TASK")){ TaskDO taskDO = taskDao.findOne(creditsDetailDO1.getTransactionId()); creditsDetailDO1.setTaskDO(taskDO); }else if(creditsDetailDO1.getTradeType().equalsIgnoreCase("HEALTH_ACTIVITY")){ ActivityDO activityDO = activityDao.findOne(creditsDetailDO1.getTransactionId()); creditsDetailDO1.setActivityDO(activityDO); } } String sqlcount = new ISqlUtils().getSql(creditsDetailDO,0,0,"count"); List> rstotal = jdbcTemplate.queryForList(sqlcount); Long count = 0L; if(rstotal!=null&&rstotal.size()>0){ count = (Long) rstotal.get(0).get("total"); } return Envelop.getSuccessListWithPage(HealthBankMapping.api_success, creditsDetailDOS,page,size,count); } /** * 获取账户信息 * * @param creditsDetailDO * @return */ public Envelop findByTradeDirection(CreditsDetailDO creditsDetailDO){ AccountDO accountDO = accountDao.findOne(creditsDetailDO.getAccountId()); String sql = "SELECT SUM(cd.integrate) as total FROM wlyy_health_bank_credits_detail cd where cd.trade_direction = "+creditsDetailDO.getTradeDirection(); List> rstotal = jdbcTemplate.queryForList(sql); if (rstotal!= null && rstotal.size()>0){ if (rstotal.get(0).get("total") == null){ accountDO.setUsedTotal(0); }else { accountDO.setUsedTotal(Integer.parseInt(rstotal.get(0).get("total").toString())); } } return Envelop.getSuccess(HealthBankMapping.api_success,accountDO); } /* public Envelop exchangeGoods(GoodsDO goodsDO){ CreditsDetailDO creditsLogDetailDO = new CreditsDetailDO(); Envelop envelop = new Envelop<>(); envelop.setObj(true); return envelop; } */ public Envelop selectByRanking(List patientIds, Integer page, Integer size){ StringBuffer buffer = new StringBuffer(); if (patientIds !=null && patientIds.size()!=0){ buffer.append(" ba.patient_id in("); for (int i=0;i accountDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(AccountDO.class)); String sqlCount = "SELECT count(1) AS total"+ " FROM " + " wlyy_health_bank_account ba, " + " ( " + " SELECT " + " SUM(cd.integrate) AS total, " + " cd.patient_id AS patient_id " + " FROM " + " wlyy_health_bank_credits_detail cd " + " WHERE " + " cd.trade_direction = - 1 " + " GROUP BY " + " cd.patient_id " + " ) cd1 " + "WHERE " + buffer + "AND cd1.patient_id = ba.patient_id " + "ORDER BY " + " ba.create_time, " + " (ba.total + cd1.total) DESC "; List> rstotal = jdbcTemplate.queryForList(sqlCount); Long count = 0L; if(rstotal!=null&&rstotal.size()>0){ count = (Long) rstotal.get(0).get("total"); } return Envelop.getSuccessListWithPage(HealthBankMapping.api_success, accountDOS,page,size,count); } /** * 添加积分 * * @param creditsDetailDO * @return */ public Envelop insert(CreditsDetailDO creditsDetailDO){ if (creditsDetailDO.getTradeDirection() == 1){ if (creditsDetailDO.getTradeType().equals("HEALTH_TASK")){ TaskDetailDO taskDetailDO = new TaskDetailDO(); taskDetailDO.setIntegrate(creditsDetailDO.getIntegrate()); taskDetailDO.setTaskId(creditsDetailDO.getTransactionId()); taskDetailDO.setSaasId(creditsDetailDO.getSaasId()); taskDetailDO.setPatientId(creditsDetailDO.getPatientId()); taskDetailDO.setTradeDirection(creditsDetailDO.getTradeDirection()); taskDetailDO.setStatus("1"); taskDetailDao.save(taskDetailDO); } } CreditsDetailDO creditsDetailDO1 =credittsLogDetailDao.save(creditsDetailDO); AccountDO accountDO = accountDao.findOne(creditsDetailDO1.getAccountId()); if (creditsDetailDO1.getTradeDirection() == 1){ accountDO.setTotal(accountDO.getTotal()+creditsDetailDO1.getIntegrate()); }else if (creditsDetailDO.getTradeDirection() == -1){ accountDO.setTotal(accountDO.getTotal()-creditsDetailDO1.getIntegrate()); } accountDao.save(accountDO); Envelop envelop = new Envelop<>(); envelop.setObj(true); return envelop; } }