package com.bigdatan.b2c.controller.front;
import com.bigdatan.b2c.entity.Goods;
import com.bigdatan.b2c.entity.GoodsPrice;
import com.bigdatan.b2c.entity.StoreGoods;
import com.bigdatan.b2c.entity.User;
import com.bigdatan.b2c.service.ICommonExchangeService;
import com.bigdatan.b2c.service.IGoodsPriceService;
import com.bigdatan.b2c.service.IGoodsService;
import com.bigdatan.b2c.service.IStoreGoodsService;
import com.bigdatan.b2c.vo.GoodsVO;
import constant.SystemCode;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import util.JsonResponse;
import util.PageResult;
import util.SessionUtil;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
;
/**
*
* 商品管理模块 前台
*/
@RestController
@RequestMapping("/front/goods/goods")
public class GoodsController {
@Resource
private IGoodsService goodsService;
@Resource
ICommonExchangeService commonExchangeService;
@Resource
private IGoodsPriceService goodsPriceService;
@Resource
private IStoreGoodsService storeGoodsService;
/**
* 商品列表
*/
//@GetMapping("/getGoodsByPage")
@RequestMapping(method = {RequestMethod.POST, RequestMethod.GET},value = "/getGoodsByPage")
public JsonResponse<PageResult<Goods>> getGoodsByPage(
PageResult<Goods> page, Goods goods, HttpServletRequest request) {
JsonResponse<PageResult<Goods>> result = new JsonResponse<PageResult<Goods>>();
goodsService.queryByPageFront(page, goods);
if (page.getTotal() != 0) {
result.setRes(SystemCode.SUCCESS);
result.setObj(page);
} else {
result.setRes(SystemCode.FAILURE);
}
return result;
}
/**
* 商品详情
*/
//@GetMapping("/getGoodsById")
@RequestMapping(method = {RequestMethod.POST, RequestMethod.GET},value = "/getGoodsById")
public JsonResponse<Goods> getGoodsById(Integer goodsId,
HttpServletRequest request) {
JsonResponse<Goods> result = new JsonResponse<Goods>();
Goods goods = goodsService.selectByPrimaryKey(goodsId);
if (goods != null) {
result.setRes(SystemCode.SUCCESS);
result.setObj(goods);
}
return result;
}
/**
* 依据商品分类id获取所有商品,供前台调用
*/
//@GetMapping("/getPageFrontByGoodsCategory")
@RequestMapping(method = {RequestMethod.POST, RequestMethod.GET},value = "/getPageFrontByGoodsCategory")
public JsonResponse<List<GoodsVO>> getPageFrontByGoodsCategory(Goods goods,
HttpServletRequest request) {
JsonResponse<List<GoodsVO>> result = new JsonResponse<List<GoodsVO>>();
List<Goods> goodsList = goodsService.getPageFrontByGoodsCategory(goods);
if (goodsList != null) {
List<GoodsVO> goodsVOList = new ArrayList<GoodsVO>();
if (goodsList != null) {
// 获取商品规格
for (int i = 0; i < goodsList.size(); i++) {
Goods tempGoods = goodsList.get(i);
GoodsVO temGoodsVO = new GoodsVO(tempGoods);
User user = SessionUtil.getUser(request);
List<GoodsPrice> listGoodsPrice = goodsPriceService
.findAllNormalGoodsPriceByGoodsId(tempGoods
.getGoodsId());
// 换算价格
listGoodsPrice = commonExchangeService
.getCurrentUserGoodsprice(user, listGoodsPrice);
if (listGoodsPrice == null || listGoodsPrice.size() == 0) {
temGoodsVO.setVo_countGoodsPrice(0);
} else {
temGoodsVO.setVo_countGoodsPrice(listGoodsPrice.size());
for (int j = 0; j < listGoodsPrice.size(); j++) {
if (temGoodsVO.getVo_retailPrice() == 0) {
temGoodsVO.setVo_retailPrice(listGoodsPrice
.get(j).getRetailPrice());
} else if (temGoodsVO.getVo_retailPrice() > listGoodsPrice
.get(j).getRetailPrice()) {
temGoodsVO.setVo_retailPrice(listGoodsPrice
.get(j).getRetailPrice());
}
}
temGoodsVO.setVo_shoppingCartNum(listGoodsPrice.get(0)
.getBuyPrice());
temGoodsVO.setVo_priceId(listGoodsPrice.get(0)
.getPriceId());
temGoodsVO.setVo_unitName(listGoodsPrice.get(0)
.getUnitName());
}
goodsVOList.add(temGoodsVO);
}
}
result.setRes(SystemCode.SUCCESS);
result.setObj(goodsVOList);
}
return result;
}
/**
* 依据商品分类id获取所有商品,供前台调用,传的是VO对象,包含商品所有信息及附加信息
*/
@ResponseBody
@RequestMapping("/getPageFrontVOByGoodsCategory")
public JsonResponse<PageResult<GoodsVO>> getPageFrontVOByGoodsCategory(
PageResult<Goods> page, Goods goods, HttpServletRequest request) {
JsonResponse<PageResult<GoodsVO>> result = new JsonResponse<PageResult<GoodsVO>>();
int category =-1;
try {
category = Integer.parseInt(java.net.URLDecoder.decode(Integer.toString(goods.getCategoryId()), "UTF-8"));
} catch (UnsupportedEncodingException e) {
category=-1;
e.printStackTrace();
}
goods.setCategoryId(category);
goodsService.getPageFrontByGoodsCategory(page, goods);
List<Goods> goodsList = page.getDataList();
List<GoodsVO> goodsVOList = new ArrayList<GoodsVO>();
PageResult<GoodsVO> resultVO = new PageResult<GoodsVO>();
resultVO.setPageNo(page.getPageNo());
resultVO.setPages(page.getPages());
resultVO.setPageSize(page.getPageSize());
resultVO.setTotal(page.getTotal());
if (goodsList != null) {
// 获取商品规格
for (int i = 0; i < goodsList.size(); i++) {
Goods tempGoods = goodsList.get(i);
GoodsVO temGoodsVO = new GoodsVO(tempGoods);
User user = SessionUtil.getUser(request);
List<GoodsPrice> listGoodsPrice = goodsPriceService
.findAllNormalGoodsPriceByGoodsId(tempGoods
.getGoodsId());
// 换算价格
listGoodsPrice = commonExchangeService
.getCurrentUserGoodsprice(user, listGoodsPrice);
if (listGoodsPrice == null || listGoodsPrice.size() == 0) {
temGoodsVO.setVo_countGoodsPrice(0);
} else {
temGoodsVO.setVo_countGoodsPrice(listGoodsPrice.size());
for (int j = 0; j < listGoodsPrice.size(); j++) {
if (temGoodsVO.getVo_retailPrice() == 0) {
temGoodsVO.setVo_retailPrice(listGoodsPrice
.get(j).getRetailPrice());
} else if (temGoodsVO.getVo_retailPrice() > listGoodsPrice
.get(j).getRetailPrice()) {
temGoodsVO.setVo_retailPrice(listGoodsPrice
.get(j).getRetailPr