
package com.tmerclub.cloud.auth.controller;

import com.tmerclub.cloud.api.auth.bo.UidInfoBO;
import com.tmerclub.cloud.api.auth.constant.LoginType;
import com.tmerclub.cloud.api.auth.constant.SysTypeEnum;
import com.tmerclub.cloud.api.auth.vo.TokenInfoVO;
import com.tmerclub.cloud.api.delivery.feign.StationFeignClient;
import com.tmerclub.cloud.api.delivery.vo.StationVO;
import com.tmerclub.cloud.api.rbac.dto.ClearUserPermissionsCacheDTO;
import com.tmerclub.cloud.auth.dto.AuthenticationDTO;
import com.tmerclub.cloud.auth.manager.PasswordManager;
import com.tmerclub.cloud.auth.manager.TokenStore;
import com.tmerclub.cloud.auth.service.AuthAccountService;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.rbac.service.MenuPermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author tmerclub
 * @date 2020/6/30
 */
@RestController
@Tag(name = "自提点登录")
public class StationLoginController {

    @Autowired
    private TokenStore tokenStore;
    @Autowired
    private PasswordManager passwordManager;
    @Autowired
    private AuthAccountService authAccountService;
    @Autowired
    private MenuPermissionService menuPermissionService;
    @DubboReference
    private StationFeignClient stationFeignClient;


    @PostMapping("/ua/stationLogin")
    @Operation(summary = "自提点账号密码(用于前端登录)", description = "通过账号/手机号/用户名密码登录")
    public ServerResponseEntity<TokenInfoVO> login(@Valid @RequestBody AuthenticationDTO authenticationDTO) {
        authenticationDTO.setSysType(SysTypeEnum.STATION.value());
        // 账号密码校验
        ServerResponseEntity<UidInfoBO> uidInfoBOResponse = authAccountService.getUserInfoInTokenByInputUserNameAndPassword(
                authenticationDTO.getUserName(),
                authenticationDTO.getPassWord(),
                authenticationDTO.getSysType(),
                LoginType.PASSWORD.value()
        );

        if (!uidInfoBOResponse.isSuccess()) {
            return ServerResponseEntity.transform(uidInfoBOResponse);
        }

        UidInfoBO data = uidInfoBOResponse.getData();
        // 校验门店状态是否正常
        StationVO stationVO = stationFeignClient.getStationByInside(data.getTenantId()).getData();
        if (stationVO == null) {
            throw new LuckException("门店不存在");
        }
        if (!Objects.equals(stationVO.getStatus(), 1)) {
            throw new LuckException("该门店已关闭，请联系管理员");
        }
        // 清理权限缓存
        ClearUserPermissionsCacheDTO clearUserPermissionsCacheDTO = new ClearUserPermissionsCacheDTO();
        clearUserPermissionsCacheDTO.setSysType(data.getSysType());
        clearUserPermissionsCacheDTO.setUserId(data.getUserId());
        menuPermissionService.clearUserPermissionsCache(clearUserPermissionsCacheDTO.getUserId(), clearUserPermissionsCacheDTO.getSysType());
        data.setIsPassShop(1);
        // token存储返回
        return ServerResponseEntity.success(tokenStore.storeAndGetVo(data));
    }
}
