package com.rf.psychological.wxpay.controller;
import com.rf.psychological.security.SafetyProcess;
import com.rf.psychological.enums.OrderStatus;
import com.rf.psychological.base.rest.BaseController;
import com.rf.psychological.utils.Result;
import com.rf.psychological.wxpay.model.OrderInfo;
import com.rf.psychological.wxpay.service.OrderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:商品订单管理
 * @Author: zsf
 * @Date: 2022/6/22
 */
@RestController
@RequestMapping("/api/orderInfo")
@Api(tags = "商品订单管理")
@Slf4j
@ConditionalOnProperty(prefix="wx.pay",name = "is_lan", havingValue = "false")
public class OrderInfoController extends BaseController {

    @Autowired
    private OrderInfoService orderInfoService;

    @GetMapping("/findOrderInfoList")
    @ApiOperation("查询订单")

    public Result findOrderInfoList(int pageNum,int pageSize,String productId,String userId,String orderStatus){
        try {
            Page<OrderInfo> infoPage = orderInfoService.findOrderInfoList(pageNum,pageSize,productId,userId,orderStatus);
            return success(infoPage);
        }catch (Exception e){
            e.printStackTrace();
            return fail("查询失败");
        }
    }

    /**
     * 查询本地订单状态
     * @param orderNo
     * @return
     */
    @SafetyProcess
    @ApiOperation("查询本地订单状态")
    @GetMapping("/queryOrderStatus/{orderNo}")
    public Result queryOrderStatus(@PathVariable String orderNo){

        OrderInfo info = orderInfoService.getOrderStatus(orderNo);
        if (info == null ){
            return success(null,"请确认订单编号，查询为空");
        }
        String orderStatus = info.getOrderStatus();
        if(OrderStatus.SUCCESS.getType().equals(orderStatus)){
            return success(0,"支付成功"); //支付成功
        }

        return success(101,"支付中......");
    }

    /**
     * 查询本地订单状态
     * @param orderNo
     * @return
     */
    @SafetyProcess
    @ApiOperation("查询已完成订单详情")
    @GetMapping("/queryOrderDetail/{orderNo}")
    public Result queryOrderDetail(@PathVariable String orderNo){
        try {
            OrderInfo info = orderInfoService.getOrderDetail(orderNo);
            return success(info);
        }catch (Exception e){
            e.printStackTrace();
            log.info(e.getMessage());
            return fail();
        }

    }

    @SafetyProcess
    @ApiOperation("查询用户结果支付情况")
    @GetMapping("/queryOrderByResultIdAndUserId/{productId}/{userId}/{resultId}")
    public Result queryOrderByResultIdAndUserIdAndProId(@PathVariable("productId") String productId ,
                                                        @PathVariable("userId") String userId,
                                                        @PathVariable("resultId") String resultId ){
        return success(orderInfoService.queryOrderByResultIdAndUserIdAndProId(productId,userId,resultId));
    }

    @SafetyProcess
    @ApiOperation("查询订单支付情况")
    @GetMapping("/queryOrderByResultIdAndUserId/{userId}/{resultId}")
    public Result queryOrderByResultIdAndUserId(@PathVariable("userId") String userId, @PathVariable("resultId") String resultId ){
        try {
            OrderInfo info = orderInfoService.getOrderByUserIdAndResultId(userId,resultId);
            return success(info);
        }catch (Exception e){
            e.printStackTrace();
            return fail();
        }
    }
}
