package com.sw.mes.jip.utils;

import java.lang.reflect.Field;
import java.security.SecureRandom;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.sw.mes.core.annotation.Jinko;
import com.sw.mes.core.dto.sap.ReportSapGoodsmvtDto;
import com.sw.mes.core.utils.DateUtil;
import com.sw.mes.jip.dto.TokenDto;
import com.sw.mes.jip.entity.JipHeaderConfig;
import com.sw.mes.sap.dto.jip.JipISBCINFO;
import com.sw.mes.utils.StringUtils;

@Jinko(value = "针对晶科Jip数据处理工具类",author = "liru")
public class JipUtil {
    public static Map<String,Object> getMap(Object obj, String key){
        Object resultObj = ((Map<String,Object>)obj).get(key);
        if(resultObj == null) return null;

        if(resultObj instanceof Map){
            return (Map<String, Object>) resultObj;
        }else{
            return null;
        }
    }

    public static List<Object> getList(Object obj, String key){
        Object resultObj = ((Map<String,Object>)obj).get(key);
        if(resultObj == null) return null;

        if(resultObj instanceof List){
            return (List<Object>) resultObj;
        }else{
            return null;
        }
    }


    public static <T> T convert(Class<T> clazz, Object objMap) throws IllegalAccessException, InstantiationException,RuntimeException {
        if(objMap ==null || !(objMap instanceof Map)){
            throw new RuntimeException("object is null");
        }

        Map<String, Object> map = (Map<String,Object>)objMap;
        if (map == null || map.isEmpty()) {
            throw new RuntimeException("object is null");
        }


        // 创建目标对象实例
        T obj = clazz.newInstance();

        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String fieldName = entry.getKey();

            try {
                Field field = clazz.getDeclaredField(fieldName);

                // 设置字段可访问性（如果有需要）
                field.setAccessible(true);

                // 根据键值对更新对应字段的值
                field.set(obj, entry.getValue());
            } catch (NoSuchFieldException e) {
                continue;
            }
        }

        return obj;
    }

    
    public static JipISBCINFO initISBCINFO(JipHeaderConfig source) {
    	return initISBCINFO(source,null);
    }
    
    
    

    public static JipISBCINFO initISBCINFO(JipHeaderConfig source, ReportSapGoodsmvtDto reportSapGoodsmvtDto){
        if(source==null)return null;
        //设置配置信息
        JipISBCINFO jipisbcinfo = new JipISBCINFO();
        jipisbcinfo.setIFID(source.getIfId());
        jipisbcinfo.setIFNAME(source.getIfName());
        jipisbcinfo.setMSGID(source.getMsgId());
        jipisbcinfo.setRESEND_ID(source.getResendId());
        jipisbcinfo.setBUSTYP(source.getBustyp());
        jipisbcinfo.setSENDER(source.getSender());
        jipisbcinfo.setRECEIVER(source.getReceiver());
        jipisbcinfo.setTEXT1(source.getText1());
        jipisbcinfo.setTEXT2(source.getText2());
        jipisbcinfo.setTEXT3(source.getText3());
        jipisbcinfo.setTEXT4(source.getText4());
        jipisbcinfo.setTEXT5(source.getText5());

        //初始化公用变化数据信息
        if(reportSapGoodsmvtDto==null || StringUtils.isBlank(reportSapGoodsmvtDto.getUuidValue())) {
        	jipisbcinfo.setUUID(UUID.randomUUID().toString().replace("-", ""));
        	
        	if(reportSapGoodsmvtDto!=null)
        		reportSapGoodsmvtDto.setUuidValue(jipisbcinfo.getUUID());
        }else {
        	jipisbcinfo.setUUID(reportSapGoodsmvtDto.getUuidValue());
        }
        
        jipisbcinfo.setSEND_DT(DateUtil.formatDate(new Date(),DateUtil.longFormats));
        
        SecureRandom random = new SecureRandom();
        int num = random.nextInt(10)+1;
        jipisbcinfo.setMSGID("MES-"+DateUtil.formatDate(new Date(),"yyyyMMddHHmmssSSS")+num);
        jipisbcinfo.setRESEND_ID(randomThreeNumberGenerator()+"");

        return jipisbcinfo;
    }

    public static TokenDto initToken(JipHeaderConfig source){
        if(source==null)return null;
        //设置配置信息
        TokenDto token = new TokenDto();
        token.setTokenKey(source.getTokenKey());
        token.setTokenPassword(source.getTokenPassword());
        return token;
    }


    public static JipISBCINFO getJipISBCINFO(JipHeaderConfig source){
        if(source==null)return null;
        //设置配置信息
        JipISBCINFO jipisbcinfo = new JipISBCINFO();
        jipisbcinfo.setIFID(source.getIfId());
        jipisbcinfo.setIFNAME(source.getIfName());
        jipisbcinfo.setMSGID(source.getMsgId());
        jipisbcinfo.setRESEND_ID(source.getResendId());
        jipisbcinfo.setBUSTYP(source.getBustyp());
        jipisbcinfo.setSENDER(source.getSender());
        jipisbcinfo.setRECEIVER(source.getReceiver());
        jipisbcinfo.setTEXT1(source.getText1());
        jipisbcinfo.setTEXT2(source.getText2());
        jipisbcinfo.setTEXT3(source.getText3());
        jipisbcinfo.setTEXT4(source.getText4());
        jipisbcinfo.setTEXT5(source.getText5());

        //初始化公用变化数据信息
        jipisbcinfo.setUUID(UUID.randomUUID().toString().replace("-", ""));
        jipisbcinfo.setSEND_DT(DateUtil.formatDate(new Date(),DateUtil.longFormats));

        return jipisbcinfo;
    }

    public static int randomThreeNumberGenerator(){
        // 创建Random对象
    	SecureRandom random = new SecureRandom();
        // 生成0到999之间的随机数
        int number = random.nextInt(1000);
        return number;
    }

}
