package com.ywt.alipaympapi.web.controller;

import com.ywt.alipaympapi.core.utils.BizUtil;
import com.ywt.alipaympapi.core.utils.Checker;
import com.ywt.alipaympapi.models.BaseResponse2;
import com.ywt.alipaympapi.models.auth.AlipayMpCfg;
import com.ywt.alipaympapi.models.msg.SendRegMsgReq;
import com.ywt.alipaympapi.service.AuthService;
import com.ywt.alipaympapi.service.MessageService;
import com.ywt.core.Anonymous;
import com.ywt.core.exception.AppMessageException;
import com.ywt.model.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Walker
 * Created on 2022/9/28
 */
@Slf4j
@RestController("/msg")
@RequestMapping({"/msg"})
public class MsgController {
    @Autowired
    AuthService authService;

    @Autowired
    MessageService messageService;

    @Anonymous(enable = true)
    @RequestMapping(value = {"/sendRegMsg"}, method = RequestMethod.POST)
    public @ResponseBody BaseResponse sendRegMsg(@RequestBody SendRegMsgReq reqData) {
        BaseResponse baseResponse = new BaseResponse();
        try {
            int orderId = Checker.getIntegerValue(reqData.getOrderId());
            String orderNo = Checker.getStringValue(reqData.getOrderNo());
            String orderCreateTime = Checker.getStringValue(reqData.getOrderCreateTime());
            String orderAmountStr = Checker.getStringValue(reqData.getOrderAmountStr());
            String tradeNo = Checker.getStringValue(reqData.getTradeNo());
            String hospitalName = Checker.getStringValue(reqData.getHospitalName());
            String deptName = Checker.getStringValue(reqData.getDeptName());
            String doctorName = Checker.getStringValue(reqData.getDoctorName());
            String doctorId = Checker.getStringValue(reqData.getDoctorId());
            String patientName = Checker.getStringValue(reqData.getPatientName());
            String regDate = Checker.getStringValue(reqData.getRegDate());
            String deptLoc = Checker.getStringValue(reqData.getDeptLoc());
            String alipayUid = Checker.getStringValue(reqData.getAlipayUid());
            int hospitalId = Checker.getIntegerValue(reqData.getHospitalId());
            int alipayMpTerminal = BizUtil.getAlipayMpTerminalIdByHospitalId(hospitalId);
            String merchantOrderStatus = Checker.getStringValue(reqData.getMerchantOrderStatus());
            AlipayMpCfg cfg = authService.getCfg(alipayMpTerminal);
            String appId = cfg.getAppId();
            messageService.sendRegMsg(orderId, orderNo, alipayUid, orderCreateTime, orderAmountStr, tradeNo, hospitalName, deptName,
                    doctorName, doctorId, patientName, regDate, deptLoc, appId, merchantOrderStatus);
            return baseResponse.succeed();
        } catch (AppMessageException appMessageException) {
            return baseResponse.error(appMessageException.getMessage());
        } catch (Exception e) {
            log.error("MsgController#sendRegMsg(reqData={} ):\n {}", reqData, e.getMessage(), e);
            return baseResponse.error(e.getMessage());
        }
    }
}
