package com.ssm.controller;


import com.ssm.Utils.PageBean;
import com.ssm.entity.Demo;
import com.ssm.service.DemoService;
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 org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("demo")
public class DemoController {

	@Autowired
	public DemoService demoService;
	@RequestMapping("findAllByPage")
	public ModelAndView findAllByPage(@RequestParam int page){
		ModelAndView mv=new ModelAndView("admin/demo/list");
		PageBean<Demo> allProPageBean= demoService.getDemoAll(page);
		System.out.println(allProPageBean);
		mv.addObject("allProPageBean",allProPageBean);
		return mv;
	}
	//后台通过demoid查询
	@RequestMapping("findById")
	public String getDemoById(Integer did, Model model){
		Demo demo= demoService.getDemoById(did);
		model.addAttribute("findByDid", demo);
		return "admin/demo/look";
	}
	//添加demo
	@RequestMapping("add")
	public ModelAndView add(){
		ModelAndView mv=new ModelAndView("admin/demo/add");
		return mv;
	}
	//添加成功
	@RequestMapping("addOk")
	public String addok(Demo demo, HttpServletRequest request) throws Exception {
		System.out.println(demo);
		demoService.addDemo(demo);
		return "redirect:/demo/findAllByPage.action?page=1";
	}
	//调出更新demo
	@RequestMapping("updateById")
	public String updateById(Integer did, Model model){
		Demo demo=demoService.getDemoById(did);
		model.addAttribute("findByDid", demo);
		return "admin/demo/edit";
	}
	//demo更新成功
	@RequestMapping("updateOk")
	public String updateOk(Demo demo, HttpServletRequest request) throws Exception{
		System.out.println(demo);
		demoService.updateDemo(demo);
		return "redirect:/demo/findAllByPage.action?page=1";
	}
	//删除商品
	@RequestMapping("deleteById")
	public String deleteById(Integer did){
		demoService.deleteDemoById(did);
		return "redirect:/demo/findAllByPage.action?page=1";
	}

	//后台通过条件搜索商品信息
	@RequestMapping("searchDemoByCondition")
	public ModelAndView searchDemoByCondition(@RequestParam String condition){
		ModelAndView mv=new ModelAndView("admin/demo/search");
		List<Demo> demoList= demoService.searchDemoByCondition(condition);
		mv.addObject("srList",demoList);
		return mv;
	}

	//前台通过id查询demo信息
	@RequestMapping("/findDemoByDid")
	public String demoFindByDid(@RequestParam int did,Model model) throws Exception {
		System.out.println("前台通过id查询商品信息");
		Demo demo= demoService.getDemoById(did);
		model.addAttribute("demo", demo);
		return "demo";
	}
}
