package com.gxwebsoft.common.core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.entity.ShopCoupon;
import com.gxwebsoft.shop.entity.ShopUserCoupon;
import com.gxwebsoft.shop.service.ShopCouponService;
import com.gxwebsoft.shop.service.ShopUserCouponService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据库修复工具控制器
 * 仅在开发环境启用，用于修复数据库问题
 *
 * @author WebSoft
 * @since 2025-01-15
 */
@Slf4j
@Tag(name = "数据库修复工具")
@RestController
@RequestMapping("/api/database-fix")
// @ConditionalOnProperty(name = "spring.profiles.active", havingValue = "dev")
public class DatabaseFixController extends BaseController {

    @Autowired
    private ShopUserCouponService shopUserCouponService;

    @Autowired
    private ShopCouponService shopCouponService;

    @Operation(summary = "检查BigDecimal null值问题")
    @GetMapping("/check-bigdecimal-nulls")
    public ApiResult<Map<String, Object>> checkBigDecimalNulls() {
        try {
            Map<String, Object> result = new HashMap<>();

            // 检查用户优惠券表
            List<ShopUserCoupon> userCoupons = shopUserCouponService.list();
            long userCouponNullReducePrice = userCoupons.stream()
                    .mapToLong(c -> c.getReducePrice() == null ? 1 : 0)
                    .sum();
            long userCouponNullMinPrice = userCoupons.stream()
                    .mapToLong(c -> c.getMinPrice() == null ? 1 : 0)
                    .sum();

            // 检查优惠券模板表
            List<ShopCoupon> coupons = shopCouponService.list();
            long couponNullReducePrice = coupons.stream()
                    .mapToLong(c -> c.getReducePrice() == null ? 1 : 0)
                    .sum();
            long couponNullMinPrice = coupons.stream()
                    .mapToLong(c -> c.getMinPrice() == null ? 1 : 0)
                    .sum();

            Map<String, Object> userCouponStats = new HashMap<>();
            userCouponStats.put("totalRecords", userCoupons.size());
            userCouponStats.put("nullReducePrice", userCouponNullReducePrice);
            userCouponStats.put("nullMinPrice", userCouponNullMinPrice);

            Map<String, Object> couponStats = new HashMap<>();
            couponStats.put("totalRecords", coupons.size());
            couponStats.put("nullReducePrice", couponNullReducePrice);
            couponStats.put("nullMinPrice", couponNullMinPrice);

            result.put("shopUserCoupon", userCouponStats);
            result.put("shopCoupon", couponStats);
            result.put("needsFix", userCouponNullReducePrice > 0 || userCouponNullMinPrice > 0 ||
                                   couponNullReducePrice > 0 || couponNullMinPrice > 0);

            return success("检查完成", result);

        } catch (Exception e) {
            log.error("检查BigDecimal null值失败", e);
            return fail("检查失败: " + e.getMessage(),null);
        }
    }

    @Operation(summary = "修复BigDecimal null值问题")
    @PostMapping("/fix-bigdecimal-nulls")
    public ApiResult<Map<String, Object>> fixBigDecimalNulls() {
        try {
            Map<String, Object> result = new HashMap<>();
            int userCouponFixed = 0;
            int couponFixed = 0;

            // 修复用户优惠券表
            List<ShopUserCoupon> userCoupons = shopUserCouponService.list();
            for (ShopUserCoupon userCoupon : userCoupons) {
                boolean needUpdate = false;

                if (userCoupon.getReducePrice() == null) {
                    userCoupon.setReducePrice(BigDecimal.ZERO);
                    needUpdate = true;
                }

                if (userCoupon.getMinPrice() == null) {
                    userCoupon.setMinPrice(BigDecimal.ZERO);
                    needUpdate = true;
                }

                if (needUpdate) {
                    shopUserCouponService.updateById(userCoupon);
                    userCouponFixed++;
                }
            }

            // 修复优惠券模板表
            List<ShopCoupon> coupons = shopCouponService.list();
            for (ShopCoupon coupon : coupons) {
                boolean needUpdate = false;

                if (coupon.getReducePrice() == null) {
                    coupon.setReducePrice(BigDecimal.ZERO);
                    needUpdate = true;
                }

                if (coupon.getMinPrice() == null) {
                    coupon.setMinPrice(BigDecimal.ZERO);
                    needUpdate = true;
                }

                if (needUpdate) {
                    shopCouponService.updateById(coupon);
                    couponFixed++;
                }
            }

            result.put("userCouponFixed", userCouponFixed);
            result.put("couponFixed", couponFixed);
            result.put("totalFixed", userCouponFixed + couponFixed);

            log.info("BigDecimal null值修复完成: 用户优惠券{}条, 优惠券模板{}条", userCouponFixed, couponFixed);

            return success("修复完成", result);

        } catch (Exception e) {
            log.error("修复BigDecimal null值失败", e);
            return fail("修复失败: " + e.getMessage(), null);
        }
    }

    @Operation(summary = "测试优惠券接口")
    @GetMapping("/test-coupon-api")
    public ApiResult<Map<String, Object>> testCouponApi() {
        try {
            Map<String, Object> result = new HashMap<>();

            // 测试查询用户优惠券
            List<ShopUserCoupon> userCoupons = shopUserCouponService.list(
                    new QueryWrapper<ShopUserCoupon>().last("LIMIT 5")
            );

            // 测试查询优惠券模板
            List<ShopCoupon> coupons = shopCouponService.list(
                    new QueryWrapper<ShopCoupon>().last("LIMIT 5")
            );

            result.put("userCouponsCount", userCoupons.size());
            result.put("couponsCount", coupons.size());
            result.put("userCouponsSample", userCoupons);
            result.put("couponsSample", coupons);
            result.put("testStatus", "SUCCESS");

            return success("测试成功", result);

        } catch (Exception e) {
            log.error("测试优惠券接口失败", e);
            Map<String, Object> errorResult = new HashMap<>();
            errorResult.put("testStatus", "FAILED");
            errorResult.put("errorMessage", e.getMessage());
            errorResult.put("errorType", e.getClass().getSimpleName());

            return fail("测试失败: " + e.getMessage(), errorResult);
        }
    }

    @Operation(summary = "获取修复指南")
    @GetMapping("/guide")
    public ApiResult<Map<String, String>> getFixGuide() {
        Map<String, String> guide = new HashMap<>();

        guide.put("step1", "GET /api/database-fix/check-bigdecimal-nulls - 检查null值问题");
        guide.put("step2", "POST /api/database-fix/fix-bigdecimal-nulls - 修复null值问题");
        guide.put("step3", "GET /api/database-fix/test-coupon-api - 测试修复效果");
        guide.put("step4", "重启应用，验证优惠券功能正常");

        guide.put("note1", "此工具仅在开发环境可用");
        guide.put("note2", "修复前建议备份数据库");
        guide.put("note3", "修复完成后可以删除此控制器");

        return success("获取成功", guide);
    }
}
