package com.eitc.patient.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
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.core.domain.entity.SysUser;
import com.eitc.common.utils.JWTUtil;
import com.eitc.patient.domain.CollectionDoctor;
import com.eitc.patient.service.ICollectionDoctorService;
import com.eitc.patient.vo.CollectionDoctorVO;
import com.eitc.system.utils.UserCacheUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
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.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * CollectionDoctorController
 *
 * @author mtx
 * @date: 2024/06/17 16:23
 */
@Api(tags = "APP收藏医生")
@RestController
@RequestMapping("/app/collection-doctor")
public class AppCollectionDoctorController extends BaseController {

    @Resource
    private ICollectionDoctorService collectionDoctorService;

    /**
     * 收藏医生
     *
     * @param vo 收藏医生
     * @return 返回结果
     * @author mtx
     * @date: 2024/6/17 16:29
     */
    @PostMapping("/collect-doctor")
    @ApiOperation("收藏医生")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "String", paramType = "query"),
    })
    public AjaxResult collectDoctors(@Valid @RequestBody CollectionDoctorVO vo) {
        CollectionDoctor collectionDoctor = new CollectionDoctor();
        collectionDoctor.setDoctorId(vo.getDoctorId());
        collectionDoctor.setAppUserId(JWTUtil.getAppUserId());
        return toAjax(collectionDoctorService.save(collectionDoctor));
    }

    /**
     * 取消收藏
     *
     * @param vo 取消收藏
     * @return 返回结果
     * @author mtx
     * @date: 2024/6/17 16:29
     */
    @PostMapping("/cancel-collect-doctor")
    @ApiOperation("取消收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "String", paramType = "query"),
    })
    public AjaxResult cancelCollectDoctor(@Valid @RequestBody CollectionDoctorVO vo) {
        return toAjax(collectionDoctorService.remove(Wrappers.<CollectionDoctor>lambdaQuery()
                .eq(CollectionDoctor::getAppUserId, JWTUtil.getAppUserId())
                .eq(CollectionDoctor::getDoctorId, vo.getDoctorId())));
    }

    @GetMapping("/list")
    @ApiOperation("查询收藏医生列表")
    public AjaxResult list() {
        // 获取收藏医生列表
        List<CollectionDoctor> list = collectionDoctorService.list(Wrappers.<CollectionDoctor>lambdaQuery()
                .eq(CollectionDoctor::getAppUserId, JWTUtil.getAppUserId()));

        if (CollectionUtils.isEmpty(list)) {
            return success(new ArrayList<>());
        }
        // 根据收藏列表封装医生信息
        List<CollectionDoctorVO> voList = new ArrayList<>();
        for (CollectionDoctor collectionDoctor : list) {
            SysUser user = UserCacheUtil.getSysUserById(collectionDoctor.getDoctorId());
            List<Long> postList = UserCacheUtil.getPostNameByUserId(Long.valueOf(collectionDoctor.getDoctorId()));
            CollectionDoctorVO vo = new CollectionDoctorVO();
            vo.setDoctorId(collectionDoctor.getDoctorId());
            vo.setAvatar(user.getAvatar());
            vo.setDoctorName(user.getNickName());
            vo.setPostName(postList.stream().map(String::valueOf).collect(Collectors.joining(",")));
            vo.setLabel(user.getDoctorProficient());
            vo.setBriefIntroduction(user.getDoctorBlurb());
            voList.add(vo);
        }

        return success(voList);
    }


}
