package com.unis.basic.info.modular.mq.sub;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.gson.Gson;
import com.unis.basic.info.modular.gjjmapper.mapper.GjjMapper;
import com.unis.basic.info.modular.mq.dto.RecevieDTO;
import com.unis.basic.info.modular.mq.dto.UploadDTO;
import com.unis.basic.info.modular.mq.dto.jkEnum;
import com.unis.basic.info.modular.mq.utils.JsonUtils;
import com.unis.basic.info.modular.mq.utils.SecureMngr;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的订阅
 *
 * @author ybw
 * @version V1.0
 * @className MySub
 * @date 2024/5/9
 **/
@Component
public class MyMessageListener implements MessageListener {
    private Log log = LogFactory.getLog(this.getClass());

    @Resource
    private RestTemplate restTemplate;

/*	@Bean
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}*/


    @Resource
    private GjjMapper gjjMapper;

    @Value("${jsgjjsjUrl}")
    private String jsgjjsjUrl;


    @Override
    public void onMessage(Message message, byte[] bytes) {

        //1、转换为对象
        byte[] body = message.getBody();
        if (body.length > 0) {
            RecevieDTO recevieDTO = JsonUtils.mqMessageToObj1(message.getBody());
            if (ObjectUtils.isNotEmpty(recevieDTO)) {
                this.getDate(recevieDTO);
            }
        }
    }


    private synchronized void getDate(RecevieDTO recevieDTO) {

        /**
         * 拼接业务数据
         */
        String czbz = recevieDTO.getCzbz();
        Integer orgId = recevieDTO.getOrgId();
        String tableName = recevieDTO.getTableName();
        String ywId = recevieDTO.getYwId();

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, List<Map<String, Object>>> map1 = new HashMap<String, List<Map<String, Object>>>();
        List<Map<String, List<Map<String, Object>>>> ywData = new ArrayList<Map<String, List<Map<String, Object>>>>();

        Map<String, Object> pjsjmap = new HashMap<>();
        if (StringUtils.isNotEmpty(tableName)) {
            if (tableName.equals(jkEnum.dw.getTableName())) {//仓储单位信息

                pjsjmap = gjjMapper.getDwGjjDate(ywId);

            } else if (tableName.equals(jkEnum.cbgm.getTableName())) {//储备规模

                pjsjmap = gjjMapper.getCbgmGjjDate(ywId);

            }else if (tableName.equals(jkEnum.xmxx.getTableName())) {//项目信息

                pjsjmap = gjjMapper.getXmxxGjjDate(ywId);

            }else if (tableName.equals(jkEnum.lhjh.getTableName())) {//轮换计划信息

                pjsjmap = gjjMapper.getLhjhGjjDate(ywId);

            }else if (tableName.equals(jkEnum.lhjhmx.getTableName())) {//轮换计划明细信息

                pjsjmap = gjjMapper.getLhjhmxGjjDate(ywId);

            }else if (tableName.equals(jkEnum.cbjh.getTableName())) {//储备计划

                pjsjmap = gjjMapper.getCbjhGjjDate(ywId);

            }

            if (ObjectUtils.isEmpty(pjsjmap)) {
                pjsjmap.put("DEP_KQDM", orgId);
                pjsjmap.put("CZBZ", czbz);

                list.add(pjsjmap);
                map1.put(tableName, list);
                ywData.add(map1);

                Gson Gson = new Gson();
                String a = Gson.toJson(ywData);
                //数据加密
                String bodyStr = null;

                try {
                    bodyStr = SecureMngr.encryptByAES(SecureMngr.AES_KEY, a);

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                LocalDateTime date = LocalDateTime.now();
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                String formattedDate = date.format(formatter).toString();

                UploadDTO sendData = new UploadDTO();
                sendData.setOrgNo(orgId + "");
                sendData.setBody(bodyStr);
                sendData.setSendTime(formattedDate);
                sendData.setId("9caaa29a-7297-4195-89ee-b7bbefccc9aa");
                sendData.setVer("1.0");


                /**
                 * restTemplate调用
                 */
                //String url = "http://localhost:49183/service/API/CJCURE/DateUpload";//
                // 发送post请求，并打印结果，以String类型接收响应结果JSON字符串
                restTemplate.postForObject(jsgjjsjUrl, sendData, Object.class);
            }
        }

    }


    /**
     * 业务处理-测试
     *
     * @param baseDTO
     * @methodName: handle
     * @return: void
     * @author: ybw
     * @date: 2024/5/9
     **/
	/*private void handleTest(BaseDTO<TestDTO> baseDTO) {
		switch (baseDTO.getBizOperateType()) {
			case INSERT:
				log.info("新增");
				break;
			case UPDATE:
				log.info("修改");
				break;
			case DELETE:
				log.info("删除");
				break;
			default:
				log.info("其他");
				break;
		}
	}*/


}
