package com.yihu.jw.service;/** * Created by nature of king on 2018/6/8. */ import com.yihu.jw.dao.AccountDao; import com.yihu.jw.dao.ActivityDao; import com.yihu.jw.dao.TaskDao; import com.yihu.jw.dao.TaskPatientDetailDao; import com.yihu.jw.entity.health.bank.*; import com.yihu.jw.entity.util.AesEncryptUtils; import com.yihu.jw.restmodel.web.MixEnvelop; import com.yihu.jw.rm.health.bank.HealthBankMapping; import com.yihu.jw.util.ISqlUtils; import com.yihu.jw.util.TransforSqlUtl; import com.yihu.mysql.query.BaseJpaService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.util.Date; import java.util.List; import java.util.Map; /** * @author wangzhinan * @create 2018-06-08 15:43 * @desc 任务参与 Service **/ @Service @Transactional public class TaskPatientDtailService extends BaseJpaService { private Logger logger = LoggerFactory.getLogger(TaskPatientDtailService.class); @Autowired private TaskPatientDetailDao taskPatientDetailDao; @Autowired private JdbcTemplate jdbcTemplate; @Autowired private AccountDao accountDao; @Autowired private TaskDao taskDao; @Autowired private ActivityDao activityDao; /** * 查看任务参与情况 * * @param taskPatientDetailDO 任务参与对象 * * @param page 页码 * @param size 分页大小 * @return */ public MixEnvelop selectByCondition(TaskPatientDetailDO taskPatientDetailDO, Integer page, Integer size){ String sql = new ISqlUtils().getSql(taskPatientDetailDO,page,size,"*"); List taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class)); String sqlcount = new ISqlUtils().getSql(taskPatientDetailDO,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 MixEnvelop.getSuccessListWithPage(HealthBankMapping.api_success, taskPatientDetailDOS,page,size,count); } /** * 添加任务参与 * * @param taskPatientDetailDO 任务参与对象 * @return */ public MixEnvelop insert(TaskPatientDetailDO taskPatientDetailDO) throws Exception{ MixEnvelop envelop = new MixEnvelop<>(); taskPatientDetailDO.setCreateTime(new Date()); taskPatientDetailDO.setUpdateTime(new Date()); taskPatientDetailDO.setStatus(Integer.parseInt("0")); // String accountSql = "select * from wlyy_health_bank_account where patient_id = '"+taskPatientDetailDO.getPatientId()+"'"; // List accountDOS = jdbcTemplate.query(accountSql,new BeanPropertyRowMapper(AccountDO.class)); List accountDOS = accountDao.findByPatientId(taskPatientDetailDO.getPatientId()); if (accountDOS == null || accountDOS.size() ==0){ AccountDO accountDO = new AccountDO(); accountDO.setSaasId("dev"); accountDO.setStatus(1); if(taskPatientDetailDO.getPatientIdcard().length()>=4){// 判断是否长度大于等于4 String cardNumber=taskPatientDetailDO.getPatientIdcard().substring(taskPatientDetailDO.getPatientIdcard().length()- 4,taskPatientDetailDO.getPatientIdcard().length());//截取两个数字之间的部分 int random = (int)((Math.random()*9+1)*100000); accountDO.setCardNumber(cardNumber+Integer.toString(random)); } accountDO.setAccountName(taskPatientDetailDO.getName()); accountDO.setTotal(0); accountDO.setCouponTotal(0L); accountDO.setPatientId(taskPatientDetailDO.getPatientId()); accountDO.setCardNumber(taskPatientDetailDO.getPatientIdcard()); accountDO.setHospital(taskPatientDetailDO.getHospital()); accountDO.setHospitalName(taskPatientDetailDO.getHospitalName()); accountDO.setCreateTime(new Date()); accountDO.setUpdateTime(new Date()); accountDao.save(accountDO); } String activitySql = "select * from wlyy_health_bank_task where transaction_id = '" + taskPatientDetailDO.getActivityId() +"'"; List taskDOList = jdbcTemplate.query(activitySql,new BeanPropertyRowMapper(TaskDO.class)); StringBuffer buffer = new StringBuffer(); buffer.append(" and task_id IN ("); for (TaskDO taskDO : taskDOList){ buffer.append("'"+taskDO.getId()+"'").append(","); } buffer.deleteCharAt(buffer.length()-1); buffer.append(")"); String sql = "select * from wlyy_health_bank_task_patient_detail where patient_openid = '"+taskPatientDetailDO.getPatientOpenid()+"'"+buffer; List taskPatientDetailDOList = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class)); if (taskPatientDetailDOList != null && taskPatientDetailDOList.size() !=0){ throw new Exception("该微信账号已报名过!"); } for (TaskDO taskDO : taskDOList){ taskPatientDetailDO.setTotal(Long.parseLong("0")); taskPatientDetailDO.setTaskId(taskDO.getId()); taskPatientDetailDO.setCouponTotal(0L); taskPatientDetailDao.save(taskPatientDetailDO); } envelop.setObj(true); return envelop; } /** * 更新任务参与 * * @param taskPatientDetailDO 任务参与对象 * @return */ public MixEnvelop update(TaskPatientDetailDO taskPatientDetailDO){ MixEnvelop envelop = new MixEnvelop<>(); String sql = ISqlUtils.getUpdateSql(taskPatientDetailDO); jdbcTemplate.update(sql); envelop.setObj(true); return envelop; } /** * 获取参与活动信息 * * @param openId 微信id * @param idCard 身份证号 * @param unionId * @param taskCode 任务标识 * @return */ public TaskPatientDetailDO selectByPatientId(String openId,String idCard,String unionId,String taskCode){ logger.info("openId:"+openId+"idCard:"+idCard+"unionId:"+unionId+"taskCode:"+taskCode); String sql ="select * from wlyy_health_bank_task_patient_detail where " + " (patient_idcard = '"+ AesEncryptUtils.encrypt(idCard)+"' OR union_id ='"+unionId+"') AND task_id IN " + "(select id from wlyy_health_bank_task where task_code = '"+taskCode+"' )"; sql = TransforSqlUtl.wlyyHealthBankTaskPatientDetailAll(sql); List taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class)); logger.info("参与信息:"+taskPatientDetailDOS); if (taskPatientDetailDOS!=null&&taskPatientDetailDOS.size()!=0){ return taskPatientDetailDOS.get(0); }else { return null; } } public TaskPatientDetailDO selectByPatientIdAndTaskId(String openId,String idCard,String unionId,String taskId){ logger.info("openId:"+openId+"idCard:"+idCard+"unionId:"+unionId+"taskCode:"+taskId); String sql ="select * from wlyy_health_bank_task_patient_detail where " + " (patient_idcard = '"+AesEncryptUtils.encrypt(idCard)+"' OR union_id ='"+unionId+"') AND task_id = '"+taskId+"'" ; sql = TransforSqlUtl.wlyyHealthBankTaskPatientDetailAll(sql); List taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class)); logger.info("参与信息:"+taskPatientDetailDOS); return taskPatientDetailDOS.get(0); } /** * 获取参与详情 * * @param page * @param size * @return */ public List select(Integer page,Integer size){ String sql ="SELECT * FROM `wlyy_health_bank_task_patient_detail` btpd " + "GROUP BY btpd.activity_id, btpd.patient_id ORDER BY btpd.create_time limit "+(page-1)*size+","+size; sql = TransforSqlUtl.wlyyHealthBankTaskPatientDetailAll(sql); List taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(TaskPatientDetailDO.class)); for (TaskPatientDetailDO taskPatientDetailDO : taskPatientDetailDOS){ ActivityDO activityDO = activityDao.selectById(taskPatientDetailDO.getActivityId()); taskPatientDetailDO.setActivityDO(activityDO); } return taskPatientDetailDOS; } }