package com.qnfhq.modules.c2c.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.map.MapUtil;
import com.qnfhq.annotation.RepeatSubmit;
import com.qnfhq.common.utils.MessageUtils;
import com.qnfhq.common.utils.Result;
import com.qnfhq.common.validator.ValidatorUtils;
import com.qnfhq.modules.c2c.dto.C2cMerchantApplyDTO;
import com.qnfhq.modules.c2c.dto.C2cMerchantCheckDTO;
import com.qnfhq.modules.c2c.entity.C2cMerchantConfigEntity;
import com.qnfhq.modules.c2c.entity.C2cMerchantEntity;
import com.qnfhq.modules.c2c.service.C2cMerchantConfigService;
import com.qnfhq.modules.c2c.service.C2cMerchantService;
import com.qnfhq.modules.c2c.service.C2cOrderService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.Map;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * C2C认证商家
 *
 * @author yelz 30262728@qq.com
 * @since 1.0.0 2025-11-17
 */
@RestController
@RequestMapping("/merchant")
@Tag(name="C2C认证商家表")
public class C2cMerchantController {

    @Resource
    private C2cMerchantService c2cMerchantService;

    @Resource
    private C2cOrderService c2cOrderService;

    @Resource
    private C2cMerchantConfigService c2cMerchantConfigService;

    @Operation(summary = "检查c2c商家申请条件")
    @PostMapping("/checkApply")
    public Result checkApply(@RequestBody C2cMerchantCheckDTO appUserMerchant) {
        ValidatorUtils.validateEntity(appUserMerchant);

        Result result = new Result();
        //查冻结保证金
        C2cMerchantConfigEntity merchantConfig = c2cMerchantConfigService.getMerchantConfig(appUserMerchant.getLegalCoin().toUpperCase());
        if(merchantConfig==null) {
            return result.error(MessageUtils.message("c2c.merchant.apply.not.config"));//未配置认证商家保证金
        }
        Integer marginL = merchantConfig.getMargin();
        BigDecimal margin = new BigDecimal(marginL);

        Map<String, Object> data = c2cMerchantService.checkApply(margin, StpUtil.getLoginIdAsLong());
        return result.ok(data);
    }

    @Operation(summary = "c2c商户申请")
    @PostMapping("/apply")
    @RepeatSubmit(interval = 3000, message = "请求过于频繁")
    public Result apply(@RequestBody C2cMerchantApplyDTO appUserMerchant) {
        c2cMerchantService.apply(appUserMerchant,StpUtil.getLoginIdAsLong());
        return new Result();
    }


    @Operation(summary = "商家详情")
    @GetMapping(value = "/info/{id}")
    public Result getInfo(@PathVariable("id") Long id)
    {
        C2cMerchantEntity merchant = c2cMerchantService.selectById(id);
        return new Result().ok(merchant);
    }


    @Operation(summary = "查询当前用户是否商家")
    @GetMapping(value = "/myinfo")
    public Result myinfo()
    {
        C2cMerchantEntity merchant = c2cMerchantService.getMerchantEntity(StpUtil.getLoginIdAsLong());
        Map<String,Object> data = MapUtil.of("merchant",merchant);
        data.put("isMt",merchant == null ? 0 : 1);
        return new Result().ok(data);
    }


}
