package com.fs.app.controller;


import cn.hutool.json.JSONUtil;
import com.fs.app.param.DrugReportAddParam;
import com.fs.app.param.DrugReportFinishParam;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.R;
import com.fs.his.domain.FsDoctor;
import com.fs.his.domain.FsDrugReport;
import com.fs.his.domain.FsFollow;
import com.fs.his.domain.FsInquiryOrderLogs;
import com.fs.his.param.FsDrugReportListDParam;
import com.fs.his.param.FsFollowListDParam;
import com.fs.his.service.IFsDoctorService;
import com.fs.his.service.IFsDrugReportCountService;
import com.fs.his.service.IFsDrugReportService;
import com.fs.his.service.IFsFollowService;
import com.fs.his.vo.FsDrugReportDVO;
import com.fs.his.vo.FsDrugReportListDVO;
import com.fs.his.vo.FsFollowListDVO;
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.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;


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

    @Autowired
    private IFsDrugReportService drugReportService;

    @Autowired
    private IFsFollowService followService;

    @Autowired
    RedisTemplate<String, String> redisTemplate;
    @Autowired
    private IImService imService;
    @Autowired
    private IFsDrugReportCountService fsDrugReportCountService;

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

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

    @ApiOperation("提交报告")
    @PostMapping("/addReport")
    public R addReport(@Validated @RequestBody DrugReportAddParam param, HttpServletRequest request){
        FsFollow follow=followService.selectFsFollowByFollowId(param.getFollowId());

        FsDrugReport report=new FsDrugReport();
        BeanUtils.copyProperties(param,report);
        report.setDoctorId(Long.parseLong(getDoctorId()));
        report.setStatus(1);
        report.setStoreOrderId(follow.getStoreOrderId());
        report.setUserId(follow.getUserId());
        report.setStatus(1);
        if(drugReportService.insertFsDrugReport(report)>0){
            //发送给用户
            MsgDTO msgDTO=new MsgDTO();
            MsgCustomDTO customDTO=new MsgCustomDTO();
            customDTO.setType("drugReport");
            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("TIMCustomElem");
            DrugReportMsgDTO dto=new DrugReportMsgDTO();
            dto.setTitle("用药报告单");
            String ext= JSONUtil.toJsonStr(dto);
            msg.setMsgContent(new MsgDataFormatDTO("drugReport",ext,report.getReportId().toString()));
            msgs.add(msg);
            msgDTO.setMsgBody(msgs);
            imService.sendMsg(msgDTO);

            fsDrugReportCountService.addReportCountByUserIdAndDoctorId(follow.getUserId(),follow.getDoctorId());

            return R.ok();
        }
        else{
            return R.error();
        }

    }
    @ApiOperation("完成咨询")
    @PostMapping("/finishDrugReport")
    @RepeatSubmit
    public R finishDrugReport(@Validated @RequestBody DrugReportFinishParam param, HttpServletRequest request){
        FsFollow follow=followService.selectFsFollowByFollowId(param.getFollowId());
        //发送给用户
        MsgDTO msgDTO=new MsgDTO();
        MsgCustomDTO customDTO=new MsgCustomDTO();
        customDTO.setType("finishDrugReport");
        customDTO.setImType(0);
        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.delete("DrugReport:doctorId:" + follow.getDoctorId() + ":userId:" + follow.getUserId());
        return R.ok();

    }

}
