
package com.tmerclub.cloud.user.controller.app;

import com.tmerclub.cloud.api.auth.constant.SysTypeEnum;
import com.tmerclub.cloud.api.auth.dto.ThirdApiAbutmentUserDto;
import com.tmerclub.cloud.api.auth.feign.AccountFeignClient;
import com.tmerclub.cloud.api.auth.feign.TokenFeignClient;
import com.tmerclub.cloud.api.auth.vo.AuthAccountVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.user.service.ThirdApiAbutmenService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author xxw
 * @version 1.0
 * @description: 用户端第三方接口对接
 * @since 2023/12/15 15:10
 */
@RestController()
@RequestMapping("/ua/server/user")
@Tag(name = "第三方接口对接(用户端)")
public class ThirdApiAbutmentController {

    @Autowired
    private ThirdApiAbutmenService thirdApiAbutmenService;
    @DubboReference
    private TokenFeignClient tokenStore;
    @Autowired
    private AccountFeignClient accountFeignClient;
    /**
     * 注册用户
     */
    @PostMapping("/register")
    @Operation(summary = "注册用户")
    public ServerResponseEntity<Void> regiserUser(@RequestBody ThirdApiAbutmentUserDto thirdApiAbutmentUserDto) {
        thirdApiAbutmenService.regiserUser(thirdApiAbutmentUserDto);
        return ServerResponseEntity.success();
    }

    /**
     * 更新用户名称
     */
    @PutMapping("/update")
    @Operation(summary = "更新用户名称")
    public ServerResponseEntity<Void> updateUser(@RequestBody ThirdApiAbutmentUserDto thirdApiAbutmentUserDto) {
        thirdApiAbutmenService.updateUser(thirdApiAbutmentUserDto);
        return ServerResponseEntity.success();
    }

    /**
     * 将用户token以及过期时间同步到商城
     */
    @PostMapping("/token")
    @Operation(summary = "将用户token以及过期时间同步到商城")
    public ServerResponseEntity<Void> userTokenSync(@RequestBody ThirdApiAbutmentUserDto thirdApiAbutmentUserDto) {
        // 获取用户在商城的uid
        ServerResponseEntity<AuthAccountVO> userIdAndSysType = accountFeignClient.getByUserIdAndSysType(thirdApiAbutmentUserDto.getUserId(), SysTypeEnum.ORDINARY.value());
        thirdApiAbutmentUserDto.setUid(userIdAndSysType.getData().getUid());
        tokenStore.userTokenSync(thirdApiAbutmentUserDto);
        return ServerResponseEntity.success();
    }


    /**
     * 禁用用户
     */
    @PutMapping("/disable")
    @Operation(summary = "禁用用户")
    public ServerResponseEntity<Void> disableUser(@RequestBody ThirdApiAbutmentUserDto thirdApiAbutmentUserDto) {
        thirdApiAbutmenService.disableUser(thirdApiAbutmentUserDto.getUserId());
        return ServerResponseEntity.success();
    }
}
