package com.fs.app.controller;


import cn.hutool.json.JSONUtil;
import com.fs.app.annotation.Login;
import com.fs.app.controller.AppBaseController;
import com.fs.common.core.domain.R;
import com.fs.his.domain.FsDisease;
import com.fs.his.domain.FsDoctor;
import com.fs.his.domain.FsFollow;
import com.fs.his.domain.FsFollowTemp;
import com.fs.his.param.FsCouponReceiveParam;
import com.fs.his.param.FsDiseaseListUParam;
import com.fs.his.param.FsFollowListUParam;
import com.fs.his.service.*;
import com.fs.his.vo.FsDiseaseListUVO;
import com.fs.his.vo.FsFollowListUVO;
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.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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


@Api("随访接口")
@RestController
@RequestMapping(value="/app/follow")
public class FollowController extends AppBaseController {

    @Autowired
    private IFsFollowService followService;
    @Autowired
    private IFsDoctorService doctorService;
    @Autowired
    private IFsFollowTempService followTempService;
    @Autowired
    private IFsUserService userService;
    @Autowired
    IImService imService;
    Logger logger= LoggerFactory.getLogger(getClass());

    @Login
    @ApiOperation("获取随访列表")
    @GetMapping("/getFollowList")
    public R getFollowList(FsFollowListUParam param)
    {
        param.setUserId(Long.parseLong(getUserId()));
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsFollowListUVO> list=followService.selectFsFollowListUVO(param);
        PageInfo<FsFollowListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }

    @Login
    @ApiOperation("获取随访详情")
    @GetMapping("/getFollowById")
    public R getFollowById(@RequestParam("followId")Long followId, HttpServletRequest request){
        FsFollow follow=followService.selectFsFollowByFollowId(followId);
        if(!follow.getUserId().equals(Long.parseLong(getUserId()))){
            return R.error("非法操作");
        }
        FsDoctor doctor=doctorService.selectFsDoctorByDoctorId(follow.getDoctorId());
        return R.ok().put("follow",follow).put("doctor",doctor);
    }

    @Login
    @ApiOperation("提交随访")
    @PostMapping("/doFollow")
    public R doFollow(HttpServletRequest request, @Validated @RequestBody FsFollow param)
    {
        FsFollow follow=followService.selectFsFollowByFollowId(param.getFollowId());
        if(follow.getWriteStatus()!=0){
            return R.error("非法操作");
        }
        param.setWriteStatus(1);
        param.setUpdateTime(new Date());
        if(followService.updateFsFollow(param)>0){
            MsgDTO msgDTO=new MsgDTO();
            msgDTO.setTo_Account("D-"+follow.getDoctorId());
            msgDTO.setFrom_Account("U-"+follow.getUserId());
            MsgCustomDTO customDTO1=new MsgCustomDTO();
            customDTO1.setType("follow");
            customDTO1.setImType(2);
            customDTO1.setOrderId(follow.getStoreOrderId().toString());
            customDTO1.setFollowId(follow.getFollowId().toString());
            msgDTO.setCloudCustomData(JSONUtil.toJsonStr(customDTO1));

            List<MsgDataDTO> msgs=new ArrayList<>();
            MsgDataDTO msg=new MsgDataDTO();
            String ext=JSONUtil.toJsonStr(follow);
            String followId=follow.getFollowId().toString();
            msg.setMsgContent(new MsgDataFormatDTO("follow",ext,followId));
            msg.setMsgType("TIMCustomElem");//TIMCustomElem
            msgs.add(msg);
            msgDTO.setMsgBody(msgs);
            MsgResponseDTO msgResponseDTO = imService.sendMsg(msgDTO);
            logger.info("用户发送随访:"+msgResponseDTO);
            userService.followAddIntegral(follow.getUserId(),follow.getFollowId());

            //发送给用户
            MsgDTO msgDTO2=new MsgDTO();
            MsgCustomDTO customDTO2=new MsgCustomDTO();
            customDTO2.setType("startDrugReport");
            customDTO2.setImType(2);
            customDTO2.setOrderId(follow.getStoreOrderId().toString());
            customDTO2.setFollowId(follow.getFollowId().toString());
            msgDTO2.setCloudCustomData(JSONUtil.toJsonStr(customDTO2));
            msgDTO2.setFrom_Account("D-"+follow.getDoctorId());
            msgDTO2.setTo_Account("U-"+follow.getUserId());
            List<MsgDataDTO> msgs2=new ArrayList<>();
            MsgDataDTO msg2=new MsgDataDTO();
            msg2.setMsgType("TIMTextElem");
            msg2.setMsgContent(new MsgDataFormatDTO("您好，我是您的专属药师，您有任何问题都可以先留言给我，请耐心等待一下，用户咨询量较多，我会按咨询顺序一一回复哒~"));
            msgs2.add(msg2);
            msgDTO2.setMsgBody(msgs2);
            imService.sendMsg(msgDTO2);
            return R.ok("操作成功");

        }
        else{
            return R.error("操作失败");
        }
    }

}
