package com.fs.app.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.fs.common.core.domain.R;
import com.fs.crm.vo.CrmMsgTypeVO;
import com.fs.his.domain.FsDoctor;
import com.fs.his.service.IFsDoctorService;
import com.fs.system.service.ISysDictDataService;
import com.fs.system.vo.DictVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.fs.doctor.domain.DoctorMsg;
import com.fs.doctor.service.IDoctorMsgService;


import javax.servlet.http.HttpServletRequest;


/**
 * 医生消息Controller
 * 
 * @author fs
 * @date 2025-11-04
 */
@RestController
@RequestMapping("/doctor/msg")
public class DoctorMsgController extends AppBaseController
{
    @Autowired
    private IDoctorMsgService doctorMsgService;

    @Autowired
    private IFsDoctorService doctorService;

    @Autowired
    private ISysDictDataService dictDataService;


    @GetMapping("/getMsgCount")
    public R getMsgCount(HttpServletRequest request){
        Long count= doctorMsgService.selectDoctorMsgCountsByDoctorId(Long.parseLong(getDoctorId()));
        return R.ok().put("counts",count);
    }

    @GetMapping("/getMsg")
    public R getMsg(HttpServletRequest request){
        Long doctorId = Long.parseLong(getDoctorId());
        FsDoctor doctor = doctorService.selectFsDoctorByDoctorId(doctorId);
        //获取用户未读总数
        //获取所有类型
        List<DictVO> types;
        if (doctor.getDoctorType() == 1) {
            //医生返回 1-医生开方 3-信息采集
            types = dictDataService.selectDictDataListByType("doctor_msg_type")
                    .stream()
                    .filter(v -> v.getDictValue().equals("1") || v.getDictValue().equals("3"))
                    .collect(Collectors.toList());
        } else {
            //药师返回 2- 药师审核
            types = dictDataService.selectDictDataListByType("doctor_msg_type")
                    .stream()
                    .filter(v -> v.getDictValue().equals("2"))
                    .collect(Collectors.toList());
        }
        List<CrmMsgTypeVO> counts=new ArrayList<>();
        for(DictVO v:types){
            Long count= doctorMsgService.selectDoctorMsgCountsByDoctorIdAndType(doctorId,Integer.parseInt(v.getDictValue()));
            CrmMsgTypeVO typeBO=new CrmMsgTypeVO();
            typeBO.setMsgType(Integer.parseInt(v.getDictValue()));
            typeBO.setTotal(count);
            typeBO.setMsgTypeName(v.getDictLabel());
            counts.add(typeBO);
        }
        return R.ok().put("counts",counts);
    }

    @GetMapping("/getMsgList")
    public R getMsgList(
            HttpServletRequest request,
            @ApiParam(required = true, name = "type", value = "类型") @RequestParam(value = "type", required = false) Integer type
    ){
        int pageNum =1;
        int pageSize = 10;

        Long doctorId = Long.parseLong(getDoctorId());
        DoctorMsg map = new DoctorMsg();
        map.setType(type);
        map.setDoctorId(doctorId);

        PageHelper.startPage(pageNum, pageSize);
        List<DoctorMsg> list = doctorMsgService.selectDoctorMsgList(map);
        PageInfo<DoctorMsg> listPageInfo=new PageInfo<>(list);

        return R.ok().put("data",listPageInfo);
    }

    @PostMapping("/setRead")
    public R setRead(@RequestBody DoctorMsg msg)
    {
        doctorMsgService.updateDoctorMsg(msg);
        return R.ok();
    }

    @PostMapping("/setAllRead")
    public R setAllRead()
    {
        Long doctorId = Long.parseLong(getDoctorId());
        doctorMsgService.setAllRead(doctorId);
        return R.ok();
    }

}
