package com.zcloud.domain.command;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.exception.BizException;
import com.zcloud.domain.command.convertor.DomainGroupUserCoConvertor;
import com.zcloud.domain.domain.gateway.DomainGroupGateway;
import com.zcloud.domain.domain.model.DomainGroupE;
import com.zcloud.domain.dto.DomainDepartmentUserAddCmd;
import com.zcloud.domain.dto.DomainGroupUpdateCmd;
import com.zcloud.domain.dto.DomainGroupUserAddCmd;
import com.zcloud.domain.persistence.dataobject.*;
import com.zcloud.domain.persistence.repository.DomainGroupRepository;
import com.zcloud.domain.persistence.repository.DomainGroupUserRepository;
import com.zcloud.domain.persistence.repository.ProjectTaskGroupUserRepository;
import com.zcloud.gbscommon.utils.Tools;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2026-01-27 11:29:28
 */
@Component
@AllArgsConstructor
public class DomainGroupUpdateExe {
    private final DomainGroupGateway domainGroupGateway;
    private final DomainGroupRepository domainGroupRepository;
    private final DomainGroupUserCoConvertor domainDepartmentUserCoConvertor;
    private final DomainGroupUserRepository domainGroupUserRepository;
    private final ProjectTaskGroupUserRepository projectTaskGroupUserRepository;

    @Transactional(rollbackFor = Exception.class)
    public void execute(DomainGroupUpdateCmd domainGroupUpdateCmd) {
        DomainGroupDO info = domainGroupRepository.getById(domainGroupUpdateCmd.getId());
        if (info == null) {
            throw new BizException("领域组不存在");
        }
        DomainGroupE domainGroupE = new DomainGroupE();
        BeanUtils.copyProperties(domainGroupUpdateCmd, domainGroupE);
        boolean res = domainGroupGateway.update(domainGroupE);
        if (!res) {
            throw new BizException("修改失败");
        }
        // 如果删除的用户存在未完成的课题任务，不能删除
        List<DomainGroupUserDO> domainGroupUserDOList = domainGroupUserRepository.getListByGroupId(info.getDomainGroupId());
        //获取所有userId
        List<Long> oldUserIdList = domainGroupUserDOList.stream().map(DomainGroupUserDO::getUserId).collect(Collectors.toList());
        List<DomainGroupUserAddCmd> domainGroupUserAddCmdList1 = domainGroupUpdateCmd.getDomainGroupUserAddCmdList();
        List<Long> newUserIdList = domainGroupUserAddCmdList1.stream().map(DomainGroupUserAddCmd::getUserId).collect(Collectors.toList());

        if(CollUtil.isNotEmpty(oldUserIdList)){
            //获取删除的用户id,newUserIdList中不在oldUserIdList中的用户id
            List<Long> deleteUserIdList = oldUserIdList.stream().filter(userId -> !newUserIdList.contains(userId)).collect(Collectors.toList());
            if(CollUtil.isNotEmpty(deleteUserIdList)){
                List<ProjectTaskGroupUserDO> projectTaskGroupUserDOList =projectTaskGroupUserRepository.getListByUserIdList(deleteUserIdList);
                if(CollUtil.isNotEmpty(projectTaskGroupUserDOList)){
                    //获取按照userId去重，然后获取所有用户名称
                    List<String> userNameList = projectTaskGroupUserDOList.stream().map(ProjectTaskGroupUserDO::getUserName).distinct().collect(Collectors.toList());
                    throw new BizException(userNameList+"下有未完成课题任务，不能删除");
                }
            }
        }

        //删除
        domainGroupUserRepository.deleteByDomainGroupId(info.getDomainGroupId());
        List<DomainGroupUserAddCmd> domainGroupUserAddCmdList = domainGroupUpdateCmd.getDomainGroupUserAddCmdList();

        List<DomainGroupUserDO> domainGroupUserDOListResp =domainDepartmentUserCoConvertor.converCmdsToDOs(domainGroupUserAddCmdList);
        domainGroupUserDOListResp.forEach(
                domainGroupUserDO -> {
                    domainGroupUserDO.setDomainGroupId(info.getDomainGroupId());
                    domainGroupUserDO.setDomainGroupUserId(Tools.get32UUID());
                }
        );
        domainGroupUserRepository.saveBatch(domainGroupUserDOListResp);
    }
}

