package com.fs.app.controller;


import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.fs.app.annotation.Login;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.R;
import com.fs.common.utils.StringUtils;
import com.fs.core.utils.OrderCodeUtils;
import com.fs.his.domain.*;
import com.fs.his.dto.TestReportPatientDTO;
import com.fs.his.param.*;
import com.fs.his.service.*;
import com.fs.his.utils.TestReportUtil;
import com.fs.his.vo.FsDoctorListUVO;
import com.fs.his.vo.FsTestTempItemListUVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

import static cn.hutool.core.date.DateUtil.ageOfNow;


@Api("测一测接口")
@RestController
@RequestMapping(value="/app/test")
public class TestController extends  AppBaseController {

    @Autowired
    private IFsTestTempService testTempService;
    @Autowired
    private IFsTestReportService testReportService;
    @Autowired
    private IFsTestTempItemService testTempItemService;
    @Autowired
    private IFsPatientService patientService;
    @Autowired
    private IFsUserService userService;



    @Autowired
    TestReportUtil testReportUtil;

    @GetMapping("/getTestList")
    public R getTestList(FsDoctorListUParam param)
    {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        FsTestTemp map=new FsTestTemp();
        map.setStatus(1);
        List<FsTestTemp> list=testTempService.selectFsTestTempList(map);
        PageInfo<FsTestTemp> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }
    @GetMapping("/getTestDetails")
    public R getTestDetails(@RequestParam("tempId")Long tempId)
    {
        FsTestTemp temp=testTempService.selectFsTestTempByTempId(tempId);
        return R.ok().put("data",temp);
    }
    @GetMapping("/getTestTempDetails")
    public R getTestTempDetails(@RequestParam("tempId")Long tempId)
    {
        FsTestTempItemListUParam param=new FsTestTempItemListUParam();
        param.setTempId(tempId);
        List<FsTestTempItemListUVO>  list=testTempItemService.selectFsTestTempItemListUVO(param);
        return R.ok().put("items",list);
    }


