package com.fs.app.controller;

import com.fs.app.annotation.Login;
import com.fs.common.annotation.Log;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.enums.BusinessType;
import com.fs.his.domain.FsDoctorArticle;
import com.fs.his.domain.FsHealthTongue;
import com.fs.his.domain.FsPatient;
import com.fs.his.domain.FsUser;
import com.fs.his.mapper.FsDoctorArticleMapper;
import com.fs.his.mapper.FsUserMapper;
import com.fs.his.param.FsHealthTongueListUParam;
import com.fs.his.param.FsHealthTongueUParam;
import com.fs.his.service.IFsDoctorArticleService;
import com.fs.his.service.IFsHealthTongueService;
import com.fs.his.service.IFsPatientService;
import com.fs.his.vo.FsHealthTongueListUVO;
import com.fs.his.vo.FsHealthTongueUVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 舌苔Controller
 *
 * @author fs
 * @date 2024-09-19
 */
@Api("健康舌苔")
@RestController
@RequestMapping("/app/healthTongue")
public class HealthTongueController extends AppBaseController
{
    @Autowired
    private IFsHealthTongueService fsHealthTongueService;
    @Autowired
    private FsUserMapper fsUserMapper;

    @Autowired
    private IFsPatientService fsPatientService;
    @Autowired
    private FsDoctorArticleMapper fsDoctorArticleMapper;
    /**
     * 查询舌苔列表
     */
    @Login
    @ApiOperation("查询舌苔列表")
    @GetMapping("/getHealthTongueList")
    public R getHealthTongueList(FsHealthTongueListUParam param)
    {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        param.setUserId(Long.parseLong(getUserId()));
        List<FsHealthTongueListUVO> list = fsHealthTongueService.selectFsHealthTongueUListVO(param);
        PageInfo<FsHealthTongueListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }

    @Login
    @ApiOperation("查询舌苔")
    @GetMapping("/myReport")
    public R myReport()
    {
        FsHealthTongueUVO fsHealthTongue= fsHealthTongueService.selectFsHealthTongueByUserId(Long.parseLong(getUserId()));
        FsDoctorArticle fsDoctorArticle = fsDoctorArticleMapper.selectFsDoctorArticleByArticleByNameBodyUrl(fsHealthTongue.getTypeName());
        String url="";
        String imageUrl="";
        if (fsDoctorArticle!=null){
            url=fsDoctorArticle.getVideoUrl();
            imageUrl=fsDoctorArticle.getImageUrl();
        }
        FsUser fsUser = fsUserMapper.selectFsUserByUserId(Long.parseLong(getUserId()));
        return R.ok().put("data",fsHealthTongue).put("url",url).put("isAddQw",fsUser.getIsAddQw()).put("imageUrl",imageUrl);
    }

    @Login
    @ApiOperation("次数")
    @GetMapping("/getCount")
    public R getCount()
    {
        return R.ok().put("data","不限次数");
//        FsUser fsUser = fsUserMapper.selectFsUserByUserId(Long.parseLong(getUserId()));
//        if(fsUser.getIsVip()==1){
//           return R.ok().put("data","不限次数");
//        }
//        Long count = fsHealthTongueService.selectFsHealthTongueCountByUserId(Long.parseLong(getUserId()));
//        return R.ok().put("data",2-count);
    }


    @Login
    @ApiOperation("AI舌诊报告")
    @GetMapping(value = "/getHealthTongueById/{id}")
    public R getInfo(@PathVariable("id") Long id)
    {
        FsHealthTongueUVO fsHealthTongue = fsHealthTongueService.selectFsHealthTongueUVOById(id);
        if (fsHealthTongue == null) {
            return R.error("记录不存在");
        }
        // 仅允许查看本人舌诊报告，防止越权
        if (fsHealthTongue.getUserId() == null || !fsHealthTongue.getUserId().equals(Long.parseLong(getUserId()))) {
            return R.error("无权查看该报告");
        }
        FsDoctorArticle fsDoctorArticle = fsDoctorArticleMapper.selectFsDoctorArticleByArticleByNameBodyUrl(fsHealthTongue.getTypeName());
        String url="";
        String imageUrl="";
        if (fsDoctorArticle!=null){
            url=fsDoctorArticle.getVideoUrl();
            imageUrl=fsDoctorArticle.getImageUrl();
        }

        return R.ok().put("data",fsHealthTongue).put("url",url).put("isAddQw",0).put("imageUrl",imageUrl);
    }

    /**
     * 新增舌苔
     */
    @Login
    @ApiOperation("新增舌苔")
    @PostMapping("/add")
    public R add(@RequestBody FsHealthTongueUParam param)
    {
        Long count = fsHealthTongueService.selectFsHealthTongueAllCountByUserId(Long.parseLong(getUserId()));
        FsPatient fsPatient=null;
        if (param.getPatientId()!=null){
             fsPatient = fsPatientService.selectFsPatientByPatientId(param.getPatientId());
            if (fsPatient!=null){
                param.setSex( fsPatient.getSex());
                Date today = new Date();
                param.setAge(today.getYear() - fsPatient.getBirthday().getYear());
                param.setName(fsPatient.getPatientName());
            }
        }
        if (count>2){
            if (fsPatient==null){
                return R.error("没有就诊人信息");
            }
        }

        if (param.getTongueUrl()==null||param.getTongueUrl()==""){
            return R.error("没有图片");
        }


        param.setPatientId(param.getPatientId());
//        FsUser fsUser = fsUserMapper.selectFsUserByUserId(Long.parseLong(getUserId()));
//        System.out.println(fsUser);
//        if(fsUser.getIsVip()==0){
//            Long count = fsHealthTongueService.selectFsHealthTongueCountByUserId(Long.parseLong(getUserId()));
//            if (count>=2){
//                return R.error("次数已用完");
//            }
//        }
        param.setUserId(Long.parseLong(getUserId()));
        return fsHealthTongueService.insertFsHealthTongueByImgUrl(param);
    }
}
