package org.jeecg.modules.ctop.service.impl;

import cn.com.ctop.bytedance.mapper.MaterialReportMapper;
import cn.com.ctop.common.module.utils.Check;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.ctop.entity.Project;
import org.jeecg.modules.ctop.entity.ProjectMember;
import org.jeecg.modules.ctop.mapper.ProjectMemberMapper;
import org.jeecg.modules.ctop.service.IProjectMemberService;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目成员
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2019-11-19
 */
@Service
public class ProjectMemberServiceImpl extends ServiceImpl<ProjectMemberMapper, ProjectMember> implements IProjectMemberService {

    @Autowired
    private MaterialReportMapper materialReportMapper;
    @Autowired
    private ProjectMemberMapper memberMapper;

    @Override
    public void addMember(SysUser sysUser, Project project) {
        Map<String, Object> map = new HashMap<>();
        map.put("project_id", project.getId());
        map.put("user_id", sysUser.getId());
        this.removeByMap(map);
        ProjectMember member = new ProjectMember();
        member.setProjectId(project.getId());
        member.setProjectName(project.getProjectName());
        member.setUserId(sysUser.getId());
        member.setUserName(sysUser.getRealname());
        String roleCode = materialReportMapper.getRoleCodeByUserId(sysUser.getId());
        if (!Check.isNull(roleCode)) {
            member.setRoleCode(roleCode);
        }
        this.save(member);
    }

    /**
     * 查询所有项目
     *
     * @param userId
     * @return
     */
    @Override
    public List<Long> selectProjectsByUserId(String userId) {
        return memberMapper.selectProjectsByUserId(userId);
    }
}
