package com.fs.app.service.impl;

import com.fs.audit.dto.CreateAuditRequestDTO;
import com.fs.audit.enums.AuditTypeEnum;
import com.fs.audit.service.AuditService;
import com.fs.doctor.domain.Doctor;
import com.fs.doctor.enums.IsOriginal;
import com.fs.task.dto.PublicIndexDto;
import com.fs.delivery.domain.CtLongVideo;
import com.fs.delivery.domain.CtShortVideo;
import com.fs.delivery.mapper.CtLongVideoMapper;
import com.fs.delivery.mapper.CtShortVideoMapper;
import com.fs.task.dto.TaskDeliveryDto;
import com.fs.app.service.AppTaskService;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.uuid.IdUtils;
import com.fs.delivery.domain.CtArticle;
import com.fs.delivery.mapper.CtArticleMapper;
import com.fs.task.domain.TaskDeliveryInfo;
import com.fs.task.domain.TaskInfo;
import com.fs.task.enums.TaskFinishAuditStatus;
import com.fs.task.enums.TaskStatusEnum;
import com.fs.task.mapper.TaskDeliveryInfoMapper;
import com.fs.task.mapper.TaskInfoMapper;
import com.fs.task.service.ITaskInfoService;
import com.huaweicloud.sdk.cdn.v1.model.TasksObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 讲者Service业务层处理
 *
 * @author fs
 * @date 2025-12-30
 */
@Service
public class AppTaskServiceImpl implements AppTaskService {

    @Autowired
    private TaskDeliveryInfoMapper taskDeliveryInfoMapper;
    @Autowired
    private TaskInfoMapper taskInfoMapper;
    @Autowired
    private ITaskInfoService taskInfoService;
    @Autowired
    private CtArticleMapper ctArticleMapper;
    @Autowired
    private CtLongVideoMapper ctLongVideoMapper;
    @Autowired
    private CtShortVideoMapper ctShortVideoMapper;
    @Autowired
    private AuditService auditService;

