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.AllFruitProductRequest; import org.xmx0632.deliciousfruit.api.v1.bo.AllFruitProductResponse; import org.xmx0632.deliciousfruit.api.v1.bo.FruitProductBo; import org.xmx0632.deliciousfruit.api.v1.bo.GetProductByIDRequest; import org.xmx0632.deliciousfruit.api.v1.bo.GetProductByIDResponse; import org.xmx0632.deliciousfruit.api.v1.bo.GetProductByIDResponse.FruitProductBoForByID; import org.xmx0632.deliciousfruit.api.v1.bo.GetProductsByCategoryRequest; import org.xmx0632.deliciousfruit.api.v1.bo.GetProductsByCategoryResponse; import org.xmx0632.deliciousfruit.api.v1.bo.Result; import org.xmx0632.deliciousfruit.api.v1.bo.TerminalType; import org.xmx0632.deliciousfruit.api.v1.helper.CategoryToClient; import org.xmx0632.deliciousfruit.api.v1.helper.DateHelper; 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.FruitCategory; import org.xmx0632.deliciousfruit.entity.FruitProduct; import org.xmx0632.deliciousfruit.entity.FruitSubcategory; import org.xmx0632.deliciousfruit.entity.UserAccount; import org.xmx0632.deliciousfruit.global.ConfigConstant; import org.xmx0632.deliciousfruit.service.ConfigService; import org.xmx0632.deliciousfruit.service.FruitCategoryService; import org.xmx0632.deliciousfruit.service.FruitProductService; import org.xmx0632.deliciousfruit.service.FruitPromotionService; import org.xmx0632.deliciousfruit.service.FruitSubcategoryService; /** * FruitProduct的API的Controller * * @author Jefferson-pengtao */ @Controller @RequestMapping(value = "/api/v1/fruitproduct") public class FruitProductApiController { private static Logger log = LoggerFactory .getLogger(FruitCategoryApiController.class); @Autowired private FruitProductService fruitProductService; @Autowired private FruitCategoryService fruitCategoryService; @Autowired private FruitSubcategoryService fruitSubcategoryService; @Autowired private ConfigService configService; @Autowired private FruitPromotionService fruitPromotionService; @RequestMapping(value = "/getAll", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity<AllFruitProductResponse> getAll( @RequestBody AllFruitProductRequest allFruitProductRequest, ServletRequest request) { UserAccount userAccount = WebHelper.getCurrentUser(request); log.debug("UserAccount:{}", userAccount); log.debug("Request:{}", allFruitProductRequest); AllFruitProductResponse response = new AllFruitProductResponse(); TerminalType type = TerminalType.toType(allFruitProductRequest .getTerminalType()); // 检测是否有效的终端类型 if (type == null) { response.setResult(new Result(Result.FAIL, Result.MSG_ERR_NOT_VALID_TERMINAL_TYPE)); return new ResponseEntity<AllFruitProductResponse>(response, HttpStatus.OK); } List<FruitProduct> fruitProducts = fruitProductService .getAllFruitProduct(); for (FruitProduct fruitProduct : fruitProducts) { if (!FruitProduct.STATUS_ONLINE.equalsIgnoreCase(fruitProduct .getOnline()) || !FruitProduct.PIC_STATUS_READY .equalsIgnoreCase(fruitProduct.getPicStatus())) { continue; } FruitProductBo fruitProductBo = convertToProductBo(fruitProduct, type); response.getFruitProducts().add(fruitProductBo); } response.setResult(new Result(Result.SUCCESS, "")); response.setTTL(TTLHelper.genTTL(configService)); log.debug("response:{}", response); return new ResponseEntity<AllFruitProductResponse>(response, HttpStatus.OK); } private String getFruitCategoryId(FruitProduct fruitProduct) { FruitSubcategory fruitSubcategory = fruitProduct.getFruitSubcategory(); if (fruitSubcategory == null) { log.warn( "can not find fruitSubcategory for fruitProduct with id:{}", fruitProduct.getId()); return ""; } FruitCategory fruitCategory = fruitSubcategory.getFruitCategory(); if (fruitCategory == null) { log.warn("can not find fruitCategory for fruitProduct with id:{}", fruitProduct.getId()); return ""; } String idsNeedSub = configService .getByName(ConfigConstant.CATEGORY_SYN_SUB); if (CategoryToClient.needSubCategoryID(fruitCategory.getId(), idsNeedSub)) { return CategoryToClient.formatSubCategoryID(fruitSubcategory .getId()); } else { return CategoryToClient.formatCategoryID(fruitCategory.getId()); } } private FruitProductBo convertToProductBo(FruitProduct fruitProduct, TerminalType type) { FruitProductBo fruitProductBo = BeanMapper.map(fruitProduct, FruitProductBo.class); String fruitCategoryId = getFruitCategoryId(fruitProduct); fruitProductBo.setFruitCategoryId(fruitCategoryId); fruitProductBo.setDescriptionPicUrl(PictureUrlHelper.genPictureUrl( type, ConfigConstant.FRUIT_PRODUCT_DIR, fruitProductBo.getDescriptionPicUrl(), configService)); fruitProductBo.setPicUrl(PictureUrlHelper.genPictureUrl(type, ConfigConstant.FRUIT_PRODUCT_DIR, fruitProductBo.getPicUrl(), configService)); fruitProductBo.setExpirationDate(DateHelper.getDateString(fruitProduct .getExpirationDate())); fruitProductBo.setPromotion(fruitPromotionService .getProductGiftInfoByProduct(type, fruitProduct)); fruitProductBo.setSeconddiscount(fruitProduct.getDeductForSecond()); return fruitProductBo; } private FruitProductBoForByID convertToProductBoForByID( FruitProduct fruitProduct, TerminalType type) { FruitProductBo fruitProductBo = convertToProductBo(fruitProduct, type); FruitProductBoForByID fruitProductBoForByID = BeanMapper.map( fruitProductBo, FruitProductBoForByID.class); fruitProductBoForByID.setFruitcategoryiconurl(PictureUrlHelper .genPictureUrl(type, ConfigConstant.CATEGORY_DIR, fruitProduct .getFruitSubcategory().getFruitCategory().getIconUrl(), configService)); return fruitProductBoForByID; } @RequestMapping(value = "/getByCategory", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity<GetProductsByCategoryResponse> getByCategory( @RequestBody GetProductsByCategoryRequest getProductsByCategoryRequest, ServletRequest request) { UserAccount userAccount = WebHelper.getCurrentUser(request); log.debug("UserAccount:{}", userAccount); log.debug("Request:{}", getProductsByCategoryRequest); GetProductsByCategoryResponse response = new GetProductsByCategoryResponse(); TerminalType type = TerminalType.toType(getProductsByCategoryRequest .getTerminalType()); // 检测是否有效的终端类型 if (type == null) { response.setResult(new Result(Result.FAIL, Result.MSG_ERR_NOT_VALID_TERMINAL_TYPE)); log.debug("response:{}", response); return new ResponseEntity<GetProductsByCategoryResponse>(response, HttpStatus.OK); } // 检测有没有ID if (getProductsByCategoryRequest.getFruitCategoryId() == null || "".equals(getProductsByCategoryRequest.getFruitCategoryId())) { response.setResult(new Result(Result.FAIL, Result.MSG_ERR_NO_ID)); log.debug("response:{}", response); return new ResponseEntity<GetProductsByCategoryResponse>(response, HttpStatus.OK); } FruitCategory fruitCategory = null; FruitSubcategory fruitSubcategory = null; String categoryId = getProductsByCategoryRequest.getFruitCategoryId(); if (CategoryToClient.isCategory(categoryId)) { fruitCategory = fruitCategoryService .getFruitCategory(CategoryToClient .getCategoryID(categoryId)); } else if (CategoryToClient.isSubCategory(categoryId)) { fruitSubcategory = fruitSubcategoryService .getFruitSubcategory(CategoryToClient .getSubCategoryID(categoryId)); } // 检测水果分类是否存在 if (fruitCategory == null && fruitSubcategory == null) { response.setResult(new Result(Result.FAIL, Result.MSG_ERR_NOT_EXIST)); log.debug("response:{}", response); return new ResponseEntity<GetProductsByCategoryResponse>(response, HttpStatus.OK); } List<FruitProduct> fruitProducts = new ArrayList<FruitProduct>(); if (fruitCategory != null) { List<FruitSubcategory> fruitSubcategories = fruitCategory .getFruitSubcategoryList(); for (FruitSubcategory subcategory : fruitSubcategories) { List<FruitProduct> products = subcategory.getFruitProducts(); if (products != null && products.size() > 0) { fruitProducts.addAll(products); } } } if (fruitSubcategory != null) { List<FruitProduct> products = fruitSubcategory.getFruitProducts(); if (products != null && products.size() > 0) { fruitProducts.addAll(products); } } for (FruitProduct fruitProduct : fruitProducts) { if (!FruitProduct.STATUS_ONLINE.equalsIgnoreCase(fruitProduct .getOnline()) || !FruitProduct.PIC_STATUS_READY .equalsIgnoreCase(fruitProduct.getPicStatus())) { continue; } FruitProductBo fruitProductBo = convertToProductBo(fruitProduct, type); response.getFruitProducts().add(fruitProductBo); } response.setResult(Result.SUCCESS_RESULT); response.setTTL(TTLHelper.genTTL(configService)); log.debug("response:{}", response); return new ResponseEntity<GetProductsByCategoryResponse>(response, HttpStatus.OK); } @RequestMapping(value = "/getProductByID", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity<GetProductByIDResponse> getProductByID( @RequestBody GetProductByIDRequest getProductByIDRequest, ServletRequest request) { UserAccount userAccount = WebHelper.getCurrentUser(request); log.debug("UserAccount:{}", userAccount); log.debug("Request:{}", getProductByIDRequest); GetProductByIDResponse response = new GetProductByIDResponse(); TerminalType type = TerminalType.toType(getProductByIDRequest .getTerminalType()); // 检测是否有效的终端类型 if (type == null) { response.setResult(new Result(Result.FAIL, Result.MSG_ERR_NOT_VALID_TERMINAL_TYPE)); log.debug("response:{}", response); return new ResponseEntity<GetProductByIDResponse>(response, HttpStatus.OK); } String productId; // 检测有没有ID if (getProductByIDRequest.getFruitProductId() == null || "".equals(getProductByIDRequest.getFruitProductId())) { response.setResult(new Result(Result.FAIL, Result.MSG_ERR_NO_ID)); log.debug("response:{}", response); return new ResponseEntity<GetProductByIDResponse>(response, HttpStatus.OK); } productId = getProductByIDRequest.getFruitProductId(); FruitProduct fruitProduct = fruitProductService .findByProductId(productId); if (fruitProduct != null) { if (!FruitProduct.STATUS_ONLINE.equalsIgnoreCase(fruitProduct .getOnline()) || !FruitProduct.PIC_STATUS_READY .equalsIgnoreCase(fruitProduct.getPicStatus())) { } else { FruitProductBoForByID fruitProductBo = convertToProductBoForByID( fruitProduct, type); response.setFruitProduct(fruitProductBo); } } response.setResult(new Result(Result.SUCCESS, "")); response.setTTL(TTLHelper.genTTL(configService)); log.debug("response:{}", response); return new ResponseEntity<GetProductByIDResponse>(response, HttpStatus.OK); } }