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.slf4j.Logger; import org.slf4j.LoggerFactory; 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 Logger logger = LoggerFactory.getLogger(ListController.class); private static final int PAGE_SIZE = 20; // 常量定义为局部常量更合适 @Autowired private ISpellbuyrecordService spellbuyrecordService; @Autowired private ISpellbuyproductService spellbuyproductService; @Autowired private IProducttypeService productTypeService; private static IProducttypeService theProductTypeService; /** * 首页产品列表展示 */ @GetMapping("/index") public String index( @RequestParam(required = false) String id, @RequestParam(required = false) String typeId, @RequestParam(required = false) String pages, @RequestParam(required = false, defaultValue = "1") Integer pageNo, Model model) { // 处理分页参数 if (pages != null) { pageNo = Integer.parseInt(pages.split("_")[1]); } // 处理分类和品牌参数 String tId = null; String brandId = null; String typeName = null; String brandName = null; if (StringUtil.isNotBlank(typeId)) { if (typeId.indexOf("b") != -1) { brandId = typeId.split("b")[1]; tId = typeId.split("b")[0]; typeName = StringUtil.isNotBlank(tId) ? productTypeService.getById(tId).getTypeName() : 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(); List tList = productTypeService.listByBrand(tId); List brandList; int j = 0; for (int i = 0; i < tList.size(); i++) { if (StringUtil.isNotBlank(brandId) && Integer.parseInt(brandId) == tList.get(i).getTypeId()) { j = i; } } if (j > 16 && 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); } // 处理产品列表数据 List productList = new ArrayList<>(); String pageString = ""; int resultCount = 0; if ("hot20".equals(id)) { Pagination hotPage = spellbuyrecordService.ProductByTypeIdList(tId, brandId, "hot", pageNo, PAGE_SIZE); resultCount = hotPage.getResultCount(); productList = convertToProductJSON((List) hotPage.getList()); pageString = buildPageString(resultCount, pageNo, id, tId); } else if ("date20".equals(id)) { Pagination datePage = spellbuyrecordService.ProductByTypeIdList(tId, brandId, "date", pageNo, PAGE_SIZE); resultCount = datePage.getResultCount(); productList = convertToProductJSON((List) datePage.getList()); pageString = buildPageString(resultCount, pageNo, id, tId); } else if ("price20".equals(id)) { Pagination pricePage = spellbuyrecordService.ProductByTypeIdList(tId, brandId, "price", pageNo, PAGE_SIZE); resultCount = pricePage.getResultCount(); productList = convertToProductJSON((List) pricePage.getList()); pageString = buildPageString(resultCount, pageNo, id, tId); } else if ("priceAsc20".equals(id)) { Pagination pricePage = spellbuyrecordService.ProductByTypeIdList(tId, brandId, "priceAsc", pageNo, PAGE_SIZE); resultCount = pricePage.getResultCount(); productList = convertToProductJSON((List) pricePage.getList()); pageString = buildPageString(resultCount, pageNo, id, tId); } else if ("about20".equals(id)) { Pagination aboutPage = spellbuyrecordService.ProductByTypeIdList(tId, brandId, "about", pageNo, PAGE_SIZE); resultCount = aboutPage.getResultCount(); productList = convertToProductJSON((List) aboutPage.getList()); pageString = buildPageString(resultCount, pageNo, id, tId); } else if ("surplus20".equals(id)) { Pagination surplusPage = spellbuyrecordService.ProductByTypeIdList(tId, brandId, "surplus", pageNo, PAGE_SIZE); resultCount = surplusPage.getResultCount(); productList = convertToProductJSON((List) surplusPage.getList()); pageString = buildPageString(resultCount, pageNo, id, tId); } // 向视图传递数据 model.addAttribute("productList", productList); model.addAttribute("producttyList", producttyList); model.addAttribute("brandList", brandList); model.addAttribute("typeName", typeName); model.addAttribute("brandName", brandName); model.addAttribute("pageString", pageString); model.addAttribute("resultCount", resultCount); return "index"; } /** * 检查商品状态接口 */ @GetMapping("/isStatus") @ResponseBody public String isStatus(@RequestParam String id) { Spellbuyproduct spellbuyproduct = spellbuyproductService.findByFKProductId(id); 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"; } /** * 初始化静态Service引用 */ @PostConstruct public void init() { if (this.productTypeService != null) { theProductTypeService = this.productTypeService; } } /** * 静态方法获取产品类型列表 */ public static List getTypeSubList() { return theProductTypeService.getAllProductTypeTree(); } /** * 转换对象数组为ProductJSON列表 */ private List convertToProductJSON(List sourceList) { List result = new ArrayList<>(); for (Object[] item : sourceList) { ProductJSON productJSON = new ProductJSON(); // 根据原代码逻辑,不同排序类型的数组元素顺序可能不同 Spellbuyproduct spellbuyproduct = (Spellbuyproduct) item[0]; Product product = (Product) item[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()); result.add(productJSON); } return result; } /** * 构建分页HTML字符串 */ private String buildPageString(int resultCount, int pageNo, String id, String tId) { String baseUrl = ApplicationListenerImpl.sysConfigureJson.getWwwUrl() + "/list/" + id; if (tId != null && !tId.isEmpty()) { baseUrl += "/" + tId; } return PaginationUtil.getPaginationHtml(resultCount, PAGE_SIZE, pageNo, 2, 5, baseUrl + "/p_"); } }