@Override
  public int sell(Map<Integer, Integer> sells, Date date, String bz, String uId, int shopId)
      throws Exception {
    int ret = 0;
    SqlSession sqlSession = null;

    try {
      sqlSession = SessionUtils.getSession();

      SellMapper sellMapper = sqlSession.getMapper(SellMapper.class);
      SellDetailMapper sellDetailMapper = sqlSession.getMapper(SellDetailMapper.class);
      StockMapper stockMapper = sqlSession.getMapper(StockMapper.class);

      Sell sell = new Sell();
      sell.setsBz(bz);
      sell.setsDate(date);
      sell.setShopId(shopId);
      sell.setuId(uId);

      sellMapper.insert(sell);

      Iterator<Integer> iterator = sells.keySet().iterator();
      List<Stock> stocks = stockMapper.selectAll();

      while (iterator.hasNext()) {
        SellDetail sellDetail = new SellDetail();
        sellDetail.setSellId(sell.getsId());
        sellDetail.setpId(iterator.next());
        sellDetail.setsNum(sells.get(sellDetail.getpId()));
        sellDetailMapper.insert(sellDetail);

        for (Stock stock : stocks) {
          if (stock.getShopId() == shopId && stock.getpId() == sellDetail.getpId()) {
            if (stock.getsNum() >= sellDetail.getsNum()) {
              stock.setsNum(stock.getsNum() - sellDetail.getsNum());
              stockMapper.updateByPrimaryKey(stock);
            } else {
              throw new Exception();
            }
          }
        }
      }

      sqlSession.commit();
      ret = 1;
    } catch (Exception e) {
      sqlSession.rollback();
      throw e;
    } finally {
      SessionUtils.closeSession(sqlSession);
    }

    return ret;
  }
  @Override
  public List<Object> querySell(Date start, Date end, int shopId, int orderId) throws Exception {

    List<Object> list = new ArrayList<Object>();
    SqlSession sqlSession = null;

    try {
      sqlSession = SessionUtils.getSession();

      ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);
      SellMapper sellMapper = sqlSession.getMapper(SellMapper.class);
      SellDetailMapper sellDetailMapper = sqlSession.getMapper(SellDetailMapper.class);
      UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
      ShopMapper shopMapper = sqlSession.getMapper(ShopMapper.class);

      if (orderId != -1) {
        Sell sell = sellMapper.selectByPrimaryKey(orderId);

        if (sell != null) {
          Map<String, Object> map = new HashMap<String, Object>();
          map.put("oId", sell.getsId());
          map.put("date", sell.getsDate());
          User user = userMapper.selectByPrimaryKey(sell.getuId());
          map.put("user", user.getuName());
          List<SellDetail> sellDetails = sellDetailMapper.selectBySellId(sell.getsId());
          int price = 0;
          for (SellDetail sellDetail : sellDetails) {
            Product product = productMapper.selectByPrimaryKey(sellDetail.getpId());
            price += sellDetail.getsNum() * product.getpPrice();
          }
          map.put("price", price);
          Shop shop = shopMapper.selectByPrimaryKey(sell.getShopId());
          map.put("shop", shop.getsName());
          list.add(map);
        }

        return list;
      } else {
        List<Sell> sells = null;
        if (shopId == 0) {
          sells = sellMapper.selectAll();
        } else {
          sells = sellMapper.selectByShopId(shopId);
        }

        for (Sell sell : sells) {
          if ((end == null ? true : sell.getsDate().before(end))
              && (start == null ? true : sell.getsDate().after(start))) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("oId", sell.getsId());
            map.put("date", sell.getsDate());
            User user = userMapper.selectByPrimaryKey(sell.getuId());
            map.put("user", user.getuName());
            List<SellDetail> sellDetails = sellDetailMapper.selectBySellId(sell.getShopId());
            int price = 0;
            for (SellDetail sellDetail : sellDetails) {
              Product product = productMapper.selectByPrimaryKey(sellDetail.getpId());
              price += sellDetail.getsNum() * product.getpPrice();
            }
            map.put("price", price);
            Shop shop = shopMapper.selectByPrimaryKey(sell.getShopId());
            map.put("shop", shop.getsName());
            list.add(map);
          }
        }
      }
    } catch (Exception e) {
      throw e;
    } finally {
      SessionUtils.closeSession(sqlSession);
    }

    return list;
  }