package com.example.demo.controller; import com.example.demo.Constants; import com.example.demo.model.Diancan; import com.example.demo.model.DiancanForm; import com.example.demo.model.Menu; import com.example.demo.repository.DiancanRespository; import com.example.demo.repository.MenuRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; import javax.persistence.criteria.Predicate; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; import java.util.List; @Controller @RequestMapping("/api") public class WebController { @Autowired private MenuRepository menuRepository; @Autowired private DiancanRespository diancanRespository; @RequestMapping("/index") public String index(Model model,Diancan diancan){ List diancans= diancanRespository.findAll((root,query,cb)->{ List predicates = new ArrayList<>(); if(!StringUtils.isEmpty(diancan.getZhuohao())){ predicates.add(cb.like(root.get("zhuohao"),"%"+diancan.getZhuohao()+"%")); } if(!StringUtils.isEmpty(diancan.getXingming())){ predicates.add(cb.like(root.get("xingming"),"%"+diancan.getXingming()+"%")); } if(!StringUtils.isEmpty(diancan.getTel())){ predicates.add(cb.equal(root.get("tel"),diancan.getTel())); } if(diancan.getYuding()!=null){ predicates.add(cb.equal(root.get("yuding"),diancan.getYuding())); } return query.where(predicates.toArray(new Predicate[0])).getRestriction(); }); model.addAttribute(Constants.LIST,diancans); if(diancan.getYuding()==null){ diancan.setYuding(true); } model.addAttribute(Constants.QUERY,diancan); return "index"; } @RequestMapping("/order/del/{id}") public void del(Model model, @PathVariable String id, HttpServletResponse response) throws IOException { diancanRespository.deleteById(id); response.sendRedirect("/api/index"); } @GetMapping("/order") public String getOrder(Model model){ model.addAttribute(Constants.SPAN_NAME,"点餐画面"); model.addAttribute(Constants.FENLEI,menuRepository.getFenlei()); return "order"; } @PostMapping("/order") public void postOrder(Model model, HttpServletResponse response, DiancanForm diancanForm) throws IOException { response.sendRedirect("/api/index"); } @GetMapping("/order/{id}") public String getEditOrder(Model model,@PathVariable String id){ model.addAttribute(Constants.SPAN_NAME,"修改画面"); return "order"; } @PostMapping("/order/{id}") public void postEditOrder(Model model,@PathVariable String id,HttpServletResponse response) throws IOException { response.sendRedirect("/api/index"); } @GetMapping("/order/detail/{id}") public String getDetailOrder(Model model,@PathVariable String id){ model.addAttribute(Constants.SPAN_NAME,"详细画面"); return "order"; } @GetMapping("/order/pay/{id}") public String getPayOrder(Model model,@PathVariable String id){ model.addAttribute(Constants.SPAN_NAME,"支付画面"); return "order"; } @GetMapping(path = "/getPinMu",produces = {"application/json;charset=UTF-8"}) @ResponseBody public List getPinMu(Menu menu){ if( !StringUtils.isEmpty(menu.getFenlei())){ return menuRepository.getPinmuByFenlei(menu.getFenlei()); }else{ return new ArrayList<>(); } } @GetMapping(path = "/getMenu",produces = {"application/json;charset=UTF-8"}) @ResponseBody public List getMenu(Menu menu){ if( !StringUtils.isEmpty(menu.getFenlei())&&!StringUtils.isEmpty(menu.getPinmu())){ return menuRepository.findByFenleiAndPinmu(menu.getFenlei(),menu.getPinmu()); }else{ return new ArrayList<>(); } } @GetMapping(path = "/hasYuding",produces = {"application/json;charset=UTF-8"}) @ResponseBody public Boolean hasYuding(Diancan diancan){ if(!StringUtils.isEmpty(diancan.getZhuohao())){ return diancanRespository.countByZhuohao(diancan.getZhuohao())==1; }else{ return false; } } }