public boolean isExist(Room room) { // TODO 是否存在相同的房源信息 if (StringUtils.isBlank(room.getRoom_name())) { return true; } String sql = "select count(*) from " + Room.TABLENAME + " where room_name = ?"; if (room.getId() != null && room.getId() > 0) { sql += " and id != " + room.getId(); } System.out.println("SQL :" + sql); int count = jdbcTemplate.queryForInt(sql, new Object[] {room.getRoom_name()}); System.out.println("room_name:" + room.getRoom_name() + "count :" + count); return (count > 0 ? true : false); }
public Map<String, Object> save(Room room) { // 保存或修改房源 Map<String, Object> result = new HashMap<String, Object>(); if (room == null) { result.put("error", "房源信息为空..."); } if (!isExist(room)) { // 如果不存在 房源名称是否重复 if (room.getId() != null && room.getId() > 0) // 修改的情况 { /* * 新增的字段 bed; wardrobe; air_conditioning;tv; kitchen; bashroom */ String[] updateFields = new String[] { "room_name = ?", "address = ?", "price = ?", "rental_start_time = ?", "rental_end_time = ?", "status = ?", "`desc` = ?", "size = ?", "kind = ?", "floor = ?", "orient = ?", "occupancy = ?", "metro=?", "comm_id = ?", "bed=?", "wardrobe=?", "air_conditioning=?", "tv=?", "gasstove=?", "washing=?", "heater = ?", "refrigerator = ?", " microwaveOven= ?", "room_host= ?", "recommend=?", "recommendTarget=?", "cityid=?,decoration = ?,rent_type = ?,company_id=?,smartlock=?", "isRecommend=?" }; Object[] fieldsValue = new Object[] { room.getRoom_name(), room.getAddress(), room.getPrice(), room.getRental_start_time(), room.getRental_end_time(), room.getStatus(), room.getDesc(), room.getSize(), room.getKind(), room.getFloor(), room.getOrient(), room.getOccupancy(), room.getMetro(), room.getComm_id(), room.getBed(), // 床 room.getWardrobe(), // 衣柜 room.getAir_conditioning(), // 空调 room.getTv(), // 电视 room.getGasstove(), // 燃气灶 room.getWashing(), // 洗衣机 room.getHeater(), // 热水器 room.getRefrigerator(), // 冰箱 room.getMicrowaveOven(), // 微波炉 room.getRoom_host(), room.getRecommend(), room.getRecommendTarget(), room.getCityid(), room.getDecoration(), room.getRent_type(), room.getCompany_id(), room.getSmartlock(), room.getIsRecommend() }; /* * for( int i = 0; i < updateFields.length; i++ ){ * System.out.println(updateFields[i] + ":" + String.valueOf( * fieldsValue[i] )); } */ String sql = "update " + Room.TABLENAME + " set " + StringUtils.join(updateFields, ",") + " where id =" + room.getId().toString(); // System.out.println(sql ); int affected = jdbcTemplate.update(sql, fieldsValue); if (affected > 0) { result.put("success", "修改成功"); } else { result.put("success", "修改失败"); } } else { // 新增 String sql = "insert into " + Room.TABLENAME + " (room_name," + "address,price," + "rental_start_time," + "rental_end_time," + "status,size," + "kind,floor," + "orient,occupancy," + "metro,`desc`," + "comm_id," + " bed,wardrobe," + "air_conditioning," + "tv,gasstove," + "washing,heater,refrigerator,microwaveOven,room_host," + "recommend," + "recommendTarget,cityid,createtime,decoration,rent_type,company_id, smartlock, isRecommend) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; System.out.println(sql); int affected = jdbcTemplate.update( sql, new Object[] { room.getRoom_name(), room.getAddress(), room.getPrice(), room.getRental_start_time(), room.getRental_end_time(), room.getStatus(), room.getSize(), room.getKind(), room.getFloor(), room.getOrient(), room.getOccupancy(), room.getMetro(), room.getDesc(), room.getComm_id(), room.getBed(), room.getWardrobe(), room.getAir_conditioning(), room.getTv(), room.getGasstove(), // 燃气灶 room.getWashing(), // 洗衣机 room.getHeater(), // 热水器 room.getRefrigerator(), // 冰箱 room.getMicrowaveOven(), // 微波炉 room.getRoom_host(), room.getRecommend(), room.getRecommendTarget(), room.getCityid(), CalendarUtil.getInstance().getCurrentTime(), room.getDecoration(), room.getRent_type(), room.getCompany_id(), room.getSmartlock(), room.getIsRecommend() }); // 获得id,再更新房源编号 if (affected > 0) { String query = "select last_insert_id() as id from " + Image.TABLENAME + " limit 1"; System.out.println("图片id查询语句:" + query); int id = jdbcTemplate.queryForInt(query); String roomSeq = room.getRoomSeq() + StringUtils.leftPad(id + "", 5, "0"); affected = jdbcTemplate.update( "update " + Room.TABLENAME + " set roomSeq = ? where id = ?", new Object[] {roomSeq, id}); if (affected > 0) { System.out.println("房源保存成功,且房源编号更新成功..."); } result.put("success", "保存成功"); } else { result.put("success", "保存失败"); } } } else { result.put("error", "房源名称已存在"); } return result; }