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.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;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 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,passWordRuleParam.getType())
                .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())
                .set(UserStrategy::getLockPwdCheck,passWordRuleParam.getLockPwdCheck())
                .set(UserStrategy::getLogout,passWordRuleParam.getLogoutDay())
                .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());
        passWordRuleParam.setLockPwdCheck(userStrategy.getLockPwdCheck());
        passWordRuleParam.setLogoutDay(userStrategy.getLogout());
//        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();
    }

    @Override
    public List<PassWordRuleParam> getUserStrategyList() {
        List<UserStrategy> list = iUserStrategyService.list();
        List<PassWordRuleParam> collect = list.stream().map(temp -> {
            PassWordRuleParam passWordRuleParam = new PassWordRuleParam();

            passWordRuleParam.setErrorsCount(temp.getLimitPwdTimes());
            passWordRuleParam.setReleaseTime(temp.getLockPwdTime());
            passWordRuleParam.setPasswordExpirationMonth(temp.getLimitPwdDate());
            passWordRuleParam.setMaxUseUser(temp.getMaxNum());
            passWordRuleParam.setSleepDay(temp.getSleep());
            passWordRuleParam.setLockPwdCheck(temp.getLockPwdCheck());
            passWordRuleParam.setLogoutDay(temp.getLogout());
            passWordRuleParam.setType(temp.getType());
//        AuthClient authClient = iAuthClientService.lambdaQuery().last(" limit 1").one();
//        passWordRuleParam.setSessionTime(authClient.getAccessTokenValidity());
//        passWordRuleParam.setSessionRefreshTime(authClient.getRefreshTokenValidity());
            return passWordRuleParam;
        }).collect(Collectors.toList());
        return collect;
    }
}
