package com.fs.company.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.company.domain.CompanyUserChangeApplyUser;
import com.fs.company.mapper.CompanyUserChangeApplyUserMapper;
import com.fs.company.service.ICompanyUserChangeApplyUserService;
import com.fs.company.vo.CompanyUserChangeApplyUserVO;
import com.fs.system.mapper.SysDictDataMapper;
import com.fs.system.vo.DictVO;
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 CompanyUserChangeApplyUserServiceImpl extends ServiceImpl<CompanyUserChangeApplyUserMapper, CompanyUserChangeApplyUser> implements ICompanyUserChangeApplyUserService {

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询申请记录关联用户
     * @param applyId   申请记录ID
     * @return  list
     */
    @Override
    public List<CompanyUserChangeApplyUserVO> getApplyUsers(Long applyId) {
        List<CompanyUserChangeApplyUserVO> list = baseMapper.getApplyUsers(applyId);
        List<DictVO> dictVOS = sysDictDataMapper.selectDictDataListByType("sys_course_project");

        // Create a map for faster lookup (dictValue -> dictVO)
        Map<String, String> projectMap = dictVOS.stream()
                .collect(Collectors.toMap(DictVO::getDictValue, DictVO::getDictLabel));

        // Iterate through the list and set projectName where projectId matches dictValue
        for (CompanyUserChangeApplyUserVO vo : list) {
            if (vo.getProjectId() != null) {
                String projectName = projectMap.get(vo.getProjectId().toString());
                if (projectName != null) {
                    vo.setProjectName(projectName);
                }
            }
        }

        return list;
    }

    /**
     * 修改申请记录关联用户销售
     * @param applyId 申请记录ID
     */
    @Override
    public void changeUser(Long applyId) {
        baseMapper.changeUser(applyId);
    }
}
