package com.cool.store.controller.webb;

import cn.hutool.jwt.JWTUtil;
import com.cool.store.request.YztTokenGenRequest;
import com.cool.store.response.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 三方验签
 * </p>
 *
 * @author wangff
 * @since 2026/4/12
 */
@Slf4j
@RestController
@RequestMapping("/pc/third/token")
@Api(tags = "三方验签")
public class ThirdTokenController {

    private static final long EXPIRE_SECONDS = 3600;

    @Value("${third.party.appKey}")
    private String appKey;
    @Value("${third.party.secret}")
    private String secret;

    @PostMapping("/generate")
    @ApiOperation("营帐通JWT")
    public ResponseResult<String> generateToken(@RequestBody @Validated YztTokenGenRequest request) {
        Map<String, Object> payload = new HashMap<>();
        payload.put("platform", "crm");
        payload.put("storeSn", request.getStoreNum());
        payload.put("appKey", appKey);
        payload.put("exp", System.currentTimeMillis() / 1000 + EXPIRE_SECONDS);
        return ResponseResult.success(JWTUtil.createToken(payload, secret.getBytes()));
    }
}
