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.mapper.WorkUserProjectMapper;
import cn.wisenergy.model.app.WorkProject;
import cn.wisenergy.model.app.WorkUser;
import cn.wisenergy.model.app.WorkUserProject;
import cn.wisenergy.model.enums.ManagerEnum;
import cn.wisenergy.service.WorkUserProjectService;
import cn.wisenergy.service.WorkUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

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

    @Autowired
    private WorkUserProjectMapper workUserProjectMapper;

    @Autowired
    private WorkUserService workUserService;

    @Autowired
    WorkProjectMapper workProjectMapper;

    /**
     * 功能：根据userId获取项目id
     *
     * @param userId 用户id
     * @return 项目id
     */
    @Override
    public List<Integer> getProjectIdByUserId(Integer userId) {
        log.info("WorkUserProjectServiceImpl[]getProjectIdByUserId[]input.param.userId:{}" + userId);
        if (null == userId) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        QueryWrapper<WorkUserProject> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<WorkUserProject> workUserProjects = workUserProjectMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(workUserProjects)) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.WORK_PROJECT_IS_NULL);
        }
        ArrayList<Integer> list = new ArrayList<>();
        for (WorkUserProject project : workUserProjects) {
            list.add(project.getProjectId());
        }
        return list;
    }


    @Override
    public List<WorkProject> getUserManageProject(Integer userId) {
        log.info("WorkUserProjectServiceImpl[]getUserManageProject[]input.param" + userId);
        if (userId == null) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        WorkUser user = workUserService.getById(userId);
        Integer level = user.getLevel();
        if (!level.equals(ManagerEnum.IS_PROJECT_DIRECTOR.getCode()) || !level.equals(ManagerEnum.IS_DEPARTMENT_DIRECTOR.getCode())) {
            return null;
        }
        QueryWrapper<WorkProject> wrapper = new QueryWrapper<>();
        //项目负责人查看管理项目
        if (level.equals(ManagerEnum.IS_PROJECT_DIRECTOR.getCode())) {
            wrapper.eq("manager_id", user.getId());

        }
        //部门负责人查看部门项目
        if (level.equals(ManagerEnum.IS_DEPARTMENT_DIRECTOR.getCode())) {
            wrapper.eq("dept_id", user.getDeptId());
        }
        //项目未结
        wrapper.eq("is_conclusion", 0);
        //查询
        return workProjectMapper.selectList(wrapper);
    }
}
