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.JWTUtil;
import com.eitc.patient.domain.CollectionDoctor;
import com.eitc.patient.service.ICollectionDoctorService;
import com.eitc.patient.service.IDoctorService;
import com.eitc.patient.vo.DoctorVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * DoctorController
 *
 * @author mtx
 * @date: 2024/06/13 14:53
 */
@Api(tags = "APP医生")
@RestController
@RequestMapping("/app/doctor")
public class AppDoctorController extends BaseController {

    @Resource
    private IDoctorService doctorService;
    @Resource
    private ICollectionDoctorService collectionDoctorService;


    @GetMapping("/list")
    @ApiOperation("出诊医生列表")
    public AjaxResult list(@RequestParam(name = "deptName", required = false) String deptName,
                           @RequestParam(name = "selectDateStr", required = false) String selectDateStr) throws ParseException {
        // 查询出诊医生
        List<DoctorVO> list = doctorService.list(selectDateStr);
        list = list.stream().filter(li -> Objects.equals(li.getShowPatient(), 1)).collect(Collectors.toList());
        // 根据搜索条件筛选
        if (StringUtils.isNotBlank(deptName)) {
            list = list.stream().filter(li -> deptName.equals(li.getDeptName())).collect(Collectors.toList());
        }
        // 封装收藏状态
        for (DoctorVO doctorVO : list) {
            long counted = collectionDoctorService.count(Wrappers.<CollectionDoctor>lambdaQuery()
                    .eq(CollectionDoctor::getDoctorId, doctorVO.getUserId())
                    .eq(CollectionDoctor::getAppUserId, JWTUtil.getAppUserId()));
            doctorVO.setIsCollection(counted > 0 ? 1 : 0);
            doctorVO.setReceivePatientNum(666);
        }

        return success(list);
    }


}
