package com.bw.controller;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.apache.bcel.classfile.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
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.multipart.MultipartFile;
import com.alibaba.fastjson.JSON;
import com.bw.bean.Sedan;
import com.bw.service.SedanService;
import com.bw.utils.HLUtils;
import com.github.pagehelper.PageInfo;
@Controller
public class MyController {
@Autowired
SedanService sedanService;
@Autowired
RedisTemplate redisTemplate;
@Autowired
ElasticsearchTemplate elasticsearchTemplate;
@Autowired
KafkaTemplate<String, String> kafkaTemplate;
@RequestMapping("sousuo")
public Object sousuo(Model model,String keyword,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize) {
PageInfo<Sedan> info = (PageInfo<Sedan>) HLUtils.findByHighLight(elasticsearchTemplate, Sedan.class, pageNum, pageSize, new String[] {"name","typea"}, "id", keyword);
model.addAttribute("info", info);
model.addAttribute("key", keyword);
return "sousuo";
}
@SuppressWarnings("unchecked")
@RequestMapping("list")
public String list(Model model,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize) {
if (pageNum>1) {
PageInfo<Sedan> info = sedanService.info(pageNum, pageSize);
model.addAttribute("info", info);
}
if (pageNum==1) {
//先看redis中有没有
List<Sedan> list = redisTemplate.opsForList().range("sedan", 0, -1);
if (list==null||list.size()==0) {
PageInfo<Sedan> info = sedanService.info(pageNum, pageSize);
System.err.println("从mysql中查询了数据!!!!!!!!!!!!!");
redisTemplate.opsForList().leftPushAll("sedan", info.getList().toArray());
model.addAttribute("info", info);
}else {
//redis数据不为空
System.err.println("从redis中查出了数据");
PageInfo<Sedan> info = sedanService.info(pageNum, pageSize);
model.addAttribute("info", info);
}
}
return "list";
}
@RequestMapping("toadd")
public Object toadd() {
return "add";
}
@RequestMapping("save")
public Object add(Sedan sedan,HttpServletRequest request,MultipartFile myFile) throws IllegalStateException, IOException {
String startName = UUID.randomUUID().toString();
String url=request.getRealPath("/load/");
File file = new File(url+startName);
myFile.transferTo(file);
sedan.setPic(startName);
sedanService.save(sedan);
String jsonString = JSON.toJSONString(sedan);
kafkaTemplate.send("sedan","add="+jsonString);
System.err.println("发送卡夫卡消息!!!!!");
return "redirect:list";
}
@RequestMapping("toUpdate")
public Object toUpdate(int id,Model model) {
Sedan sedan=sedanService.selectOne(id);
model.addAttribute("sedan", sedan);
return "update";
}
@RequestMapping("update")
public Object update(Sedan sedan,HttpServletRequest request,MultipartFile myFile) throws IllegalStateException, IOException {
String startName = UUID.randomUUID().toString();
String url=request.getRealPath("/load/");
File file = new File(url+startName);
myFile.transferTo(file);
sedan.setPic(startName);
sedanService.update(sedan);
String jsonString = JSON.toJSONString(sedan);
kafkaTemplate.send("sedan","update="+jsonString);
return "redirect:list";
}
@RequestMapping("seare")
public Object seare(Model model,int id) {
Sedan sedan = sedanService.selectOne(id);
model.addAttribute("sedan", sedan);
return "show";
}
}