package com.goafanti.app.controller;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.goafanti.comment.bo.CommentInput;
import com.goafanti.comment.controller.UserCommentController;
import com.goafanti.comment.service.CommentService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.model.JtConsultOrder;
import com.goafanti.order.bo.JtOrderInput.JtSingleOrder;
import com.goafanti.order.enums.CommodityType;
import com.goafanti.order.service.JtConsultOrderService;
import com.goafanti.order.service.JtOrderService;
import com.goafanti.order.service.OrderService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@RestController
@RequestMapping(value = "/app/order")
public class AppOrderController extends CertifyApiController{
	
	@Resource
	private OrderService orderServiceImpl;
	@Resource
	private CommentService	commentService;
	@Resource
	private JtOrderService	jtOrderService;
	@Resource 
	private JtConsultOrderService jtConsultOrderService;
	/**
	 * 进入添加意向订单
	 * @param commodityId 商品编号
	 * @param commodityType 订单类型
	 * @return
	 */
	@RequestMapping(value = "/toAddIntention" , method = RequestMethod.GET)
	public Result toAddIntention(String commodityId,Integer commodityType){
		Result res = new Result();
		if(StringUtils.isBlank(commodityId) || null == commodityType){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","商品编号和商品类型"));
			return res;
		}
		if(!CommodityType.containsType(commodityType)){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","订单类型错误"));
			return res;
		}
		res.setData(orderServiceImpl.selectCommodityDetail(commodityId, commodityType));
		return res;
	}
	
