package org.xmx0632.deliciousfruit.web.share; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springside.modules.mapper.BeanMapper; import org.xmx0632.deliciousfruit.api.v1.bo.FruitProductBo; import org.xmx0632.deliciousfruit.api.v1.bo.TerminalType; import org.xmx0632.deliciousfruit.api.v1.helper.PictureUrlHelper; import org.xmx0632.deliciousfruit.entity.FruitProduct; import org.xmx0632.deliciousfruit.entity.FruitStory; import org.xmx0632.deliciousfruit.entity.Order; import org.xmx0632.deliciousfruit.entity.OrderProduct; import org.xmx0632.deliciousfruit.entity.OrderProductFree; import org.xmx0632.deliciousfruit.service.ConfigService; import org.xmx0632.deliciousfruit.service.FruitProductService; import org.xmx0632.deliciousfruit.service.OrderService; import com.google.common.collect.Maps; @Controller @RequestMapping(value = "/share/order") public class ShareOrderController { private static Logger log = LoggerFactory .getLogger(ShareStoryController.class); @Autowired private ResourceBundleMessageSource messageSource; private static Map<String, String> sortTypes = null; private static Map<String, String> allStatus = Maps.newHashMap(); static { allStatus.put(FruitStory.STATUS_CREATING, "creating"); allStatus.put(FruitStory.STATUS_OFFLINE, "offline"); allStatus.put(FruitStory.STATUS_ONLINE, "online"); } private Map<String, String> getSortTypes(Locale locale) { if (sortTypes == null) { sortTypes = Maps.newLinkedHashMap(); sortTypes.put("auto", messageSource.getMessage("common.auto", null, locale)); sortTypes.put("name", messageSource.getMessage("fruitStory.name", null, locale)); } return sortTypes; } @Autowired private FruitProductService fruitProductService; @Autowired private OrderService orderService; @Autowired private ConfigService configService; @RequestMapping(value = "view/{orderNumber}", method = RequestMethod.GET) public String view(@PathVariable("orderNumber") String orderNumber, Model model) { // TODO 测试,将订单号写死 orderNumber = "ORDER-00001"; String imgAddress = PictureUrlHelper.genTerminalRootUrl( TerminalType.IOS_RETINA, configService); model.addAttribute("imgAddress", imgAddress); Order order = orderService.findByOrderNumber(orderNumber); List<OrderProduct> products = order.getProductList(); List<FruitProductBo> productBos = new ArrayList<FruitProductBo>(); for (OrderProduct orderProduct : products) { FruitProductBo fruitProductBo = convertToProductBo(fruitProductService .findByProductId(orderProduct.getProductId())); fruitProductBo.setQuantity(orderProduct.getQuantity()); productBos.add(fruitProductBo); } List<OrderProductFree> productsFree = order.getProductsFree(); List<FruitProductBo> productFreeBos = new ArrayList<FruitProductBo>(); for (OrderProductFree orderProductFree : productsFree) { FruitProductBo fruitProductBo = convertToProductBo(fruitProductService .findByProductId(orderProductFree.getProductId())); fruitProductBo.setQuantity(orderProductFree.getQuantity()); productFreeBos.add(fruitProductBo); } model.addAttribute("productFreeBos", productFreeBos); model.addAttribute("productBos", productBos); return "share/orderShare"; } private FruitProductBo convertToProductBo(FruitProduct fruitProduct) { FruitProductBo fruitProductBo = BeanMapper.map(fruitProduct, FruitProductBo.class); fruitProductBo.setDescriptionPicUrl(PictureUrlHelper .toWaterMarkFileName(fruitProductBo.getDescriptionPicUrl(), configService)); fruitProductBo.setPicUrl(PictureUrlHelper.toWaterMarkFileName( fruitProductBo.getPicUrl(), configService)); fruitProductBo.setSeconddiscount(fruitProduct.getDeductForSecond()); return fruitProductBo; } }