@Override
  public List<Object> getProductByShop(int shopId) throws Exception {
    List<Object> list = new ArrayList<Object>();
    SqlSession sqlSession = null;

    try {
      sqlSession = SessionUtils.getSession();

      StockMapper stockMapper = sqlSession.getMapper(StockMapper.class);
      ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);

      List<Stock> stocks = stockMapper.selectByshopId(shopId);

      for (Stock stock : stocks) {
        Map<String, Object> map = new HashMap<String, Object>();
        Product product = productMapper.selectByPrimaryKey(stock.getpId());

        map.put("name", product.getpName());
        map.put("num", stock.getsNum());
        map.put("pid", stock.getpId());
        map.put("price", product.getpPrice());

        list.add(map);
      }

    } catch (Exception e) {
      throw e;
    } finally {
      SessionUtils.closeSession(sqlSession);
    }

    return list;
  }
  @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 int stockOut(Map<Integer, Integer> outStocks, Date date, String bz, String uId, int shopId)
      throws Exception {
    int ret = 0;
    SqlSession sqlSession = null;

    try {
      sqlSession = SessionUtils.getSession();

      OutStockMapper outStockMapper = sqlSession.getMapper(OutStockMapper.class);
      OutStockDetailMapper outStockDetailMapper = sqlSession.getMapper(OutStockDetailMapper.class);
      StockMapper stockMapper = sqlSession.getMapper(StockMapper.class);

      OutStock outStock = new OutStock();
      outStock.setoBz(bz);
      outStock.setoDate(date);
      outStock.setsId(shopId);
      outStock.setuId(uId);

      outStockMapper.insert(outStock);

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

      while (iterator.hasNext()) {
        OutStockDetail outStockDetail = new OutStockDetail();
        outStockDetail.setOutstockId(outStock.getoId());
        outStockDetail.setpId(iterator.next());
        outStockDetail.setoNum(outStocks.get(outStockDetail.getpId()));
        outStockDetailMapper.insert(outStockDetail);

        boolean shopFlag = false, warehouseFlag = false;
        for (Stock stock : stocks) {
          if (stock.getShopId() == shopId && stock.getpId() == outStockDetail.getpId()) {
            stock.setsNum(stock.getsNum() + outStockDetail.getoNum());
            stockMapper.updateByPrimaryKey(stock);
            shopFlag = true;
            if (shopFlag && warehouseFlag) break;
          } else if (stock.getShopId() == 1 && stock.getpId() == outStockDetail.getpId()) {
            if (stock.getsNum() >= outStockDetail.getoNum()) {
              stock.setsNum(stock.getsNum() - outStockDetail.getoNum());
              stockMapper.updateByPrimaryKey(stock);
              warehouseFlag = true;
              if (shopFlag && warehouseFlag) break;
            } else {
              throw new Exception();
            }
          }
        }
        if (!warehouseFlag) {
          throw new Exception();
        }
        if (!shopFlag) {
          Stock stock = new Stock();
          stock.setShopId(shopId);
          stock.setpId(outStockDetail.getpId());
          stock.setsMaxnum(Integer.MAX_VALUE);
          stock.setsMinnum(0);
          stock.setsNum(outStockDetail.getoNum());
          stockMapper.insert(stock);
        }
      }

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

    return ret;
  }