    @Override
    @Transactional
    public void completeTask(TaskDeliveryDto taskDeliveryDto) {

        String projectLink = "";
        // 通过type，判断资源进  4文章表，5短视频表，6长视频表
        if (taskDeliveryDto.getTaskType() == 4){
            CtArticle ctArticle = new CtArticle();
            ctArticle.setAttachmentUrl(taskDeliveryDto.getAttachmentUrl());
            ctArticle.setCoverImage(taskDeliveryDto.getCoverImage());
            ctArticle.setTagIds(taskDeliveryDto.getTagIds());
            ctArticle.setTitle(taskDeliveryDto.getTitle());
            ctArticle.setContent(taskDeliveryDto.getContent());
            ctArticle.setCompanyId(taskDeliveryDto.getCompanyId());
            ctArticle.setTaskType(4L);
            ctArticleMapper.insertCtArticle(ctArticle);
            projectLink = ctArticle.getId().toString();
        }else if (taskDeliveryDto.getTaskType() == 5){
            CtShortVideo ctShortVideo = new CtShortVideo();
            ctShortVideo.setVideoUrl(taskDeliveryDto.getAttachmentUrl());
            ctShortVideo.setCoverUrl(taskDeliveryDto.getCoverImage());
            ctShortVideo.setTags(taskDeliveryDto.getTagIds());
            ctShortVideo.setTitle(taskDeliveryDto.getTitle());
            ctShortVideo.setSummary(taskDeliveryDto.getContent());
            ctShortVideo.setCompanyId(taskDeliveryDto.getCompanyId());
            ctShortVideo.setTaskType(5L);
            ctShortVideo.setIsOriginal(IsOriginal.YES.getCode());
            ctShortVideoMapper.insertCtShortVideo(ctShortVideo);
            projectLink = ctShortVideo.getId().toString();
        }else if (taskDeliveryDto.getTaskType() == 6){
            CtLongVideo ctLongVideo = new CtLongVideo();
            ctLongVideo.setVideoUrl(taskDeliveryDto.getAttachmentUrl());
            ctLongVideo.setCoverUrl(taskDeliveryDto.getCoverImage());
            ctLongVideo.setTags(taskDeliveryDto.getTagIds());
            ctLongVideo.setTitle(taskDeliveryDto.getTitle());
            ctLongVideo.setSummary(taskDeliveryDto.getContent());
            ctLongVideo.setCompanyId(taskDeliveryDto.getCompanyId());
            ctLongVideo.setTaskType(6L);
            ctLongVideo.setIsOriginal(IsOriginal.YES.getCode());
            ctLongVideoMapper.insertCtLongVideo(ctLongVideo);
            projectLink = ctLongVideo.getId().toString();
        }else if (taskDeliveryDto.getTaskType() == 3){
            CtLongVideo ctLongVideo = new CtLongVideo();
            ctLongVideo.setVideoUrl(taskDeliveryDto.getAttachmentUrl());
            ctLongVideo.setCoverUrl(taskDeliveryDto.getCoverImage());
            ctLongVideo.setTags(taskDeliveryDto.getTagIds());
            ctLongVideo.setTitle(taskDeliveryDto.getTitle());
            ctLongVideo.setSummary(taskDeliveryDto.getContent());
            ctLongVideo.setCompanyId(taskDeliveryDto.getCompanyId());
            ctLongVideo.setTaskType(3L);
            ctLongVideo.setIsOriginal(IsOriginal.YES.getCode());
            ctLongVideoMapper.insertCtLongVideo(ctLongVideo);
            projectLink = ctLongVideo.getId().toString();
        }



        // 保存交付物信息
        TaskDeliveryInfo taskDeliveryInfo = new TaskDeliveryInfo();
        taskDeliveryInfo.setDeliveryName(taskDeliveryDto.getTitle());
        taskDeliveryInfo.setCreateTime(DateUtils.getNowDate());
        taskDeliveryInfo.setDeliveryNo(IdUtils.fastSimpleUUID());
        taskDeliveryInfo.setProjectLink(projectLink);
        taskDeliveryInfo.setTaskId(taskDeliveryDto.getTaskId());
        taskDeliveryInfo.setTaskType(taskDeliveryDto.getTaskType());
        taskDeliveryInfoMapper.insertTaskDeliveryInfo(taskDeliveryInfo);

        // 任务改为完成待审核
        TaskInfo taskInfo = new TaskInfo();
        taskInfo.setId(taskDeliveryInfo.getTaskId());
        taskInfo.setFinishAuditStatus(TaskFinishAuditStatus.PENDING.getCode());
        taskInfo.setAuditStatus(TaskStatusEnum.UN_FINISHED.getCode().longValue());
        taskInfo.setCompanyId(taskDeliveryDto.getCompanyId());
        taskInfoMapper.updateTaskInfo(taskInfo);

        TaskInfo entity = taskInfoMapper.selectTaskInfoById(taskDeliveryInfo.getTaskId());

        // 生成完成审核得审核流
        handleFinishAuditInit(entity, entity.getCompanyUserId());
        taskInfoMapper.updateTaskInfo(entity);

    }

    @Override
    public List<PublicIndexDto> getClassroomList(PublicIndexDto publicIndexDto) {
        List<PublicIndexDto> publicIndexDtoList = taskInfoMapper.getClassroomList(publicIndexDto);
        publicIndexDtoList.forEach(item -> {
            Doctor doctor = taskInfoMapper.getTaskDoctorDetail(item.getId().toString());
            item.setInstitution(doctor.getInstitution());
            item.setDoctorName(doctor.getDoctorName());
            item.setJobTitle(doctor.getJobTitle());
            item.setDepartment(doctor.getDepartment());
        });
        return publicIndexDtoList;
    }

    /**
     * 任务的创建审核初始化处理器
     *
     * @param taskInfo
     * @param userId
     */
    private void handleFinishAuditInit(TaskInfo taskInfo, Long userId) {
        CreateAuditRequestDTO createAuditRequestDTO = new CreateAuditRequestDTO();
        createAuditRequestDTO.setBusinessId(taskInfo.getId());
        createAuditRequestDTO.setBusinessType(AuditTypeEnum.TASK_COMPLETE_AUDIT.getCode());
        createAuditRequestDTO.setTempCode(AuditTypeEnum.TASK_COMPLETE_AUDIT.getCode());
        createAuditRequestDTO.setInitiatorId(userId);
        createAuditRequestDTO.setCompanyId(taskInfo.getCompanyId());
        createAuditRequestDTO.setCompanyId(taskInfo.getCompanyId());
        Long finishAuditInstanceId = auditService.createAudit(createAuditRequestDTO);
        taskInfo.setFinishAuditInstanceId(finishAuditInstanceId);
    }




}