	/**
	 * 添加意向订单
	 * @param orderNo 订单编号
	 * @param commodityId 商品id
	 * @param commodityMode 商品规格
	 * @param createTime 创建时间
	 * @param commodityQuantity 商品数量
	 * @param commodityType 商品类型
	 * @return 
	 * @throws ParseException 
	 * @throws NumberFormatException 
	 */
	@RequestMapping(value = "/addIntention" , method = RequestMethod.POST)
	public Result addIntention(String orderNo,String commodityId,String commodityMode,Integer commodityQuantity,Integer commodityType,
			String createTime,String remarks) throws NumberFormatException, ParseException {
		Result res = new Result();
		if(StringUtils.isBlank(orderNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单编号"));
			return res;
		}
		if(StringUtils.isBlank(commodityId)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","商品id"));
			return res;
		}
		if(StringUtils.isBlank(createTime)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单时间"));
			return res;
		}
		if(commodityQuantity == null){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单数量"));
			return res;
		}
		if(commodityType == null){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单类型"));
			return res;
		}
		if(!CommodityType.containsType(commodityType)){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","订单类型"));
			return res;
		}
		orderServiceImpl.addIntention(orderNo,commodityId,commodityMode,commodityQuantity,commodityType,createTime,remarks);
		return res;
	}
	
	/**
	 * 查看订单列表
	 * @param confirm  我的意向-false 我的订单 - true
	 * @return
	 */
	@RequestMapping(value = "/orderList", method = RequestMethod.POST)
	public Result orderList(boolean confirm,Integer pageNo,Integer pageSize) {
		Result res = new Result();
		res.setData(orderServiceImpl.selectOrderList(confirm,pageNo,pageSize));
		return res;
	}
	
	/**
	 * 查看订单详情
	 * @return
	 */
	@RequestMapping(value ="/orderDetails", method = RequestMethod.GET)
	public Result orderDetails(String orderNo) {
		Result res = new Result();
		if(StringUtils.isBlank(orderNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","订单编号"));
			return res;
		}
		res.setData(orderServiceImpl.selectOrderDetail(orderNo));
		return res;
	}
	
	/**
	 * 订单支付(首付)
	 * @param orderNo 订单编号
	 * @param fundAmount 支付金额
	 * @return 备注
	 */
	@RequestMapping(value = "/payForFirst", method = RequestMethod.POST)
	public Result payForFirst(String orderNo,String fundAmount,String remarks) {
		Result res = new Result();
		if(StringUtils.isBlank(orderNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单编号"));
			return res;
		}
		if(StringUtils.isBlank(fundAmount)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","支付金额"));
			return res;
		}
		orderServiceImpl.updatePayForFirst(orderNo,new BigDecimal(fundAmount),remarks);
		return res;
	}
	
	/**
	 * 支付尾款
	 * @param orderNo 订单编号
	 * @param fundAmount 支付金额
	 * @param remarks 备注
	 * @return
	 */
	@RequestMapping(value = "/payForLast", method = RequestMethod.POST)
	public Result payForLast(String orderNo,String fundAmount,String remarks){
		Result res = new Result();
		if(StringUtils.isBlank(orderNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单编号"));
			return res;
		}
		if(StringUtils.isBlank(fundAmount)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","支付金额"));
			return res;
		}
		orderServiceImpl.updatePayForLast(orderNo,new BigDecimal(fundAmount),remarks);
		return res;
	}
	
	/**
	 * 查询支付流水列表
	 * @param orderNo
	 * @return
	 */
	@RequestMapping(value = "/fundingHistory", method = RequestMethod.POST)
	public Result fundingHistory(String orderNo,Integer pageNo,Integer pageSize) {
		Result res = new Result();
		if(StringUtils.isNotBlank(orderNo))
			res.setData(orderServiceImpl.selectOrderBillList(orderNo,pageNo,pageSize));
		else
			res.setData(orderServiceImpl.selectOrderBillList(null,pageNo,pageSize));
		return res;
	}
	
	/**
	 * 订单提现
	 * @param orderNo
	 * @return
	 */
	@RequestMapping(value = "/applyForWithdraw", method = RequestMethod.POST)
	public Result applyForWithdraw(String orderNo,String remarks){
		Result res = new Result();
		if(StringUtils.isBlank(orderNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单编号"));
			return res;
		}
		orderServiceImpl.updateApplyForWithdraw(orderNo,remarks);
		return res;
	}
	
	/**
	 * 取消订单
	 * @param orderNo
	 * @return
	 */
	@RequestMapping(value = "/applyForCancel", method = RequestMethod.POST)
	public Result applyForCancel(String orderNo){
		Result res = new Result();
		if(StringUtils.isBlank(orderNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单编号"));
			return res;
		}
		orderServiceImpl.updateApplyForCancel(orderNo);
		return res;
	}
	
	/**
	 * 确认意向
	 * @param orderNo
	 * @param confirm
	 * @return
	 */
	@RequestMapping(value="/confirmIntention", method = RequestMethod.POST)
	public Result confirmIntention(String orderNo,boolean confirm){
		Result res = new Result();
		if(StringUtils.isBlank(orderNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单编号"));
			return res;
		}
		orderServiceImpl.updateConfirmIntention(orderNo,confirm);
		return res;
	}
	
	/**
	 * 申请退款
	 * @param orderNo
	 * @return
	 */
	@RequestMapping(value = "/applyForRefund",method = RequestMethod.POST)
	public Result applyForRefund(String orderNo){
		Result res = new Result();
		if(StringUtils.isBlank(orderNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单编号"));
			return res;
		}
		orderServiceImpl.updateApplyForRefund(orderNo);
		return res;
	}
	
	/**
	 * 确认退款
	 * @param orderNo
	 * @param confirm
	 * @return
	 */
	@RequestMapping(value = "/confirmRefund", method = RequestMethod.POST)
	public Result confirmRefund(String orderNo,boolean confirm){
		Result res = new Result();
		if(StringUtils.isBlank(orderNo)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单编号"));
			return res;
		}
		orderServiceImpl.updateConfirmRefund(orderNo,confirm);
		return res;
	}
	/*
	 * 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;
	}
	/**
	 * 新增评价
	 */
	@RequestMapping(value = "/applyComment", method = RequestMethod.POST)
	public Result addNewComment(HttpServletRequest request,CommentInput commentInput) {
		Result result=new Result();
		String ip=UserCommentController.getClientIp(request);
		if(StringUtils.isBlank(commentInput.getCommodityId())) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"商品id为空","商品id"));return result;
		}
		if(commentInput.getStar()==null || commentInput.getStar()<0 || commentInput.getStar()>5) {
			result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","评分"));return result;
		}
		/*if(StringUtils.isBlank(commentInput.getOrderNo())) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","订单编号"));return result;
		}*/
		if(StringUtils.isBlank(commentInput.getContent()) || commentInput.getContent().length()>512) {
			result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","内容限制1-512字符"));return result;
		}
		
		int res=commentService.addNewComment(commentInput,ip);
		if(res ==-1) {
			result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","提交失败"));return result;
		}
		result.setData(res);
		
		return result;
	}
	/**
	 * 提交订单
	 * @param orderList
	 * @return
	 */
	@RequestMapping(value="/submitOrder",method = RequestMethod.POST)
	public Result submitOrder(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;
	} 
	/**
	 * 提交订单
	 * @param orderList
	 * @return
	 */
	@RequestMapping(value="/submitConsultOrder",method = RequestMethod.POST)
	public Result submitConsultOrder(String orderList) {
		Result result=new Result();
		Gson gson=new Gson();
		List<JtConsultOrder>orders=gson.fromJson(orderList,new TypeToken<ArrayList<JtConsultOrder>>() {}.getType());
		if(orders==null || orders.size()<=0) {result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","列表不符合规范,")); return result;}
		int addRes=jtConsultOrderService.addConsultOrder(orders);
		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;
	}  
	
}