package cn.wisenergy.service.impl;

import cn.wisenergy.common.utils.exception.BASE_RESP_CODE_ENUM;
import cn.wisenergy.common.utils.exception.BaseCustomException;
import cn.wisenergy.mapper.WorkProjectMapper;
import cn.wisenergy.model.app.WorkProject;
import cn.wisenergy.model.app.WorkUser;
import cn.wisenergy.model.dto.ProjectDto;
import cn.wisenergy.model.enums.ManagerEnum;
import cn.wisenergy.service.WorkProjectService;
import cn.wisenergy.service.WorkUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chenqi
 * @since 2021-01-19
 */
@Service
@Slf4j
public class WorkProjectServiceImpl implements WorkProjectService {

    @Autowired
    private WorkProjectMapper workProjectMapper;

    @Autowired
    private WorkUserService workUserService;

    /**
     * 功能：根据项目id获取项目
     *
     * @param id
     * @return
     */
    @Override
    public WorkProject getById(Integer id) {
        log.info("WorkProjectServiceImpl[]getById[]input.param.id:" + id);
        if (null == id) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        WorkProject workProject = workProjectMapper.selectById(id);
        if (null == workProject) {
            return null;
        }
        return workProject;
    }

    @Override
    public List<ProjectDto> getNameById(List<Integer> ids) {
        log.info("WorkProjectServiceImpl[]getNameById[]input.param.ids:" + ids);
        if (CollectionUtils.isEmpty(ids)) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        List<WorkProject> workProjects = workProjectMapper.selectBatchIds(ids);
        ArrayList<ProjectDto> list = new ArrayList<>();
        for (WorkProject workProject : workProjects) {
            ProjectDto projectDto = new ProjectDto();
            BeanUtils.copyProperties(workProject, projectDto);
            list.add(projectDto);
        }

        return list;
    }

    /**
     * 功能： 查询当前用户所管理的项目
     *
     * @param id
     * @return
     */
    @Override
    public List<ProjectDto> getByManagerId(Integer id) {
        log.info("WorkProjectServiceImpl[]getByManagerId[]input.param.id:" + id);
        if (null == id) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        QueryWrapper<WorkProject> wrapper = new QueryWrapper<>();
        wrapper.eq("manager_id", id);
        List<WorkProject> workProjects = workProjectMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(workProjects)) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.MANAGER_NOT_PROJECT);
        }

        List<ProjectDto> projectDtos = new ArrayList<>();

        for (WorkProject workProject : workProjects) {
            ProjectDto projectDto = new ProjectDto();
            BeanUtils.copyProperties(workProject, projectDto);
            projectDtos.add(projectDto);
        }

        return projectDtos;
    }


    @Override
    public List<WorkProject> getUserManageProjects(Integer userId) {
        log.info("WorkProjectServiceImpl[]getUserManageProjects[]input.param" + userId);
        if (userId == null) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        WorkUser user = workUserService.getUserById(userId);
        List<WorkProject> workProjects = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        if (user.getLevel() == ManagerEnum.IS_PROJECT_DIRECTOR.getCode()) {
            map.put("managerId", userId);
            workProjects = workProjectMapper.getProjectsByCriteria(map);
        }
        //用户是部门级别，查询该部门下的所有项目
        if (user.getLevel() == ManagerEnum.IS_DEPARTMENT_DIRECTOR.getCode()) {
            map.put("deptId", user.getDeptId());
            workProjects = workProjectMapper.getProjectsByCriteria(map);
        }
        return workProjects;
    }
}
