package com.liquidnet.service.kylin.controller;


import com.github.pagehelper.PageInfo;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.kylin.dto.param.PayAgainParam;
import com.liquidnet.service.kylin.dto.param.PayOrderParam;
import com.liquidnet.service.kylin.dto.param.SyncOrderParam;
import com.liquidnet.service.kylin.dto.vo.KylinOrderListVo;
import com.liquidnet.service.kylin.dto.vo.OrderDetailsVo;
import com.liquidnet.service.kylin.dto.vo.PayResultVo;
import com.liquidnet.service.kylin.service.impl.KylinOrderTicketsServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author liquidnet
 * @since 2021-05-20
 */
@Api(tags = "前端-订单相关")
@RestController
@RequestMapping("order")
@Validated
public class KylinOrderTicketsController {

    @Autowired
    private KylinOrderTicketsServiceImpl orderTicketsService;

    @PostMapping("pre")
    @ApiOperation("下单")
    @ApiResponse(code = 200, message = "接口返回对象参数")
    public ResponseDto<PayResultVo> checkCanOrder(@RequestBody @Valid PayOrderParam payOrderParam) {
        return orderTicketsService.checkCanOrder(payOrderParam);
    }

    @PostMapping("payAgain")
    @ApiOperation("再次支付")
    @ApiResponse(code = 200, message = "接口返回对象参数")
    public ResponseDto<PayResultVo> payAgain(@RequestBody @Valid PayAgainParam payAgainParam) {
        return orderTicketsService.payAgain(payAgainParam);
    }

    @PostMapping("syncOrder")
    @ApiOperation("支付回调")
    @ApiResponse(code = 200, message = "接口返回对象参数")
    public String payAgain(@RequestBody @Valid SyncOrderParam syncOrderParam) {
        return orderTicketsService.syncOrder(syncOrderParam);
    }

    @GetMapping("list")
    @ApiOperation("订单列表")
    @ApiResponse(code = 200, message = "接口返回对象参数")
    public ResponseDto<PageInfo<List<KylinOrderListVo>>> orderList(@RequestParam("page")  @NotNull @Min(1) int page,
                                                                  @RequestParam("size")  @NotNull @Min(1) int size) {
        return ResponseDto.success(orderTicketsService.orderList(page,size));
    }

    @GetMapping("details")
    @ApiOperation("订单详情")
    @ApiResponse(code = 200, message = "接口返回对象参数")
    public ResponseDto<OrderDetailsVo> orderDetails(@RequestParam(value = "orderId",required = false) @NotNull(message = "订单id不能为空") String orderId) {
        return ResponseDto.success(orderTicketsService.orderDetails(orderId));
    }

    @GetMapping("checkPayment")
    @ApiOperation("订单状态")
    @ApiResponse(code = 200, message = "接口返回对象参数")
    public ResponseDto<HashMap> checkOrderResult(@RequestParam("orderId") @NotNull(message = "订单id不能为空") String orderId) {
        Integer status = orderTicketsService.checkOrderResult(orderId).getData();
        HashMap<String,Integer> map = new HashMap<>();
        map.put("status",status);
        return ResponseDto.success(map);
    }



    @PostMapping("checkOrderTime")
    @ApiOperation("订单过期脚本")
    @ApiResponse(code = 200, message = "接口返回对象参数")
    public ResponseDto<Boolean> checkOrderTime() {
        return ResponseDto.success(orderTicketsService.checkOrderTime(null));
    }
}
