package com.ruoyi.web.modules.diagnosis.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.modules.diagnosis.domain.AppOnlineConsultVetQuery;
import com.ruoyi.web.modules.diagnosis.domain.BizConsultMessage;
import com.ruoyi.web.modules.diagnosis.domain.BizConsultSession;
import com.ruoyi.web.modules.diagnosis.domain.ConsultOpenBody;
import com.ruoyi.web.modules.diagnosis.domain.VetConsultSendBody;
import com.ruoyi.web.modules.diagnosis.domain.vo.AppOnlineConsultVetListVo;
import com.ruoyi.web.modules.diagnosis.service.IAppOnlineConsultService;
import com.ruoyi.web.modules.diagnosis.service.IBizVetOnlineConsultService;
import com.ruoyi.web.modules.diagnosis.support.AppOnlineConsultPageSupport;

/**
 * 移动端在线问诊（兽医列表 + 问诊会话）。
 */
@RestController
@RequestMapping("/app/onlineConsult")
public class BizAppOnlineConsultController extends BaseController
{
    @Autowired
    private IAppOnlineConsultService appOnlineConsultService;

    @Autowired
    private IBizVetOnlineConsultService bizVetOnlineConsultService;

    /**
     * 可在线问诊的已发布兽医列表（资料字段一次返回，无需详情接口）。
     */
    @GetMapping("/vet/list")
    public TableDataInfo vetList(AppOnlineConsultVetQuery query)
    {
        AppOnlineConsultPageSupport.startPage(query);
        List<AppOnlineConsultVetListVo> list = appOnlineConsultService.selectOnlineConsultVetList(query);
        return getDataTable(list);
    }

    /**
     * 当前用户的兽医问诊会话列表（消息 Tab）。
     */
    @GetMapping("/session/list")
    public TableDataInfo sessionList()
    {
        BizConsultSession query = new BizConsultSession();
        query.setAskerUserId(SecurityUtils.getUserId());
        startPage();
        List<BizConsultSession> list = bizVetOnlineConsultService.selectAskerSessionList(query);
        return getDataTable(list);
    }

    /**
     * 打开或复用与指定兽医的问诊会话（曾问诊过则返回原会话）。
     */
    @Log(title = "移动端在线问诊打开会话", businessType = BusinessType.INSERT)
    @PostMapping("/session/open")
    public AjaxResult openSession(@RequestBody ConsultOpenBody body)
    {
        if (body == null || body.getVetResourceId() == null)
        {
            return error("请选择兽医");
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String name = user.getNickName() != null ? user.getNickName() : user.getUserName();
        BizConsultSession session = bizVetOnlineConsultService.openOrGetVetSession(body.getVetResourceId(),
                user.getUserId(), name);
        return success(session);
    }

    /**
     * 问诊消息历史（向上翻页）。
     */
    @GetMapping("/session/{sessionId}/messages")
    public AjaxResult messages(@PathVariable("sessionId") Long sessionId,
            @RequestParam(value = "beforeId", required = false) Long beforeId,
            @RequestParam(value = "pageSize", required = false) Integer pageSize)
    {
        List<BizConsultMessage> rows = bizVetOnlineConsultService.selectAskerSessionMessages(sessionId, beforeId,
                pageSize, SecurityUtils.getUserId());
        return success(rows);
    }

    @Log(title = "移动端在线问诊发送消息", businessType = BusinessType.INSERT)
    @PostMapping("/session/{sessionId}/message")
    public AjaxResult send(@PathVariable("sessionId") Long sessionId, @RequestBody VetConsultSendBody body)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String name = user.getNickName() != null ? user.getNickName() : user.getUserName();
        BizConsultMessage message = bizVetOnlineConsultService.sendAskerMessage(sessionId, body, user.getUserId(), name);
        return success(message);
    }

    @Log(title = "移动端在线问诊隐藏会话", businessType = BusinessType.UPDATE)
    @PostMapping("/session/{sessionId}/hide")
    public AjaxResult hide(@PathVariable("sessionId") Long sessionId)
    {
        return toAjax(bizVetOnlineConsultService.hideSessionForVet(sessionId, SecurityUtils.getUserId()));
    }
}
