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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.database.entity.ExperimentUser;
import org.jeecg.modules.database.mapper.ExperimentUserMapper;
import org.jeecg.modules.database.service.IExperimentUserService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Description: 试验人员
 * @Author: jeecg-boot
 * @Date: 2024-08-30
 * @Version: V1.0
 */
@Service
public class ExperimentUserServiceImpl extends ServiceImpl<ExperimentUserMapper, ExperimentUser> implements IExperimentUserService {

    @Autowired
    private ISysUserService userService;

    @Override
    public List<ExperimentUser> getByExperimentId(String experimentId) {
        return list(new LambdaQueryWrapper<ExperimentUser>().eq(ExperimentUser::getExperimentId, experimentId));
    }

    @Override
    public void removeByExperimentId(String experimentId) {
        List<ExperimentUser> userList = getByExperimentId(experimentId);
        userList.forEach(user -> removeById(user.getId()));
    }

    @Override
    public void fetchUserName(ExperimentUser experimentUser) {
        if (Objects.isNull(experimentUser)) {
            return;
        }
        SysUser user = userService.getById(experimentUser.getUserId());
        if (Objects.nonNull(user)) {
            experimentUser.setUserName(user.getRealname());
        }
    }
}
