package com.fs.app.controller;

import com.fs.app.annotation.Login;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.R;
import com.fs.course.param.FsCourseProductOrderCreateParam;
import com.fs.course.param.FsCourseProductOrderListUParam;
import com.fs.course.service.IFsCourseProductOrderService;
import com.fs.course.vo.FsCourseProductOrderListUVO;
import com.fs.his.param.ApplyCourseProductOrderRefundParam;
import com.fs.his.param.FsCourseProductOrderComputeParam;
import com.fs.his.param.FsCourseProductOrderDoPayParam;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api("拍商品订单接口")
@RestController
@RequestMapping(value="/app/courseProductOrder")
public class CourseProductOrderController extends AppBaseController {

    @Autowired
    private IFsCourseProductOrderService courseProductOrderService;

    @Login
    @ApiOperation("订单列表")
    @GetMapping("/getCourseProductOrderList")
    public R getCourseOrderList(FsCourseProductOrderListUParam param)
    {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        param.setUserId(Long.parseLong(getUserId()));
        List<FsCourseProductOrderListUVO> productOrderListUVOS = courseProductOrderService.selectFsCourseProductOrderListUVO(param);
        PageInfo<FsCourseProductOrderListUVO> pageInfo = new PageInfo<>(productOrderListUVOS);
        return R.ok().put("data",pageInfo);
    }

    @ApiOperation("获取拍商品订单详情")
    @GetMapping("/getCourseProductByCourseOrderId")
    public R getCourseProductByCourseOrderId(@RequestParam("courseOrderId") Long courseOrderId) {
        FsCourseProductOrderListUVO productOrderListUVO = courseProductOrderService.selectCourseProductByCourseOrderId(courseOrderId);
        return R.ok().put("data", productOrderListUVO);
    }

    @Login
    @ApiOperation("创建订单")
    @PostMapping("/createOrder")
    public R createOrder(@Validated @RequestBody FsCourseProductOrderCreateParam param, HttpServletRequest request){
        param.setUserId(Long.parseLong(getUserId()));
        //param.setCompanyUserId(getCompanyUserId());
        return courseProductOrderService.createOrder(param);
    }

    @Login
    @ApiOperation("计算订单")
    @PostMapping("/compute")
    public R payment(HttpServletRequest request, @Validated @RequestBody FsCourseProductOrderComputeParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        return courseProductOrderService.computeOrder(param);
    }

    @Login
    @RepeatSubmit
    @ApiOperation("支付")
    @PostMapping("/payment")
    public R payment(HttpServletRequest request, @Validated @RequestBody FsCourseProductOrderDoPayParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        return courseProductOrderService.payment(param);
    }

    @Login
    @RepeatSubmit
    @ApiOperation("申请退款")
    @PostMapping("/applyRefund")
    public R applyRefund(HttpServletRequest request, @Validated @RequestBody ApplyCourseProductOrderRefundParam param)
    {
        return courseProductOrderService.applyRefund(param);
    }
}
