package com.zcloud.domain.command;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.exception.BizException;
import com.zcloud.domain.command.convertor.ProjectTaskGroupUserCoConvertor;
import com.zcloud.domain.domain.enums.ProjectTaskExecuteStatusEnum;
import com.zcloud.domain.domain.gateway.ProjectTaskGroupUserGateway;
import com.zcloud.domain.domain.model.ProjectTaskGroupUserE;
import com.zcloud.domain.dto.ProjectTaskDetailAddCmd;
import com.zcloud.domain.dto.ProjectTaskGroupUserAddCmd;
import com.zcloud.domain.persistence.dataobject.ProjectTaskDO;
import com.zcloud.domain.persistence.dataobject.ProjectTaskGroupUserDO;
import com.zcloud.domain.persistence.repository.ProjectTaskGroupUserRepository;
import com.zcloud.domain.persistence.repository.ProjectTaskRepository;
import com.zcloud.gbscommon.todolistmq.TodoListEventPusherUtil;
import com.zcloud.gbscommon.todolistmq.event.TodoListCompleteEvent;
import com.zcloud.gbscommon.utils.Tools;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2026-01-29 11:28:09
 */
@Component
@AllArgsConstructor
@Slf4j
public class ProjectTaskGroupUserAddExe {
    private final ProjectTaskGroupUserGateway projectTaskGroupUserGateway;
    private final ProjectTaskGroupUserCoConvertor projectTaskGroupUserCoConvertor;
    private final ProjectTaskGroupUserRepository projectTaskGroupUserRepository;
    private final ProjectTaskRepository projectTaskRepository;
    private final TodoListEventPusherUtil todoListEventPusherUtil;
    @Transactional(rollbackFor = Exception.class)
    public boolean execute(ProjectTaskGroupUserAddCmd cmd) {
        ProjectTaskGroupUserE projectTaskGroupUserE = new ProjectTaskGroupUserE();
        BeanUtils.copyProperties(cmd, projectTaskGroupUserE);
        boolean res = false;
        try {
            res = projectTaskGroupUserGateway.add(projectTaskGroupUserE);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (!res) {
            throw new BizException("保存失败");
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveGroupUser(ProjectTaskDetailAddCmd cmd) {

        List<ProjectTaskGroupUserAddCmd> projectTaskGroupUserAddCmdList = cmd.getProjectTaskGroupUserAddCmdList();
        if(CollUtil.isEmpty(projectTaskGroupUserAddCmdList)){
            throw new BizException("请选择领域组成员");
        }
        List<ProjectTaskGroupUserDO> projectTaskGroupUserDOList = projectTaskGroupUserCoConvertor.converCmdsToDOs(projectTaskGroupUserAddCmdList);
        projectTaskGroupUserDOList.forEach(
                info -> {
                    info.setProjectTaskGroupUserId(Tools.get32UUID());
                    info.setProjectTaskId(cmd.getProjectTaskId());
                    info.setDomainGroupId(cmd.getDomainGroupId());
                    info.setExecuteCorpinfoId(cmd.getExecuteCorpinfoId());
                }
        );
        projectTaskGroupUserRepository.saveBatch(projectTaskGroupUserDOList);


        ProjectTaskDO projectTaskDO = projectTaskRepository.getInfoByTaskId(cmd.getProjectTaskId());
        if(projectTaskDO== null){
            throw new BizException("课题任务不存在");
        }
        Integer code = projectTaskDO.getExecuteStatus();
        if (new Date().compareTo(projectTaskDO.getExecutedStartDate()) >= 0) {
            if(new Date().compareTo(projectTaskDO.getExecutedEndDate())>=0){
                code = ProjectTaskExecuteStatusEnum.FINISHED.getCode();
            }else{
                code = ProjectTaskExecuteStatusEnum.EXECUTING.getCode();
            }

        }else{
            code = ProjectTaskExecuteStatusEnum.WAIT_PERFECT.getCode();
        }
        projectTaskRepository.updateExecuteStatusBatchById(code, Arrays.asList(projectTaskDO.getId()));
        try{
            //评分完成待办
            TodoListCompleteEvent event1 = new TodoListCompleteEvent();
            event1.setForeignSubsidiaryKey(projectTaskDO.getId());// 业务附表ID 没有附表时为foreignKey的值
            log.info("完成待办,请求：{}",projectTaskDO.getId());
            boolean b = todoListEventPusherUtil.sendMessageCompleteEvent(event1);
            log.info("完成待办,结果：{}",b);
        }catch (Exception e){
            log.error("删除待办失败",e);
        }
    }
}

