package gms.cuit.controller;
import gms.cuit.entity.*;
import gms.cuit.service.AdminService;
import gms.cuit.utils.ExportExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.Principal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
@RestController()
@RequestMapping("/admin")
public class AdminController {
@Autowired
private ServletContext servletContext;
@Autowired
AdminService adminService;
/*
* 注意: 查询使用get, 其它使用post!!!!! restful规范!!!
* */
@GetMapping("/getAdminName")
public Gms_Admin getAdminName(Principal principal){
Gms_Admin admin = new Gms_Admin();
admin.setAdmin_Username(principal.getName());
return admin;
}
/*
* 进行密码更新,最后返回空json代表更新成功了
* */
@PostMapping("/updatePassword")
public String updatePassword(@RequestBody Map<String,String> map){
String username = map.get("username");
String newpassword = map.get("newpassword");
adminService.updatePassword(username,newpassword);
return "{}";
}
/**
* 一次把summary页面所需所有参数以json形式返回
* @return summaryTodayVistors,summaryTodayOrderProfit,summaryTodayOrderCount,
* summaryOrderCountByDateAndVen,summaryVenueVisDateByDate,summaryVenueVisCountByDate,
* latestOrderList
*/
@GetMapping("/getSummary")
public Map<String,Object> getSummary(){
Map<String,Object> map = new HashMap<>();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
String currentDate = dateFormat.format( new Date());
//获取今天订单利润
Double summaryTodayOrderProfit = adminService.getSummaryTodayOrderProfit(currentDate);
//获取今天订单人数
Integer summaryTodayOrderCount = adminService.getSummaryTodayOrderCount(currentDate);
//获取今天订单按场馆分类
String summaryOrderCountByDateAndVen = adminService.getSummaryOrderCountByDateAndVen(currentDate);
//获取近期的访问量日期
String summaryVenueVisDateByDate = "";
String summaryVenueVisCountByDate = "";
//获取今天访问量
Integer summaryTodayVistors = null;
for(int i=-6;i<=0;i++) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DAY_OF_MONTH, i);
summaryVenueVisDateByDate += sdf.format(calendar.getTime());//日期拼接 05-20
Integer vvInteger = (Integer) servletContext.getAttribute(dateFormat.format(calendar.getTime()));// 2020-05-20
if(vvInteger==null) vvInteger= (new Random().nextInt(80)) + 10;//随机访问量
servletContext.setAttribute(dateFormat.format(calendar.getTime()), vvInteger);
summaryVenueVisCountByDate += vvInteger.toString();
if(i!=0) {
summaryVenueVisDateByDate += " ";
summaryVenueVisCountByDate += " ";
}
if(i==0) summaryTodayVistors = vvInteger;
}
//获取最近订单
int get_count = 5;
List<Gms_Order> latestOrderList = adminService.getLatestOrderList(get_count);
map.put("summaryTodayOrderProfit",summaryTodayOrderProfit);
map.put("summaryTodayOrderCount",summaryTodayOrderCount);
map.put("summaryOrderCountByDateAndVen",summaryOrderCountByDateAndVen);
map.put("summaryVenueVisDateByDate",summaryVenueVisDateByDate);
map.put("summaryVenueVisCountByDate",summaryVenueVisCountByDate);
map.put("summaryTodayVistors",summaryTodayVistors);
map.put("latestOrderList",latestOrderList);
System.out.println(map);
return map;
}
/**
* 一次把venue页面所需所有参数以json形式返回
* @return query_key,currentPage,currentCount,
* totalCount,totalPage,list
* 使用post是因为要传递json
*/
@PostMapping("/getVenue")
public Map<String,Object> getVenue(@RequestBody Map<String,String> reqmap){
Map<String,Object> map = new HashMap<>();
int currentPage = 1;
int currentCount = 5;
String currentPageStr = reqmap.get("currentPage");
String currentCountStr = reqmap.get("currentCount");
if(currentPageStr!=null&¤tPageStr.trim().equals("")==false) currentPage = Integer.parseInt(currentPageStr);
if(currentCountStr!=null&¤tCountStr.trim().equals("")==false) currentCount = Integer.parseInt(currentCountStr);
String query_key = reqmap.get("query_key");
if(query_key==null) query_key="";
PageBean<Gms_Venue> pageBean_venue = adminService.queryVenueByKey(currentPage, currentCount, query_key);
map.put("query_key", query_key);
map.put("currentPage",pageBean_venue.getCurrentPage());
map.put("currentCount",pageBean_venue.getCurrentCount());
map.put("totalCount",pageBean_venue.getTotalCount());
map.put("totalPage",pageBean_venue.getTotalPage());
map.put("list",pageBean_venue.getList());
return map;
}
/**
* 添加Venue
* @return
*/
@PostMapping("/addVenue")
public String addVenue(@RequestBody Gms_Venue gms_venue){
gms_venue.setVenue_Id(UUID.randomUUID().toString().replace("-", ""));
adminService.addVenue(gms_venue);
return "{}";
}
/**
* 修改Venue
* @return
*/
@PostMapping("/updateVenue")
public String updateVenue(@RequestBody Gms_Venue gms_venue){
adminService.updateVenue(gms_venue);
return "{}";
}
/**
* 删除Venue
* @return
*/
@PostMapping("/delVenue")
public String delVenue(@RequestBody Gms_Venue gms_venue){
adminService.delVenue(gms_venue);
return "{}";
}
/**
* 一次把order页面所需所有参数以json形式返回
* @return query_key,currentPage,currentCount,
* totalCount,totalPage,sortStatus,list
* 使用post是因为要传递json
*/
@PostMapping("/getOrder")
public Map<String,Object> getOrder(@RequestBody Map<String,String> reqmap){
Map<String,Object> map = new HashMap<>();
int currentPage = 1;
int currentCount = 5;
String currentPageStr = reqmap.get("currentPage");
String currentCountStr = reqmap.get("currentCount");
if(currentPageStr!=null&¤tPageStr.trim().equals("")==false) currentPage = Integer.parseInt(currentPageStr);
if(currentCountStr!=null&¤tCountStr.trim().equals("")==false) currentCount = Integer.parseInt(currentCountStr);
String query_key = reqmap.get("query_key");
if(query_key==null) query_key="";
//分页数据处理
PageBean<Gms_Order> pageBean_order = new PageBean<Gms_Order>();
pageBean_order.setCurrentPage(currentPage);
pageBean_order.setCurrentCount(currentCount);
int totalCount = adminService.getOrderTotalCountByKeyQuery(query_key);
pageBean_order.setTotalCount(totalCount);
int totalPage = (int) Math.ceil(1.0*totalCount/currentCount);
pageBean_order.setTotalPage(totalPage);
int index = (currentPage-1)*currentCount;
String sortStatus = reqmap.get("sortStatus");
if(sortStatus==null) sortStatus="7";
pageBean_order.setList(adminService.queryOrderByKey(index, currentCount, query_key, sortStatus));
map.put("sortStatus",sortStatus);
map.put("query_key", query_key);
map.p