package cd.casic.ci.process.process.service.group.impl;

import cd.casic.ci.process.dto.req.group.GroupAddReq;
import cd.casic.ci.process.dto.req.group.GroupListReq;
import cd.casic.ci.process.dto.req.group.GroupUpdateReq;
import cd.casic.ci.process.dto.resp.group.GroupListResp;
import cd.casic.ci.process.process.converter.GroupConverter;
import cd.casic.ci.process.process.dao.pipeline.PipGroupDao;
import cd.casic.ci.process.process.dataObject.pipgroup.PipGroup;
import cd.casic.ci.process.process.service.group.GroupService;
import cd.casic.ci.process.process.service.pipeline.PipelineService;
import cd.casic.ci.process.util.WebFrameworkUtils;
import cd.casic.framework.commons.exception.ServiceException;
import cd.casic.framework.commons.exception.enums.GlobalErrorCodeConstants;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class GroupServiceImpl extends ServiceImpl<PipGroupDao, PipGroup> implements GroupService {
    @Resource
    private GroupConverter converter;
    @Resource
    private PipGroupDao groupDao;
    @Resource
    private PipelineService pipelineService;
    @Override
    public void addGroup(GroupAddReq req) {
        PipGroup pipGroup = converter.reqToDto(req);
        save(pipGroup);
    }

    @Override
    public void editGroup(GroupUpdateReq req) {
        PipGroup pipGroup = converter.reqToDto(req);
        updateById(pipGroup);
    }

    @Override
    public List<GroupListResp> listGroup(GroupListReq req) {
        // 查询数量
        return groupDao.getList(req.getGroupName(), WebFrameworkUtils.getLoginUserIdStr());
    }

    @Override
    public void deleteGroup(String id) {
        if (pipelineService.getGroupCount(id) != 0) {
            throw new ServiceException(GlobalErrorCodeConstants.PIPELINE_ERROR.getCode(),"分组下辖流水线不为空不允许删除");
        }
        removeById(id);
    }
}
