package com.zcloud.domain.command;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.exception.BizException;
import com.zcloud.domain.command.convertor.DomainDepartmentUserCoConvertor;
import com.zcloud.domain.domain.gateway.DomainDepartmentGateway;
import com.zcloud.domain.domain.model.DomainDepartmentE;
import com.zcloud.domain.dto.DomainDepartmentUpdateCmd;
import com.zcloud.domain.dto.DomainDepartmentUserAddCmd;
import com.zcloud.domain.persistence.dataobject.DomainDepartmentDO;
import com.zcloud.domain.persistence.dataobject.DomainDepartmentUserDO;
import com.zcloud.domain.persistence.dataobject.ProjectTaskScoreInfoDO;
import com.zcloud.domain.persistence.repository.DomainDepartmentRepository;
import com.zcloud.domain.persistence.repository.DomainDepartmentUserRepository;
import com.zcloud.domain.persistence.repository.ProjectTaskScoreInfoRepository;
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:28:52
 */
@Component
@AllArgsConstructor
public class DomainDepartmentUpdateExe {
    private final DomainDepartmentGateway domainDepartmentGateway;
    private final DomainDepartmentRepository domainDepartmentRepository;
    private final DomainDepartmentUserRepository domainDepartmentUserRepository;
    private final DomainDepartmentUserCoConvertor domainDepartmentUserCoConvertor;
    private final ProjectTaskScoreInfoRepository projectTaskScoreInfoRepository;

    @Transactional(rollbackFor = Exception.class)
    public void execute(DomainDepartmentUpdateCmd domainDepartmentUpdateCmd) {
        //查询是否存在
        DomainDepartmentDO info = domainDepartmentRepository.getById(domainDepartmentUpdateCmd.getId());
        if (info == null) {
            throw new BizException("领域管理部门不存在");
        }
        //查询领域类型是否存在
        domainDepartmentRepository.checkDomainType(domainDepartmentUpdateCmd.getDomainType(),domainDepartmentUpdateCmd.getId());

        DomainDepartmentE domainDepartmentE = new DomainDepartmentE();
        BeanUtils.copyProperties(domainDepartmentUpdateCmd, domainDepartmentE);
        boolean res = domainDepartmentGateway.update(domainDepartmentE);
        if (!res) {
            throw new BizException("修改失败");
        }
        // 如果删除的用户存在未完成的课题任务，不能删除
        List<DomainDepartmentUserDO> listByDomainDepartmentId = domainDepartmentUserRepository.getListByDomainDepartmentId(info.getDomainDepartmentId());
        //获取所有userId
        List<Long> oldUserIdList = listByDomainDepartmentId.stream().map(DomainDepartmentUserDO::getUserId).collect(Collectors.toList());
        List<DomainDepartmentUserAddCmd> domainDepartmentUserAddCmdList = domainDepartmentUpdateCmd.getDomainDepartmentUserAddCmdList();
        List<Long> newUserIdList = domainDepartmentUserAddCmdList.stream().map(DomainDepartmentUserAddCmd::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());
            // TODO
            if(CollUtil.isNotEmpty(deleteUserIdList)){
                List<ProjectTaskScoreInfoDO> projectTaskScoreInfoDOList =projectTaskScoreInfoRepository.getListByUserIdList(deleteUserIdList);
                if(CollUtil.isNotEmpty(projectTaskScoreInfoDOList)){
                    //获取按照userId去重，然后获取所有用户名称
                    List<String> userNameList = projectTaskScoreInfoDOList.stream().map(ProjectTaskScoreInfoDO::getUserName).distinct().collect(Collectors.toList());
                    throw new BizException(userNameList+"下有未完成课题任务，不能删除");
                }
            }
        }

        //删除人员信息
        domainDepartmentUserRepository.deleteByDomainDepartmentId(info.getDomainDepartmentId());


        List<DomainDepartmentUserDO> domainDepartmentUserDOList =domainDepartmentUserCoConvertor.converCmdsToDOs(domainDepartmentUserAddCmdList);
        domainDepartmentUserDOList.forEach(
                domainDepartmentUserDO-> {
                    domainDepartmentUserDO.setDomainDepartmentId(info.getDomainDepartmentId());
                    domainDepartmentUserDO.setDomainDepartmentUserId(Tools.get32UUID());
                }
        );
        domainDepartmentUserRepository.saveBatch(domainDepartmentUserDOList);
    }
}

