package org.xmx0632.deliciousfruit.api.v1;
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.AllFruitCategoryRequest;
import org.xmx0632.deliciousfruit.api.v1.bo.AllFruitCategoryResponse;
import org.xmx0632.deliciousfruit.api.v1.bo.FruitCategoryBo;
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.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.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;
/**
* FruitCategory的API的Controller
*
* @author Jefferson-pengtao
*/
@Controller
@RequestMapping(value = "/api/v1/fruitcategory")
public class FruitCategoryApiController {
private static Logger log = LoggerFactory
.getLogger(FruitCategoryApiController.class);
@Autowired
private FruitCategoryService fruitCategoryService;
@Autowired
private ConfigService configService;
@RequestMapping(value = "/getAll", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<AllFruitCategoryResponse> getAll(
@RequestBody AllFruitCategoryRequest allFruitCategoryRequest,
ServletRequest request) {
UserAccount userAccount = WebHelper.getCurrentUser(request);
log.debug("UserAccount:{}", userAccount);
log.debug("Request:{}", allFruitCategoryRequest);
AllFruitCategoryResponse response = new AllFruitCategoryResponse();
TerminalType type = TerminalType.toType(allFruitCategoryRequest
.getTerminalType());
// 检测是否有效的终端类型
if (type == null) {
response.setResult(new Result(Result.FAIL,
Result.MSG_ERR_NOT_VALID_TERMINAL_TYPE));
return new ResponseEntity<AllFruitCategoryResponse>(response,
HttpStatus.OK);
}
List<FruitCategory> fruitCategorys = fruitCategoryService
.getAllFruitCategory();
String idsNeedSub = configService
.getByName(ConfigConstant.CATEGORY_SYN_SUB);
for (FruitCategory fruitCategory : fruitCategorys) {
if (CategoryToClient.needSubCategoryID(fruitCategory.getId(),
idsNeedSub)) {
List<FruitSubcategory> fruitSubcategorys = fruitCategory
.getFruitSubcategoryList();
for (FruitSubcategory fruitSubcategory : fruitSubcategorys) {
response.getFruitCategories().add(
toBoFromSubCategory(fruitSubcategory, type));
}
} else {
response.getFruitCategories().add(
toBoFromCategory(fruitCategory, type));
}
}
response.setResult(new Result(Result.SUCCESS, ""));
response.setTTL(TTLHelper.genTTL(configService));
log.debug("response:{}", response);
return new ResponseEntity<AllFruitCategoryResponse>(response,
HttpStatus.OK);
}
private FruitCategoryBo toBoFromSubCategory(
FruitSubcategory fruitSubcategory, TerminalType type) {
FruitCategoryBo fruitSubCategoryBo = new FruitCategoryBo();
fruitSubCategoryBo.setFruitCategoryId(CategoryToClient
.formatSubCategoryID(fruitSubcategory.getId()));
fruitSubCategoryBo.setName(fruitSubcategory.getName());
fruitSubCategoryBo.setIconUrl(PictureUrlHelper.genPictureUrl(type,
ConfigConstant.CATEGORY_DIR, fruitSubcategory
.getFruitCategory().getIconUrl(), configService));
return fruitSubCategoryBo;
}
private FruitCategoryBo toBoFromCategory(FruitCategory fruitCategory,
TerminalType type) {
FruitCategoryBo fruitCategoryBo = BeanMapper.map(fruitCategory,
FruitCategoryBo.class);
fruitCategoryBo.setFruitCategoryId(CategoryToClient
.formatCategoryID(fruitCategory.getId()));
fruitCategoryBo.setIconUrl(PictureUrlHelper.genPictureUrl(type,
ConfigConstant.CATEGORY_DIR, fruitCategoryBo.getIconUrl(),
configService));
return fruitCategoryBo;
}
}