package com.web.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.web.bean.Order;
import com.web.bean.Organization;
import com.web.bean.Project;
import com.web.bean.ProjectPackage;
import com.web.bean.User;
import com.web.service.OrganizationService;
import com.web.service.PackageService;
import com.web.service.ProjectService;
import com.web.service.user.OrderService;
@Controller
public class HomeController {
@RequestMapping("home")
public ModelAndView index() {
ModelAndView mav = new ModelAndView("home");
PackageService packageService = new PackageService();
List<ProjectPackage> packages = packageService.getPackageByNumber(8);
mav.addObject("packages", packages);
return mav;
}
@RequestMapping("appoint")
public ModelAndView yuyue() {
ModelAndView mav = new ModelAndView("appoint");
PackageService packageService = new PackageService();
List<ProjectPackage> packages = packageService.getPackageByNumber(999);
mav.addObject("packages", packages);
return mav;
}
@RequestMapping("orderlist")
public ModelAndView orderlist(HttpSession session) {
ModelAndView mav = new ModelAndView("orderlist");
String status = "0";
User user = (User) session.getAttribute("currentUser");
OrderService orderService = new OrderService();
if (user != null) {
List<Order> orders = orderService.getOrderByUserId(user.getId());
mav.addObject("orders", orders);
status = "1";
}
mav.addObject("status", status);
return mav;
}
@RequestMapping("orderDetail")
public ModelAndView orderDetail(int orderId,HttpSession session) {
ModelAndView mav = new ModelAndView("orderDetail");
OrderService orderService = new OrderService();
Order order = orderService.getDaoByOrderId(orderId);
PackageService packageService = new PackageService();
ProjectPackage pp = packageService.getPackageById(order.getPackageId());
ProjectService projectService = new ProjectService();
Project pro = projectService.getProjectById(Integer.valueOf(order.getAddtion()));
order.setPrice(pp.getPrice()+pro.getPrice()-10);
mav.addObject("order", order);
mav.addObject("pp",pp);
mav.addObject("pro",pro);
System.out.println("order="+order+"\npp="+pp+"\npro="+pro);
return mav;
}
@RequestMapping("login")
public ModelAndView login() {
ModelAndView mav = new ModelAndView("login");
return mav;
}
@RequestMapping("register")
public ModelAndView register() {
ModelAndView mav = new ModelAndView("register");
return mav;
}
@RequestMapping("logout")
public ModelAndView logout(HttpSession session) {
session.setAttribute("currentUser", null);
ModelAndView mav = new ModelAndView("home");
PackageService packageService = new PackageService();
List<ProjectPackage> packages = packageService.getPackageByNumber(8);
mav.addObject("packages", packages);
return mav;
}
@RequestMapping("buy")
public ModelAndView appointment(int packageid, HttpSession session) {
ModelAndView mav = new ModelAndView("buy");
PackageService packageService = new PackageService();
ProjectPackage package1 = packageService.getPackageById(packageid);
mav.addObject("package1", package1);
ProjectService projectService = new ProjectService();
// 获取额外的项目
List<Project> projects = projectService.getAllProject();
projects = cutListMaxSize(projects);
mav.addObject("pros", projects);
OrganizationService organizationService = new OrganizationService();
List<Organization> organizations = organizationService.getAllOrganization();
organizations = cutListMaxSize1(organizations);
mav.addObject("organizations", organizations);
return mav;
}
public static List<Project> cutListMaxSize(List<Project> list) {
int max = list.size();
int index[] = new int[] { 0, 3, 6, 9, 12 };
List<Project> res = new ArrayList<>();
for (int i = 0; i < index.length; i++) {
res.add(list.get(index[i] + new Random().nextInt(3)));
}
return res;
}
public static List<Organization> cutListMaxSize1(List<Organization> list) {
int max = list.size();
int index[] = new int[] { 0, 3, 6, 9, 12 };
List<Organization> res = new ArrayList<>();
for (int i = 0; i < index.length; i++) {
res.add(list.get(index[i] + new Random().nextInt(3)));
}
return res;
}
}