|  | @ -0,0 +1,212 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.repository.service;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.iot.device.IotDeviceDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.iot.device.IotDeviceSimDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.iot.device.IotPatientDeviceDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.iot.dict.IotSystemDictDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.repository.dao.IotDeviceDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.repository.dao.IotDeviceSimDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.repository.dao.IotPatientDeviceDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.repository.dao.IotSystemDictDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.restmodel.iot.device.IotOrderVO;
 | 
	
		
			
				|  |  | import com.yihu.jw.restmodel.web.MixEnvelop;
 | 
	
		
			
				|  |  | import com.yihu.jw.rm.iot.IotRequestMapping;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.http.HttpClientUtil;
 | 
	
		
			
				|  |  | import com.yihu.mysql.query.BaseJpaService;
 | 
	
		
			
				|  |  | import org.apache.commons.lang.StringUtils;
 | 
	
		
			
				|  |  | 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 org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.util.ArrayList;
 | 
	
		
			
				|  |  | import java.util.List;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * @author HZY
 | 
	
		
			
				|  |  |  * @vsrsion 1.0
 | 
	
		
			
				|  |  |  * Created at 2020/5/7
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | @Service
 | 
	
		
			
				|  |  | @Transactional
 | 
	
		
			
				|  |  | public class IotDeviceSimService extends BaseJpaService<IotDeviceSimDO, IotDeviceSimDao> {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private IotPatientDeviceDao iotPatientDeviceDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private IotDeviceSimDao iotDeviceSimDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private IotDeviceDao iotDeviceDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private JdbcTemplate jdbcTemplate;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private IotSystemDictDao iotSystemDictDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private HttpClientUtil httpClientUtil;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private final static String jobUrl = "http://localhost:10031/job/reStartById?taskId=data_sim_Balance_remind_job";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 增加SIM卡管理
 | 
	
		
			
				|  |  |      * @param advancePayment
 | 
	
		
			
				|  |  |      * @param payment
 | 
	
		
			
				|  |  |      * @param remainingBalance
 | 
	
		
			
				|  |  |      * @param status
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public void editSIM(String id,String advancePayment,String payment,String remainingBalance,Integer status ) {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         IotDeviceSimDO simDO = iotDeviceSimDao.findOne(id);
 | 
	
		
			
				|  |  |         simDO.setAdvancePayment(Float.parseFloat(advancePayment));
 | 
	
		
			
				|  |  |         simDO.setPayment(Float.parseFloat(payment));
 | 
	
		
			
				|  |  |         simDO.setRemainingBalance(Float.parseFloat(remainingBalance));
 | 
	
		
			
				|  |  |         simDO.setStatus(status);
 | 
	
		
			
				|  |  |         iotDeviceSimDao.save(simDO);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      *新增SIM卡
 | 
	
		
			
				|  |  |      * @param json
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public MixEnvelop<IotOrderVO, IotOrderVO> addSIM(String json) {
 | 
	
		
			
				|  |  |         IotDeviceSimDO sim = JSONObject.parseObject(json, IotDeviceSimDO.class);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         IotDeviceSimDO deviceSimDaoBySim = iotDeviceSimDao.findBySim(sim.getSim());
 | 
	
		
			
				|  |  |         if(deviceSimDaoBySim!=null){
 | 
	
		
			
				|  |  |             return MixEnvelop.getError("SIM已存在");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         IotPatientDeviceDO patientSim = iotPatientDeviceDao.findBySim(sim.getSim());
 | 
	
		
			
				|  |  |         if (patientSim!=null){
 | 
	
		
			
				|  |  |             sim.setDeviceName(patientSim.getDeviceName());
 | 
	
		
			
				|  |  |             sim.setDeviceSn(patientSim.getDeviceSn());
 | 
	
		
			
				|  |  |             sim.setContactsName(patientSim.getPatientName());
 | 
	
		
			
				|  |  |             sim.setContactsMobile(patientSim.getMobile());
 | 
	
		
			
				|  |  |         } else {
 | 
	
		
			
				|  |  |             IotDeviceDO deviceSim = iotDeviceDao.findBySimNo(sim.getSim());
 | 
	
		
			
				|  |  |             if (deviceSim!=null){
 | 
	
		
			
				|  |  |                 sim.setDeviceName(deviceSim.getName());
 | 
	
		
			
				|  |  |                 sim.setDeviceSn(deviceSim.getDeviceSn());
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         sim.setRemainingBalance(sim.getAdvancePayment()-sim.getPayment());
 | 
	
		
			
				|  |  |         sim.setStatus(1);
 | 
	
		
			
				|  |  |         sim.setDel("0");
 | 
	
		
			
				|  |  |         iotDeviceSimDao.save(sim);
 | 
	
		
			
				|  |  |         return MixEnvelop.getSuccess(IotRequestMapping.DeviceSim.message_success_add);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 条件分页查找
 | 
	
		
			
				|  |  |      * @param page
 | 
	
		
			
				|  |  |      * @param size
 | 
	
		
			
				|  |  |      * @param status
 | 
	
		
			
				|  |  |      * @param sim
 | 
	
		
			
				|  |  |      * @param sn
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public MixEnvelop<IotDeviceSimDO, IotDeviceSimDO> conditionQueryPage(Integer page, Integer size, String status, String sim, String sn){
 | 
	
		
			
				|  |  |         StringBuffer sql = new StringBuffer("SELECT c.* from iot_device_sim c WHERE 1=1 ");
 | 
	
		
			
				|  |  |         StringBuffer sqlCount = new StringBuffer("SELECT COUNT(c.id) count from iot_device_sim c  WHERE 1=1 ");
 | 
	
		
			
				|  |  |         List<Object> args = new ArrayList<>();
 | 
	
		
			
				|  |  |         if(StringUtils.isNotBlank(status)){
 | 
	
		
			
				|  |  |             sql.append(" and c.status=? ");
 | 
	
		
			
				|  |  |             sqlCount.append("and c.status='").append(status).append("' ");
 | 
	
		
			
				|  |  |             args.add(status);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if(StringUtils.isNotBlank(sim)){
 | 
	
		
			
				|  |  |             sql.append(" and (c.sim like '%").append(sim).append("%')");
 | 
	
		
			
				|  |  |             sqlCount.append(" and (c.sim like '%").append(sim).append("%')");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if(StringUtils.isNotBlank(sn)){
 | 
	
		
			
				|  |  |             sql.append(" and (c.device_sn like '%").append(sn).append("%')");
 | 
	
		
			
				|  |  |             sqlCount.append(" and (c.device_sn like '%").append(sn).append("%')");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         sql.append("order by c.update_time desc limit ").append((page-1)*size).append(",").append(size);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         List<IotDeviceSimDO> list = jdbcTemplate.query(sql.toString(),args.toArray(),new BeanPropertyRowMapper(IotDeviceSimDO.class));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         List<Map<String,Object>> countList = jdbcTemplate.queryForList(sqlCount.toString());
 | 
	
		
			
				|  |  |         long count = Long.valueOf(countList.get(0).get("count").toString());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return MixEnvelop.getSuccessListWithPage(IotRequestMapping.DeviceSim.message_success_find,list, page, size,count);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 分页查找所有应用SIM卡信息
 | 
	
		
			
				|  |  |      * @param page
 | 
	
		
			
				|  |  |      * @param size
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public MixEnvelop<IotDeviceSimDO, IotDeviceSimDO> findAllSim(Integer page, Integer size) {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         StringBuffer sql= new StringBuffer("SELECT DISTINCT * FROM iot_device_sim ");
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         sql.append(" limit ").append((page-1)*size).append(",").append(size);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         List<IotDeviceSimDO> list = jdbcTemplate.query(sql.toString(),new BeanPropertyRowMapper(IotDeviceSimDO.class));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //获取总数
 | 
	
		
			
				|  |  |         long count = list.size();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return MixEnvelop.getSuccessListWithPage(IotRequestMapping.DeviceSim.message_success_find,list,page, size,count);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      *SIM余额提醒
 | 
	
		
			
				|  |  |      * @param time
 | 
	
		
			
				|  |  |      * @param money
 | 
	
		
			
				|  |  |      * @param status
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public void remind(String time, String money, String status) {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         IotSystemDictDO sim = iotSystemDictDao.findByDictName("SIM_BALANCE_REMIND").get(0);
 | 
	
		
			
				|  |  |         sim.setCode(money);
 | 
	
		
			
				|  |  |         sim.setStatus(Integer.parseInt(status));
 | 
	
		
			
				|  |  |         sim.setTime(time);
 | 
	
		
			
				|  |  |         iotSystemDictDao.save(sim);
 | 
	
		
			
				|  |  |         //触发JOB工程
 | 
	
		
			
				|  |  |         httpClientUtil.get(jobUrl,"UTF-8");
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取所有需要余额提醒的SIM卡
 | 
	
		
			
				|  |  |      * @param time
 | 
	
		
			
				|  |  |      * @param code
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public List<IotDeviceSimDO> findAllRemindSim(String time, String code) {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         StringBuffer sql = new StringBuffer("SELECT a.* FROM iot_device_sim a WHERE 1=1");
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         List<Object> args = new ArrayList<>();
 | 
	
		
			
				|  |  |         if (StringUtils.isNotBlank(time)){
 | 
	
		
			
				|  |  |             sql.append("  AND DAY(CURRENT_TIMESTAMP)=?");
 | 
	
		
			
				|  |  |             args.add(time);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if(StringUtils.isNotBlank(code)){
 | 
	
		
			
				|  |  |             sql.append(" and a.remaining_balance <=? ");
 | 
	
		
			
				|  |  |             args.add(code);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         List<IotDeviceSimDO> list = jdbcTemplate.query(sql.toString(),args.toArray(),new BeanPropertyRowMapper(IotDeviceSimDO.class));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return list;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |