package com.fs.app.controller;

import com.fs.app.annotation.Login;
import com.fs.common.core.domain.R;
import com.fs.course.domain.FsUserVipOrder;
import com.fs.course.domain.FsUserVipPackage;
import com.fs.course.param.FsUserVipOrderCreateUParam;
import com.fs.course.param.FsUserVipOrderPayUParam;
import com.fs.course.service.IFsUserVipOrderService;
import com.fs.course.service.IFsUserVipPackageService;
import com.fs.course.vo.FsUserVipPackageListUVO;
import com.fs.his.domain.FsUser;
import com.fs.his.mapper.FsUserMapper;
import com.fs.his.service.IFsUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.time.LocalDate;
import java.time.ZoneId;
import java.util.List;

@Api("Vip接口")
@RestController
@RequestMapping(value="/app/userVip")
public class UserVipController extends  AppBaseController{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private IFsUserVipOrderService fsUserVipOrderService;
    @Autowired
    private IFsUserVipPackageService fsUserVipPackageService;
    @Autowired
    private IFsUserService userService;
    @Autowired
    private FsUserMapper fsUserMapper;

    @Login
    @ApiOperation("VIP套餐列表")
    @GetMapping("/getPackageList")
    public R getPackageList()
    {
        List<FsUserVipPackageListUVO> list = fsUserVipPackageService.selectFsUserVipPackageListUVO();
        return R.ok().put("data",list);
    }


    @Login
    @ApiOperation("创建订单")
    @PostMapping("/createOrder")
    public R createOrder(@Validated @RequestBody FsUserVipOrderCreateUParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        return fsUserVipOrderService.createVipPurchaseOrder(param);
    }

    @ApiOperation("获取订单详情")
    @GetMapping("/getVipOrderById")
    public R getVipOrderById(@RequestParam("orderId")Long orderId, HttpServletRequest request){
        FsUserVipOrder order=fsUserVipOrderService.selectFsUserVipOrderByOrderId(orderId);
        if (order!=null){
            FsUserVipPackage vipPackage = fsUserVipPackageService.selectFsUserVipPackageByPackageId(order.getPackageId());
            return R.ok().put("data",order).put("package",vipPackage);
        }
        return R.error("订单不存在");
    }


    @Login
    @ApiOperation("支付")
    @PostMapping("/weChatPayment")
    public R payment(@Validated @RequestBody FsUserVipOrderPayUParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        return fsUserVipOrderService.payment(param);
    }


    @Login
    @ApiOperation("支付宝支付")
    @PostMapping("/aliPayment")
    public R aliPayment(@Validated @RequestBody FsUserVipOrderPayUParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        return fsUserVipOrderService.aliPayment(param);
    }


    @ApiOperation("测试支付成功")
    @PostMapping("/test")
    public R test(@RequestParam String payCode,@RequestParam String tradeNo)
    {
        System.out.println("payCode:"+payCode);
        return fsUserVipOrderService.payConfirm("",payCode,tradeNo,"",1,null,null);
    }

    @Login
    @ApiOperation("清除vip")
    @PostMapping("/clearVip")
    public R clearVip(HttpServletRequest request)
    {
        return userService.clearUserVip(Long.parseLong(getUserId()));
    }

    @PostMapping("/vipExpiration")
    public R vipExpiration(HttpServletRequest request)
    {
        List<FsUser> list = fsUserMapper.selectFsUserIsVip();
        LocalDate today = LocalDate.now();

        for (FsUser user : list) {
            LocalDate vipEndDate = user.getVipEndDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            if (vipEndDate.isBefore(today) || vipEndDate.isEqual(today)) {
                FsUser userMap = new FsUser();
                userMap.setUserId(user.getUserId());
                userMap.setIsVip(0);
                userMap.setVipStatus(2);
                fsUserMapper.updateFsUser(userMap);
                // 添加日志记录
                logger.info("User ID: " + user.getUserId() + " VIP status updated.");
            }
        }
        return R.ok();
    }

    public boolean isSameYearMonthDay(LocalDate date1, LocalDate date2) {
        return date1.isEqual(date2);
    }
}
