package com.njcn.user.service.impl;

import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.user.pojo.param.PassWordRuleParam;
import com.njcn.user.pojo.po.AuthClient;
import com.njcn.user.pojo.po.User;
import com.njcn.user.pojo.po.UserStrategy;
import com.njcn.user.service.IAuthClientService;
import com.njcn.user.service.IUserService;
import com.njcn.user.service.IUserStrategyService;
import com.njcn.user.service.IPassWordRuleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Description:
 * Date: 2024/7/11  18:06【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class PassWordRuleServiceImpl implements IPassWordRuleService {

    private final IUserStrategyService iUserStrategyService;

    private final IAuthClientService iAuthClientService;

    private final IUserService userService;
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Boolean ruleUpdate(PassWordRuleParam passWordRuleParam) {

        iUserStrategyService.lambdaUpdate().eq(UserStrategy::getType,"1")
                .eq(UserStrategy::getState, DataStateEnum.ENABLE.getCode())
                .set(UserStrategy::getLimitPwdTimes,passWordRuleParam.getErrorsCount())
                .set(UserStrategy::getLimitPwdDate,passWordRuleParam.getPasswordExpirationMonth())
                .set(UserStrategy::getLockPwdTime,passWordRuleParam.getReleaseTime())
                .set(UserStrategy::getMaxNum,passWordRuleParam.getMaxUseUser())
                .set(UserStrategy::getSleep,passWordRuleParam.getSleepDay())
                .update();

        iAuthClientService.lambdaUpdate()
                .set(AuthClient::getRefreshTokenValidity,passWordRuleParam.getSessionRefreshTime())
                .set(AuthClient::getAccessTokenValidity,passWordRuleParam.getSessionTime())
                .update();
        return true;
    }

    @Override
    public PassWordRuleParam getRule() {
        PassWordRuleParam passWordRuleParam = new PassWordRuleParam();
        UserStrategy userStrategy = iUserStrategyService.getUserStrategy(1);

        passWordRuleParam.setErrorsCount(userStrategy.getLimitPwdTimes());
        passWordRuleParam.setReleaseTime(userStrategy.getLockPwdTime());
        passWordRuleParam.setPasswordExpirationMonth(userStrategy.getLimitPwdDate());
        passWordRuleParam.setMaxUseUser(userStrategy.getMaxNum());
        passWordRuleParam.setSleepDay(userStrategy.getSleep());

        AuthClient authClient = iAuthClientService.lambdaQuery().last(" limit 1").one();
        passWordRuleParam.setSessionTime(authClient.getAccessTokenValidity());
        passWordRuleParam.setSessionRefreshTime(authClient.getRefreshTokenValidity());
        return passWordRuleParam;
    }

    @Override
    public Boolean unlockRoot() {
        userService.lambdaUpdate().eq(User::getLoginName,"root").set(User::getState,DataStateEnum.ENABLE.getCode()).update();
        return true;
    }

    @Override
    public UserStrategy getUserStrategy() {
        return iUserStrategyService.lambdaQuery()
                .eq(UserStrategy::getType, 1)
                .eq(UserStrategy::getState, DataStateEnum.ENABLE.getCode())
                .one();
    }
}
