package com.itsol.quantrivanphong.manager.project.projectgroup.bussiness;

import com.itsol.quantrivanphong.model.Project;
import com.itsol.quantrivanphong.manager.project.project.repository.ProjectRepository;
import com.itsol.quantrivanphong.manager.project.projectgroup.common.EmployeeRepositoryImpl;
import com.itsol.quantrivanphong.manager.project.projectgroup.common.ProjectGroupUtils;
import com.itsol.quantrivanphong.manager.project.projectgroup.dto.ProjectGroupDTO;
import com.itsol.quantrivanphong.model.Eproject;
import com.itsol.quantrivanphong.manager.project.projectgroup.repository.ProjectGroupRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectGroupBussinessImpl implements ProjectGroupBussiness {

    @Autowired
    private ProjectGroupRepository projectGroupRepository;

    @Autowired
    private ProjectRepository projectRepository;

    @Autowired
    private EmployeeRepositoryImpl employeeRepository;

    @Autowired
    private ProjectGroupUtils utils;

    @Override
    public List<ProjectGroupDTO> getGroupByProjectId(Integer projectId) {
        Project project = projectRepository.findByEmtityId(projectId);
        if (project != null) {
            List<Eproject> lstModels = projectGroupRepository.finByProperty("project", project, "joinDate", "DESC");
            List<ProjectGroupDTO> lstDTO = utils.getListDTO(lstModels);
            return lstDTO;
        }
        return null;
    }

    @Override
    public String saveEmployeeProject(ProjectGroupDTO dto) {
        String views = "";
        try{
            projectGroupRepository.saveEntity(utils.Model(dto));
            views="thêm thành công nhân viên vào nhóm dự án";
        }catch (Exception e){
            views="thêm không thành công!";
        }
        return views;
    }

    @Override
    public String updateEmployeeProject(ProjectGroupDTO dto) {
        String views="";
        Eproject eproject = projectGroupRepository.findByEmtityId(dto.getId());
        if(eproject!=null){
            Eproject updateProjectGroup = projectGroupRepository.updateEntity(utils.Model(dto));
            if(updateProjectGroup!=null){
                views = "Sửa thành công";
            }
        }else{
            views = "đối tượng không tồn tại";
        }
        return null;
    }

    @Override
    public ProjectGroupDTO findEmployeeProject(String username) {
        return null;
    }

    @Override
    public String outProjectGroup(Integer[] ids) {
        String views = "";
        int count=0;
        count = projectGroupRepository.deleteEntity(ids);
        if(count!=0){
            views="Bạn đã cho "+count+" thoát ra khỏi nhóm";
        }else{
            views=" thành viên không tồn tại";
        }
        return views;
    }
}
