/**
  * 修改单个房型日历价格方法
  *
  * @author chivalry
  * @param request
  * @return 2015-10-13
  */
 @SuppressWarnings("rawtypes")
 @PolicyJournal(accessLevel = AccessLevel.PRIVATE)
 @RequestMapping(
     value = "/do_hotelRoomPrice_edit",
     method = {RequestMethod.POST})
 @ResponseBody
 public Response do_hotelRoomPrice_edit(
     HttpServletRequest request,
     String hotelId,
     String roomId,
     String roomPrice,
     String calendarDate) {
   // 返回对象
   Response response = new Response();
   LoginUser user = LoginSessionUtil.getSystemSession(request);
   // 设置客房日历信息并更新数据
   HotelRoomCalendarBo hotelRoomCalendar = new HotelRoomCalendarBo();
   hotelRoomCalendar.setHotelId(hotelId);
   hotelRoomCalendar.setRoomId(roomId);
   hotelRoomCalendar.setRoomPrice(roomPrice);
   int results = 1;
   hotelRoomCalendar.setCalendarDate(calendarDate);
   hotelRoomCalendar.setUserbaseId(user.getId().toString());
   results = this.hotelRoomCalendarService.updateHotelRoomCalendar(hotelRoomCalendar);
   if (results == 1) {
     response.setSucceessMessage("修改成功");
   } else {
     response.setFailMessage("修改失败");
   }
   return response;
 }
 /**
  * 查询生成单个房型日历价格方法
  *
  * @author chivalry
  * @param request
  * @return 2015-10-13
  */
 @SuppressWarnings("rawtypes")
 @PolicyJournal(accessLevel = AccessLevel.PRIVATE)
 @RequestMapping(
     value = "/to_hotelRoomPrice_add",
     method = {RequestMethod.POST})
 @ResponseBody
 public Response to_hotelRoomPrice_add(
     HttpServletRequest request, String hotelNameId, String hotelRoomId, String selectdate) {
   // 返回对象
   Response response = new Response();
   LoginUser user = LoginSessionUtil.getSystemSession(request);
   // 根据roomId查询room的默认价格
   Map<String, Object> roomMap = this.hotelRoomService.selectHotelRoomByPk(hotelRoomId);
   String defaultPrice = "1000.00";
   if (null != roomMap) {
     if (roomMap.get("price") != null) {
       defaultPrice = roomMap.get("price").toString();
     }
   }
   // 设置客房日历信息并批量插入数据
   List<HotelRoomCalendarBo> hotelRoomCalendarList = new ArrayList<HotelRoomCalendarBo>();
   HotelRoomCalendarBo hotelRoomCalendar = new HotelRoomCalendarBo();
   hotelRoomCalendar.setHotelId(hotelNameId);
   hotelRoomCalendar.setRoomId(hotelRoomId);
   hotelRoomCalendar.setRoomPrice(defaultPrice);
   int results = 1;
   hotelRoomCalendar.setCalendarDate(selectdate);
   // 先查询该日期下房型是否存在价格
   List<Map<String, Object>> roomPriceList =
       this.hotelRoomCalendarService.selectRoomPriceListByExample(hotelRoomCalendar);
   Map<String, Object> map = new HashMap<String, Object>();
   if (roomPriceList.size() == 0) {
     // 不存在价格日历插入数据
     //	    	results=this.hotelRoomCalendarService.insertHotelRoomCalendar(hotelRoomCalendar);
     hotelRoomCalendar.setUserbaseId(user.getId().toString());
     hotelRoomCalendarList.add(hotelRoomCalendar);
     if (hotelRoomCalendarList != null
         && hotelRoomCalendarList.size() > 0
         && hotelRoomCalendarList.get(0).getCalendarDate() != null
         && hotelRoomCalendarList.get(0).getCalendarDate() != "") {
       results = this.hotelRoomCalendarService.insertHotelRoomCalendar(hotelRoomCalendarList);
     }
     map.put("roomPrice", hotelRoomCalendar.getRoomPrice());
   } else {
     // 存在读取价格
     map.put("roomPrice", roomPriceList.get(0).get("roomPrice"));
   }
   if (results >= 1) {
     response.setSucceessMessage("执行成功");
     response.setRtData(map);
   } else {
     response.setFailMessage("读取价格失败");
   }
   return response;
 }
 /**
  * 客房删除方法
  *
  * @author chivalry
  * @param request
  * @return 2015-10-13
  */
 @SuppressWarnings("rawtypes")
 @PolicyJournal(accessLevel = AccessLevel.PRIVATE)
 @RequestMapping(
     value = "/do_delete_hotelRoom",
     method = {RequestMethod.POST})
 @ResponseBody
 public Response do_delete_hotelRoom(HttpServletRequest request, String id) {
   // 返回对象
   Response response = new Response();
   HotelRoomBo hotelRoom = new HotelRoomBo();
   hotelRoom.setId(id);
   hotelRoom.setStatus("0");
   int results = this.hotelRoomService.updateHotelRoom(hotelRoom);
   HotelRoomPlanBo hotelRoomPlan = new HotelRoomPlanBo();
   hotelRoomPlan.setRoomId(id);
   this.hotelRoomPlanService.deleteRoomPlanByPk(hotelRoomPlan);
   if (results == 1) {
     response.setSucceessMessage("删除成功");
   } else {
     response.setFailMessage("删除失败");
   }
   return response;
 }
  /**
   * 客房批量生成日历价格方法
   *
   * @author chivalry
   * @param request
   * @return 2015-10-13
   */
  @SuppressWarnings("rawtypes")
  @PolicyJournal(accessLevel = AccessLevel.PRIVATE)
  @RequestMapping(
      value = "/do_hotelRoomsPrice_add",
      method = {RequestMethod.POST})
  @ResponseBody
  public Response do_hotelRoomsPrice_add(
      HttpServletRequest request, String hotelNameId, String hotelRoomId) {
    // 返回对象
    Response response = new Response();
    LoginUser user = LoginSessionUtil.getSystemSession(request);
    // 根据roomId查询room的默认价格
    Map<String, Object> roomMap = this.hotelRoomService.selectHotelRoomByPk(hotelRoomId);
    String defaultPrice = "1000";
    if (null != roomMap) {
      if (roomMap.get("price") != null) {
        defaultPrice = roomMap.get("price").toString();
      }
    }
    // 设置客房日历信息并批量插入数据
    // 计算90天的所有时间批量插入数据
    int results = 1;
    Calendar c_begin = new GregorianCalendar();
    Calendar c_end = new GregorianCalendar();

    //	     DateFormatSymbols dfs = new DateFormatSymbols();
    //	     String[] weeks = dfs.getWeekdays();
    // 从当前时间开始
    Calendar systime = Calendar.getInstance();
    c_begin.set(
        systime.get(Calendar.YEAR),
        systime.get(Calendar.MONTH),
        systime.get(Calendar.DATE)); // Calendar的月从0-11
    c_end.set(
        systime.get(Calendar.YEAR),
        systime.get(Calendar.MONTH),
        systime.get(Calendar.DATE)); // Calendar的月从0-11
    // 获取90天后的时间
    c_end.add(Calendar.DAY_OF_YEAR, hotelRoom_generate_days);
    int count = 1;
    // 结束日期下滚一天是为了包含最后一天
    c_end.add(Calendar.DAY_OF_YEAR, 1);
    //	     System.out.println("开始时间是:"+new java.sql.Date(c_begin.getTime().getTime()));
    //	     System.out.println("结束时间是:"+new java.sql.Date(c_end.getTime().getTime()));
    List<HotelRoomCalendarBo> hotelRoomCalendarList = new ArrayList<HotelRoomCalendarBo>();
    while (c_begin.before(c_end)) {
      HotelRoomCalendarBo hotelRoomCalendar = new HotelRoomCalendarBo();
      hotelRoomCalendar.setHotelId(hotelNameId);
      hotelRoomCalendar.setRoomId(hotelRoomId);
      hotelRoomCalendar.setRoomPrice(defaultPrice);
      //	   System.out.println("**************"+new java.sql.Date(c_begin.getTime().getTime()));
      hotelRoomCalendar.setCalendarDate(new java.sql.Date(c_begin.getTime().getTime()).toString());
      // 先查询该日期下房型是否存在价格
      List<Map<String, Object>> roomPriceList =
          this.hotelRoomCalendarService.selectRoomPriceListByExample(hotelRoomCalendar);
      if (roomPriceList.size() == 0) {
        // 不存在价格日历插入数据
        hotelRoomCalendar.setUserbaseId(user.getId().toString());
        hotelRoomCalendarList.add(hotelRoomCalendar);
      }
      if (c_begin.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
        count++;
      }
      c_begin.add(Calendar.DAY_OF_YEAR, 1);
    }
    if (hotelRoomCalendarList != null
        && hotelRoomCalendarList.size() > 0
        && hotelRoomCalendarList.get(0).getCalendarDate() != null
        && hotelRoomCalendarList.get(0).getCalendarDate() != "") {
      results = this.hotelRoomCalendarService.insertHotelRoomCalendar(hotelRoomCalendarList);
    }
    if (results >= 1) {
      response.setSucceessMessage("执行成功");
    } else {
      response.setFailMessage("执行失败");
    }
    return response;
  }
  /**
   * 酒店客房修改
   *
   * @author chivalry
   * @param request
   * @return 2015-10-12
   */
  @SuppressWarnings("rawtypes")
  @PolicyJournal(accessLevel = AccessLevel.PRIVATE)
  @RequestMapping(
      value = "/do_edit_hotelRoom",
      method = {RequestMethod.POST})
  @ResponseBody
  public Response do_edit_hotelRoom(
      HttpServletRequest request,
      @Valid HotelRoomBo hotelRoom,
      BindingResult result,
      HotelRoomPlanBo hotelRoomPlan) {
    // 返回对象
    Response response = new Response(); // 服务端验证
    if (result.hasErrors()) {
      ValidateParamUtils.analyzeErrors(result, response);
      return response;
    }
    int results = 1;
    results = this.hotelRoomService.updateHotelRoom(hotelRoom);
    // 根据客房主键ID删除客房产品
    hotelRoomPlan.setId(null);
    hotelRoomPlan.setRoomId(hotelRoom.getId());
    results = this.hotelRoomPlanService.deleteRoomPlanByPk(hotelRoomPlan);
    // 新增客房产品
    hotelRoomPlan.setHotelId(hotelRoom.getHotelId());
    hotelRoomPlan.setBedType(hotelRoom.getBedType());
    hotelRoomPlan.setRoomStatus(hotelRoom.getRoomStatus());

    String[] breakfastTypeStr = request.getParameterValues("isBreakfast");
    String[] cancelRuleStr = request.getParameterValues("cancelRule");
    String[] discountAmountStr = request.getParameterValues("discount");
    String[] cashBackStr = request.getParameterValues("cashBack");
    String[] roomPlanName = request.getParameterValues("roomPlanName");
    String[] payType = request.getParameterValues("payType");
    String[] campaignDaysStr = request.getParameterValues("campaignDays");
    List<HotelRoomPlanBo> list = new ArrayList<HotelRoomPlanBo>();
    if (breakfastTypeStr != null) {
      for (int i = 0; i < breakfastTypeStr.length; i++) {
        hotelRoomPlan.setBreakfastType(breakfastTypeStr[i]);
        hotelRoomPlan.setCancelRule(cancelRuleStr[i]);
        hotelRoomPlan.setDiscountAmount(discountAmountStr[i]);
        hotelRoomPlan.setCashBack(cashBackStr[i]);
        hotelRoomPlan.setRoomPlanName(roomPlanName[i]);
        hotelRoomPlan.setPayType(payType[i]);
        hotelRoomPlan.setCampaignDays(campaignDaysStr[i]);
        if (hotelRoomPlan.getDiscountAmount() == null
            || StringUtils.isBlank(hotelRoomPlan.getDiscountAmount())) {
          response.setFailMessage("折扣未填写完整!");
          return response;
        }
        if (hotelRoomPlan.getCashBack() == null
            || StringUtils.isBlank(hotelRoomPlan.getCashBack())) {
          response.setFailMessage("返现未填写完整!");
          return response;
        }
        if (hotelRoomPlan.getRoomPlanName() == null
            || StringUtils.isBlank(hotelRoomPlan.getRoomPlanName())) {
          response.setFailMessage("产品名称未填写完整!");
          return response;
        }
        if (hotelRoomPlan.getCampaignDays() == null
            || StringUtils.isBlank(hotelRoomPlan.getCampaignDays())) {
          response.setFailMessage("起订天数未填写完整!");
          return response;
        }
        double price = 0;
        double discountAmount = Double.parseDouble(discountAmountStr[i]);
        double cashBack = Double.parseDouble(cashBackStr[i]);
        double prices = Double.parseDouble(hotelRoom.getPrice());
        price = prices * discountAmount / 10 + cashBack;
        hotelRoomPlan.setPrice(Double.toString(price));
        HotelRoomPlanBo hotelRoomPlans = new HotelRoomPlanBo();
        BeanUtils.copyProperties(hotelRoomPlan, hotelRoomPlans);
        list.add(hotelRoomPlans);
      }
    }
    if (list != null
        && list.size() > 0
        && list.get(0).getBreakfastType() != null
        && list.get(0).getBreakfastType() != "") {
      results = this.hotelRoomPlanService.insertHotelRoomPlan(list);
    }
    if (results >= 0) {
      response.setSucceessMessage("修改成功");
    } else {
      response.setFailMessage("修改失败");
    }
    return response;
  }
  /**
   * 酒店房间新增方法
   *
   * @author chivalry
   * @param request
   * @return 2015-09-21
   */
  @SuppressWarnings("rawtypes")
  @PolicyJournal(accessLevel = AccessLevel.PRIVATE)
  @RequestMapping(
      value = "/do_hotelRoom_add",
      method = {RequestMethod.POST})
  @ResponseBody
  public Response do_hotelRoom_add(
      HttpServletRequest request,
      @Valid HotelRoomBo hotelRoom,
      BindingResult result,
      HotelRoomPlanBo hotelRoomPlan) {
    // 返回对象
    LoginUser user = LoginSessionUtil.getSystemSession(request);
    // 判断当前用户是否为加盟商
    if (user.getAgentId() != null) {
      String id = user.getAgentId().toString();
      hotelRoomPlan.setAgentId(id);
    } else {
      hotelRoomPlan.setAgentId("0");
    }
    Response response = new Response(); // 服务端验证
    if (result.hasErrors()) {
      ValidateParamUtils.analyzeErrors(result, response);
      return response;
    }

    // 返回插入酒店信息主键ID,设置房间产品信息
    // 插入图片关联关系
    String imageIds = request.getParameter("imageIds");
    String[] imageidsStr = imageIds.split(",");
    List<Map<String, Object>> imageList = new ArrayList<Map<String, Object>>();
    for (int i = 0; i < imageidsStr.length; i++) {
      if (StringUtils.isNotBlank(imageidsStr[i])) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("roomId", hotelRoom.getId());
        map.put("hotelId", null);
        map.put("groupHotelId", null);
        map.put("imageId", imageidsStr[i]);
        imageList.add(map);
      }
    }
    //		 if(imageList!=null&&imageList.size()>0&&imageList.get(0).get("imageId")!=null
    //	    		 &&imageList.get(0).get("imageId")!=""){
    //			 imageService.insertRelation(imageList);
    //		 }

    hotelRoomPlan.setHotelId(hotelRoom.getHotelId());
    hotelRoomPlan.setBedType(hotelRoom.getBedType());
    hotelRoomPlan.setRoomId(hotelRoom.getId());
    hotelRoomPlan.setRoomStatus(hotelRoom.getRoomStatus());

    String[] breakfastTypeStr = request.getParameterValues("isBreakfast");
    String[] cancelRuleStr = request.getParameterValues("cancelRule");
    String[] discountAmountStr = request.getParameterValues("discount");
    String[] cashBackStr = request.getParameterValues("cashBack");
    String[] roomPlanName = request.getParameterValues("roomPlanName");
    String[] payType = request.getParameterValues("payType");
    String[] campaignDaysStr = request.getParameterValues("campaignDays");

    List<HotelRoomPlanBo> list = new ArrayList<HotelRoomPlanBo>();
    for (int i = 0; i < breakfastTypeStr.length; i++) {
      hotelRoomPlan.setBreakfastType(breakfastTypeStr[i]);
      hotelRoomPlan.setCancelRule(cancelRuleStr[i]);
      hotelRoomPlan.setDiscountAmount(discountAmountStr[i]);
      hotelRoomPlan.setCashBack(cashBackStr[i]);
      hotelRoomPlan.setRoomPlanName(roomPlanName[i]);
      hotelRoomPlan.setPayType(payType[i]);
      hotelRoomPlan.setCampaignDays(campaignDaysStr[i]);
      if (hotelRoomPlan.getDiscountAmount() == null
          || StringUtils.isBlank(hotelRoomPlan.getDiscountAmount())) {
        response.setFailMessage("折扣未填写完整!");
        return response;
      }
      if (hotelRoomPlan.getCashBack() == null || StringUtils.isBlank(hotelRoomPlan.getCashBack())) {
        response.setFailMessage("返现未填写完整!");
        return response;
      }
      if (hotelRoomPlan.getRoomPlanName() == null
          || StringUtils.isBlank(hotelRoomPlan.getRoomPlanName())) {
        response.setFailMessage("产品名称未填写完整!");
        return response;
      }
      if (hotelRoomPlan.getCampaignDays() == null
          || StringUtils.isBlank(hotelRoomPlan.getCampaignDays())) {
        response.setFailMessage("起订天数未填写完整!");
        return response;
      }
      double price = 0;
      double discountAmount = Double.parseDouble(discountAmountStr[i]);
      double cashBack = Double.parseDouble(cashBackStr[i]);
      double prices = Double.parseDouble(hotelRoom.getPrice());
      price = prices * discountAmount / 10 + cashBack;
      hotelRoomPlan.setPrice(Double.toString(price));
      HotelRoomPlanBo hotelRoomPlans = new HotelRoomPlanBo();
      BeanUtils.copyProperties(hotelRoomPlan, hotelRoomPlans);
      list.add(hotelRoomPlans);
    }
    //		if(list!=null&&list.size()>0&&list.get(0).getBreakfastType()!=null
    //	    		 &&list.get(0).getBreakfastType()!=""){
    //		results=this.hotelRoomPlanService.insertHotelRoomPlan(list);
    //		}
    int results = this.hotelRoomService.insertHotelRoomAndOther(hotelRoom, imageList, list);
    if (results >= 1) {
      response.setSucceessMessage("添加成功");
    } else {
      response.setFailMessage("添加失败");
    }
    return response;
  }
  /**
   * 批量修改价格
   *
   * @param request
   * @return 2015-10-31
   * @throws ParseException
   */
  @PolicyJournal(accessLevel = AccessLevel.PRIVATE)
  @RequestMapping("/do_hotelRoomPrices_edit")
  @ResponseBody
  public Response do_hotelRoomPrices_edit(HttpServletRequest request) throws ParseException {
    Response res = new Response();
    Calendar c_begin = new GregorianCalendar();
    Calendar c_end = new GregorianCalendar();
    String startDate = request.getParameter("startDate");
    String endDate = request.getParameter("endDate");
    String hotelRoomId = request.getParameter("hotelRoomId");
    String price = request.getParameter("price");
    String hotelNameId = request.getParameter("hotelNameId");
    LoginUser user = LoginSessionUtil.getSystemSession(request);
    // 从开始时间开始
    Calendar start = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date1 = sdf.parse(startDate);
    start.setTime(date1);
    // 从结束时间结束
    Calendar end = Calendar.getInstance();
    Date date2 = sdf.parse(endDate);
    end.setTime(date2);

    c_begin.set(
        start.get(Calendar.YEAR),
        start.get(Calendar.MONTH),
        start.get(Calendar.DATE)); // Calendar的月从0-11
    c_end.set(
        end.get(Calendar.YEAR), end.get(Calendar.MONTH), end.get(Calendar.DATE)); // Calendar的月从0-11
    int count = 1;
    int results = 1;
    // 结束日期下滚一天是为了包含最后一天
    c_end.add(Calendar.DAY_OF_YEAR, 1);
    //	     System.out.println("开始时间是:"+new java.sql.Date(c_begin.getTime().getTime()));
    //	     System.out.println("结束时间是:"+new java.sql.Date(c_end.getTime().getTime()));
    List<HotelRoomCalendarBo> hotelRoomCalendarList = new ArrayList<HotelRoomCalendarBo>();
    while (c_begin.before(c_end)) {
      HotelRoomCalendarBo hotelRoomCalendar = new HotelRoomCalendarBo();
      hotelRoomCalendar.setHotelId(hotelNameId);
      hotelRoomCalendar.setRoomId(hotelRoomId);
      hotelRoomCalendar.setRoomPrice(price);
      //	   System.out.println("**************"+new java.sql.Date(c_begin.getTime().getTime()));
      hotelRoomCalendar.setCalendarDate(new java.sql.Date(c_begin.getTime().getTime()).toString());
      // 先查询该日期下房型是否存在价格
      List<Map<String, Object>> roomPriceList =
          this.hotelRoomCalendarService.selectRoomPriceListByExample(hotelRoomCalendar);
      if (roomPriceList.size() == 0) {
        // 不存在价格日历插入数据
        hotelRoomCalendar.setUserbaseId(user.getId().toString());
        hotelRoomCalendarList.add(hotelRoomCalendar);
      } else {
        // 存在就删除数据再插入
        int r = hotelRoomCalendarService.deleteByExample(hotelRoomCalendar);
        hotelRoomCalendar.setUserbaseId(user.getId().toString());
        hotelRoomCalendarList.add(hotelRoomCalendar);
      }
      if (c_begin.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
        count++;
      }
      c_begin.add(Calendar.DAY_OF_YEAR, 1);
    }
    if (hotelRoomCalendarList != null
        && hotelRoomCalendarList.size() > 0
        && hotelRoomCalendarList.get(0).getCalendarDate() != null
        && hotelRoomCalendarList.get(0).getCalendarDate() != "") {
      results = this.hotelRoomCalendarService.insertHotelRoomCalendar(hotelRoomCalendarList);
    }

    if (results >= 1) {
      res.setSucceessMessage("执行成功");
    } else {
      res.setFailMessage("执行失败");
    }
    return res;
  }