package com.gree.mall.miniapp.controller.es;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.PayDetail;
import com.gree.mall.miniapp.bean.es.EsOrderBuyBean;
import com.gree.mall.miniapp.bean.es.EsOrderInfoBean;
import com.gree.mall.miniapp.bean.order.OrderBuyBean;
import com.gree.mall.miniapp.constant.Constant;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.helper.ResponseHelper;
import com.gree.mall.miniapp.logic.es.OrderPayLogic;

import com.gree.mall.miniapp.plus.entity.EsOrderInfo;
import com.gree.mall.miniapp.plus.entity.EsOrderRefund;
import com.gree.mall.miniapp.utils.IpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

/**
 * @author change
 */
@RequiredArgsConstructor
@Validated
@RestController
@Api(value = "二手订单API", tags ={"二手订单API"} )
@RequestMapping(value = "/es/orderPay", produces = "application/json; charset=utf-8")
public class OrderPayController {

    @Autowired
    OrderPayLogic orderPayLogic;

    @Autowired
    RedisLockRegistry redisLockRegistry;



    @PostMapping("/buy")
    @ApiOperation(value = "提交订单")
    public ResponseHelper<PayDetail> buy(
            @RequestBody EsOrderBuyBean esOrderBuyBean,
            HttpServletRequest request
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + esOrderBuyBean.getEsGoodsId());
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            String ip = IpUtil.getIpAddr(request);
            PayDetail buy = orderPayLogic.buy(esOrderBuyBean,ip,request);

