package me.mofun.controller; import me.mofun.entity.Product; import me.mofun.entity.Producttype; import me.mofun.entity.Spellbuyproduct; import me.mofun.entity.pojo.ProductJSON; import me.mofun.entity.vo.Pagination; import me.mofun.service.IProducttypeService; import me.mofun.service.ISpellbuyproductService; import me.mofun.service.ISpellbuyrecordService; import me.mofun.test.tree.MenuNode; import me.mofun.util.ApplicationListenerImpl; import me.mofun.util.PaginationUtil; import me.mofun.util.StringUtil; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.PostConstruct; import java.util.ArrayList; import java.util.List; @Controller @RequestMapping("/list") public class ListController { private static final long serialVersionUID = 8452833122481904678L; @Autowired private ISpellbuyrecordService spellbuyrecordService; @Autowired private ISpellbuyproductService spellbuyproductService; @Autowired private IProducttypeService productTypeService; private static IProducttypeService theProductTypeService; private Logger logger = Logger.getLogger(this.getClass()); @GetMapping("/index") public String index( @RequestParam(value = "id", required = false) String id, @RequestParam(value = "typeId", required = false) String typeId, @RequestParam(value = "pages", required = false) String pages, @RequestParam(value = "pageNo", defaultValue = "1") int pageNo, Model model) { // 处理页码 if (pages != null) { pageNo = Integer.parseInt(pages.split("_")[1]); } String tId = null; String brandId = null; String typeName = null; String brandName = null; // 处理类型和品牌ID if (StringUtil.isNotBlank(typeId)) { if (typeId.indexOf("b") != -1) { brandId = typeId.split("b")[1]; tId = typeId.split("b")[0]; if (StringUtil.isNotBlank(tId)) { typeName = productTypeService.getById(tId).getTypeName(); } else { typeName = productTypeService.getById("1000").getTypeName(); } brandName = productTypeService.findBrandById(brandId).getTypeName(); } else { tId = typeId; typeName = productTypeService.getById(tId).getTypeName(); } } else { typeName = productTypeService.getById("1000").getTypeName(); } // 处理产品类型列表 List producttyList = productTypeService.listByProductList(); model.addAttribute("producttyList", producttyList); // 处理品牌列表 List tList = productTypeService.listByBrand(tId); int j = 0; for (int i = 0; i < tList.size(); i++) { if (StringUtil.isNotBlank(brandId)) { if (Integer.parseInt(brandId) == tList.get(i).getTypeId()) { j = i; } } } List brandList; if (j > 16) { if (StringUtil.isNotBlank(brandId)) { brandList = new ArrayList<>(); for (Producttype type : tList) { if (Integer.parseInt(brandId) == type.getTypeId()) { brandList.add(type); } } for (Producttype type : tList) { if (Integer.parseInt(brandId) != type.getTypeId()) { brandList.add(type); } } } else { brandList = productTypeService.listByBrand(tId); } } else { brandList = productTypeService.listByBrand(tId); } model.addAttribute("brandList", brandList); // 处理产品列表数据 List productList = new ArrayList<>(); int resultCount = 0; String pageString = ""; int pageSize = 20; if ("hot20".equals(id)) { Pagination hotPage = spellbuyrecordService.ProductByTypeIdList(tId, brandId, "hot", pageNo, pageSize); List hotList = (List) hotPage.getList(); productList = buildProductJSONList(hotList); resultCount = hotPage.getResultCount(); pageString = buildPageString(resultCount, pageSize, pageNo, id, tId); } else if ("date20".equals(id)) { Pagination datePage = spellbuyrecordService.ProductByTypeIdList(tId, brandId, "date", pageNo, pageSize); List dateList = (List) datePage.getList(); productList = buildProductJSONList(dateList); resultCount = datePage.getResultCount(); pageString = buildPageString(resultCount, pageSize, pageNo, id, tId); } else if ("price20".equals(id)) { Pagination pricePage = spellbuyrecordService.ProductByTypeIdList(tId, brandId, "price", pageNo, pageSize); List priceList = (List) pricePage.getList(); productList = buildProductJSONList(priceList); resultCount = pricePage.getResultCount(); pageString = buildPageString(resultCount, pageSize, pageNo, id, tId); } else if ("priceAsc20".equals(id)) { Pagination pricePage = spellbuyrecordService.ProductByTypeIdList(tId, brandId, "priceAsc", pageNo, pageSize); List priceList = (List) pricePage.getList(); productList = buildProductJSONList(priceList); resultCount = pricePage.getResultCount(); pageString = buildPageString(resultCount, pageSize, pageNo, id, tId); } else if ("about20".equals(id)) { Pagination aboutPage = spellbuyrecordService.ProductByTypeIdList(tId, brandId, "about", pageNo, pageSize); List aboutList = (List) aboutPage.getList(); productList = buildProductJSONList(aboutList); resultCount = aboutPage.getResultCount(); pageString = buildPageString(resultCount, pageSize, pageNo, id, tId); } else if ("surplus20".equals(id)) { Pagination surplusPage = spellbuyrecordService.ProductByTypeIdList(tId, brandId, "surplus", pageNo, pageSize); List surplusList = (List) surplusPage.getList(); productList = buildProductJSONList(surplusList); resultCount = surplusPage.getResultCount(); pageString = buildPageString(resultCount, pageSize, pageNo, id, tId); } // 将数据添加到模型 model.addAttribute("productList", productList); model.addAttribute("resultCount", resultCount); model.addAttribute("pageString", pageString); model.addAttribute("typeName", typeName); model.addAttribute("brandName", brandName); model.addAttribute("tId", tId); model.addAttribute("brandId", brandId); return "index"; } /** * 构建产品JSON列表 */ private List buildProductJSONList(List objectList) { List productList = new ArrayList<>(); for (Object[] obj : objectList) { ProductJSON productJSON = new ProductJSON(); Product product; Spellbuyproduct spellbuyproduct; // 根据原代码逻辑处理对象转换(兼容jdk1.8的调整) if ("hot20".equals(2) || "date20".equals(1)) { product = (Product) obj[1]; spellbuyproduct = (Spellbuyproduct) obj[0]; } else { product = (Product) obj[0]; spellbuyproduct = (Spellbuyproduct) obj[1]; } productJSON.setCurrentBuyCount(spellbuyproduct.getSpellbuyCount()); productJSON.setHeadImage(product.getHeadImage()); productJSON.setProductId(spellbuyproduct.getFkProductId()); productJSON.setProductName(product.getProductName()); productJSON.setProductPrice(spellbuyproduct.getSpellbuyPrice()); productJSON.setSinglePrice(spellbuyproduct.getSpSinglePrice()); productJSON.setLogicURL(product.getLogicURL()); productJSON.setProductTitle(product.getProductTitle()); productJSON.setProductStyle(product.getStyle()); productList.add(productJSON); } return productList; } /** * 构建分页字符串 */ private String buildPageString(int resultCount, int pageSize, int pageNo, String id, String tId) { String baseUrl = ApplicationListenerImpl.sysConfigureJson.getWwwUrl() + "/list/" + id; if (tId != null && !tId.isEmpty()) { baseUrl += "/" + tId; } return PaginationUtil.getPaginationHtml(resultCount, pageSize, pageNo, 2, 5, baseUrl + "/p_"); } /** * 查询商品是否满员 * 请求地址: /list/isStatus?id=xxx */ @GetMapping("/isStatus") @ResponseBody public String isStatus(@RequestParam("id") String productId) { Spellbuyproduct spellbuyproduct = spellbuyproductService.findByFKProductId(productId); if (spellbuyproduct.getSpStatus() == 1) { return "false"; } else { int surplus = spellbuyproduct.getSpellbuyPrice() - spellbuyproduct.getSpellbuyCount(); return surplus == 0 ? "false" : String.valueOf(surplus); } } /** * 首页左侧产品类目列表 */ @GetMapping("/getProductTypeSubList") public String getProductTypeSubList(Model model) { List menuNodeList = productTypeService.getAllProductTypeTree(); logger.info("menuNodeList.size=" + menuNodeList.size()); model.addAttribute("menuNodeList", menuNodeList); return "getMenuList"; } @PostConstruct public void init() { if (this.productTypeService != null) { theProductTypeService = this.productTypeService; } } public static List getTypeSubList() { return theProductTypeService.getAllProductTypeTree(); } // 仅保留服务类的getter(如果有外部依赖) public IProducttypeService getTheProductTypeService() { return theProductTypeService; } }