    @Login
    @ApiOperation("提交")
    @PostMapping("/doReport")
    @RepeatSubmit
    public R doReport(@Validated  @RequestBody FsTestDoReportParam param, HttpServletRequest request){
        String orderSn =  OrderCodeUtils.getOrderSn();
        if(StringUtils.isEmpty(orderSn)){
            return R.error("订单生成失败，请重试");
        }
        FsTestReport testReport = new FsTestReport();
        testReport.setUserId(Long.parseLong(getUserId()));
        testReport.setPatientId(param.getPatientId());
        FsPatient patient=patientService.selectFsPatientByPatientId(param.getPatientId());
        TestReportPatientDTO patientDTO=new TestReportPatientDTO();
        patientDTO.setName(patient.getPatientName());
        patientDTO.setAge(ageOfNow(patient.getBirthday()));
        patientDTO.setSex(patient.getSex());
        FsUser user=userService.selectFsUserByUserId(Long.parseLong(getUserId()));
        patientDTO.setAvatar(user.getAvatar());
        testReport.setPatientJson(JSON.toJSONString(patientDTO));
        //testReport.setPatientJson("{\"name\":\"John\",\"age\":30}");
        testReport.setReportSn(orderSn);

        testReport.setFormJson(param.getFormJson());
        //testReport.setFormJson("[{\"itemId\":1,\"title\":\"您精神充分吗？\",\"option\":\"总是\"},{\"itemId\":2,\"title\":\"您精神好吗？\",\"option\":\"没有\"},{\"itemId\":3,\"title\":\"您感到疲倦吗？\",\"option\":\"有时\"},{\"itemId\":4,\"title\":\"您总是否注意到自己的情绪变化？\",\"option\":\"经常\"},{\"itemId\":5,\"title\":\"您总是否感到焦虑或紧张？\",\"option\":\"总是\"},{\"itemId\":6,\"title\":\"您总是否感到忧郁或沮丧？\",\"option\":\"总是\"},{\"itemId\":7,\"title\":\"您总是否感到愉快和高兴？\",\"option\":\"没有\"},{\"itemId\":8,\"title\":\"您总是否感到兴奋或多动？\",\"option\":\"没有\"},{\"itemId\":9,\"title\":\"您总是否感到疲劳或缺乏活力？\",\"option\":\"总是\"},{\"itemId\":10,\"title\":\"您总是否感到困倦或昏昏欲睡？\",\"option\":\"有时\"},{\"itemId\":11,\"title\":\"您总是否容易入睡或睡得不好？\",\"option\":\"总是\"},{\"itemId\":12,\"title\":\"您总是否感到睡眠不足或没有休息好？\",\"option\":\"总是\"},{\"itemId\":13,\"title\":\"您的食欲总是否正常？\",\"option\":\"总是\"},{\"itemId\":14,\"title\":\"您总是否感到胃不舒服或消化不良？\",\"option\":\"有时\"},{\"itemId\":15,\"title\":\"您总是否感到头痛或身体不舒服？\",\"option\":\"总是\"},{\"itemId\":16,\"title\":\"您总是否感到头晕或眩晕？\",\"option\":\"没有\"},{\"itemId\":17,\"title\":\"您总是否感到胸闷或呼吸困难？\",\"option\":\"没有\"},{\"itemId\":18,\"title\":\"您总是否感到心跳加快或心悸？\",\"option\":\"没有\"},{\"itemId\":19,\"title\":\"您总是否感到恶心或想呕吐？\",\"option\":\"有时\"},{\"itemId\":20,\"title\":\"您总是否感到害怕或不安全？\",\"option\":\"有时\"},{\"itemId\":21,\"title\":\"您总是否感到无缘无故地紧张或害怕？\",\"option\":\"有时\"},{\"itemId\":22,\"title\":\"您总是否感到无缘无故地恐慌？\",\"option\":\"没有\"},{\"itemId\":23,\"title\":\"您总是否感到无法控制自己的念头或冲动？\",\"option\":\"没有\"},{\"itemId\":24,\"title\":\"您总是否突然忘记了一些重要事情？\",\"option\":\"没有\"},{\"itemId\":25,\"title\":\"您总是否感到紧张不安或烦躁易怒？\",\"option\":\"有时\"},{\"itemId\":26,\"title\":\"您总是否担心发生可怕的事情？\",\"option\":\"没有\"},{\"itemId\":27,\"title\":\"您总是否感到无法控制自己的担忧？\",\"option\":\"有时\"},{\"itemId\":28,\"title\":\"您总是否感到战栗或发抖？\",\"option\":\"没有\"},{\"itemId\":29,\"title\":\"您总是否感到平静和放松？\",\"option\":\"有时\"},{\"itemId\":30,\"title\":\"您总是否感到幸福和满足？\",\"option\":\"有时\"},{\"itemId\":31,\"title\":\"您总是否感到自信或有成就感？\",\"option\":\"有时\"},{\"itemId\":32,\"title\":\"您总是否感到自尊心受挫？\",\"option\":\"没有\"},{\"itemId\":33,\"title\":\"您总是否感到没有什么价值或没有作用？\",\"option\":\"总是\"},{\"itemId\":34,\"title\":\"您总是否感到烦闷或无聊？\",\"option\":\"有时\"},{\"itemId\":35,\"title\":\"您总是否感到应付不了事情或感到无所适从？\",\"option\":\"总是\"},{\"itemId\":36,\"title\":\"您总是否感到紧张或易激动？\",\"option\":\"有时\"},{\"itemId\":37,\"title\":\"您总是否感到精力旺盛或有活力？\",\"option\":\"没有\"},{\"itemId\":38,\"title\":\"您总是否感到开心和愉悦？\",\"option\":\"没有\"},{\"itemId\":39,\"title\":\"您总是否感到过度兴奋或多动不安？\",\"option\":\"没有\"},{\"itemId\":40,\"title\":\"您总是否感到疲劳或缺乏活力？\",\"option\":\"总是\"},{\"itemId\":41,\"title\":\"您总是否感到嗜睡或昏昏欲睡？\",\"option\":\"有时\"},{\"itemId\":42,\"title\":\"您总是否感到入睡困难或睡眠质量差？\",\"option\":\"总是\"},{\"itemId\":43,\"title\":\"您总是否感到睡眠不足或没有休息好？\",\"option\":\"总是\"},{\"itemId\":44,\"title\":\"您的食欲总是否正常？\",\"option\":\"总是\"},{\"itemId\":45,\"title\":\"您总是否感到食欲不振或没有兴趣进食？\",\"option\":\"有时\"},{\"itemId\":46,\"title\":\"您总是否感到厌食或拒绝进食？\",\"option\":\"没有\"},{\"itemId\":47,\"title\":\"您总是否感到胃不舒服或消化不良？\",\"option\":\"有时\"},{\"itemId\":48,\"title\":\"您总是否感到胸痛或不舒服？\",\"option\":\"总是\"},{\"itemId\":49,\"title\":\"您总是否感到头痛或身体不舒服？\",\"option\":\"总是\"},{\"itemId\":50,\"title\":\"您总是否感到头晕或眩晕？\",\"option\":\"没有\"},{\"itemId\":51,\"title\":\"您总是否感到胸闷或呼吸困难？\",\"option\":\"没有\"},{\"itemId\":52,\"title\":\"您总是否感到心跳加快或心悸？\",\"option\":\"没有\"},{\"itemId\":53,\"title\":\"您总是否感到恶心或想呕吐？\",\"option\":\"有时\"},{\"itemId\":54,\"title\":\"您总是否感到害怕或不安全？\",\"option\":\"有时\"},{\"itemId\":55,\"title\":\"您总是否感到无缘无故地紧张或害怕？\",\"option\":\"有时\"},{\"itemId\":56,\"title\":\"您总是否感到无缘无故地恐慌？\",\"option\":\"没有\"},{\"itemId\":57,\"title\":\"您总是否感到无法控制自己的念头或冲动？\",\"option\":\"没有\"},{\"itemId\":58,\"title\":\"您总是否突然忘记了一些重要事情？\",\"option\":\"没有\"},{\"itemId\":59,\"title\":\"您总是否感到紧张不安或烦躁易怒？\",\"option\":\"有时\"},{\"itemId\":60,\"title\":\"您总是否担心发生可怕的事情？\",\"option\":\"没有\"},{\"itemId\":61,\"title\":\"您总是否感到无法控制自己的担忧？\",\"option\":\"有时\"},{\"itemId\":62,\"title\":\"您总是否感到战栗或发抖？\",\"option\":\"没有\"},{\"itemId\":63,\"title\":\"您总是否感到平静和放松？\",\"option\":\"有时\"},{\"itemId\":64,\"title\":\"您总是否感到幸福和满足？\",\"option\":\"有时\"},{\"itemId\":65,\"title\":\"您总是否感到自信或有成就感？\",\"option\":\"有时\"},{\"itemId\":66,\"title\":\"您总是否感到自尊心受挫？\",\"option\":\"没有\"},{\"itemId\":67,\"title\":\"您总是否感到没有什么价值或没有作用？\",\"option\":\"总是\"}]");
        testReport.setTempId(param.getTempId());
        return testReportService.doReport(testReport);
    }

    @GetMapping("/getTestReport")
    public R getTestReport(@RequestParam("reportId")Long reportId)
    {
        FsTestReport testReport=testReportService.selectFsTestReportByReportId(reportId);
        return R.ok().put("report",testReport);
    }
    @GetMapping("/getTestReportImg")
    public R getTestReportImg(@RequestParam("reportId")Long reportId)
    {
        FsTestReport testReport=testReportService.selectFsTestReportByReportId(reportId);
        return R.ok().put("img",testReport.getImgUrl());
    }
}
