package com.yc.education.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.yc.education.model.Classify;
import com.yc.education.model.Product;
import com.yc.education.service.ClassifyService;
import com.yc.education.service.ProductService;

@Controller
public class UserController {
	
	@Autowired
	private ProductService productService;
	
	@Autowired
	private ClassifyService classifyService;
	
	//用户请求的地址：http://localhost/gongshi/user/caps_list.html
	//在productController与UserController中，将商品总数量直接写死了，未进行动态查询
	
	@RequestMapping("/classifyList.html")
	public String classifyList(Model model) {
		List<Classify> list = classifyService.findAllClassify();
		model.addAttribute("bean1",list);
		return "/user/too_classify";
	}
	
	@RequestMapping("/index.html")
	public String findAllProduct(@RequestParam(value="uid",defaultValue="0")Integer uid,String name,Model model) {
		Integer classifyid = 0;//分类类型默认为0
		if(uid!=0||uid!=null) {
			classifyid = uid;
		}
		List<Product> pB=productService.findProductByClassify(classifyid,name);
		model.addAttribute("bean",pB);
		return "/user/caps_list";
	}
	
	@RequestMapping("/caps_detail.html")
	public String capsDetail(Integer pid) {
		int count = productService.fingdCount();
		if(pid<=count) {
			for(int i=1;i<=count;i++) {
				if(i==pid) {
					return "/user/caps_detail"+i;
				}
			}
			return "/user/caps_list";
		}else {
			return "/user/caps_list";}
	}
	
	

}
