package com.fs.app.service.impl;

import com.fs.app.service.AppDrugResearchService;
import com.fs.app.service.AppTaskService;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.DictUtils;
import com.fs.common.utils.uuid.IdUtils;
import com.fs.company.enums.IsPeriodEnums;
import com.fs.delivery.domain.CtArticle;
import com.fs.delivery.domain.CtLongVideo;
import com.fs.delivery.domain.CtShortVideo;
import com.fs.delivery.mapper.CtArticleMapper;
import com.fs.delivery.mapper.CtLongVideoMapper;
import com.fs.delivery.mapper.CtShortVideoMapper;
import com.fs.doctor.domain.Doctor;
import com.fs.doctor.enums.IsOriginal;
import com.fs.doctor.mapper.DoctorMapper;
import com.fs.survey.domain.SurveyTaskPeriod;
import com.fs.survey.service.DynamicTableService;
import com.fs.survey.service.ISurveyTaskPeriodService;
import com.fs.system.vo.CommonCompanyInfo;
import com.fs.task.domain.TaskDeliveryInfo;
import com.fs.task.domain.TaskInfo;
import com.fs.task.dto.PublicIndexDto;
import com.fs.task.dto.TaskDeliveryDto;
import com.fs.task.enums.ServiceConfirmStatusEnum;
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.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 讲者Service业务层处理
 *
 * @author fs
 * @date 2025-12-30
 */
@Service
public class AppDrugResearchServiceImpl implements AppDrugResearchService {
    @Autowired
    private ISurveyTaskPeriodService surveyTaskPeriodService;
    @Autowired
    private DynamicTableService dynamicTableService;
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private TaskInfoMapper taskInfoMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertDrugResearch(Map<String, Object> dataMap, CommonCompanyInfo commonCompanyInfo) {
        String tableName = dataMap.get("table_name").toString();
        Long periodNum;
        // 问卷或者病例征集填写之后都要添加一个  已创建待审核的任务
//        TaskInfo entity = new TaskInfo();
        String doctorName = doctorMapper.selectDoctorNameById(Long.parseLong(dataMap.get("user_id").toString()));


        if (dataMap.get("periodNum") != null) {
            periodNum = Long.valueOf(dataMap.get("periodNum").toString());
            dataMap.remove("periodNum");
            // 把对应期数修改为已填
            SurveyTaskPeriod surveyTaskPeriod = new SurveyTaskPeriod();
            surveyTaskPeriod.setId(periodNum);
            surveyTaskPeriod.setIsPeriod(IsPeriodEnums.ENABLED.getCode());
            surveyTaskPeriodService.updateSurveyTaskPeriod(surveyTaskPeriod);

            // 任务类型为问卷
//            entity.setTaskType(9L);


            String taskTypeStr = DictUtils.getDictLabel("task_type", "9");
//            entity.setTaskName(String.format("%s医生的%s",doctorName,taskTypeStr));
        }else {
            // // 任务类型为病例征集
//            entity.setTaskType(8L);
            String taskTypeStr = DictUtils.getDictLabel("task_type", "8");
//            entity.setTaskName(String.format("%s医生的%s",doctorName,taskTypeStr));
        }

        dataMap.remove("table_name");

        dynamicTableService.insertDynamicData(tableName,dataMap);


//        // 一个医生一条任务
//        entity.setDoctorId(Long.parseLong(dataMap.get("user_id").toString()));
//        // 设置积分 可以从问卷表中拿或前端传
//        BigDecimal integral = BigDecimal.valueOf(20);
//
//        // 初始化状态
//        entity.setTaskStatus(TaskStatusEnum.UN_FINISHED.getCode());
//        entity.setCreateAuditStatus(1);
//        entity.setServiceConfirmStatus(ServiceConfirmStatusEnum.PENDING.getCode());
//        entity.setCreateTime(DateUtils.getNowDate());
//        entity.setTaskIntegral(integral);
//        entity.setApplyTime(DateUtils.getNowDate());
//        entity.setTaskStatusName(TaskStatusEnum.UN_FINISHED.getInfo());
//        entity.setTaskStatus(TaskStatusEnum.UN_FINISHED.getCode());
//
//        entity.setCompanyId(commonCompanyInfo.getCompanyId());
//        entity.setCompanyUserId(commonCompanyInfo.getCompanyUserId());
//        // ✅ 记录创建人类型  0客服(默认) 1系统用户
//        entity.setCaUserType(0);
//
//
//        // 临时编号
//        entity.setTaskNo("TMP_" + UUID.randomUUID().toString().replace("-", ""));
//
//
//        entity.setFinishAuditStatus(TaskFinishAuditStatus.NOCREATE.getCode());
//        taskInfoMapper.insertTaskInfo(entity);
    }
}
