/**
 * @Author: Kane Wang <wangkane@qq.com>
 * @Date: 2025-03-15 18:40:54
 * @LastEditors: Kane
 * @LastModified: 2025-03-16 08:59:16
 * @FilePath: /cpicxim-huixiabao/src/main/java/com/cpic/xim/huixiabao/web/controllers/MediCover/MediCoverController.java
 * @Description:
 * 
 *               Copyright (c) 2025 by Kane All rights reserved
 */
package com.cpic.xim.huixiabao.web.controllers.MediCover;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.web.bind.annotation.PostMapping;

import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cpic.xim.utils.secrecy.*;
import com.cpic.xim.huixiabao.web.data.nhs.xyx.HmbXyxInfo;
import com.cpic.xim.huixiabao.web.data.nhs.xyx.HmbXyxInfoItem;
import com.cpic.xim.huixiabao.db.HuixiabaoDbUtils;


@Controller
@RequestMapping( "/huixiabao" )
public class MediCoverController
{
    private static Logger logger     = LoggerFactory.getLogger( MediCoverController.class );
    private static String privateKey = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAMBanAXTnHChd6W/vYq6eVmmr1R2p25tghivDEb8qrgxHMCY3lqYhJAw3jgQAH3HzSOFxZ3Mz/WcRaW3CzWfAZ0mm5dcVY0qEgyT/1BOsdhLwKa3BBuURRCEp5ZPapErm3Ugl/OBxjEURgtc9JnVBOECc9Kvk3FXvuN4tDWl18lxAgMBAAECgYBBAJ3HLkqomTg4sAIaloeZr0WC0pIkNCeJteX9OzdqBVbFEyPZE7+AhrMh2O6BkvqU7YjpmaQRSR0UMjY8iZ5eaJ8vkPJPFgoJI14dqj3Pby46GYqC8iPHTeFN35RL6Sgktt9i/loLoiRj6ifQ2AqnlE3e66Iaw1Zdnx1Lt9/T7QJBAP6PZatPD4UM9kQ0xO1QmJ+IsgLBhv3fhn2i8X4NSJGJ1yi9+JWnQ+CfBZ2u/V0op1Wz5kIDK7BA40o2Ntlf6acCQQDBcSNGlyE5sSSJS39pnbTqxNjKoakBQ5LFIrk4ld8Vuk0H7dcdfUittHoDnNMn9TKIIAvxo0s4fQe8WMPqH2cnAkEA4x49r6JSTntKHYCXUsTd5zim2h6gulF8RQvkOnrPjEpXVVppN9yMixRWhmFiXIOxCJ4BilQZ3p+GKuhD953DcQJBAKdGjpqcVb7t0AppnuRV2/yn/FA9O0g8hs6yGDk2YJMJZ1NT+JEnpZGQX3KASphjEC9mhu2Np55RgD7/A2uRfEECQQCdyFLVuPMSnCRAXvwb0EegaM0sS4/bT3/fix7RpKqv7tsUfXXsL1bbxxp9b/FBJe834s6wqq0U2wTlfVHXD1Oh";
    private static String publicyKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDAWpwF05xwoXelv72KunlZpq9UdqdubYIYrwxG/Kq4MRzAmN5amISQMN44EAB9x80jhcWdzM/1nEWltws1nwGdJpuXXFWNKhIMk/9QTrHYS8CmtwQblEUQhKeWT2qRK5t1IJfzgcYxFEYLXPSZ1QThAnPSr5NxV77jeLQ1pdfJcQIDAQAB";

    /**
     * 接收医保局财政支持小药箱数据。
     * 
     * @param request
     * @return
     */
    @PostMapping( "/xyx" )
    @ResponseBody
    public static MediCoverResponse getHmbXyxInfo(
            @RequestBody MediCoverRequestBody request
    )
    {
        ObjectMapper      jsonMapper = new ObjectMapper();
        MediCoverResponse response   = null;
        HmbXyxInfo        xyxInfo    = null;

        String decryptData = AESUtils.decryptData( request.getKey(), privateKey, request.getBizContent() );

        logger.info( "【接收小药箱数据】请求参数<{}>", request.toString() );
        logger.info( "【接收小药箱数据】解密参数<{}>", decryptData );

        try
        {
            xyxInfo = jsonMapper.readValue( decryptData, HmbXyxInfo.class );

            HuixiabaoDbUtils.saveHmbXyxInfoToMysql( xyxInfo.getHmbList() );
        }
        catch ( ClassNotFoundException error )
        {
            logger.error( "加载mysql驱动失败：{}", error.getMessage() );

            // 解析json错误。通知调用者
            response = new MediCoverResponse( "500",
                                              "加载mysql驱动失败：" + error.getMessage(),
                                              "",
                                              "",
                                              "",
                                              false );
        }
        catch ( SQLException error )
        {
            // 执行sql错误。通知调用者
            logger.error( "执行sql失败：{}", error.getMessage() );

            response = new MediCoverResponse( "500",
                                              "执行sql失败：" + error.getMessage(),
                                              "",
                                              "",
                                              "",
                                              false );
        }
        catch ( Exception error )
        {
            // 解析json错误。通知调用者
            response = new MediCoverResponse( "500",
                                              "解析JSON失败：{}" + error.getMessage(),
                                              "",
                                              "",
                                              "",
                                              false );

        }


        if ( response == null )
        {
            // 处理成功
            response = new MediCoverResponse( "20000",
                                              "处理成功",
                                              "",
                                              "",
                                              "",
                                              true );
        }

        return response;
    }
}