package com.fs.app.controller;


import com.fs.app.annotation.Login;
import com.fs.common.core.domain.R;
import com.fs.common.utils.ServletUtils;
import com.fs.his.domain.FsDepartment;
import com.fs.his.domain.FsDoctor;
import com.fs.his.domain.FsHospital;
import com.fs.his.domain.FsUserDoctor;
import com.fs.his.param.FsDoctorPingListUParam;
import com.fs.his.param.FsDoctorListUParam;
import com.fs.his.param.FsUserDoctorFollowUParam;
import com.fs.his.param.FsUserDoctorListUParam;
import com.fs.his.service.*;
import com.fs.his.vo.FsDoctorListUVO;
import com.fs.his.vo.FsDoctorPingUVO;
import com.fs.his.vo.FsUserDoctorListUVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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


@Api("医生接口")
@RestController
@RequestMapping(value="/app/doctor")
public class DoctorController extends  AppBaseController {

    @Autowired
    private IFsUserDoctorService userDoctorService;
    @Autowired
    private IFsDoctorService doctorService;
    @Autowired
    private IFsHospitalService hospitalService;
    @Autowired
    private IFsDepartmentService departmentService;


    @Autowired
    private IFsInquiryOrderPingService inquiryOrderPingService;



    @GetMapping("/getDoctorList")
    @Cacheable(value = "getDoctorList", key = "#param")
    public R getDoctorList(FsDoctorListUParam param)
    {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsDoctorListUVO> list=doctorService.selectFsDoctorListUVO(param);
        PageInfo<FsDoctorListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }

    @Login
    @GetMapping("/getMyDoctorList")
    public R getMyDoctorList(FsUserDoctorListUParam param)
    {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        param.setUserId(Long.parseLong(getUserId()));
        List<FsUserDoctorListUVO> list=userDoctorService.getMyDoctorList(param);
        PageInfo<FsUserDoctorListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }
    @GetMapping("/getDoctorDetails")
    @Cacheable(value = "getDoctorDetails", key = "#doctorId")
    public R getDoctorByDoctorId(@RequestParam("doctorId")Long doctorId)
    {

        FsDoctor doctor=doctorService.selectFsDoctorByDoctorId(doctorId);
        FsHospital hospital=hospitalService.selectFsHospitalByHospitalId(doctor.getHospitalId());
        FsDepartment department=departmentService.selectFsDepartmentByDeptId(doctor.getDeptId());

        Map<String,Object> map=new HashMap<>();
        map.put("doctor",doctor);
        map.put("hospital",hospital);
        map.put("department",department);
        return R.ok().put("data",map);
    }


    @GetMapping("/getDoctorPingList")
    public R getDoctorPingList(FsDoctorPingListUParam param)
    {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsDoctorPingUVO> list=inquiryOrderPingService.selectFsDoctorPingListUVO(param);
        PageInfo<FsDoctorPingUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }
    @Login
    @ApiOperation("获取是否关注")
    @GetMapping("/checkFollow")
    public R checkFollow(@RequestParam("doctorId")Long doctorId, HttpServletRequest request){
        String headValue =  ServletUtils.getRequest().getHeader("APPToken");
        Claims claims=jwtUtils.getClaimByToken(headValue);
        if(claims!=null){
            String userId = claims.getSubject().toString();
            return userDoctorService.checkFollow(doctorId,Long.parseLong(userId));
        }
        else{
            return R.error("未登录");
        }

    }
    @Login
    @ApiOperation("关注")
    @PostMapping("/doFollow")
    @Transactional
    @Synchronized
    public R doFollow(@RequestBody FsUserDoctorFollowUParam param, HttpServletRequest request){
        R r=userDoctorService.checkFollow(param.getDoctorId(),Long.parseLong(getUserId()));
        if(r.get("isFollow").equals(1)){
            //取消关注
            userDoctorService.deleteFollow(param.getDoctorId(),Long.parseLong(getUserId()));
            return R.ok("已取消关注");
        }
        else{
            //关注
            FsUserDoctor map=new FsUserDoctor();
            map.setType(1);
            map.setDoctorId(param.getDoctorId());
            map.setUserId(Long.parseLong(getUserId()));
            map.setCreateTime(new Date());
            userDoctorService.insertFsUserDoctor(map);
            return R.ok("已关注");
        }
    }

    @ApiOperation("随机获取一个协定方医生")
    @GetMapping("/getAgreement")
    public R getAgreement(){
        return R.ok().put("doctor",userDoctorService.getAgreement());
    }

    @ApiOperation("获取医生列表")
    @GetMapping("/getOptionsVO")
    public R getOptionsVO(){
        return R.ok().put("data",doctorService.selectAllDocVoList());
    }
}
