package com.eitc.patient.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eitc.common.core.controller.BaseController;
import com.eitc.common.core.domain.AjaxResult;
import com.eitc.common.utils.bean.BeanUtils;
import com.eitc.patient.domain.AppUser;
import com.eitc.patient.domain.ChatDisease;
import com.eitc.patient.domain.ChatDiseaseInfo;
import com.eitc.patient.domain.ChatHistory;
import com.eitc.patient.service.IAppUserService;
import com.eitc.patient.service.IChatDiseaseInfoService;
import com.eitc.patient.service.IChatDiseaseService;
import com.eitc.patient.service.IChatHistoryService;
import com.eitc.patient.vo.AppUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;





@Api(tags = "聊天消息接口")
@RestController
@RequestMapping("chat")
public class ChatController extends BaseController {


    @Autowired
    private IAppUserService appUserService;
    @Autowired
    private IChatHistoryService chatHistoryService;
    @Autowired
    private IChatDiseaseService chatDiseaseService;
    @Autowired
    private IChatDiseaseInfoService chatDiseaseInfoService;
    /**
     * 医生获取患者聊天列表
     * @param dockerId
     * @return
     */

    @GetMapping("/getUserList")
    @ApiOperation("医生获取患者聊天列表")
    public AjaxResult getUserList(String dockerId) {
        List<ChatHistory> list = chatHistoryService.list(Wrappers.<ChatHistory>lambdaQuery()
//                .eq(ChatHistory::getIsRead, 0)
                .eq(ChatHistory::getUserId, dockerId));
        List<AppUserVo> appUserList = new ArrayList<>();
        if (list != null && !list.isEmpty()) {
            Map<String, List<ChatHistory>> map = list.stream().filter(d->d.getIsRead()==0).collect(Collectors.groupingBy(ChatHistory::getAppUserId));
            List<String> collect = list.stream().map(ChatHistory::getAppUserId).distinct().collect(Collectors.toList());
            collect.removeAll(Collections.singletonList(null));
            appUserList = appUserService.listByIds(collect).stream().map(x->{
                AppUserVo appUserVo = new AppUserVo();
                BeanUtils.copyProperties(x,appUserVo);
                if (map.get(x.getId())!=null){
                    appUserVo.setMsgNumber((long) map.get(x.getId()).size());
                }
                return appUserVo;
            }).collect(Collectors.toList());
        }
        return AjaxResult.success(appUserList);
    }

    /**
     * 医生获取未读消息
     * @param dockerId
     * @return
     */
    @ApiOperation("医生获取未读消息")
    @GetMapping("/getMsg")
    public AjaxResult get(String dockerId) {
        List<ChatHistory> list = chatHistoryService.list(Wrappers.<ChatHistory>lambdaQuery()
                .eq(ChatHistory::getIsRead, 0)
                .eq(ChatHistory::getUserId, dockerId));


        Boolean isRead = false;
        if (list != null) {
            List<String> collect = list.stream().map(ChatHistory::getAppUserId).distinct().collect(Collectors.toList());
            collect.removeAll(Collections.singletonList(null));
            if (!collect.isEmpty()){
                Collection<AppUser> appUsers = appUserService.listByIds(collect);
                if (appUsers != null && !appUsers.isEmpty()){
                    isRead = true;
                }
            }
        }
        return AjaxResult.success("10886",isRead);
    }




    /**
     * 医生获取未读消息
     * @param patientId
     * @return
     */
    @ApiOperation("医生获取病历卡")
    @GetMapping("/getChatDisease")
    public AjaxResult getChatDisease(String patientId) {
        if (patientId==null){
            throw new RuntimeException("参数错误");
        }
        List<ChatDisease> list = chatDiseaseService.list(Wrappers.<ChatDisease>lambdaQuery()
                .eq(ChatDisease::getPatientId, patientId));
        if (list != null && !list.isEmpty()){
            ChatDisease chatDisease = list.get(0);
            String collect = chatDiseaseInfoService.list(Wrappers.<ChatDiseaseInfo>lambdaQuery()
                    .eq(ChatDiseaseInfo::getChatDiseaseId, chatDisease.getId())).stream().map(ChatDiseaseInfo::getUrl).collect(Collectors.joining(","));
            chatDisease.setUrl(collect);
            return success(chatDisease);
        }else{
            return success();
        }
    }

}
