package jnpf.personnels.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.base.UserInfo;
import jnpf.cultivate.utils.CultivatePerUtils;
import jnpf.model.enums.FtbPersonnelsCofigEnum;
import jnpf.model.personnels.dto.audit.FtbPersonnelsAuditSubConfigVO;
import jnpf.model.personnels.dto.config.MasterConfigUserBoudDto;
import jnpf.model.personnels.po.FtbPersonnelsAuditSubConfig;
import jnpf.model.personnels.vo.task.FtbPersonnelsAuditInfoVO;
import jnpf.personnels.mapper.FtbPersonnelsAuditSubConfigMapper;
import jnpf.personnels.service.FtbPersonnelsAuditSubConfigService;
import jnpf.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class FtbPersonnelsAuditSubConfigServiceImpl extends ServiceImpl<FtbPersonnelsAuditSubConfigMapper, FtbPersonnelsAuditSubConfig> implements FtbPersonnelsAuditSubConfigService {

    @Autowired
    private CultivatePerUtils cultivate;

    @Override
    public FtbPersonnelsAuditInfoVO queryAuditSubConfig(String orgId, FtbPersonnelsCofigEnum cofigEnum) {
        UserInfo user = UserProvider.getUser();
        String userId = user.getUserId();
        FtbPersonnelsAuditSubConfigVO auditSubConfigVO = baseMapper.queryAuditSubConfig(orgId, userId, cofigEnum.getConfigType());
        if (ObjectUtil.isEmpty(auditSubConfigVO)) {
            return null;
        }
        return FtbPersonnelsAuditSubConfigVO.coverFtbPersonnelsAuditInfoVO(auditSubConfigVO);
    }

    @Override
    public List<Map<String, Object>> isItAnApprover(String userId) {
        List<Map<String, Object>> itAnApprover = baseMapper.isItAnApprover(userId);
        List<String> orgId = itAnApprover.stream().map(item -> String.valueOf(item.get("orgId"))).collect(Collectors.toList());
        Map<String, String> map = cultivate.convertOrganizationalId(orgId);
        Map<String, String> orgNames = cultivate.queryOrgNames(orgId);
        itAnApprover.forEach(item -> {
            Integer configType = Integer.valueOf(String.valueOf(item.get("configType")));
            String configName = FtbPersonnelsCofigEnum.getConfigName(configType);
            String orgId1 = String.valueOf(item.get("orgId"));
            item.put("orgId", map.get(orgId1));
            if (orgNames.containsKey(orgId1)) {
                item.put("orgName", orgNames.get(orgId1));
            }
            item.put("configType", configName);
        });
        return itAnApprover;
    }

    @Override
    public List<MasterConfigUserBoudDto> queryUserBound(String userId) {
        return baseMapper.queryUserBound(userId);
    }

    @Override
    public Long checkWhetherTheCurrentPersonIsTheApprover(String userId) {
        return baseMapper.checkWhetherTheCurrentPersonIsTheApprover(userId);
    }
}
