package com.fs.app.controller;


import cn.hutool.json.JSONUtil;
import com.fs.app.annotation.Login;
import com.fs.app.param.FsDrugReportPingParam;
import com.fs.app.param.FsDrugReportStartParam;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.his.domain.FsDrugReport;
import com.fs.his.domain.FsFollow;
import com.fs.his.param.FsDrugReportListDParam;
import com.fs.his.param.FsDrugReportListUParam;
import com.fs.his.service.IFsDrugReportService;
import com.fs.his.service.IFsFollowService;
import com.fs.his.service.IFsUserService;
import com.fs.his.vo.*;
import com.fs.im.dto.*;
import com.fs.im.service.IImService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;


@Api("用药报告接口")
@RestController
@RequestMapping(value="/app/drugReport")
public class DrugReportController extends AppBaseController {

    @Autowired
    private IFsDrugReportService drugReportService;
    @Autowired
    private IFsUserService userService;
    @Autowired
    private IFsFollowService followService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private IImService imService;
    org.slf4j.Logger logger= LoggerFactory.getLogger(getClass());
    @ApiOperation("获取用药咨询医生")
    @GetMapping("/getUserFollowDoctor")
    @Login
    public R getUserFollowDoctor( )
    {
        FsUserFollowDoctorVO userFollowDoctorVO= userService.getUserFollowDoctorAndOrderId(Long.parseLong(getUserId()));
        return R.ok().put("data",userFollowDoctorVO);
    }

    @ApiOperation("获取报告列表")
    @GetMapping("/getDrugReportList")
    public R getDrugReportList(FsDrugReportListUParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsDrugReportListUVO> list=drugReportService.selectFsDrugReportListUVO(param);
        PageInfo<FsDrugReportListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }

    @ApiOperation("获取详情")
    @GetMapping("/getDrugReportById")
    public R getDrugReportById(@RequestParam("reportId")Long reportId, HttpServletRequest request){
        FsDrugReportUVO reportDVO=drugReportService.selectFsDrugReportUVOByReportId(reportId);
        return R.ok().put("data",reportDVO);
    }

    @ApiOperation("评价报告")
    @PostMapping("/pingReport")
    @RepeatSubmit
    @Login
    public R pingReport(@Validated  @RequestBody FsDrugReportPingParam param, HttpServletRequest request){

        FsDrugReport report=drugReportService.selectFsDrugReportByReportId(param.getReportId());
        if(report.getStatus().equals(1)){
            FsDrugReport map=new FsDrugReport();
            BeanUtils.copyProperties(param,map);
            map.setStatus(2);
            if(drugReportService.updateFsDrugReport(map)>0){
                return R.ok();
            }
            else{
                return R.error();
            }
        }
        else {
            return R.error("已评价");
        }


    }

    @ApiOperation("开启咨询")
    @PostMapping("/startDrugReport")
    @RepeatSubmit
    public R startDrugReport(@Validated @RequestBody FsDrugReportStartParam param, HttpServletRequest request){
        FsFollow follow=followService.selectFsFollowByFollowId(param.getFollowId());
        String s = redisTemplate.opsForValue().get("DrugReport:doctorId:" + follow.getDoctorId() + ":userId:" + follow.getUserId());
        if (s!=null){
            return R.ok();
        }
        //发送给用户
        MsgDTO msgDTO=new MsgDTO();
        MsgCustomDTO customDTO=new MsgCustomDTO();
        customDTO.setType("startDrugReport");
        customDTO.setImType(2);
        customDTO.setOrderId(follow.getStoreOrderId().toString());
        customDTO.setFollowId(follow.getFollowId().toString());
        msgDTO.setCloudCustomData(JSONUtil.toJsonStr(customDTO));
        msgDTO.setFrom_Account("D-"+follow.getDoctorId());
        msgDTO.setTo_Account("U-"+follow.getUserId());
        List<MsgDataDTO> msgs=new ArrayList<>();
        MsgDataDTO msg=new MsgDataDTO();
        msg.setMsgType("TIMTextElem");
        msg.setMsgContent(new MsgDataFormatDTO("您好，我是您的专属药师，您有任何问题都可以先留言给我，请耐心等待一下，用户咨询量较多，我会按咨询顺序一一回复哒~"));
        msgs.add(msg);
        msgDTO.setMsgBody(msgs);
        imService.sendMsg(msgDTO);
        redisTemplate.opsForValue().set("DrugReport:doctorId:" + follow.getDoctorId() + ":userId:" + follow.getUserId(),follow.getFollowId()+"");
        return R.ok();

    }

}
