package com.yida.data.device.consume.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.device.consume.service.H5ConsumeService;
import com.yida.data.device.dto.consume.CreateConsumeOrderDTO;
import com.yida.data.device.dto.consume.H5ConsumeOrderPageDTO;
import com.yida.data.device.dto.consume.H5ConsumeRecordPageDTO;
import com.yida.data.device.vo.consume.ConsumePayInfoVO;
import com.yida.data.device.vo.consume.ConsumeVipVO;
import com.yida.data.device.vo.consume.H5ConsumeOrderPageVO;
import com.yida.data.device.vo.consume.H5ConsumeRecordPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 消费机前端controller
 *
 * @author ZYJ
 * @date 2023/3/16
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("h5/consume")
@Api(tags = "消费机前端api")
public class H5ConsumeController {

    private final H5ConsumeService h5ConsumeService;

    @GetMapping("/checkVip")
    @ApiOperation("查询学生消费机会员状态")
    public ResultBean<ConsumeVipVO> checkVip(
            @ApiParam(value = "学生id", required = true) @RequestParam Long studentId,
            @ApiParam(value = "学校id", required = true) @RequestParam Long schoolId) {
        return ResultBean.buildSuccess(h5ConsumeService.checkVip(studentId, schoolId));
    }

    @GetMapping("/reportLoss")
    @ApiOperation("挂失")
    public ResultBean<String> reportLoss(@ApiParam(value = "学生id", required = true) @RequestParam Long studentId) {
        return ResultBean.buildSuccess(h5ConsumeService.reportLoss(studentId));
    }

    @GetMapping("/unfreeze")
    @ApiOperation("解冻")
    public ResultBean<String> unfreeze(@ApiParam(value = "学生id", required = true) @RequestParam Long studentId) {
        return ResultBean.buildSuccess(h5ConsumeService.unfreeze(studentId));
    }

    @PostMapping("/createConsumeOrder")
    @ApiOperation("生成消费订单(充值)")
    public ResultBean<ConsumePayInfoVO> createConsumeOrder(@RequestBody CreateConsumeOrderDTO dto) {
        return ResultBean.buildSuccess(h5ConsumeService.createConsumeOrder(dto));
    }

    @GetMapping("/consumeOrderSuccess")
    @ApiOperation("消费订单完成(前台支付成功后调用)")
    public ResultBean<String> consumeOrderSuccess(
            @ApiParam(value = "订单号", required = true) @RequestParam String orderCode) {
        h5ConsumeService.consumeOrderSuccess(orderCode);
        return ResultBean.buildSuccess();
    }

    @PostMapping(value = "listH5ConsumeOrderPage")
    @ApiOperation(value = "前端查询消费订单列表数据(充值记录)")
    public ResultBean<IPage<H5ConsumeOrderPageVO>> listH5ConsumeOrderPage(@RequestBody H5ConsumeOrderPageDTO dto) {
        IPage<H5ConsumeOrderPageVO> page = h5ConsumeService.listH5ConsumeOrderPage(dto);
        return ResultBean.buildSuccess(page);
    }

    @GetMapping(value = "getH5ConsumeOrderInfo")
    @ApiOperation(value = "前端查询消费订单详情(充值记录)")
    public ResultBean<H5ConsumeOrderPageVO> getH5ConsumeOrderInfo(
            @ApiParam(value = "订单id") @RequestParam(required = false) Long orderId,
            @ApiParam(value = "订单号") @RequestParam(required = false) String orderCode) {
        return ResultBean.buildSuccess(h5ConsumeService.getH5ConsumeOrderInfo(orderId, orderCode));
    }

    @GetMapping("continuePayOrder")
    @ApiOperation("继续支付")
    public ResultBean<ConsumePayInfoVO> continuePayOrder(
            @ApiParam(value = "订单号", required = true) @RequestParam String orderCode,
            @ApiParam(value = "学校id", required = true) @RequestParam Long deptId) {
        ConsumePayInfoVO infoVO = h5ConsumeService.continuePayOrder(orderCode, deptId);
        return ResultBean.buildSuccess(infoVO);
    }

    @GetMapping(value = "cancelOrder")
    @ApiOperation(value = "取消订单")
    public ResultBean<String> cancelOrder(
            @ApiParam(value = "订单号", required = true) @RequestParam String orderCode) {
        h5ConsumeService.cancelOrder(orderCode);
        return ResultBean.buildSuccess();
    }

    @PostMapping(value = "listH5ConsumeRecordPage")
    @ApiOperation(value = "前端查询学生消费记录列表")
    public ResultBean<IPage<H5ConsumeRecordPageVO>> listH5ConsumeRecordPage(@RequestBody H5ConsumeRecordPageDTO dto) {
        IPage<H5ConsumeRecordPageVO> page = h5ConsumeService.listH5ConsumeRecordPage(dto);
        return ResultBean.buildSuccess(page);
    }
}
