package org.xmx0632.deliciousfruit.service; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import org.springside.modules.persistence.DynamicSpecifications; import org.springside.modules.persistence.SearchFilter; import org.xmx0632.deliciousfruit.api.v1.bo.PromotionRuleResponse; import org.xmx0632.deliciousfruit.api.v1.bo.PromotionRuleResponse.PromotionProductBo; import org.xmx0632.deliciousfruit.api.v1.bo.TerminalType; import org.xmx0632.deliciousfruit.api.v1.helper.DateHelper; import org.xmx0632.deliciousfruit.api.v1.helper.PictureUrlHelper; import org.xmx0632.deliciousfruit.entity.FruitProduct; import org.xmx0632.deliciousfruit.entity.FruitPromotion; import org.xmx0632.deliciousfruit.entity.FruitPromotion.PromotionType; import org.xmx0632.deliciousfruit.global.ConfigConstant; import org.xmx0632.deliciousfruit.repository.FruitPromotionDao; //Spring Bean的标识. @Component @Transactional(readOnly = true) public class FruitPromotionService { private static Logger log = LoggerFactory .getLogger(FruitPromotionService.class); private FruitPromotionDao fruitPromotionDao; @Autowired private ConfigService configService; public FruitPromotion getFruitPromotion(Long id) { return fruitPromotionDao.findOne(id); } public List<FruitPromotion> getAllFruitPromotion() { return (List<FruitPromotion>) fruitPromotionDao.findAll(); } @Transactional(readOnly = false) public void saveFruitPromotion(FruitPromotion entity) { fruitPromotionDao.save(entity); } @Transactional(readOnly = false) public void deleteFruitPromotion(Long id) { fruitPromotionDao.delete(id); } public Page<FruitPromotion> getFruitPromotions( Map<String, Object> filterParams, int pageNumber, int pageSize, String sortType) { PageRequest pageRequest = buildPageRequest(pageNumber, pageSize, sortType); Specification<FruitPromotion> spec = buildSpecification(filterParams); return fruitPromotionDao.findAll(spec, pageRequest); } /** * 创建分页请求. */ private PageRequest buildPageRequest(int pageNumber, int pagzSize, String sortType) { Sort sort = null; if ("auto".equals(sortType)) { sort = new Sort(Direction.DESC, "id"); } /* * else if ("title".equals(sortType)) { sort = new Sort(Direction.ASC, * "title"); } */ return new PageRequest(pageNumber - 1, pagzSize, sort); } private Specification<FruitPromotion> buildSpecification( Map<String, Object> filterParams) { Map<String, SearchFilter> filters = SearchFilter.parse(filterParams); // filters.put("user.id", new SearchFilter("user.id", Operator.EQ, // userId)); Specification<FruitPromotion> spec = DynamicSpecifications .bySearchFilter(filters.values(), FruitPromotion.class); return spec; } // -----------------// // Setter methods // // -----------------// @Autowired(required = true) public void setFruitPromotionDao(FruitPromotionDao fruitPromotionDao) { this.fruitPromotionDao = fruitPromotionDao; } /** * 全场满立减 * * @param type * @return */ public List<PromotionRuleResponse.PromotionTotalBo> getAvailableTotalPriceOff( TerminalType type) { List<FruitPromotion> rules = getAvailablePromotionBy(PromotionType.TOTAL_DEDUCT); List<PromotionRuleResponse.PromotionTotalBo> ruleBos = new ArrayList<PromotionRuleResponse.PromotionTotalBo>(); if (rules != null) { for (FruitPromotion rule : rules) { PromotionRuleResponse.PromotionTotalBo ruleBo = new PromotionRuleResponse.PromotionTotalBo(); ruleBo.setPromotionId(String.valueOf(rule.getId())); ruleBo.setContent("全场满" + rule.getTotalPriceoff() + "元立减" + rule.getPriceoff() + "元"); ruleBo.setPicUrl(PictureUrlHelper.genPictureUrl(type, ConfigConstant.FRUIT_PROMOTION_DIR, rule.getPicUrl(), configService)); ruleBo.setStartTime(DateHelper.getDateString(rule .getStartTime())); ruleBo.setEndTime(DateHelper.getDateString(rule.getEndTime())); ruleBos.add(ruleBo); } } return ruleBos; } /** * 全场满就送 * * @param type * @return */ public List<PromotionRuleResponse.PromotionTotalBo> getAvailableTotalGift( TerminalType type) { List<FruitPromotion> rules = getAvailablePromotionBy(PromotionType.TOTAL_GIFT); List<PromotionRuleResponse.PromotionTotalBo> ruleBos = new ArrayList<PromotionRuleResponse.PromotionTotalBo>(); if (rules != null) { for (FruitPromotion rule : rules) { PromotionRuleResponse.PromotionTotalBo ruleBo = new PromotionRuleResponse.PromotionTotalBo(); if (rule.getFruitSubPromotion() != null) { ruleBo.setPromotionId(String.valueOf(rule.getId())); ruleBo.setContent("全场满" + rule.getTotalGift() + "元送" + rule.getFruitSubPromotion() .getPromotionProductName() + rule.getFruitSubPromotion().getQuantity() + rule.getFruitSubPromotion().getUnit()); ruleBo.setPicUrl(PictureUrlHelper.genPictureUrl(type, ConfigConstant.FRUIT_PROMOTION_DIR, rule.getPicUrl(), configService)); ruleBo.setStartTime(DateHelper.getDateString(rule .getStartTime())); ruleBo.setEndTime(DateHelper.getDateString(rule .getEndTime())); ruleBos.add(ruleBo); } } } return ruleBos; } /** * 某一产品的所有促销规则 * * @param type * @return */ public String getProductGiftInfoByProduct(TerminalType type, FruitProduct product) { log.debug("FruitProduct:{}", product); List<FruitPromotion> rules = getAvailablePromotionBy(PromotionType.PRODUCT_GIFT); String content = ""; if (product == null) return ""; if (rules != null) { for (FruitPromotion rule : rules) { log.debug("rule.getFruitSubPromotion().getProduct():{}", rule .getFruitSubPromotion().getProduct()); if (rule.getFruitSubPromotion() != null && product.getProductId() != null && product .getProductId() .trim() .equals(rule.getFruitSubPromotion() .getProduct().getProductId().trim())) { content += "买" + +rule.getFruitSubPromotion() .getPromotionThreshold() + rule.getFruitSubPromotion().getProduct() .getUnit() + "送" + rule.getFruitSubPromotion() .getPromotionProductName() + rule.getFruitSubPromotion().getQuantity() + rule.getFruitSubPromotion().getUnit() + ","; } } } if (content.endsWith(",")) { content = content.substring(0, content.length() - 1); } return content; } /** * 单品赠送 * * @param type * @return */ public List<PromotionRuleResponse.PromotionProductBo> getAvailableProductGift( TerminalType type) { List<FruitPromotion> rules = getAvailablePromotionBy(PromotionType.PRODUCT_GIFT); List<PromotionRuleResponse.PromotionProductBo> ruleBos = new ArrayList<PromotionRuleResponse.PromotionProductBo>(); if (rules != null) { for (FruitPromotion rule : rules) { PromotionRuleResponse.PromotionProductBo ruleBo = new PromotionRuleResponse.PromotionProductBo(); if (rule.getFruitSubPromotion() != null) { ruleBo.setPromotionId(String.valueOf(rule.getId())); String productId = String .valueOf(rule.getFruitSubPromotion().getProduct() .getProductId()); ruleBo.setProductId(productId); ruleBo.setContent("买" + rule.getFruitSubPromotion().getProduct() .getProductName() + rule.getFruitSubPromotion() .getPromotionThreshold() + rule.getFruitSubPromotion().getProduct() .getSpec() + "送" + rule.getFruitSubPromotion() .getPromotionProductName() + rule.getFruitSubPromotion().getQuantity() + rule.getFruitSubPromotion().getUnit()); ruleBo.setPicUrl(PictureUrlHelper.genPictureUrl(type, ConfigConstant.FRUIT_PROMOTION_DIR, rule.getPicUrl(), configService)); ruleBo.setStartTime(DateHelper.getDateString(rule .getStartTime())); ruleBo.setEndTime(DateHelper.getDateString(rule .getEndTime())); // TODO 临时测试修改:如果productId为9打头的就是水果故事,其他就是水果肖像 String productType = getProductType(productId); ruleBo.setProductType(productType); ruleBo.setE6Price(rule.getFruitSubPromotion().getProduct() .getE6Price()); ruleBo.setProductName(rule.getFruitSubPromotion() .getProduct().getProductName()); ruleBo.setUnit(rule.getFruitSubPromotion().getProduct() .getUnit()); ruleBos.add(ruleBo); } } } return ruleBos; } private String getProductType(String productId) { if (productId.startsWith("9")) { return PromotionProductBo.ProductType.FRUIT_STORY.getValue(); } return PromotionProductBo.ProductType.PRODUCT.getValue(); } public List<FruitPromotion> getAvailablePromotionBy(PromotionType type) { return fruitPromotionDao.getAvailablePromotionBy(new Date(), type.getValue()); } public List<FruitPromotion> getAvailablePromotionBy(Date currentDate, PromotionType type, Long productId) { return fruitPromotionDao.getAvailablePromotionBy(currentDate, type.getValue(), productId); } public List<FruitPromotion> getAvailablePromotionBy(Date currentDate, PromotionType type) { return fruitPromotionDao.getAvailablePromotionBy(currentDate, type.getValue()); } }