package cn.cxs.controller;

import java.util.ArrayList;
import java.util.List;



import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.cxs.demain.Order;

@Controller
public class OrderController {
	private String callback;
	/**
	 * 接收http请求，响应订单集合
	 */
	@RequestMapping("/loadOrderList")
	public String loadOrderList(String uid,Model model) {
		System.out.println("haha");
		Order o1=new Order();
		o1.setDate("2020-04-07");
		o1.setId("111");
		o1.setTotal(1000.0);
		
		Order o2=new Order();
		o2.setDate("2020-04-08");
		o2.setId("1112");
		o2.setTotal(1001.0);
		
		Order o3=new Order();
		o3.setDate("2020-04-09");
		o3.setId("1113");
		o3.setTotal(1002.0);
		
		return "index.jsp";
	}
	
	/**
	 * 接收http请求，响应订单集合，完成的是异步响应
	 * 将list集合序列化为json字符串响应
	 */
	@RequestMapping("/loadOrderList2")
	@ResponseBody
	public List<Order> loadOrderList2(String uid) {
		System.out.println("uid="+uid);
		Order o1=new Order();
		o1.setDate("2020-04-07");
		o1.setId("111");
		o1.setTotal(1000.0);
		
		Order o2=new Order();
		o2.setDate("2020-04-08");
		o2.setId("1112");
		o2.setTotal(1001.0);
		
		Order o3=new Order();
		o3.setDate("2020-04-09");
		o3.setId("1113");
		o3.setTotal(1002.0);
		
		List<Order>list=new ArrayList<>();
		list.add(o1);
		list.add(o2);
		list.add(o3);
		return list;
	}
	/**
	 * 接收jsonp请求，响应js的字符串到客户端
	 * @param uid
	 * @param callback
	 * @return
	 * 
	 */
	@RequestMapping("/loadOrderList3")
	@ResponseBody
	public String loadOrderList3(String uid) {
		System.out.println("uid="+uid);
		Order o1=new Order();
		o1.setDate("2020-04-07");
		o1.setId("111");
		o1.setTotal(1000.0);
		
		Order o2=new Order();
		o2.setDate("2020-04-08");
		o2.setId("1112");
		o2.setTotal(1001.0);
		
		Order o3=new Order();
		o3.setDate("2020-04-09");
		o3.setId("1113");
		o3.setTotal(1002.0);
		
		List<Order>list=new ArrayList<>();
		list.add(o1);
		list.add(o2);
		list.add(o3);
		
		
		//result是需要需要到客户端的js代码
		String result =callback+"("+JSON.toJSONString(list)+")";
		return result;
	}
	
}
