package com.boying.controller.phone; import com.boying.common.BaseController; import com.boying.common.SystemConfigProperties; import com.boying.common.util.DateUtil; import com.boying.common.util.StringUtil; import com.boying.entity.*; import com.boying.service.*; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.domain.Specification; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.*; @RestController @RequestMapping("ticket") public class TicketController extends BaseController{ @Autowired private TicketService ticketService; @Autowired private SystemConfigProperties systemConfigProperties; @Autowired private UserService userService; @Autowired private ViolationTypeService violationTypeService; @Autowired private TicketBlackService ticketBlackService; @Autowired private OrderRecordService orderRecordService; @Autowired private StreetLogoService streetLogoService; //图片回显 @RequestMapping(value = "/showImg", method = RequestMethod.GET) public void getAwardAsByteArray(HttpServletResponse response, HttpServletRequest request) throws IOException { String result = request.getParameter("result"); InputStream in = null; in = FileUtils.openInputStream(new File(systemConfigProperties.getUploadImgPath()+result)); response.setContentType(MediaType.IMAGE_PNG_VALUE); ServletOutputStream out = response.getOutputStream(); try { IOUtils.copy(in, out); out.close(); in.close(); } catch (IOException e) { } } @PostMapping("findPage") public Object findPage(int page,int pageSize,Integer type){ Pageable pageable = PageRequest.of(page-1,pageSize, Sort.Direction.DESC,"id"); if(type==null){ Specification specification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) { List list = new ArrayList(); list.add(cb.equal(root.get("status").as(Integer.class), 0)); Predicate[] arr = new Predicate[list.size()]; cq.where(list.toArray(arr)); return null; } }; return success("",ticketService.findPage(pageable,specification)); }else{ Specification specification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) { List list = new ArrayList(); list.add(cb.equal(root.get("type").as(Integer.class), type)); Predicate[] arr = new Predicate[list.size()]; cq.where(list.toArray(arr)); return null; } }; return success("",ticketService.findPage(pageable,specification)); } } @PostMapping("save") public Object save(Ticket ticket,HttpServletRequest request) throws IOException{ String imgStr = ""; MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; List imgs = multipartRequest.getFiles("imgs"); for(MultipartFile imageFile:imgs){ if (imageFile != null) { String fileExtension = FilenameUtils.getExtension(imageFile.getOriginalFilename());//获取后缀 String newFileName = System.currentTimeMillis() + "." + fileExtension;//图片名称 imgStr+=newFileName+","; String newFilePath = systemConfigProperties.getUploadImgPath() + newFileName;//上传路径 File destFile = new File(newFilePath); FileUtils.writeByteArrayToFile(destFile, imageFile.getBytes()); } } ticket.setImg(imgStr); if(ticket.getUserId()!=null){ User user = (User) userService.findById(ticket.getUserId()); ticket.setUserName(user.getName()); } if(ticket.getViolationTypeId()!=null){ ViolationType violationType = (ViolationType) violationTypeService.findById(ticket.getViolationTypeId()); ticket.setViolationTypeName(violationType.getName()); ticket.setMoney(violationType.getMoney()); } if(ticket.getId()==null){ ticket.setCode(System.currentTimeMillis()+""); updateBlackTicket(ticket); } ticket.setStatus(1);//已处理 ticketService.save(ticket); if(StringUtil.isNullOrEmpty(ticket.getQrUrl())){ ticket.setQrUrl(systemConfigProperties.getIp2()+"#/index?id="+ticket.getId()); ticketService.save(ticket); } if(!StringUtil.isNullOrEmpty(ticket.getLng())&&!StringUtil.isNullOrEmpty(ticket.getLat())){ saveLogo(ticket.getAddress(),ticket.getLng(),ticket.getLat()); } return success("保存成功",ticket); } @PostMapping("save2") public Object save2(Ticket ticket) throws IOException{ if(ticket.getUserId()!=null){ User user = (User) userService.findById(ticket.getUserId()); ticket.setUserName(user.getName()); } if(ticket.getViolationTypeId()!=null){ ViolationType violationType = (ViolationType) violationTypeService.findById(ticket.getViolationTypeId()); ticket.setViolationTypeName(violationType.getName()); ticket.setMoney(violationType.getMoney()); } if(ticket.getId()==null){ ticket.setCode(System.currentTimeMillis()+""); } updateBlackTicket(ticket); ticketService.save(ticket); ticket.setQrUrl(systemConfigProperties.getIp2()+"#/index?id="+ticket.getId()); ticket.setType(1); ticketService.save(ticket); return success("保存成功",ticket); } @PostMapping("delete") public Object delete(Long id){ Ticket ticket = (Ticket) ticketService.findById(id); ticket.setStatus(3); ticketService.save(ticket); return success("操作成功"); } public void saveLogo(String name,String lng,String lat){ if(StringUtil.isNullOrEmpty(name)||StringUtil.isNullOrEmpty(lng)){ return; } StreetLogo logo = new StreetLogo(); logo.setLat(lat); logo.setLng(lng); logo.setName(name); streetLogoService.save(logo); } private void updateBlackTicket(Ticket ticket){ Specification specification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) { List list = new ArrayList(); if(!StringUtil.isNullOrEmpty(ticket.getCarNo())){ list.add(cb.equal(root.get("carNo").as(String.class), ticket.getCarNo())); } Predicate[] arr = new Predicate[list.size()]; cq.where(list.toArray(arr)); return null; } }; List all = ticketBlackService.findAll(specification); if(all.size()==0){ TicketBlack tb = new TicketBlack(); tb.setCarNo(ticket.getCarNo()); tb.setCarType(ticket.getCarType()); tb.setColor(ticket.getColor()); tb.setViolationCount(1); ticketBlackService.save(tb); }else { TicketBlack ticketBlack = all.get(0); //ticketBlack.setStatus(1); ticketBlack.setViolationCount(ticketBlack.getViolationCount()+1); ticketBlackService.save(ticketBlack); } } @PostMapping("getZhiFa") public Object getZhiFa(Integer status,int page,int pageSize){ Pageable pageable = PageRequest.of(page-1,pageSize, Sort.Direction.DESC,"id"); Specification specification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) { List list = new ArrayList(); list.add(cb.equal(root.get("status").as(String.class), status)); list.add(cb.equal(root.get("type").as(String.class), 1)); Predicate[] arr = new Predicate[list.size()]; cq.where(list.toArray(arr)); return null; } }; Page page1 = ticketService.findPage(pageable, specification); return success("请求成功",page1); } @PostMapping("getCar") public Object getCar(String carNo,int page,int pageSize,int type){ Pageable pageable = PageRequest.of(page-1,pageSize, Sort.Direction.DESC,"id"); Specification specification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) { List list = new ArrayList(); // if(type==1){ // list.add(cb.equal(root.get("status").as(String.class), type)); // } if(type==2){ list.add(cb.equal(root.get("blackType").as(String.class), 1)); } if(!StringUtil.isNullOrEmpty(carNo)){ list.add(cb.like(root.get("carNo").as(String.class), "%"+carNo+"%")); } Predicate[] arr = new Predicate[list.size()]; cq.where(list.toArray(arr)); return null; } }; Page page1 = ticketBlackService.findPage(pageable,specification); return success("请求成功",page1); } @PostMapping("getCarList") public Object getCarList(String carNo,int page,int pageSize){ Pageable pageable = PageRequest.of(page-1,pageSize, Sort.Direction.DESC,"id"); Specification specification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) { List list = new ArrayList(); if(!StringUtil.isNullOrEmpty(carNo)){ list.add(cb.like(root.get("carNo").as(String.class), "%"+carNo+"%")); } Predicate[] arr = new Predicate[list.size()]; cq.where(list.toArray(arr)); return null; } }; Page page1 = ticketService.findPage(pageable, specification); return success("请求成功",page1); } @PostMapping("noFound") public Object noFound(Long ticketId,String remark){ Ticket ticket = (Ticket) ticketService.findById(ticketId); ticket.setStatus(2); ticket.setRemark(remark); ticketService.save(ticket); return success("请求成功"); } @PostMapping("getStatisticByYear") public Object getStatistic(int year){ Map map = new HashMap<>(); List list = new ArrayList<>(); Date yearFirst = DateUtil.getYearFirst(year); Date yearLast = DateUtil.getYearLast(year); long count = orderRecordService.countByYear(yearFirst,yearLast); Double money = orderRecordService.sumByYear(yearFirst,yearLast); map.put("yearCount",count); map.put("yearMoney",money); Calendar cal = Calendar.getInstance(); int month = cal.get(Calendar.MONTH) + 1; int year2 = cal.get(Calendar.YEAR); if(year==year2){ for(int i=month;i>0;i--){ list.add(getStatisticByMonth(year,i)); } } map.put("list",list); return success("请求成功",map); } public Object getStatisticByMonth(int year,int month){ Map map = new HashMap<>(); map.put("year",year); map.put("month",month); Date yearFirst = DateUtil.getFirstDayOfMonth(year,month); Date yearLast = DateUtil.getLastDayOfMonth(year,month); long count = orderRecordService.countByYear(yearFirst,yearLast); Double money = orderRecordService.sumByYear(yearFirst,yearLast); map.put("monthCount",count); map.put("monthMoney",money); return map; } @PostMapping("getOrderList") public Object getOrderList(int year,int month){ Date yearFirst = DateUtil.getFirstDayOfMonth(year,month); Date yearLast = DateUtil.getLastDayOfMonth(year,month); List page1 = orderRecordService.findAll2(yearFirst,yearLast); return success("请求成功",page1); } @PostMapping("findById") public Object findById(Long id){ Ticket ticket = (Ticket) ticketService.findById(id); if(ticket.getViolationTypeId()!=null){ ViolationType violationType = (ViolationType) violationTypeService.findById(ticket.getViolationTypeId()); ticket.setUserName(violationType.getCode()); ticket.setRemark(violationType.getContent()); } return success("请求成功",ticket); } }