package com.xiang.xservice.application.script.fwd.server;

import com.alibaba.fastjson.JSON;
import com.xiang.xservice.basic.common.resp.Result;
import com.xiang.xservice.application.script.fwd.service.DingTalkScriptFWDService;
import com.xiang.xservice.application.script.fwd.service.IPerformService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author: xiang
 * @Date: 2025-08-06 10:41
 */
@Slf4j
@RestController
@RequestMapping("/open/fwd/user")
@RequiredArgsConstructor
public class FwdUserController {

    private final IPerformService performService;
    private final DingTalkScriptFWDService dingTalkService;

    @GetMapping("/token")
    public Result<Void> updateUserTokenFromHttpProxy(@RequestParam Map<String, String> params) {
        String name = params.get("name");
        String token = params.get("token");
        log.info("接收到的参数有===>{}, name:{}, token:{}", JSON.toJSONString(params), name, token);
        Boolean b = performService.updateTokenByName(name, token);
        if (b) {
            String msg = "用户:" + name + "的token更新成功！";
            try {
                dingTalkService.sendScriptMsg(msg);
            } catch (Exception e) {
                log.error("消息发送失败", e);
                return Result.error("消息发送失败");
            }
        }
        return Result.success();
    }
}
