package org.xmx0632.deliciousfruit.api.v1;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.mapper.BeanMapper;
import org.xmx0632.deliciousfruit.api.v1.bo.AllFruiStoriesRequest;
import org.xmx0632.deliciousfruit.api.v1.bo.AllFruitStoriesResponse;
import org.xmx0632.deliciousfruit.api.v1.bo.FruitStoryBo;
import org.xmx0632.deliciousfruit.api.v1.bo.FruitStoryMaterialBo;
import org.xmx0632.deliciousfruit.api.v1.bo.FruitStoryMenuBo;
import org.xmx0632.deliciousfruit.api.v1.bo.FruitStoryProcedureBo;
import org.xmx0632.deliciousfruit.api.v1.bo.FruitStoryRequest;
import org.xmx0632.deliciousfruit.api.v1.bo.FruitStoryResponse;
import org.xmx0632.deliciousfruit.api.v1.bo.Result;
import org.xmx0632.deliciousfruit.api.v1.bo.TerminalType;
import org.xmx0632.deliciousfruit.api.v1.helper.PictureUrlHelper;
import org.xmx0632.deliciousfruit.api.v1.helper.TTLHelper;
import org.xmx0632.deliciousfruit.api.v1.helper.WebHelper;
import org.xmx0632.deliciousfruit.entity.FruitProduct;
import org.xmx0632.deliciousfruit.entity.FruitStory;
import org.xmx0632.deliciousfruit.entity.FruitStoryMaterial;
import org.xmx0632.deliciousfruit.entity.FruitStoryMenu;
import org.xmx0632.deliciousfruit.entity.FruitStoryProcedure;
import org.xmx0632.deliciousfruit.entity.UserAccount;
import org.xmx0632.deliciousfruit.global.ConfigConstant;
import org.xmx0632.deliciousfruit.service.ConfigService;
import org.xmx0632.deliciousfruit.service.FruitProductService;
import org.xmx0632.deliciousfruit.service.FruitPromotionService;
import org.xmx0632.deliciousfruit.service.FruitStoryMaterialService;
import org.xmx0632.deliciousfruit.service.FruitStoryMenuService;
import org.xmx0632.deliciousfruit.service.FruitStoryProcedureService;
import org.xmx0632.deliciousfruit.service.FruitStoryService;
/**
* FruitStory的API的Controller
*
* @author Jefferson-pengtao
*/
@Controller
@RequestMapping(value = "/api/v1/fruitstory")
public class FruitStoryApiController {
private static Logger log = LoggerFactory
.getLogger(FruitStoryApiController.class);
@Autowired
private FruitStoryService fruitStoryService;
@Autowired
private FruitStoryMenuService fruitStoryMenuService;
@Autowired
private FruitStoryMaterialService fruitStoryMaterialService;
@Autowired
private FruitStoryProcedureService fruitStoryProcedureService;
@Autowired
private ConfigService configService;
@Autowired
private FruitPromotionService fruitPromotionService;
@Autowired
private FruitProductService fruitProductService;
@RequestMapping(value = "/getAll", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<AllFruitStoriesResponse> getAll(
@RequestBody AllFruiStoriesRequest allFruiStoriesRequest,
ServletRequest request) {
UserAccount userAccount = WebHelper.getCurrentUser(request);
log.debug("UserAccount:{}", userAccount);
log.debug("Request:{}", allFruiStoriesRequest);
AllFruitStoriesResponse response = new AllFruitStoriesResponse();
TerminalType type = TerminalType.toType(allFruiStoriesRequest
.getTerminalType());
// 检测是否有效的终端类型
if (type == null) {
response.setResult(new Result(Result.FAIL,
Result.MSG_ERR_NOT_VALID_TERMINAL_TYPE));
log.debug("response:{}", response);
return new ResponseEntity<AllFruitStoriesResponse>(response,
HttpStatus.OK);
}
List<FruitStory> stories = fruitStoryService
.findAllFruitStoryByStatus(FruitStory.STATUS_ONLINE);
for (FruitStory story : stories) {
response.getFruitStories().add(convertToBo(story, type, request));
}
response.setResult(new Result(Result.SUCCESS, ""));
response.setTTL(TTLHelper.genTTL(configService));
log.debug("response:{}", response);
return new ResponseEntity<AllFruitStoriesResponse>(response,
HttpStatus.OK);
}
@RequestMapping(value = "/getbyid", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<FruitStoryResponse> getById(
@RequestBody FruitStoryRequest fruitStoryRequest,
ServletRequest request) {
UserAccount userAccount = WebHelper.getCurrentUser(request);
log.debug("UserAccount:{}", userAccount);
log.debug("Request:{}", fruitStoryRequest);
FruitStoryResponse response = new FruitStoryResponse();
// 检测有没有ID
if (fruitStoryRequest.getId() == null
|| "".equals(fruitStoryRequest.getId())) {
response.setResult(new Result(Result.FAIL, Result.MSG_ERR_NO_ID));
log.debug("response:{}", response);
return new ResponseEntity<FruitStoryResponse>(response,
HttpStatus.OK);
}
Long id;
// 检测ID是不是有效
try {
id = Long.parseLong(fruitStoryRequest.getId());
} catch (NumberFormatException e) {
response.setResult(new Result(Result.FAIL,
Result.MSG_ERR_NOT_VALID_ID));
log.debug("response:{}", response);
return new ResponseEntity<FruitStoryResponse>(response,
HttpStatus.OK);
}
TerminalType type = TerminalType.toType(fruitStoryRequest
.getTerminalType());
// 检测是否有效的终端类型
if (type == null) {
response.setResult(new Result(Result.FAIL,
Result.MSG_ERR_NOT_VALID_TERMINAL_TYPE));
log.debug("response:{}", response);
return new ResponseEntity<FruitStoryResponse>(response,
HttpStatus.OK);
}
FruitStory story = fruitStoryService.getFruitStory(id);
// 不存在此ID的水果故事
if (story == null) {
response.setResult(new Result(Result.FAIL, Result.MSG_ERR_NOT_EXIST));
log.debug("response:{}", response);
return new ResponseEntity<FruitStoryResponse>(response,
HttpStatus.OK);
}
// 水果故事已下线或还没创建完
if (!FruitStory.STATUS_ONLINE.equalsIgnoreCase(story.getStatus())) {
response.setResult(new Result(Result.FAIL,
Result.MSG_ERR_STORY_NOT_ONLINE));
log.debug("response:{}", response);
return new ResponseEntity<FruitStoryResponse>(response,
HttpStatus.OK);
}
FruitStoryBo fruitStoryBo = convertToBo(story, type, request);
response.setFruitStory(fruitStoryBo);
response.setResult(new Result(Result.SUCCESS, ""));
response.setTTL(TTLHelper.genTTL(configService));
return new ResponseEntity<FruitStoryResponse>(response, HttpStatus.OK);
}
private FruitStoryBo convertToBo(FruitStory story, TerminalType type,
ServletRequest request) {
FruitStoryBo fruitStoryBo = BeanMapper.map(story, FruitStoryBo.class);
fruitStoryBo.setPictureUrl(PictureUrlHelper.genPictureUrl(type,
ConfigConstant.FRUIT_STORY_DIR, fruitStoryBo.getPictureUrl(),
configService));
fruitStoryBo.setFruitStoryMenu(getFruitStoryMenuBo(
story.getFruitStoryMenu(), type, request));
return fruitStoryBo;
}
private FruitStoryMenuBo getFruitStoryMenuBo(FruitStoryMenu fruitStoryMenu,
TerminalType type, ServletRequest request) {
FruitStoryMenuBo fruitStoryMenuBo = BeanMapper.map(fruitStoryMenu,
FruitStoryMenuBo.class);
fruitStoryMenuBo.setFruitStoryPicUrl(PictureUrlHelper.genPictureUrl(
type, ConfigConstant.FRUIT_MENU_DIR,
fruitStoryMenuBo.getFruitStoryPicUrl(), configService));
fruitStoryMenuBo.setMaterials(getFruitStoryMaterialBos(fruitStoryMenu,
type, request));
fruitStoryMenuBo.setProcedures(getFruitStoryProcedureBos(
fruitStoryMenu, type, request));
FruitProduct FruitProduct = fruitProductService.findByProductId(String
.valueOf(fruitStoryMenu.getProductId()));
String promotion = fruitPromotionService.getProductGiftInfoByProduct(
type, FruitProduct);
if (FruitProduct != null)
fruitStoryMenuBo.setPrice(FruitProduct.getE6Price());
fruitStoryMenuBo.setPromotion(promotion);
return fruitStoryMenuBo;
}
private List<FruitStoryProcedureBo> getFruitStoryProcedureBos(
FruitStoryMenu fruitStoryMenu, TerminalType type,
ServletRequest request) {
List<FruitStoryProcedureBo> procedureBos = new ArrayList<FruitStoryProcedureBo>();
List<FruitStoryProcedure> fruitStoryProcedures = fruitStoryMenu
.getFruitStoryProcedureList();
for (FruitStoryProcedure fruitStoryProcedure : fruitStoryProcedures) {
FruitStoryProcedureBo fruitStoryProcedureBo = BeanMapper.map(
fruitStoryProcedure, FruitStoryProcedureBo.class);
fruitStoryProcedureBo.setProcedurePic(PictureUrlHelper
.genPictureUrl(type, ConfigConstant.FRUIT_PROCEDURE_DIR,
fruitStoryProcedureBo.getProcedurePic(),
configService));
procedureBos.add(fruitStoryProcedureBo);
}
return procedureBos;
}
private List<FruitStoryMaterialBo> getFruitStoryMaterialBos(
FruitStoryMenu fruitStoryMenu, TerminalType type,
ServletRequest request) {
List<FruitStoryMaterialBo> materialBos = new ArrayList<FruitStoryMaterialBo>();
List<FruitStoryMaterial> fruitStoryMaterials = fruitStoryMenu
.getFruitStoryMaterialList();
for (FruitStoryMaterial fruitStoryMaterial : fruitStoryMaterials) {
materialBos.add(BeanMapper.map(fruitStoryMaterial,
FruitStoryMaterialBo.class));
}
return materialBos;
}
}