| 
					
				 | 
			
			
				@ -19,6 +19,7 @@ import org.springframework.transaction.annotation.Transactional; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.util.StringUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.io.IOException; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.util.Iterator; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.util.List; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.util.Map; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.util.logging.Logger; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@ -34,6 +35,9 @@ public class MedicineUserService extends BaseJpaService<Mediicineuser, Mediicine 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private MediicineDeviceUserDao deviceUserDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private MediicineDeviceDao deviceDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private ObjectMapper objectMapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@ -242,66 +246,87 @@ public class MedicineUserService extends BaseJpaService<Mediicineuser, Mediicine 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @throws Exception 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public JSONObject allotDevice(String userIds, String deviceIds) throws Exception { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public JSONObject allotDevice(String userIds, String deviceIds, Boolean isAll) throws Exception { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        JSONObject result = new JSONObject(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if (StringUtils.isEmpty(userIds) && StringUtils.isEmpty(deviceIds)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if (StringUtils.isEmpty(userIds)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            result.put("response", ConstantUtils.FAIL); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //绑定设备 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        for (String userId : userIds.split(",")) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            if (!StringUtils.isEmpty(userId)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                //移除不绑定设备 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                String sql = "SELECT\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        "\tt.id AS id,\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        "\tt.device_id AS deviceId,\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        "\tt.user_id AS userId,\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        "\tt.del AS del,\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        "\tt.create_time AS createTime,\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        "\tt.create_user AS createUser,\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        "\tt.create_user_name AS createUserName,\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        "\tt.update_time AS updateTime,\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        "\tt.update_user AS updateUser,\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        "\tt.update_user_name AS updateUserName\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        "FROM\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        "\tt_mediicine_device_user AS t\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        "WHERE\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        "    t.user_id = '" + userId + "'\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        "AND '," + deviceIds + ",' NOT LIKE CONCAT('%,',t.device_id,',%')"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                List<Map<String,Object>> list=null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                list = hibenateUtils.createSQLQuery(sql); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                if (list != null && list.size() > 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    for (Map<String, Object> stringObjectMap : list) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        MediicineDeviceUser mediicineDeviceUser = deviceUserDao.findOne(stringObjectMap.get("id").toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        mediicineDeviceUser.setDel("0"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        deviceUserDao.save(mediicineDeviceUser); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                //全部设备 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                if(isAll) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    Iterable<Mediicinedevice> lists = deviceDao.findAll(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    Iterator iterator = lists.iterator(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    while(iterator.hasNext()){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        Mediicinedevice mediicinedevice = ((Mediicinedevice) iterator.next()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        if (mediicinedevice != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            String countSql = " select " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                    "     COUNT(DISTINCT (t.id)) as count " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                    "   from " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                    "     t_mediicine_device_user t " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                    "  where " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                    "  1=1 AND t.del = 1 AND t.device_id = '" + mediicinedevice.getId() + "' AND t.user_id = '" + userId + "'"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            int count = jdbcTemplate.queryForObject(countSql,Integer.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            if (count > 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                continue; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            MediicineDeviceUser deviceUser = new MediicineDeviceUser(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            deviceUser.setDel("1"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            deviceUser.setDeviceId(mediicinedevice.getId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            deviceUser.setUserId(userId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            deviceUserDao.save(deviceUser); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//                if (list != null && list.size() > 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//                    for (MediicineDeviceUser deviceUser : list) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//                        deviceUser.setDel("0"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//                        deviceUserDao.save(deviceUser); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                for (String deviceId : deviceIds.split(",")) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    if (!StringUtils.isEmpty(deviceId)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        String countSql = " select " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                "     COUNT(DISTINCT (t.id)) as count " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                "   from " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                "     t_mediicine_device_user t " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                "  where " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                "  1=1 AND t.del = 1 AND t.device_id = '" + deviceId + "' AND t.user_id = '" + userId + "'"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        int count = jdbcTemplate.queryForObject(countSql,Integer.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        if (count > 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            continue; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    //删除移除设备 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    String sql = "SELECT\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            "\tt.id AS id,\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            "\tt.device_id AS deviceId,\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            "\tt.user_id AS userId,\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            "\tt.del AS del,\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            "\tt.create_time AS createTime,\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            "\tt.create_user AS createUser,\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            "\tt.create_user_name AS createUserName,\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            "\tt.update_time AS updateTime,\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            "\tt.update_user AS updateUser,\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            "\tt.update_user_name AS updateUserName\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            "FROM\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            "\tt_mediicine_device_user AS t\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            "WHERE\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            "    t.user_id = '" + userId + "' AND t.del = 1\n" + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            "AND '," + deviceIds + ",' NOT LIKE CONCAT('%,',t.device_id,',%')"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    List<Map<String,Object>> list=null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    list = hibenateUtils.createSQLQuery(sql); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    if (list != null && list.size() > 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        for (Map<String, Object> stringObjectMap : list) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            MediicineDeviceUser mediicineDeviceUser = deviceUserDao.findOne(stringObjectMap.get("id").toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            mediicineDeviceUser.setDel("0"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            deviceUserDao.save(mediicineDeviceUser); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    for (String deviceId : deviceIds.split(",")) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        if (!StringUtils.isEmpty(deviceId)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            String countSql = " select " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                    "     COUNT(DISTINCT (t.id)) as count " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                    "   from " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                    "     t_mediicine_device_user t " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                    "  where " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                    "  1=1 AND t.del = 1 AND t.device_id = '" + deviceId + "' AND t.user_id = '" + userId + "'"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            int count = jdbcTemplate.queryForObject(countSql,Integer.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            if (count > 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                continue; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            MediicineDeviceUser deviceUser = new MediicineDeviceUser(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            deviceUser.setDel("1"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            deviceUser.setDeviceId(deviceId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            deviceUser.setUserId(userId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                            deviceUserDao.save(deviceUser); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        MediicineDeviceUser deviceUser = new MediicineDeviceUser(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        deviceUser.setDel("1"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        deviceUser.setDeviceId(deviceId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        deviceUser.setUserId(userId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        deviceUserDao.save(deviceUser); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 |