package com.ningdatech.pmapi.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningdatech.basic.exception.BizException;
import com.ningdatech.pmapi.organization.model.entity.DingEmployeeInfo;
import com.ningdatech.pmapi.organization.model.entity.DingOrganization;
import com.ningdatech.pmapi.organization.service.IDingEmployeeInfoService;
import com.ningdatech.pmapi.organization.service.IDingOrganizationService;
import com.ningdatech.pmapi.user.entity.UserInfo;
import com.ningdatech.pmapi.user.mapper.NdUserInfoMapper;
import com.ningdatech.pmapi.user.service.IUserInfoService;
import com.wflow.workflow.bean.dto.ProcessInstanceUserDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lierbao
 * @since 2023-02-01
 */
@Service
@RequiredArgsConstructor
public class UserInfoServiceImpl extends ServiceImpl<NdUserInfoMapper, UserInfo> implements IUserInfoService {

    private final NdUserInfoMapper userInfoMapper;
    private final IDingEmployeeInfoService dingEmployeeInfoService;

    private final IDingOrganizationService dingOrganizationService;
    @Override
    public Map<String, ProcessInstanceUserDto> getUserMapByIds(Set<String> staterUsers) {
        List<UserInfo> userInfos = userInfoMapper.selectBatchIds(staterUsers);
        return userInfos.stream().map(u -> {
            ProcessInstanceUserDto userInfo = new ProcessInstanceUserDto();
            userInfo.setUserId(String.valueOf(u.getId()));
            userInfo.setUserName(u.getRealName());
            Long accountId = u.getAccountId();
            if (Objects.isNull(accountId)){
                throw new BizException("该用户没有录入浙政钉用户信息!");
            }
            // 根据浙政钉用户ID获取单位code
            DingEmployeeInfo employeeInfo = dingEmployeeInfoService.getOne(Wrappers.lambdaQuery(DingEmployeeInfo.class)
                    .eq(DingEmployeeInfo::getAccountId, accountId).last("limit 1"));
            String organizationCode = employeeInfo.getOrganizationCode();
            // 根据 单位code获取单位名称
            DingOrganization dingOrganization = dingOrganizationService.getOne(Wrappers.lambdaQuery(DingOrganization.class)
                    .eq(DingOrganization::getOrganizationCode, organizationCode).last("limit 1"));
            String organizationName = dingOrganization.getOrganizationName();
            userInfo.setOrgCode(organizationCode);
            userInfo.setOrgName(organizationName);
            return userInfo;
        }).collect(Collectors.toMap((ProcessInstanceUserDto::getUserId), v -> v));
    }

    @Override
    public ProcessInstanceUserDto getUserInfo(String userId) {
        UserInfo userInfo = userInfoMapper.selectById(Long.valueOf(userId));
        ProcessInstanceUserDto processInstanceUserDto = new ProcessInstanceUserDto();
        processInstanceUserDto.setUserId(String.valueOf(userInfo.getId()));
        processInstanceUserDto.setUserName(userInfo.getRealName());
        Long accountId = userInfo.getAccountId();
        if (Objects.isNull(accountId)){
            throw new BizException("该用户没有录入浙政钉用户信息!");
        }
        // 根据浙政钉用户ID获取部门code
        DingEmployeeInfo employeeInfo = dingEmployeeInfoService.getOne(Wrappers.lambdaQuery(DingEmployeeInfo.class)
                .eq(DingEmployeeInfo::getId, accountId));
        String organizationCode = employeeInfo.getOrganizationCode();
        // 根据部门code获取部门名称
        DingOrganization dingOrganization = dingOrganizationService.getOne(Wrappers.lambdaQuery(DingOrganization.class)
                .eq(DingOrganization::getOrganizationCode, organizationCode));
        String organizationName = dingOrganization.getOrganizationName();
        processInstanceUserDto.setOrgCode(organizationCode);
        processInstanceUserDto.setOrgName(organizationName);
        return processInstanceUserDto;
    }
}
