package com.yihu.jw.base.endpoint.order;

import com.alibaba.fastjson.JSONObject;
import com.yihu.jw.base.service.order.PayService;
import com.yihu.jw.restmodel.ResponseContant;
import com.yihu.jw.restmodel.web.ListEnvelop;
import com.yihu.jw.restmodel.web.ObjEnvelop;
import com.yihu.jw.restmodel.web.PageEnvelop;
import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import oracle.jdbc.proxy.annotation.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Bing on 2021/7/1.
 */
@RestController
@Api(description = "支付订单管理")
@RequestMapping(value = "payManage")
public class PayOrderEndpoint extends EnvelopRestEndpoint {

    @Autowired
    private PayService payService;

    @GetMapping(value = "getOrderList")
    @ApiOperation("查询订单列表")
    public PageEnvelop getOrderList(
            @ApiParam(name="name",value = "姓名")
            @RequestParam(value = "name",required = false)String name,
            @ApiParam(name="orderNo",value = "订单号")
            @RequestParam(value = "orderNo",required = false)String orderNo,
            @ApiParam(name="status",value = "订单状态")
            @RequestParam(value = "status",required = false)String status,
            @ApiParam(name="payType",value = "支付方式支付类型1微信2支付宝3易联众支付")
            @RequestParam(value = "payType",required = false)String payType,
            @ApiParam(name="orderType",value = "订单类型")
            @RequestParam(value = "orderType",required = false)Integer orderType,
            @ApiParam(name="orderCategory",value = "订单分类")
            @RequestParam(value = "orderCategory",required = false)Integer orderCategory,
            @ApiParam(name="page")
            @RequestParam(value = "page")Integer page,
            @ApiParam(name="pageSize")
            @RequestParam(value = "pageSize")Integer pageSize
    ){
        try {
            return payService.getOrderList(name, orderNo, status,payType, orderType, orderCategory, page, pageSize);
        }catch (Exception e){
            return failedPageEnvelopException2(e);
        }
    }

    @GetMapping(value = "getRefundOrderList")
    @ApiOperation("查询退款订单列表")
    public PageEnvelop getRefundOrderList(
            @ApiParam(name="name",value = "姓名")
            @RequestParam(value = "name",required = false)String name,
            @ApiParam(name="orderNo",value = "订单号")
            @RequestParam(value = "orderNo",required = false)String orderNo,
            @ApiParam(name="status",value = "订单状态")
            @RequestParam(value = "status",required = false)String status,
            @ApiParam(name="payType",value = "支付方式支付类型1微信2支付宝3易联众支付")
            @RequestParam(value = "payType",required = false)String payType,
            @ApiParam(name="orderType",value = "订单类型")
            @RequestParam(value = "orderType",required = false)Integer orderType,
            @ApiParam(name="orderCategory",value = "订单分类")
            @RequestParam(value = "orderCategory",required = false)Integer orderCategory,
            @ApiParam(name="page")
            @RequestParam(value = "page")Integer page,
            @ApiParam(name="pageSize")
            @RequestParam(value = "pageSize")Integer pageSize
    ){
        try {
            return payService.getRefundOrderList(name, orderNo, status, payType, orderType, orderCategory, page, pageSize);
        }catch (Exception e){
            return failedPageEnvelopException2(e);
        }
    }

    @PostMapping(value = "refuseRefund")
    @ApiOperation("拒绝退款")
    public ObjEnvelop refuseRefund(
            @ApiParam(name = "orderNo",required = true)
            @RequestParam(value = "orderNo")String orderNo,
            @ApiParam(name="refuseDesc")
            @RequestParam(value = "refuseDesc")String refuseDesc
    ){
        try {
            JSONObject result =  payService.refuseRefund(orderNo,refuseDesc);
            if (result.getIntValue(ResponseContant.resultFlag) == ResponseContant.fail) {
                return ObjEnvelop.getError( result.getString(ResponseContant.resultMsg));
            }
            return ObjEnvelop.getSuccess("操作成功",result.get(ResponseContant.resultMsg));
        }catch (Exception e){
            return failedObjEnvelopException2(e);
        }
    }

    @PostMapping(value = "agreeRefund")
    @ApiOperation("同意退款")
    public ObjEnvelop agreeRefund(
            @ApiParam(name = "orderNo",required = true)
            @RequestParam(value = "orderNo")String orderNo
    ){
        try {
            JSONObject result =  payService.agreeRefund(orderNo);
            if (result.getIntValue(ResponseContant.resultFlag) == ResponseContant.fail) {
                return ObjEnvelop.getError( result.getString(ResponseContant.resultMsg));
            }
            return ObjEnvelop.getSuccess("操作成功",result.get(ResponseContant.resultMsg));
        }catch (Exception e){
            return failedObjEnvelopException2(e);
        }
    }
}
