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.WorkUserProject;
import cn.wisenergy.model.dto.UserProjectDto;
import cn.wisenergy.service.WorkUserProjectService;
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
    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)) {
            ArrayList<Integer> list = new ArrayList<>();
            for (WorkUserProject project : workUserProjects) {
                list.add(project.getProjectId());
            }
            return list;
        }
        return null;
    }




    /**
     * 根据项目id获取参与人数
     *
     * @param projectId 项目id
     * @return dto
     */
    @Override
    public List<UserProjectDto> getUserProjectByProjectId(Integer projectId) {
        log.info("WorkUserProjectServiceImpl[]getUserProjectByProjectId[]input.param.projectId:{}" + projectId);
        if (null == projectId) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        List<UserProjectDto> userByProjectId = workUserProjectMapper.getUserByProjectId(projectId);
        if (CollectionUtils.isEmpty(userByProjectId)) {
            return null;
        }
        return userByProjectId;
    }


    /**
     * 根据项目id删除数据
     * @param projectId 项目id
     * @return boolean
     */
    @Override
    public Boolean deleteByProjectId(Integer projectId) {
        log.info("WorkUserProjectServiceImpl[]deleteByProjectId[]input.param.projectId:{}" + projectId);
        if (null == projectId) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        int i = workUserProjectMapper.deleteByProjectId(projectId);
        return i != 0;
    }

    /**
     * 批量插入
     * @param userIds 用户id
     * @param projectId 项目id
     * @return boolean
     */
    @Override
    public Boolean insertBatch(List<Integer> userIds, Integer projectId) {
        log.info("WorkUserProjectServiceImpl[]deleteByProjectId[]input.param.userIds:{}projectId:{}" + userIds,projectId);
        if (CollectionUtils.isEmpty(userIds) || null == projectId) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        int i = workUserProjectMapper.insertBatch(userIds, projectId);
        return i != 0;
    }
}
