package com.goafanti.order.controller;

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

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.order.bo.JtOrderInput;
import com.goafanti.order.bo.JtOrderInput.JtSingleOrder;
import com.goafanti.order.service.JtOrderService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@RestController
@RequestMapping(value = "/api/user/jtOrder")
public class UserJtOrderController extends BaseApiController {
	@Resource
	JtOrderService jtOrderService;
	
	@RequestMapping(value="/apply",method = RequestMethod.POST)
	public Result addNewOrder(String orderList) {
		Result result=new Result();
		Gson gson=new Gson();
		List<JtSingleOrder>orders=gson.fromJson(orderList,new TypeToken<ArrayList<JtSingleOrder>>() {}.getType());
		if(orders==null || orders.size()<=0) {result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","列表不符合规范,")); return result;}
		int addRes=jtOrderService.addOrder(orders);
		if(addRes>=0) {result.setData(orders);return result;}
		else if(addRes==-1) {
			result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","商品id,")); return result;
		}
		else if(addRes==-2) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","商品类别,")); return result;
		}
		else if(addRes==-3) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","提交渠道,")); return result;
		}
		result.setData(orders);
		return result;
	} 
	
	/*
	 * userType-----0-购买者   1-出售者  2-管理员
	 * 
	 * */
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public Result getMyJtOrderList(Integer orderType,String orderNo,String startCreateDate,String endCreateDate,String sellerName,String buyerName,String commodityName,Integer userType,Integer pageNo,Integer pageSize) {
		Result result=new Result();
		if(userType==null || userType>1 || userType<0) { 
			result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","用户类型错误"));
			return result;
		}
		
		result.setData(jtOrderService.searchOrderList(orderType,orderNo, startCreateDate, endCreateDate, sellerName, buyerName, commodityName, userType, pageNo, pageSize)); 
		return result;
	}

}
