package com.zcloud.safetyDutyList.command.tasklist;

import com.alibaba.cola.exception.BizException;
import com.jjb.saas.framework.auth.model.SSOUser;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.safetyDutyList.domain.gateway.tasklist.FeedbackGateway;
import com.zcloud.safetyDutyList.domain.gateway.tasklist.TaskDetailGateway;
import com.zcloud.safetyDutyList.domain.gateway.tasklist.TaskExecutionGateway;
import com.zcloud.safetyDutyList.domain.model.tasklist.FeedbackE;
import com.zcloud.safetyDutyList.domain.model.tasklist.TaskDetailE;
import com.zcloud.safetyDutyList.domain.model.tasklist.TaskExecutionE;
import com.zcloud.safetyDutyList.domain.util.FeedbackCycleUtil;
import com.zcloud.safetyDutyList.dto.clientobject.tasklist.FeedbackCO;
import com.zcloud.safetyDutyList.dto.tasklist.FeedbackAddCmd;
import jodd.util.StringUtil;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 反馈新增执行器
 * <p>
 * 处理提交执行反馈的业务逻辑：
 *   1. 校验任务执行记录和任务模板是否存在
 *   2. 创建反馈实体并初始化，自动生成周期标识
 *   3. 保存反馈记录
 *   4. 返回新增后的反馈信息
 */
@Component
@AllArgsConstructor
public class FeedbackAddExe {
    private final FeedbackGateway feedbackGateway;
    private final TaskDetailGateway taskDetailGateway;
    private final TaskExecutionGateway taskExecutionGateway;

    public FeedbackCO execute(FeedbackAddCmd cmd) {

        SSOUser ssoUser = AuthContext.getCurrentUser();
        if (ssoUser == null) {
            throw new BizException("用户未登录");
        }

        TaskExecutionE executionE = taskExecutionGateway.getByTaskExecutionId(cmd.getTaskExecutionId());
        if (executionE == null) {
            throw new BizException("任务执行记录不存在");
        }
        TaskDetailE detailE = taskDetailGateway.getByTaskDetailId(executionE.getTaskDetailId());
        if (detailE == null) {
            throw new BizException("任务模板不存在");
        }

        FeedbackE feedbackE = new FeedbackE();
        if (StringUtil.isNotBlank(cmd.getFeedbackId())) {
            feedbackE.setFeedbackId(cmd.getFeedbackId());
        }else {
            feedbackE.init();
        }
        feedbackE.setTaskDetailId(executionE.getTaskDetailId());
        feedbackE.setTaskExecutionId(executionE.getTaskExecutionId());
        feedbackE.setTaskListId(executionE.getTaskListId());
        feedbackE.setTaskIssueId(executionE.getTaskIssueId());
        feedbackE.setFeedbackContent(cmd.getFeedbackContent());

        feedbackE.setFeedbackCorpId(ssoUser.getTenantId());
        feedbackE.setFeedbackDepartmentId(ssoUser.getOrgId());
        feedbackE.setFeedbackUserId(ssoUser.getUserId());

        LocalDateTime now = LocalDateTime.now();
        String periodFlag = FeedbackCycleUtil.generatePeriodFlag(detailE.getFeedbackCycleType(), now);
        feedbackE.setFeedbackPeriodFlag(periodFlag);
        feedbackE.setFeedbackTime(now);
        feedbackE.setFeedbackPeriodStartTime(FeedbackCycleUtil.getPeriodTime(periodFlag)[0]);
        feedbackE.setFeedbackPeriodEndTime(FeedbackCycleUtil.getPeriodTime(periodFlag)[1]);
        boolean res = feedbackGateway.add(feedbackE);
        if (!res) {
            throw new BizException("反馈保存失败");
        }

        FeedbackCO co = new FeedbackCO();
        BeanUtils.copyProperties(feedbackE, co);
        return co;
    }
}
