package com.fs.app.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fs.app.annotation.Login;
import com.fs.app.dto.DrugResearchDto;
import com.fs.app.service.AppDrugResearchService;
import com.fs.common.enums.TaskLabelEnum;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.DictUtils;
import com.fs.company.enums.IsPeriodEnums;
import com.fs.live.domain.LiveBroadcast;
import com.fs.live.dto.TaskLiveDto;
import com.fs.survey.domain.*;
import com.fs.survey.dto.PeriodDataDto;
import com.fs.common.core.domain.AjaxResult;
import com.fs.doctor.domain.Doctor;
import com.fs.doctor.service.IDoctorService;
import com.fs.survey.service.*;
import com.fs.system.vo.CommonCompanyInfo;
import com.fs.task.domain.TaskInfo;
import com.fs.task.enums.ServiceConfirmStatusEnum;
import com.fs.task.enums.TaskFinishAuditStatus;
import com.fs.task.enums.TaskFinishStatusEnum;
import com.fs.task.enums.TaskStatusEnum;
import com.fs.task.mapper.TaskInfoMapper;
import com.fs.task.service.ITaskInfoService;
import com.fs.task.vo.TaskInfoCreateVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;


@Api(tags = "用药调研管理")
@RestController
@RequestMapping(value="/app/drugResearch")
public class DrugResearchController extends AppBaseController {

    @Autowired
    private IDrugResearchService iDrugResearchService;
    @Autowired
    private ISurveyQuestionnaireVersionService iSurveyQuestionnaireVersionService;
    @Autowired
    private ISurveyQuestionnaireService iSurveyQuestionnaireService;
    @Autowired
    private IDoctorService iDoctorService;
    @Autowired
    private ISurveyTaskService iSurveyTaskService;
    @Autowired
    private ISurveyTaskPeriodService surveyTaskPeriodService;
    @Autowired
    private AppDrugResearchService appDrugResearchService;
    @Autowired
    private TaskInfoMapper taskInfoMapper;


    @Login
    @ApiOperation("获取用药调研主列表")
    @PostMapping("/getDrugResearchList")
    public AjaxResult getDrugResearchList(@RequestBody DrugResearch drugResearch) {
        startPage();
//        String userId = getUserId();
//        drugResearch.setDoctorId(Long.valueOf(userId));
        List<DrugResearch> drugResearches = iDrugResearchService.selectDrugResearchList(drugResearch);
        List<DrugResearch> newLive = new ArrayList<>();

        drugResearches.forEach(item -> {
            String targetId = TaskLabelEnum.drug + "-" + item.getId();
            TaskInfo taskInfo = taskInfoMapper.getTaskByTargetId(targetId);
            if (taskInfo != null){
                // 获取枚举值
                TaskFinishStatusEnum statusEnum = TaskFinishStatusEnum.fromCode(taskInfo.getFinishStatus().intValue());
                taskInfo.setFinishStatusName(statusEnum.getDesc());
                TaskLiveDto taskLiveDto = new TaskLiveDto();
                BeanUtils.copyProperties(taskInfo, taskLiveDto);
                item.setTaskLiveDto(taskLiveDto);
                newLive.add(item);
            }

        });


        return AjaxResult.success(getDataTable(newLive));
    }

    @Login
    @ApiOperation("获取用药调研详细信息，及调研json串")
    @PostMapping("/getDrugResearchDetail")
    public AjaxResult getDrugResearchDetail(Long id) {
        DrugResearch drugResearch = iDrugResearchService.selectDrugResearchById(id);
        SurveyQuestionnaire surveyQuestionnaire = iSurveyQuestionnaireService.selectSurveyQuestionnaireById(drugResearch.getFormId());
        SurveyQuestionnaireVersion surveyQuestionnaireVersion = iSurveyQuestionnaireVersionService.selectSurveyQuestionnaireVersionById(surveyQuestionnaire.getCurrentVersionId());
        drugResearch.setSurveyQuestionnaireVersion(surveyQuestionnaireVersion);
        return AjaxResult.success(drugResearch);
    }

    @Login
    @ApiOperation("填写用药调研")
    @PostMapping("/insertDrugResearch")
    public AjaxResult insertDrugResearch(@RequestBody Map<String, Object> dataMap) {
        Long userId = Long.valueOf(getUserId());
        Doctor doctor = iDoctorService.selectDoctorById(userId);
        CommonCompanyInfo commonCompanyInfo = getCompanyIdAndCompanyUserId(doctor.getDoctorId());

        dataMap.put("user_id", userId);
        dataMap.put("user_name", doctor.getDoctorName());
        dataMap.put("del_flag", "0");
        appDrugResearchService.insertDrugResearch(dataMap,commonCompanyInfo);



        return AjaxResult.success("提交成功");
    }

    @Login
    @ApiOperation("获取问卷调查主列表")
    @PostMapping("/getSurveyTaskList")
    public AjaxResult getSurveyTaskList(@RequestBody SurveyTask surveyTask) {
        startPage();
//        String userId = getUserId();
//        surveyTask.setDoctorId(Long.valueOf(userId));
        List<SurveyTask> surveyTaskList = iSurveyTaskService.selectSurveyTaskList(surveyTask);

        List<SurveyTask> newLive = new ArrayList<>();

        surveyTaskList.forEach(item -> {
            String targetId = TaskLabelEnum.survey + "-" + item.getId();
            TaskInfo taskInfo = taskInfoMapper.getTaskByTargetId(targetId);
            if (taskInfo != null){
                // 查询问卷期次信息
                PeriodDataDto periodDataDto = surveyTaskPeriodService.getPeriodData(item.getId());
                item.setPeriodDataDto(periodDataDto);
                // 获取枚举值
                TaskFinishStatusEnum statusEnum = TaskFinishStatusEnum.fromCode(taskInfo.getFinishStatus().intValue());
                taskInfo.setFinishStatusName(statusEnum.getDesc());
                TaskLiveDto taskLiveDto = new TaskLiveDto();
                BeanUtils.copyProperties(taskInfo, taskLiveDto);
                item.setTaskLiveDto(taskLiveDto);
                newLive.add(item);
            }

        });


        return AjaxResult.success(getDataTable(newLive));
    }

    @Login
    @ApiOperation("获取问卷调查详细信息，及问卷json串")
    @PostMapping("/getSurveyTaskDetailById")
    public AjaxResult getSurveyTaskDetailById(Long id) {
        SurveyTask surveyTask = iSurveyTaskService.selectSurveyTaskById(id);
        SurveyQuestionnaire surveyQuestionnaire = iSurveyQuestionnaireService.selectSurveyQuestionnaireById(surveyTask.getFormId());
        SurveyQuestionnaireVersion surveyQuestionnaireVersion = iSurveyQuestionnaireVersionService.selectSurveyQuestionnaireVersionById(surveyQuestionnaire.getCurrentVersionId());
        Long periodNum = surveyTaskPeriodService.getPeriodNum(surveyTask.getId());
        surveyTask.setPeriodNum(periodNum);
        surveyTask.setSurveyQuestionnaireVersion(surveyQuestionnaireVersion);
        return AjaxResult.success(surveyTask);
    }

    @Login
    @ApiOperation("获取问卷期号接口")
    @PostMapping("/getPeriodData")
    public AjaxResult getPeriodData(Long taskId) {
        PeriodDataDto periodDataDto = surveyTaskPeriodService.getPeriodData(taskId);

        return AjaxResult.success(periodDataDto);
    }



}
