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.common.core.page.TableDataInfo;
import com.fs.course.domain.FsUserCourse;
import com.fs.course.domain.FsUserCourseOrder;
import com.fs.course.domain.FsUserVipOrder;
import com.fs.course.domain.FsUserVipPackage;
import com.fs.course.param.*;
import com.fs.course.service.IFsUserCourseOrderService;
import com.fs.course.vo.FsUserCourseListUVO;
import com.fs.course.vo.FsUserCourseOrderListPVO;
import com.fs.course.vo.FsUserCourseOrderListUVO;
import com.fs.his.param.FsPackageOrderComputeParam;
import com.fs.his.param.FsPackageOrderDoPayParam;
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;

import static com.fs.his.utils.PhoneUtil.decryptAutoPhoneMk;

@Api("课堂订单接口")
@RestController
@RequestMapping(value="/app/courseOrder")
public class CourseOrderController extends  AppBaseController{

    @Autowired
    private IFsUserCourseOrderService courseOrderService;

    @Login
    @ApiOperation("订单列表")
    @GetMapping("/getCourseOrderList")
    public R getCourseOrderList(FsUserCourseOrderListUParam param)
    {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        param.setUserId(Long.parseLong(getUserId()));
        List<FsUserCourseOrderListUVO> list = courseOrderService.selectFsUserCourseOrderListUVO(param);
        PageInfo<FsUserCourseOrderListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }

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

    @Login
    @ApiOperation("创建积分兑换订单")
    @PostMapping("/createIntegralOrder")
    public R createIntegralOrder(@Validated @RequestBody FsUserCourseOrderCreateParam param, HttpServletRequest request){
        param.setUserId(Long.parseLong(getUserId()));
        return courseOrderService.createIntegralOrder(param);
    }

    @PostMapping("/testPay")
    public R testPay(@Validated @RequestBody FsUserCourseOrderDoPayParam param, HttpServletRequest request){
        return courseOrderService.test(param.getOrderId());
    }


    @Login
    @ApiOperation("计算订单")
    @PostMapping("/compute")
    public R compute(@Validated @RequestBody FsUserCourseOrderComputeParam param, HttpServletRequest request){
        return courseOrderService.computeOrder(param);

    }

    @Login
    @ApiOperation("支付")
    @PostMapping("/weChatPayment")
    public R payment(HttpServletRequest request, @Validated @RequestBody FsUserCourseOrderDoPayParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        return courseOrderService.payment(param);


    }

    @Login
    @ApiOperation("支付宝支付")
    @PostMapping("/aliPayment")
    public R aliPayment(HttpServletRequest request, @Validated @RequestBody FsUserCourseOrderDoPayParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        return courseOrderService.aliPayment(param);
    }

    @ApiOperation("获取订单详情")
    @GetMapping("/getCourseOrderById")
    public R getVipOrderById(@RequestParam("orderId")Long orderId, HttpServletRequest request){
        FsUserCourseOrderListPVO order=courseOrderService.selectFsUserCourseOrderVOByOrderId(orderId);
        if (order==null){
            return R.error("订单不存在！");
        }
        order.setUserName(decryptAutoPhoneMk(order.getUserName()));
        return R.ok().put("order",order);
    }

}
