package com.buy507.mall.portal.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.buy507.mall.common.api.CommonResult;
import com.buy507.mall.portal.domain.CartOrderParam;
import com.buy507.mall.portal.domain.OfflinePaymentParam;
import com.buy507.mall.portal.domain.OrderParam;
import com.buy507.mall.portal.domain.OrderResult;
import com.buy507.mall.portal.domain.SellerBankAccountResult;
import com.buy507.mall.portal.service.OmsPortalOrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 订单管理Controller
 */
@RestController
@Api(tags = "OmsPortalOrderController",description = "订单管理")
@RequestMapping("/member/order")
public class OmsPortalOrderController {
	
    @Autowired
    private OmsPortalOrderService portalOrderService;
    
    @ApiOperation("获取会员订单信息列表")
	@ApiImplicitParam(name = "orderStatus", value = "查询订单状态条件：0->待付款; 1->待发货; 2->待收货; 3->已完成; 4->已关闭; 6->全部; 7->待评价 ", paramType = "query")
    @RequestMapping(value = "/getMemberOrderList", method = RequestMethod.GET)
	public CommonResult<List<OrderResult>> getMemberOrderList(@RequestParam Integer orderStatus, @RequestParam Integer pageSize, @RequestParam Integer pageNum) {
		List<OrderResult> result = portalOrderService.getMemberOrderList(orderStatus, pageSize, pageNum);
		return CommonResult.success(result);
	}
	
	@ApiOperation("获取会员订单详细信息")
	@ApiImplicitParam(name = "orderId", value = "订单ID", paramType = "query")
    @RequestMapping(value = "/getMemberOrderDetailInfo", method = RequestMethod.GET)
	public CommonResult<OrderResult> getMemberOrderDetailInfo(@RequestParam Long orderId) {
		OrderResult result = portalOrderService.getMemberOrderDetailInfo(orderId);
		return CommonResult.success(result);
	}

    @ApiOperation("根据购物车信息生成订单")
    @RequestMapping(value = "/createCartOrder",method = RequestMethod.POST)
    public Object generateOrder(@RequestBody CartOrderParam orderParam) {
    	try {
    		return portalOrderService.createCartOrder(orderParam);
		} catch (Exception e) {
			return CommonResult.failed("库存不足，无法下单");
		}
    }
    
    @ApiOperation("直接购买商品生成订单")
    @RequestMapping(value = "/createOrder", method = RequestMethod.POST)
    public CommonResult createOrder(@RequestBody OrderParam orderParam){
    	try {
    		return portalOrderService.createOrder(orderParam);
    	} catch (Exception e) {
			return CommonResult.failed("森态积分不足,无法抵扣");
		}
    }

    @ApiOperation("取消订单")
    @RequestMapping(value = "/cancelOrder",method = RequestMethod.POST)
    public CommonResult cancelOrder(@RequestParam Long orderId){
    	return portalOrderService.cancelOrder(orderId);
    }
    
    @ApiOperation("删除订单")
    @RequestMapping(value = "/deleteOrder",method = RequestMethod.POST)
    public CommonResult deleteOrder(@RequestParam Long orderId){
    	return portalOrderService.deleteOrder(orderId);
    }
    
    /*
    @ApiOperation("自动取消超时订单")
    @RequestMapping(value = "/cancelTimeOutOrder",method = RequestMethod.POST)
    public Object cancelTimeOutOrder(){
        return portalOrderService.cancelTimeOutOrder();
    }
    */
    
    @ApiOperation("获取商家银行账户信息")
    @RequestMapping(value = "/getSellerBankAccount",method = RequestMethod.GET)
    public CommonResult getSellerBankAccount(){
    	SellerBankAccountResult account = portalOrderService.getSellerBankAccount();
        return CommonResult.success(account);
    }
    
    @ApiOperation("线下支付")
    @RequestMapping(value = "/payment",method = RequestMethod.POST)
    public CommonResult payment(@RequestBody OfflinePaymentParam paymentParam){
        return portalOrderService.payment(paymentParam);
    }
    
    @ApiOperation("获取物流信息")
    @ApiImplicitParam(name = "orderId", value = "订单ID", paramType = "query")
    @RequestMapping(value = "/getLogistics", method = RequestMethod.GET)
    public CommonResult getLogistics(@RequestParam Long orderId) {
        return portalOrderService.getLogistics(orderId);
    }
    
    @ApiOperation("买家确认收货")
    @ApiImplicitParam(name = "orderId", value = "订单ID", paramType = "query")
    @RequestMapping(value = "/confirmReceiving", method = RequestMethod.POST)
    public CommonResult confirmReceiving(@RequestParam Long orderId) {
        return portalOrderService.confirmReceiving(orderId);
    }
    
    @ApiOperation("积分支付")
    @ApiImplicitParam(name = "orderId", value = "订单ID", paramType = "query")
    @RequestMapping(value = "/balancePayment", method = RequestMethod.POST)
    public CommonResult balancePayment(@RequestParam Long orderId) {
        return portalOrderService.balancePayment(orderId);
    }
    
    @ApiOperation("微信预支付")
    @ApiImplicitParam(name = "orderId", value = "订单ID", paramType = "query")
    @RequestMapping(value = "/wxPrePay", method = RequestMethod.POST)
    public CommonResult<Map<String, String>> wxPrePay(@RequestParam Long orderId) {
        return portalOrderService.wxPrePay(orderId);
    }


    @ApiOperation("支付宝web跳转接口")
    @ApiImplicitParam(name = "orderId", value = "订单ID", paramType = "query")
    @RequestMapping(value = "/toAlipayWeb", method = RequestMethod.POST)
    public CommonResult<Map<String, String>> toAlipayWeb(@RequestParam Long orderId, HttpServletResponse response)
            throws IOException {
        String result = portalOrderService.toAlipayWeb(orderId);
        if(result != null){
            String charset = "UTF-8";
            response.setContentType("text/html;charset=" + charset);
            OutputStream out = response.getOutputStream();
            out.write(result.getBytes(charset));
            response.getOutputStream().flush();
        }
        return CommonResult.failed("调用支付宝支付失败");
    }


    /**
	 * 微信支付成功回调方法
	 * @param request
	 * @param response
	 */
	@RequestMapping("/wxPay/callback")
	public void wxPayCallback(HttpServletRequest request, HttpServletResponse response) throws Exception {
		portalOrderService.wxPayCallback(request, response);
	}



    /**
     * 支付宝支付回调方法
     * @param params
     */
    @ApiOperation("支付宝支付回调")
    @RequestMapping("/alipay/callback")
    public void alipayCallback(@RequestParam Map<String, String> params) {
        portalOrderService.alipayCallback(params);
    }

}
