package com.fufan.mtsaas.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fufan.mtsaas.config.RedissonCommand;
import com.fufan.mtsaas.modules.sysmgr.syuser.bo.request.UserSaveRequest;
import com.fufan.mtsaas.modules.base.company.bo.request.CompanySaveRequest;
import com.fufan.mtsaas.util.DataChangeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class CheckInputData {
    @Autowired
    private RedissonCommand redissonCommand;
    private static Map<String,Class<?>> urlMap = new HashMap<>();
    /**
     * 将Object对象里面的属性和值转化成Map对象
     *
     * @return
     * @throws IllegalAccessException
     */


    public static Map<String, String> changeToMap(Class<?> clazz)  {
        Map<String, String> map = new HashMap<String,String>();
        try {
            for (Field field : clazz.getDeclaredFields()) {
                try {
                    field.setAccessible(true);
                    String fieldName = field.getName();
                    if(StringUtils.isNotEmpty(field.get(clazz).toString()) ) {
                        if(field.get(clazz).toString().equals("是")) {
                            field.set(clazz,"1");
                        }
                        if(field.get(clazz).toString().equals("否")) {
                            field.set(clazz,"2");
                        }
                    }

                    map.put(fieldName, fieldName);
                }catch (Exception e){

                }
            }
        }catch (Exception e){

        }

        return map;
    }

    public static void change(Object obj)  {
        try {
            Class<?> clazz = obj.getClass();
            for (Field field : clazz.getDeclaredFields()) {
                try {
                    field.setAccessible(true);
                    String fieldName = field.getName();
                    if(StringUtils.isNotEmpty(field.get(obj).toString()) ) {
                        if(field.get(obj).toString().equals("是")) {
                            field.set(obj,"1");
                        }
                        if(field.get(obj).toString().equals("否")) {
                            field.set(obj,"2");
                        }
                    }
                }catch (Exception e){

                }
            }
        }catch (Exception e){

        }
    }

//   private static  void putUrlMap(){
//       urlMap.put("/UDI/Company/doUpdate", CompanySaveRequest.class);
//       urlMap.put("/UDI/RegistrationCertificate/doUpdate", TRegistrationCertificateSaveRequest.class);
//       urlMap.put("/UDI/RegistrationCertificate/doSave", TRegistrationCertificateSaveRequest.class);
//       urlMap.put("/UDI/Product/doUpdate", TProductSaveRequest.class);
//       urlMap.put("/UDI/Product/doSave", TProductSaveRequest.class);
//       urlMap.put("/UDI/Order/doUpdate", TOrderSaveRequest.class);
//       urlMap.put("/UDI/Order/doUpdate", TOrderSaveRequest.class);
//       urlMap.put("/UDI/PrintTemplate/doSaveOrUpdateModule", PrintTemplateSaveRequest.class);
//       urlMap.put("/SYS/SYuser/doUpdate", UserSaveRequest.class);
//       urlMap.put("/SYS/SYuser/doSave", UserSaveRequest.class);
//   }

    private static  void  checkData(Map mapTypes,Class<?> o){
        Map<String,String> objectMap =   changeToMap(o);
        List<String>  lostDataList = new ArrayList<>();
        List<String>  multiDataList = new ArrayList<>();
        if(mapTypes != null && objectMap != null){
           for (Object map : mapTypes.entrySet()){
                    String temp = ((Map.Entry)map).getKey()+ "";
                    if(!objectMap.containsKey(temp)){
                        multiDataList.add(temp);
                    }
                }
            for (Object map : objectMap.entrySet()){
                String temp = ((Map.Entry)map).getKey() + "";
                if(!mapTypes.containsKey(temp)){
                    lostDataList.add(temp);
                }
            }
            System.out.println("字段多余个数" + multiDataList.size() + "多余字段" + JSONObject.toJSONString(multiDataList) );
            System.out.println("字段缺失个数" + lostDataList.size() + "缺失字段" + JSONObject.toJSONString(lostDataList) );
        }else {
            System.out.println("转换异常");
        }

    }
    public static void main(String[] args) {
        // putUrlMap();
         String testData = "{\"data\":{\n" +
                 "    \"metdata\": {},\n" +
                 "    \"param\": {\n" +
                 "        \"id\": 2,\n" +
                 "        \"companyId\": null,\n" +
                 "        \"batchNo\": \"1\",\n" +
                 "        \"orderNo\": \"1\",\n" +
                 "        \"productionDate\": \"2022-06-04\",\n" +
                 "        \"expireDate\": \"2022-06-18\",\n" +
                 "        \"orderNumber\": 1,\n" +
                 "        \"printedOrderNumber\": 3,\n" +
                 "        \"productName\": \"2\",\n" +
                 "        \"minSalesUnitPi\": \"2\",\n" +
                 "        \"serialNumber\": \"oom\",\n" +
                 "        \"serialNumberLength\": 2,\n" +
                 "        \"randomNumber\": null,\n" +
                 "        \"define1\": \"是\",\n" +
                 "        \"define2\": \"是\",\n" +
                 "        \"define3\": \"是\",\n" +
                 "        \"define4\": \"否\",\n" +
                 "        \"define5\": \"否\",\n" +
                 "        \"define6\": \"否\",\n" +
                 "        \"define7\": \"7\",\n" +
                 "        \"define8\": null,\n" +
                 "        \"randomNumberLength\": 4,\n" +
                 "        \"isOpenRandom\": true\n" +
                 "    },\n" +
                 "    \"sign\": \"111111\"\n" +
                 "}}";
//         JSONObject data = JSON.parseObject(testData).getJSONObject("data").getJSONObject("param");
//        TOrderSaveRequest tOrderSaveRequest = DataChangeUtil.getDataEntity(data,TOrderSaveRequest.class);
//    //     Map aa = data.getJSONObject("data").getJSONObject("param");
////         checkData(aa,urlMap.get("/UDI/Order/doUpdate"));
//        change(tOrderSaveRequest);
//        System.out.println(tOrderSaveRequest.toString());
    }





}
