package com.ywt.alipaympapi.controller;

import com.ywt.alipaympapi.service.IsvMedicCardService;
import com.ywt.core.exception.AppMessageException;
import com.ywt.model.BaseResponse;
import com.ywt.model.PageVO;
import com.ywt.model.ResponseVO;
import com.ywt.request.card.*;
import com.ywt.response.card.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Desc
 */
@RestController
@RequestMapping("")
@Slf4j
public class MedicCardController {

    @Autowired
    private IsvMedicCardService medicCardService;


    /**
     * 绑定或新增就诊卡
     *
     * @param request
     * @return
     */
    @PostMapping("isvRequest.medicCard.createOrBind")
    public ResponseVO<CreateOrBindCardResponseData> createOrBind(CreateOrBindCardRequest request) throws Exception{

        return ResponseVO.success(medicCardService.createOrBindMedicCard(request));
    }

    /**
     * 修改就诊卡
     *
     * @param request
     * @return
     */
    @PostMapping("isvRequest.medicCard.update")
    public ResponseVO<UpdateCardResponseData> updateMedicCard(UpdateCardRequest request) throws Exception{
        return ResponseVO.success(medicCardService.updateMedicCard(request));
    }

    /**
     * 单卡查询
     *
     * @param request
     * @return
     */
    @PostMapping("isvRequest.medicCard.querySingle")
    public ResponseVO<QuerySingleCardResponseData> querySingle(QuerySingleCardRequest request) throws Exception{

        return ResponseVO.success(medicCardService.querySingleMedicCard(request));
    }

    /**
     * 默认就诊卡查询
     *
     * @param request
     * @return
     */
    @PostMapping("isvRequest.medicCard.defaultCard")
    public ResponseVO<QueryDefaultCardResponseData> defaultCard(QueryDefaultCardRequest request) throws Exception {
        return ResponseVO.success(medicCardService.queryDefaultMedicCard(request));
    }

    /**
     * 我的证件
     *
     * @param request
     * @return
     */
    @PostMapping("miniProRequest.medicCard.myCard")
    public ResponseVO<QueryDefaultCardResponseData> myCard(QueryDefaultCardRequest request) throws Exception {

        return ResponseVO.success(medicCardService.queryDefaultMedicCard(request));
    }

    /**
     * 就诊卡列表
     *
     * @param request
     * @return
     */
    @RequestMapping("isvRequest.medicCard.queryList")
    public ResponseVO<PageVO<QueryListCardResponseData>> queryList(QueryListCardRequest request) throws Exception {

        log.info("request is {}", request);

        PageVO<QueryListCardResponseData> data = medicCardService.queryListMedicCard(request);


        return ResponseVO.success(data);
    }

    /**
     * 删除就诊卡
     *
     * @param request
     * @return
     */
    @PostMapping("isvRequest.medicCard.delete")
    public ResponseVO<DeleteCardResponseData> delete(DeleteCardRequest request) throws Exception {

        if (StringUtils.isBlank(request.getCardNum())) {
            return ResponseVO.fail(500, "就诊卡卡号为空");
        }
        return ResponseVO.success(medicCardService.deleteMedicCard(request));
    }
    /**
     * 就诊人列表
     *
     * @param request
     * @return
     */
    @PostMapping("isvRequest.patient.queryList")
    public ResponseVO<List<QueryPatientListResponse>> queryPatientList(QueryPatientListRequest request) throws Exception {

        return ResponseVO.success(medicCardService.queryPatientList(request));
    }
    /**
     * 就诊人绑定默认就诊卡
     *
     * @param request
     * @return
     */
    @PostMapping("isvRequest.patient.bindMedicCard")
    public ResponseVO<PatientBindCardResponse> bindMedicCard(PatientBindCardRequest request) throws Exception {

        return ResponseVO.success(medicCardService.bindMedicCard(request));
    }
    /**
     * 删除就诊人
     *
     * @param request
     * @return
     */
    @PostMapping("isvRequest.patient.delete")
    public ResponseVO<DeletePatientResponse> deletePatient(DeletePatientRequest request) throws Exception {

        return ResponseVO.success(medicCardService.deletePatient(request));
    }
    /**
     * 添加就诊人
     *
     * @param request
     * @return
     */
    @PostMapping("isvRequest.patient.add")
    public ResponseVO<AddPatientResponse> addPatient(AddPatientRequest request) throws Exception {

        return ResponseVO.success(medicCardService.addPatient(request));
    }
    /**
     * 添加就诊人
     *
     * @param request
     * @return
     */
    @PostMapping("isvRequest.patient.update")
    public ResponseVO<UpdatePatientResponse> updatePatient(UpdatePatientRequest request) throws Exception {

        return ResponseVO.success(medicCardService.updatePatient(request));
    }
    /**
     * 查询就诊人详情
     *
     * @param request
     * @return
     */
    @PostMapping("isvRequest.patient.querySingle")
    public ResponseVO<QueryPatientDetailResponse> querySinglePatient(QueryPatientDetailRequest request) throws Exception {
        try {
            return ResponseVO.success(medicCardService.querySinglePatient(request));
        } catch (AppMessageException e) {
            return ResponseVO.fail(BaseResponse.PARAMETER_ERROR, e.getMessage());
        } catch (Exception e) {
            log.error("MedicCardController#querySinglePatient(request={} ):\n {}", request, e.getMessage(), e);
            return ResponseVO.fail(BaseResponse.APP_ERROR, e.getMessage());
        }
    }
    /**
     * 查询登录用户就诊卡列表
     *
     * @param request
     * @return
     */
    @PostMapping("isvRequest.medicCard.myCardList")
    public ResponseVO<List<MyCardListResponse>> myCardList(MyCardListRequest request) throws Exception {

        return ResponseVO.success(medicCardService.myCardList(request));
    }


}
