// 1 团购增删改查
 @Action(
     value = "grouponOpePage",
     interceptorRefs = {@InterceptorRef(value = "userActionStack")},
     results = {@Result(name = SUCCESS, location = "/userPages/grouponindex.jsp")})
 public String grouponOpePage() {
   User sessionUser = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
   Groupon findGroupon = null;
   if (groupon.getId() != null && groupon.getId() != 0) {
     findGroupon = grouponService.getGrouponDetail(groupon.getId());
     if (findGroupon == null && !findGroupon.getUser().getId().equals(sessionUser.getId())) {
       findGroupon = null;
     }
   }
   ServletActionContext.getContext().getValueStack().set("groupon", findGroupon);
   return SUCCESS;
 }
 // 4 删除一个团购项
 @Action(
     value = "delGrouponItem",
     interceptorRefs = {@InterceptorRef(value = "userActionStack")},
     results = {@Result(name = SUCCESS, type = "json")})
 public String delGrouponItem() {
   Grouponitem grouponitem = grouponService.getGrouponitem(groupon.getId());
   if (grouponitem != null) {
     grouponService.delGrouponitem(grouponitem);
   }
   PrintWriter writer = CommonUtil.getHtmlPrintWriter(ServletActionContext.getResponse());
   writer.write("success");
   writer.flush();
   writer.close();
   return SUCCESS;
 }
 // 1 进入团购的页面
 @Action(
     value = "grouponDetailPage",
     interceptorRefs = {@InterceptorRef(value = "userActionStack")},
     results = {@Result(name = SUCCESS, location = "/userPages/groupondetail.jsp")})
 public String grouponDetailPage() {
   // 获得这个学校所有的团购项目
   groupon = grouponService.getGrouponDetail(groupon.getId());
   if (groupon != null) {
     String phone = groupon.getTel();
     String phone1 = phone.substring(0, 3);
     String phone2 = phone.substring(7, 11);
     phone = phone1 + "****" + phone2;
     groupon.setTel(phone);
   }
   return SUCCESS;
 }
 /** 用户自己创建的班级购 */
 @Action(
     value = "changeGrouponState",
     interceptorRefs = {@InterceptorRef(value = "userActionStack")},
     results = {@Result(name = SUCCESS, type = "json")})
 public String changeGrouponState() {
   PrintWriter writer = CommonUtil.getHtmlPrintWriter(ServletActionContext.getResponse());
   Groupon find = grouponService.getGroupon(groupon.getId());
   if (find != null) {
     if (find.getState() == 0) find.setState(1);
     else find.setState(0);
   }
   grouponService.update(find);
   writer.write("success");
   writer.flush();
   writer.close();
   return SUCCESS;
 }
  // 2 发起一个团购
  @Action(
      value = "grouponInitiate",
      interceptorRefs = {@InterceptorRef(value = "userActionStack")},
      results = {@Result(name = SUCCESS, type = "json")})
  public String grouponInitiate() {
    User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
    PrintWriter writer = CommonUtil.getHtmlPrintWriter(ServletActionContext.getResponse());
    isbns = isbns.replaceAll("\\]", "");
    isbns = isbns.replaceAll("\\[", "");
    isbns = isbns.replaceAll("\"", "");
    if (groupon.getId() == null) {
      groupon.setId(0);
    }
    Groupon grouponInit = grouponService.getGroupon(groupon.getId());
    if (grouponInit == null) {
      Date createTime = CommonUtil.getDayDate();
      Date endTime = CommonUtil.dateOperate(createTime, 60);
      grouponInit = new Groupon();
      grouponInit.setName(groupon.getName());
      grouponInit.setTel(groupon.getTel());
      grouponInit.setEndTime(endTime);
      grouponInit.setState(0);
      grouponInit.setCreateTime(createTime);
      grouponInit.setUser(user);
      grouponInit.setSchool(user.getSchool());
      grouponInit.setRemark(groupon.getRemark());
      grouponInit.setClassNo(groupon.getClassNo());
      String[] isbnArray = isbns.split(",");
      grouponService.save(grouponInit);
      for (int i = 0; i < isbnArray.length; i++) {
        Grouponitem grouponitem = new Grouponitem();
        Book findBook = bookService.getBookFromDB(isbnArray[i]);
        grouponitem.setGroupon(grouponInit);
        grouponitem.setBook(findBook);
        grouponService.save(grouponitem);
      }
    } else {
      grouponInit.setName(groupon.getName());
      grouponInit.setTel(groupon.getTel());
      grouponInit.setState(0);
      grouponInit.setRemark(groupon.getRemark());
      grouponInit.setClassNo(groupon.getClassNo());
      grouponInit.setEndTime(CommonUtil.dateOperate(new Date(), 60));
      String[] isbnArray = isbns.split(",");
      grouponService.update(grouponInit);
      for (int i = 0; i < isbnArray.length; i++) {
        Grouponitem grouponitem = grouponService.loadGrouponitem(isbnArray[i], grouponInit.getId());
        System.out.println(grouponitem == null);
        if (grouponitem == null) {
          grouponitem = new Grouponitem();
          Book findBook = bookService.getBookFromDB(isbnArray[i]);
          grouponitem.setGroupon(grouponInit);
          grouponitem.setBook(findBook);
          grouponService.save(grouponitem);
        }
      }
    }

    writer.write("success");
    writer.flush();
    writer.close();
    return SUCCESS;
  }
  // 1 进入团购的页面
  @Action(
      value = "nextGrouponitemPage",
      interceptorRefs = {@InterceptorRef(value = "userActionStack")},
      results = {
        @Result(name = SUCCESS, location = "/userPages/grouponitem.jsp"),
        @Result(name = "over", location = "/userPages/cart.jsp")
      })
  public String nextGrouponitemPage() {
    User findUser = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
    // if()
    Groupon find = grouponService.getGroupon(groupon.getId());
    Grouponitem grouponitem = new Grouponitem();
    grouponitem.setId(page);
    grouponitem.setGroupon(find);
    Grouponitem findItem = grouponService.next(grouponitem);
    // 如果没有找到,那么意味着查询已经结束
    if (findItem == null) {
      double fee = 0.0;
      int num = 0;
      List<Cart> result = new ArrayList<Cart>();
      List<Cartitem> items = cartService.getGrouponitemsByUser(findUser.getId(), find.getId());
      for (int i = 0; i < items.size(); i++) {
        Cartitem cartitem = items.get(i);
        num += cartitem.getNum();
        if (cartitem.getSalesBook() == null) {
          if (cartitem.getBook().getDiscount() == 0.0) {
            cartitem.getBook().setDiscount(ConstantUtil.NEWBOOKDISCOUNT);
          }
          SalesBook newbookSalesBook =
              salesBookService.getSalesBookByIsbnSeller(
                  cartitem.getBook().getIsbn(), ConstantUtil.NEWBOOKPRODUCT);
          float discount = ConstantUtil.NEWBOOKDISCOUNT;
          if (newbookSalesBook != null && newbookSalesBook.getDiscount() != null) {
            discount = newbookSalesBook.getDiscount();
          }
          fee +=
              cartitem.getNum() * Math.round(cartitem.getBook().getPrice() * discount * 10) / 10d;
        } else {
          fee += cartitem.getNum() * cartitem.getSalesBook().getPrice();
        }
        Cart cart = cartitem.getCart();
        Cart findCart = getCartByList(cart.getId(), result);
        if (findCart == null) {
          Cart cartResult = new Cart();
          cartResult.setId(cart.getId());
          cartResult.setSeller(cart.getSeller());
          cartResult.setCartitems(new HashSet());
          cartResult.setTotalFee(cart.getTotalFee());
          cartResult.setUser(cart.getUser());
          cartResult.getCartitems().add(cartitem);
          cartitem.setCart(cartResult);
          result.add(cartResult);
        } else {
          findCart.getCartitems().add(cartitem);
          cartitem.setCart(findCart);
        }
      }
      System.out.println(result.size());
      ActionContext.getContext().getValueStack().set("carts", result);
      ActionContext.getContext().getValueStack().set("num", num);
      ActionContext.getContext().getValueStack().set("fee", Math.round(fee * 10) / 10d);
      return "over";
    }

    Book book = bookService.getBookByIsbn(findItem.getBook().getIsbn());
    // 如果没有拿到数据,则返回空
    ServiceArea serviceArea = findUser.getSchool().getServiceArea();
    // 如果找到了,查看新书供应商有没有此数据,如果没有,那么就给新书供应商加进去
    SalesBook newbookSalesBook =
        salesBookService.getSalesBookByIsbnSeller(
            findItem.getBook().getIsbn(), ConstantUtil.NEWBOOKPRODUCT);
    Seller seller = sellerService.getSellerById(ConstantUtil.NEWBOOKPRODUCT);
    SalesBook model =
        salesBookService.getSalesBookByIsbnSeller(findItem.getBook().getIsbn(), seller.getId());
    float bookDiscount;
    if (newbookSalesBook == null) {
      newbookSalesBook = new SalesBook();
      newbookSalesBook.setSeller(seller);
      newbookSalesBook.setBook(book);
      newbookSalesBook.setTitle(book.getTitle());
      newbookSalesBook.setAuthor(book.getAuthor());
      newbookSalesBook.setStandardPrice(book.getPrice());
      newbookSalesBook.setPublisher(book.getPublisher());
      newbookSalesBook.setImage(book.getImage());
      newbookSalesBook.setBigImage(book.getBigImage());
      salesBookService.save(newbookSalesBook);
    }
    if (model == null) {
      model = newbookSalesBook;
    }
    if (newbookSalesBook.getDiscount() == null) {
      bookDiscount = ConstantUtil.NEWBOOKDISCOUNT;
    } else {
      bookDiscount = newbookSalesBook.getDiscount();
    }
    ActionContext.getContext().getValueStack().set("bookDiscount", bookDiscount);
    newbookSalesBook.setDiscount(bookDiscount);
    newbookSalesBook.setPrice(
        Math.round(newbookSalesBook.getBook().getPrice() * bookDiscount * 10) / 10d);
    newbookSalesBook.setNum(999);
    SalesBook salesBook = newbookSalesBook;
    School findSchool = findUser.getSchool();
    List<SellerBean> sellerBeans =
        salesBookService.getSellerBeans(
            newbookSalesBook.getBook().getIsbn(), findSchool.getServiceArea().getId());
    int totalNum = cartService.getGrouponitemsByUser(findUser.getId(), find.getId()).size();
    ActionContext.getContext().getValueStack().set("sellerBeans", sellerBeans);
    ActionContext.getContext().getValueStack().set("item", findItem);
    Seller newBookSeller = sellerService.getSellerById(ConstantUtil.NEWBOOKPRODUCT);
    ActionContext.getContext().getValueStack().set("totalNum", totalNum);
    ActionContext.getContext().getValueStack().set("model", model);
    ActionContext.getContext().getValueStack().set("newBookSeller", newBookSeller);
    ActionContext.getContext()
        .getValueStack()
        .set("price", Math.round(newbookSalesBook.getBook().getPrice() * bookDiscount * 10) / 10d);
    // 获得这个学校所有的团购项目
    return SUCCESS;
  }