
package com.tmerclub.cloud.platform.controller;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import com.tmerclub.cloud.api.auth.constant.SysTypeEnum;
import com.tmerclub.cloud.api.auth.feign.AccountFeignClient;
import com.tmerclub.cloud.api.auth.vo.AuthAccountVO;
import com.tmerclub.cloud.api.platform.vo.SysUserVO;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.PrincipalUtil;
import com.tmerclub.cloud.platform.dto.ChangeAccountDTO;
import com.tmerclub.cloud.platform.service.SysUserAccountService;
import com.tmerclub.cloud.platform.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author lhd
 * @date 2020/12/21
 */
@RequestMapping("/p/sys_user/account")
@RestController
@Tag(name = "平台用户账号信息")
public class SysUserAccountController {

    @Value("${tmerclub.expose.operation.auth:}")
    private Boolean permission;

    private final SysUserAccountService SysUserAccountService;

    private final SysUserService SysUserService;

    private final AccountFeignClient accountFeignClient;

    public SysUserAccountController(SysUserAccountService sysUserAccountService, SysUserService sysUserService, AccountFeignClient accountFeignClient) {
        this.SysUserAccountService = sysUserAccountService;
        this.SysUserService = sysUserService;
        this.accountFeignClient = accountFeignClient;
    }


    @GetMapping
    @Operation(summary = "获取账号信息", description = "获取账号信息")
    public ServerResponseEntity<AuthAccountVO> getAccount(Long userId) {
        return SysUserAccountService.getByUserIdAndSysType(userId, AuthUserContext.get().getSysType());
    }


    @PostMapping
    @Operation(summary = "添加账号", description = "添加账号")
    public ServerResponseEntity<Void> addAccount(@Valid @RequestBody ChangeAccountDTO changeAccountDTO) {
        SysUserVO sysUserVO = SysUserService.getByUserId(changeAccountDTO.getUserId());
        if (sysUserVO == null) {
            return ServerResponseEntity.showFailMsg("无法获取平台账户信息");
        }
        if (Objects.equals(sysUserVO.getHasAccount(), 1)) {
            return ServerResponseEntity.showFailMsg("已有账号，无需重复添加");
        }
        if (StrUtil.isBlank(changeAccountDTO.getPassword())) {
            return ServerResponseEntity.showFailMsg("密码不能为空");
        }
        return SysUserAccountService.save(changeAccountDTO);
    }

    @PutMapping
    @Operation(summary = "修改账号", description = "修改账号")
    public ServerResponseEntity<Void> updateAccount(@Valid @RequestBody ChangeAccountDTO changeAccountDTO) {
        if (Objects.equals(changeAccountDTO.getUserId().intValue(), Constant.SUPER_ADMIN_ID) && BooleanUtil.isFalse(permission)) {
            throw new LuckException("暂无权限修改该用户的账户信息");
        }
        SysUserVO sysUserVO = SysUserService.getByUserId(changeAccountDTO.getUserId());
        if (sysUserVO == null || Objects.equals(sysUserVO.getHasAccount(), 0)) {
            return ServerResponseEntity.showFailMsg("无法获取账户信息");
        }
        ServerResponseEntity<AuthAccountVO> authAccountEntity = accountFeignClient.getByUserIdAndType(sysUserVO.getSysUserId(), SysTypeEnum.PLATFORM.value());
        if (!authAccountEntity.isSuccess()) {
            throw new LuckException(authAccountEntity.getMsg());
        }
        AuthAccountVO authAccountVO = authAccountEntity.getData();
        if (Objects.equals(authAccountVO.getIsAdmin(), 1) && !Objects.equals(sysUserVO.getSysUserId(), AuthUserContext.get().getUserId())) {
            return ServerResponseEntity.showFailMsg("您没有权限修改管理员信息");
        }
        if (!PrincipalUtil.isDbPhone(changeAccountDTO.getPhone(), authAccountVO.getPhone(), true)) {
            throw new LuckException("请输入正确的手机号");
        }
        if (changeAccountDTO.getPhone().contains(Constant.ASTERISK)) {
            changeAccountDTO.setPhone(authAccountVO.getPhone());
        }
        return SysUserAccountService.update(changeAccountDTO);
    }
}
