package com.cirle.scientific.controller.miniapp;

import cn.hutool.core.collection.CollectionUtil;
import com.cirle.scientific.dao.QkjExamineRecordAppDao;
import com.cirle.scientific.dao.QkjLungReportUnderstandAppDao;
import com.cirle.scientific.dto.course.QkjCertificationResp;
import com.cirle.scientific.dto.course.QkjLungReportUnderstandAppResp;
import com.cirle.scientific.dto.member.MemberInfoReq;
import com.cirle.scientific.po.QkjExamineRecordApp;
import com.cirle.scientific.po.QkjLungReportApp;
import com.cirle.scientific.po.QkjLungReportUnderstandApp;
import com.cirle.scientific.po.QkjPatientApp;
import com.cirle.scientific.pojo.BizCodeEnum;
import com.cirle.scientific.pojo.Result;
import com.cirle.scientific.service.LungReportUnderstandAppService;
import com.cirle.scientific.service.MemberAppService;
import com.cirle.scientific.service.QkjLungReportAppService;
import com.cirle.scientific.service.QuestionnaireService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/miniapp/user")
@Api("用户相关")
@Slf4j
@RequiredArgsConstructor
public class UserController {

    private final MemberAppService memberAppService;
    private final QkjExamineRecordAppDao qkjExamineRecordAppDao;
    private final QkjLungReportAppService qkjLungReportAppService;
    private final LungReportUnderstandAppService lungReportUnderstandAppService;
    private final QuestionnaireService questionnaireService;

    private final StringRedisTemplate stringRedisTemplate;


    @GetMapping("/findUserBaseInfo")
    @ResponseBody
    @ApiOperation(value = "获取用户基本信息")
    public Result findUserBaseInfo(HttpServletRequest request,
                                   @ApiParam(name = "memCard", value = "用户唯一标识") @RequestAttribute(name = "memCard", required = false) String memCard
    ) {
        return Result.success(memberAppService.findByMemCard(memCard));
    }

    @PostMapping("/updateUserInfo")
    @ResponseBody
    @ApiOperation(value = "更改用户信息Web")
    public Result updateUserInfoWeb(
            @RequestAttribute(name = "memCard", required = false) String memCard,
            @RequestBody MemberInfoReq req
    ) {
        if (StringUtils.isBlank(memCard)) {
            return Result.failed(BizCodeEnum.NO_LOGIN);
        }
        req.setMemCard(memCard);
        return Result.success(memberAppService.updateUserInfo(req));
    }

    @GetMapping("/get/certification/info")
    @ResponseBody
    @ApiOperation(value = "获取用户的认证信息")
    public Result getCertificationUserInfo(
            @RequestAttribute(name = "memCard", required = false) String memCard) {
        if (StringUtils.isBlank(memCard)) {
            return Result.failed(BizCodeEnum.NO_LOGIN);
        }
        //理论考核
        String theoreticalExamination = "未通过";
        QkjExamineRecordApp recordApp = qkjExamineRecordAppDao.getByMemCard(memCard, 1);
        if(Objects.nonNull(recordApp)){
            Integer score = recordApp.getScore();
            theoreticalExamination = Objects.nonNull(score) && score>=8 ? "通过":"未通过";
        }
        //技能考核
        String skillExamination = "未通过";
        QkjExamineRecordApp recordAppJn = qkjExamineRecordAppDao.getByMemCard(memCard, 2);
        if(Objects.nonNull(recordAppJn)){
            Integer result = recordAppJn.getResult();
            skillExamination = Objects.nonNull(result) && result == 2 ? "通过":"未通过";
        }
        //肺功能解读审核结果
        String lungFunctionUnscramble = "未通过";
        List<QkjLungReportUnderstandAppResp> understandAppResps = lungReportUnderstandAppService.lungReportList(memCard);
        if(CollectionUtil.isNotEmpty(understandAppResps)){
            understandAppResps = understandAppResps.stream().filter(e -> Objects.nonNull(e.getStatus()) && Objects.equals(1,e.getStatus())).collect(Collectors.toList());
            lungFunctionUnscramble = understandAppResps.size() == 10 ? "通过":"未通过";
        }
        //问卷数量
        Integer quTotal = 0;
        List<QkjPatientApp> patientApps = questionnaireService.findDoctorQuestions(memCard);
        if(CollectionUtil.isNotEmpty(patientApps)){
            quTotal = patientApps.size();
        }
        //肺功能上传数量
        Map<String, Object> lungQuantity = qkjLungReportAppService.findLungQuantity(memCard);
        Integer lungSize = (Integer) lungQuantity.getOrDefault("lungSize", 0);
        Long reportRate = (Long) lungQuantity.getOrDefault("reportRate", 0);


        QkjCertificationResp build = QkjCertificationResp.builder()
                .theoreticalExamination(theoreticalExamination)
                .skillExamination(skillExamination)
                .lungFunctionUnscramble(lungFunctionUnscramble)
                .quTotal(quTotal)
                .lungFunctionReportNum(lungSize)
                .matchQuTotal(reportRate)
                .build();
        log.info("getCertificationUserInfo memCard:{} build:{}",memCard, build);
        return Result.success(build);
    }

}
