package org.jeecg.ctop.material.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.jeecg.ctop.material.constants.Check;
import org.jeecg.ctop.material.entity.KuaishouUnitReportHourlyPojo;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Administrator
 * 2022/3/15
 **/
public class FeidNotNullUtils {


    public static void setFeidValueNotNull (Object obj) {
        try {
            for (Field field : obj.getClass().getDeclaredFields()) {
                field.setAccessible(true);
                if (field.get(obj) == null) {
                    if (field.getGenericType().toString().equals("class java.lang.String")) {
                        field.set(obj, "0");
                    } else if (field.getGenericType().toString().equals("class java.lang.Integer")) {
                        field.set(obj, 0);
                    } else if (field.getGenericType().toString().equals("class java.lang.Double")) {
                        field.set(obj, 0.0);
                    } else if (field.getGenericType().toString().equals("class java.lang.Long")) {
                        field.set(obj, 0L);
                    } else if (field.getGenericType().toString().equals("class java.math.BigDecimal")) {
                        field.set(obj, new BigDecimal(0));
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     *
     * @description: 获取自定义列数据
     * @param dateList 数据详情 example:[
     *         {
     *
     *             "realname": 2,
     *             "roleType": 3,
     *             "create_time": "2022-08-23 01:01:33",
     *             "authName": "202206-智能托管",
     *             "content_log": "测试测试测试123"
     *         },{
     *
     *             "realname": 2,
     *             "roleType": 3,
     *             "create_time": "2022-08-23 01:01:33",
     *             "authName": "20220619-促活-优选-RTA-sf_ks-ocpm-视频-低价-单品-0619-智能托管",
     *             "content_log": "测试测试测试123"
     *         }]
     *
     *
     *
     * @param array 自定义列信息
     *              example: [{
     *         "title":"账户名称",
     *         "dataIndex":"authName",
     *         "align":"center",
     *         "key":"date",
     *         "width":150,
     *         "fixed":"left"
     *     },{
     *         "title":"运营人员",
     *         "dataIndex":"realname",
     *         "align":"center",
     *         "width":150,
     *         "fixed":"left"
     *     }]
     *
     *  最终返回的结果数据 example:[{
     *             "realname": 2,
     *             "authName": "202206-智能托管"
     *         },{
     *             "realname": 2,
     *             "authName": "20220619-促活-优选-RTA-sf_ks-ocpm-视频-低价-单品-0619-智能托管"
     *         }]
     *
     *
     * @return: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @author: zianY
     */
    public static List<Map<String,Object>> getCustomDateColumnList(List dateList, JSONArray array) throws NoSuchFieldException{
        List<Map<String,Object>> resultList = new ArrayList<>();
        for (int j = 0; j < dateList.size(); j++) {
            Object obj = dateList.get(j);
            Map unitMap = Check.parseObj2Map(obj);
            /**
             * 之所以加了这三行代码 是因为 obj 中的 这三个参数 转成unitMap 后 key变成了 AClick/BClick/ROI 所以导致在根据自定义列中的fieldName获取值的时候 获取不到
             * 别问为啥不修改下字段的命名规则 或者 前端同事修改下自定义列的字段 因为 我的同事是个老厨师了
             */
            unitMap.put("aClick",unitMap.get("AClick"));
            unitMap.put("bClick",unitMap.get("BClick"));
            unitMap.put("roi",unitMap.get("ROI"));

            Map<String,Object> map = new HashMap<>();
            for (int k =0;k < array.size();k++) {
                JSONObject json = (JSONObject)array.get(k);
                String fieldName = obj.getClass().getDeclaredField(json.getString("dataIndex")).getName();
                unitMap.get(fieldName);
                map.put(fieldName, unitMap.get(fieldName));
            }
            resultList.add(map);
        }
        return resultList;
    }






}