            return ResponseHelper.success(buy);
        } finally {
            obtain.unlock();
        }

    }


    @PostMapping("/wait/pay")
    @ApiOperation(value = "支付待支付订单")
    public ResponseHelper<PayDetail> waitPayOrder(
            @ApiParam(value = "订单id",required = true) @RequestParam(required = false) String esOrderId,
            HttpServletRequest request
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + esOrderId);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            String ip = IpUtil.getIpAddr(request);
            PayDetail payDetail = orderPayLogic.waitPayOrder(esOrderId,ip);

            return ResponseHelper.success(payDetail);
        } finally {
            obtain.unlock();
        }
    }

    @PostMapping("/wait/notPay")
    @ApiOperation(value = "取消支付")
    public ResponseHelper notPay(
            @ApiParam(value = "订单id",required = true) @RequestParam String orderId,
            HttpServletRequest request
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + orderId);
        try {
            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
                throw new RemoteServiceException("系统繁忙，请稍后再试");
            }
            String ip = IpUtil.getIpAddr(request);
            orderPayLogic.notPay(orderId,ip);
            return ResponseHelper.success();
        } finally {
            obtain.unlock();
        }
    }

    @PostMapping("/send")
    @ApiOperation("发货")
    public ResponseHelper send(
            @ApiParam(value = "订单号", required = true) @RequestParam(required = true) String esOrderId,
            @ApiParam(value = "是否自提 YES是 NO否", required = true) @RequestParam(required = true) String pickType,
            @ApiParam(value = "快递单号", required = false) @RequestParam(required = false) String logisticsNo,
            @ApiParam(value = "快递公司code", required = false) @RequestParam(required = false) String logisticsCode,
            @ApiParam(value = "快递公司名称", required = false) @RequestParam(required = false) String logisticsName
    ) {
        orderPayLogic.send(esOrderId,pickType,logisticsNo,logisticsCode,logisticsName);
        return ResponseHelper.success();
    }



    @PostMapping("/payPrice")
    @ApiOperation("确认收款")
    public ResponseHelper payPrice(
            @ApiParam(value = "订单号", required = true) @RequestParam(required = true) String esOrderId,
                 @ApiParam(value = "金额", required = false) @RequestParam(required = false) BigDecimal price,
            @ApiParam(value = "收款凭证", required = false) @RequestParam(required = false) String fileUrl,
            @ApiParam(value = "备注", required = false) @RequestParam(required = false) String remark

    ) {
        orderPayLogic.payPrice(esOrderId,price,fileUrl,remark);
        return ResponseHelper.success();
    }


    @PostMapping("/payOk")
    @ApiOperation("线下单确认订单")
    public ResponseHelper payPrice(
            @ApiParam(value = "订单号", required = true) @RequestParam(required = true) String esOrderId
    ) {
        orderPayLogic.payOk(esOrderId);
        return ResponseHelper.success();
    }



    @PostMapping("/okSend")
    @ApiOperation("线下单确认收货")
    public ResponseHelper okSend(
            @ApiParam(value = "订单号", required = true) @RequestParam(required = true) String esOrderId


    ) {
        orderPayLogic.okSend(esOrderId);
        return ResponseHelper.success();
    }

    @PostMapping("/list")
    @ApiOperation("列表")
    public ResponseHelper<Page<EsOrderInfo>> list(
            @ApiParam(value = "订单号", required = false) @RequestParam(required = false) String orderId,
            @ApiParam(value = "产品名称", required = false) @RequestParam(required = false) String esGoodsName,
            @ApiParam(value = "买家名称", required = false) @RequestParam(required = false) String userName,
            @ApiParam(value = "卖家名称", required = false) @RequestParam(required = false) String sellName,
            @ApiParam(value = "买家手机", required = false) @RequestParam(required = false) String userMobile,
            @ApiParam(value = "卖家手机", required = false) @RequestParam(required = false) String sellMobile,
            @ApiParam(value = "订单状态 SAVE 待付款 WAIT 待确认  PAID 待发货 SEND 卖家发货  OVER 交易完成 TIME_OUT 超时 CANCEL 取消/关闭  WAIT_REFUND 售后中", required = false) @RequestParam(required = false) List<String> status,
            @ApiParam(value = "开始创建时间", required = false) @RequestParam(required = false) String startCreateTime,
            @ApiParam(value = "结束创建时间", required = false) @RequestParam(required = false) String endCreateTime,
            @ApiParam(value = "开始支付时间", required = false) @RequestParam(required = false) String startPayTime,
            @ApiParam(value = "结束支付时间", required = false) @RequestParam(required = false) String endPayTime,
            @ApiParam(value = "页号", required = true) @RequestParam(required = true) Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam(required = true) Integer pageSize,

            HttpServletRequest request
    ) throws RemoteServiceException {
        IPage<EsOrderInfo> list = orderPayLogic
                .list(request,esGoodsName,orderId, sellName, userName, userMobile,sellMobile, status, startCreateTime,
                        endCreateTime, startPayTime, endPayTime,pageNum, pageSize);
        return ResponseHelper.success(list);
    }

    @PostMapping("/detail")
    @ApiOperation("订单详情")
    public ResponseHelper<EsOrderInfoBean> detail(
            @ApiParam(value = "订单号", required = true) @RequestParam(required = true) String esOrderId
    ) {
        EsOrderInfoBean detail = orderPayLogic.detail(esOrderId);
        return ResponseHelper.success(detail);
    }


    @PostMapping("/refund")
    @ApiOperation("发起退款")
    public ResponseHelper refund(
            @ApiParam(value = "订单号", required = true) @RequestParam(required = true) String esOrderId,
            @ApiParam(value = "退货原因", required = true) @RequestParam(required = true) String refundNote,
            @ApiParam(value = "处理方式 REFUND 退款  GOODS_REFUND 退货退款  FAIL 驳回 GOODS 退货", required = true) @RequestParam(required = true) String refundType,
            @ApiParam(value = "退货凭证", required = false) @RequestParam(required = false) List<String> fileUrls
    ) {
        orderPayLogic.refund(esOrderId,refundType,fileUrls,refundNote);
        return ResponseHelper.success();
    }


    @PostMapping("/listRefund")
    @ApiOperation("退货列表")
    public ResponseHelper<Page<EsOrderRefund>> listRefund(
            @ApiParam(value = "订单号", required = false) @RequestParam(required = false) String orderId,
            @ApiParam(value = "产品名称", required = false) @RequestParam(required = false) String esGoodsName,
            @ApiParam(value = "买家名称", required = false) @RequestParam(required = false) String userName,
            @ApiParam(value = "卖家名称", required = false) @RequestParam(required = false) String sellName,
            @ApiParam(value = "买家手机", required = false) @RequestParam(required = false) String userMobile,
            @ApiParam(value = "卖家手机", required = false) @RequestParam(required = false) String sellMobile,
            @ApiParam(value = "处理方式 REFUND 退款  GOODS_REFUND 退货退款  FAIL 驳回 GOODS 退货", required = false) @RequestParam(required = false) List<String> status,
            @ApiParam(value = "开始创建时间", required = false) @RequestParam(required = false) String startCreateTime,
            @ApiParam(value = "结束创建时间", required = false) @RequestParam(required = false) String endCreateTime,
            @ApiParam(value = "开始支付时间", required = false) @RequestParam(required = false) String startPayTime,
            @ApiParam(value = "结束支付时间", required = false) @RequestParam(required = false) String endPayTime,
            @ApiParam(value = "页号", required = true) @RequestParam(required = true) Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam(required = true) Integer pageSize,

            HttpServletRequest request
    ) throws RemoteServiceException {
        IPage<EsOrderRefund> list = orderPayLogic
                .listRefund(request,esGoodsName,orderId, sellName, userName, userMobile,sellMobile, status, startCreateTime,
                        endCreateTime, startPayTime, endPayTime,pageNum, pageSize);
        return ResponseHelper.success(list);
    }


}
