package com.ningdatech.carapi.user.manage;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.carapi.common.constant.UserRoleTypeEnum;
import com.ningdatech.carapi.company.enumeration.CompanyTypeEnum;
import com.ningdatech.carapi.datascope.model.DataScopeDto;
import com.ningdatech.carapi.datascope.provider.DataScopeContext;
import com.ningdatech.carapi.driver.constant.UserCanLoginEnum;
import com.ningdatech.carapi.driver.entity.DriverInfo;
import com.ningdatech.carapi.driver.service.IDriverInfoService;
import com.ningdatech.carapi.sys.entity.Company;
import com.ningdatech.carapi.sys.entity.UserRole;
import com.ningdatech.carapi.sys.service.CompanyService;
import com.ningdatech.carapi.sys.service.UserRoleService;
import com.ningdatech.carapi.user.entity.NdUserAuth;
import com.ningdatech.carapi.user.entity.NdUserInfo;
import com.ningdatech.carapi.user.model.vo.UserBasicInfoVO;
import com.ningdatech.carapi.user.security.auth.constant.AuthTypeEnum;
import com.ningdatech.carapi.user.security.auth.model.UserInfoDetails;
import com.ningdatech.carapi.user.service.INdUserAuthService;
import com.ningdatech.carapi.user.service.INdUserInfoService;
import com.ningdatech.carapi.user.util.LoginUserUtil;

import lombok.RequiredArgsConstructor;

/**
 * @author liuxinxin
 * @date 2022/10/24 下午2:15
 */
@Component
@RequiredArgsConstructor
public class UserInfoManage {

    private final IDriverInfoService iDriverInfoService;

    private final CompanyService companyService;

    private final INdUserInfoService userInfoService;

    private final PasswordEncoder passwordEncoder;
    private final INdUserAuthService ndUserAuthService;
    private final UserRoleService userRoleService;

    public UserBasicInfoVO getUserBasicInfo() {
        UserBasicInfoVO userBasicInfoVO = new UserBasicInfoVO();
        UserInfoDetails user = LoginUserUtil.loginUserDetail();
        Long userId = user.getUserId();
        Long roleId = user.getRoleId();
        Optional<DataScopeDto> dataScope = DataScopeContext.getDataScope(user.getDataScope());
        if(dataScope.isPresent()){
            userBasicInfoVO.setMaxRegion(dataScope.get().getMaxRegion());
        }
        userBasicInfoVO.setNickName(user.getNickName());
        // 获取驾驶员信息
        DriverInfo driverInfo = iDriverInfoService.getOne(Wrappers.lambdaQuery(DriverInfo.class)
                .eq(DriverInfo::getUserId, userId));
        if (Objects.nonNull(driverInfo)) {
            userBasicInfoVO.setDriverId(driverInfo.getId());
        }
        userBasicInfoVO.setUserId(userId);
        userBasicInfoVO.setRoleId(roleId);
        userBasicInfoVO.setCompanyId(user.getCompanyId());
        userBasicInfoVO.setCompanyName(user.getCompanyName());
        userBasicInfoVO.setRegionId(user.getRegionId());
        userBasicInfoVO.setRegionName(user.getRegionName());

        return userBasicInfoVO;
    }

    public String createCompanyUser() {
        // 创建各企业企业信息员账号
        Map<Long, Company> companyMap = companyService.list().stream().collect(Collectors.toMap(Company::getId, c -> c));
        List<Company> companyList = companyService.list().stream().filter(c -> c.getCompanyType().equals(CompanyTypeEnum.PRODUCT_BU.getCode())
                || c.getCompanyType().equals(CompanyTypeEnum.LOGISTICS_BU.getCode())
                || c.getCompanyType().equals(CompanyTypeEnum.OTHER.getCode())).collect(Collectors.toList());
        for (Company company : companyList) {
            Long companyId = company.getId();
            Company companyInfo = companyMap.get(companyId);
            NdUserInfo userInfo = new NdUserInfo();
            userInfo.setCanLogin(UserCanLoginEnum.CAN_LOGIN.getCode());
            userInfo.setUsername(companyInfo.getName());
            userInfo.setCompanyId(companyId);
            userInfo.setCreateOn(LocalDateTime.now());
            userInfo.setUpdateOn(LocalDateTime.now());
            userInfoService.save(userInfo);

            Long userId = userInfo.getId();
            NdUserAuth userAuth = new NdUserAuth();
            userAuth.setUserId(userId);
            userAuth.setIdentifier(companyInfo.getName());
            userAuth.setCredential(passwordEncoder.encode("123456Aa"));
            userAuth.setAuthType(AuthTypeEnum.ACCOUNT_PASSWORD.getKey());
            userAuth.setIsAvailable(true);
            ndUserAuthService.save(userAuth);

            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(UserRoleTypeEnum.COMPANY_INFO_OFFICER_ROLE_ID.getId());
            userRoleService.save(userRole);
        }
        return "创建完成";
    }
}
