package com.ywt.outpatient.controllers;


import com.ywt.biz.common.util.Checker;
import com.ywt.gapi.operation_schedule.OperationScheduleService;
import com.ywt.gapi.operation_schedule.QueryScheduleListRequest;
import com.ywt.gapi.operation_schedule.QueryScheduleListResponse;
import com.ywt.gapi.operation_schedule.ScheduleInfo;
import com.ywt.outpatient.api.req.QueryOperSchReq;
import com.ywt.outpatient.api.resp.BaseResponse2;
import com.ywt.outpatient.api.resp.OperScheduleInfoResp;
import com.ywt.outpatient.api.resp.OperationSchListResp;
import com.ywt.outpatient.api.resp.PageListResponse;
import com.ywt.outpatient.core.BaseResponse;
import com.ywt.outpatient.core.utils.BizUtil;
import com.ywt.outpatient.core.utils.CheckUtil;
import com.ywt.outpatient.service.web.MedicalCardService;
import com.ywt.outpatient.service.web.UserSrv;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController("/operationSchedule")
@RequestMapping(value = {"/operationSchedule"}, method = RequestMethod.POST)
public class OperationScheduleController {
    private static Logger logger = LoggerFactory.getLogger(OperationScheduleController.class);


    @Autowired
    private UserSrv userSrv;

    @DubboReference
    private OperationScheduleService operationScheduleServiceBlockingStub;

    @Autowired
    private MedicalCardService medicalCardService;


    @RequestMapping(value = {"/queryScheduleList"}, method = RequestMethod.POST)
    public @ResponseBody
    BaseResponse2<PageListResponse<OperationSchListResp>> queryScheduleList(@RequestBody QueryOperSchReq req) {
        BaseResponse2<PageListResponse<OperationSchListResp>> baseResponse = new BaseResponse2<>();
        try {
            int userId = userSrv.getCurrentUserIdWrapped();
            if (userId <= 0) {
                CheckUtil.ensureLargerThanZero(userId, "请登录后操作");
            }
            int hospitalId = Checker.getIntegerValue(req.getHospitalId());
            if (hospitalId <= 0) {
                hospitalId = BizUtil.getHospitalIdByTerminalId(userSrv.getCurrentTerminal());
            }
            if (Checker.isNone(req.getHisPatientId())) {
                CheckUtil.ensureNotEmpty(req.getHisPatientId(), "请先绑定有效诊疗卡");
            }
            String hisPatientId = Checker.getStringValue(req.getHisPatientId());
            medicalCardService.verifyMedicalCard(userId, hospitalId, hisPatientId, 0);

            QueryScheduleListRequest request = QueryScheduleListRequest.newBuilder()
                    .setHisPatientId(req.getHisPatientId())
                    .setHospitalId(hospitalId)
                    .setPageIndex(req.getPageIndex())
                    .setPageSize(req.getPageSize())
                    .setStatus(req.getStatus())
                    .build();
            QueryScheduleListResponse res = operationScheduleServiceBlockingStub.queryScheduleList(request);
            List<ScheduleInfo> list = new ArrayList<>();
            if (res.getCode() == BaseResponse.SUCCEED) {
                PageListResponse<OperationSchListResp> resp = new PageListResponse<OperationSchListResp>();
                list = res.getScheduleInfoList();
                resp.setList(list.stream()
                        .map(d -> {
                            OperationSchListResp r = new OperationSchListResp();
                            r.setName(d.getPatientName());
                            r.setAge(d.getAge());
                            r.setGender(Checker.getIntegerValue(d.getGender()));
                            r.setHisPatientId(d.getHisPatientId());
                            r.setAdmTime(d.getAdmTime());
                            r.setOperTime(d.getOperTime());
                            r.setInfoList(d.getScheduleEventList().stream()
                                    .map(p -> {
                                        OperScheduleInfoResp o = new OperScheduleInfoResp();
                                        o.setEventName(p.getEventName());
                                        o.setStartTime(p.getStartTime());
                                        return o;
                                    }).collect(Collectors.toList()));
                            return r;
                        }).collect(Collectors.toList())
                );
                resp.setTotalPages(res.getTotalPages());
                resp.setCount(res.getCount());
                return baseResponse.succeed(resp);
            }
            return baseResponse.failed(BaseResponse.APP_ERROR, res.getInfo());
        } catch (Exception e) {
            logger.error("OperationScheduleController#(): queryScheduleList{}", e.getMessage(), e);
            return baseResponse.error(e.getMessage());
        }
    }


}
