package com.ruoyi.database.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUserByPhone;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.database.domain.BaseCouponInfo;
import com.ruoyi.database.domain.dto.ReceiveCouponDto;
import com.ruoyi.database.service.BaseCouponInfoService;
import com.ruoyi.database.util.MoneyFormatUtil;
import com.ruoyi.framework.web.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;

import java.util.List;

/**
 * 优惠卷基础信息表(BaseCouponInfo)Controller
 *
 * @author hanrenchun
 * @since 2026-04-03 18:09:43
 */
@RequestMapping("/BaseCouponInfo")
@Api(tags = "优惠卷基础信息")
@RestController
@RequiredArgsConstructor
public class BaseCouponInfoController extends BaseController {

    private final BaseCouponInfoService baseCouponInfoService;
    private final TokenService tokenService;

    @GetMapping
    @ApiOperation("查询优惠卷基础信息表")
    public TableDataInfo<BaseCouponInfo> list(BaseCouponInfo baseCouponInfo) {
        startPage();
        QueryWrapper<BaseCouponInfo> queryWrapper = new QueryWrapper<>(baseCouponInfo);
        List<BaseCouponInfo> list = baseCouponInfoService.list(queryWrapper);
        long size = baseCouponInfoService.count(queryWrapper);
        return getDataTableEnhance(list, size);
    }

    @PostMapping
    @ApiOperation("新增优惠卷基础信息表")
    @Log(title = "优惠卷基础信息表", businessType = BusinessType.INSERT)
    public AjaxResult insert(@RequestBody BaseCouponInfo baseCouponInfo) {
        return toAjax(baseCouponInfoService.save(baseCouponInfo));
    }

    @PutMapping
    @ApiOperation("修改优惠卷基础信息表")
    @Log(title = "优惠卷基础信息表", businessType = BusinessType.UPDATE)
    public AjaxResult update(@RequestBody BaseCouponInfo baseCouponInfo) {
        return toAjax(baseCouponInfoService.updateById(baseCouponInfo));
    }

    @DeleteMapping
    @ApiOperation("删除优惠卷基础信息表")
    @Log(title = "优惠卷基础信息表", businessType = BusinessType.DELETE)
    public AjaxResult delete(@RequestParam("idList") List<Long> idList) {
        return toAjax(baseCouponInfoService.removeByIds(idList));
    }


    @GetMapping("/byOrderId")
    @ApiOperation("根据订单id查询优惠劵领取状态")
    public AjaxResult getBaseCouponInfo(@RequestParam String orderId, HttpServletRequest request) {
        LoginUserByPhone loginUserByPhone = tokenService.getLoginUserByPhone(request);
        if (loginUserByPhone == null) {
            return AjaxResult.error("用户未登录");
        }
//        if (amount.compareTo(new BigDecimal(300)) < 0) {
//            BaseCouponInfo couponInfo = new BaseCouponInfo();
//            couponInfo.setOrderId(orderId);
//            couponInfo.setReceiveState(false);
//            return AjaxResult.success(couponInfo);
//        }
        BaseCouponInfo baseCouponInfo = baseCouponInfoService.lambdaQuery()
                .eq(BaseCouponInfo::getOrderId, orderId)
                .last("LIMIT 1")
                .one();

        if (baseCouponInfo == null) {
            BaseCouponInfo couponInfo = new BaseCouponInfo();
            couponInfo.setOrderId(orderId);
//            couponInfo.setOrderAmount(amount);
            couponInfo.setReceiveState(true);
            couponInfo.setDiscountAmount(new BigDecimal("20.00"));
            couponInfo.setDiscountAmountCn(MoneyFormatUtil.formatMoney(couponInfo.getDiscountAmount()));
//            couponInfo.setOrderAmountCn(MoneyFormatUtil.formatMoney(amount));
            baseCouponInfoService.save(couponInfo);
            return AjaxResult.success(couponInfo);
        }

        baseCouponInfo.setDiscountAmountCn(MoneyFormatUtil.formatMoney(baseCouponInfo.getDiscountAmount()));
//        baseCouponInfo.setOrderAmountCn(MoneyFormatUtil.formatMoney(amount));
        return AjaxResult.success(baseCouponInfo);

    }


    @PostMapping("/receive")
    @ApiOperation("领取优惠卷")
    public AjaxResult receive(@RequestParam String orderId, @RequestParam String plateNo, HttpServletRequest request) {
        LoginUserByPhone loginUserByPhone = tokenService.getLoginUserByPhone(request);
        if (loginUserByPhone == null) {
            return AjaxResult.error("用户未登录");
        }

        BaseCouponInfo one = baseCouponInfoService.lambdaQuery()
                .eq(BaseCouponInfo::getOrderId, orderId)
                .one();
        if (one == null){
            BaseCouponInfo couponInfo = new BaseCouponInfo();
            couponInfo.setOrderId(orderId);
//            couponInfo.setOrderAmount(amount);
            couponInfo.setReceiveState(false);
            couponInfo.setPhone(loginUserByPhone.getPhone());
            couponInfo.setDiscountAmount(new BigDecimal("20.00"));
            couponInfo.setReceiveTime(System.currentTimeMillis() / 1000L);
            couponInfo.setPlateNo(plateNo);
            couponInfo.setDiscountAmountCn(MoneyFormatUtil.formatMoney(couponInfo.getDiscountAmount()));
//            couponInfo.setOrderAmountCn(MoneyFormatUtil.formatMoney(amount));
            baseCouponInfoService.save(couponInfo);
            return AjaxResult.success(couponInfo);
        }
        if (one.isReceiveState()){
            one.setReceiveState(false);
        }else {
            return AjaxResult.error("该优惠卷已被领取");
        }
        one.setPlateNo(plateNo);
        one.setPhone(loginUserByPhone.getPhone());
        one.setReceiveTime(System.currentTimeMillis() / 1000L);
        boolean b = baseCouponInfoService.updateById(one);
        if (b){
            return AjaxResult.success(one);
        }else {
            return AjaxResult.error("领取失败，稍后在试");
        }

    }


    public static void main(String[] args) {
        BigDecimal num1 = new BigDecimal("123.45");
        BigDecimal num2 = new BigDecimal("123.45");
        BigDecimal num3 = new BigDecimal("456.78");

        // 比较num1和num2
        int result1 = num1.compareTo(num2);
        System.out.println("num1 和 num2 的比较结果: " + result1); // 输出0，因为它们相等

        // 比较num1和num3
        int result2 = num1.compareTo(num3);
        System.out.println("num1 和 num3 的比较结果: " + result2); // 输出负值，因为num1小于num3

        // 比较num3和num1
        int result3 = num3.compareTo(num1);
        System.out.println("num3 和 num1 的比较结果: " + result3); // 输出正值，因为num3大于num1
    }


}
