package cn.estsh.i3plus.ext.mes.apiservice.util;

import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.tool.MathOperation;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import org.springframework.util.StringUtils;

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;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-bu
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2022-10-28 13:42
 */
public class BusiExtUtil {

    /**
     * 根据fields 比对指定的对象字段值，记录存在差异的字段值
     * list.get(0) 源数据
     * list.get(1) 新数据
     * @param sourceData 源数据对象
     * @param newData 新数据对象
     * @param clazz 类
     * @param fields 字段名集合
     * @param <T>
     * @return
     */
    public static <T> Map<String, List<String>> checkParamValue(T sourceData, T newData, Class clazz, List<String> fields){
        Map<String, List<String>> resultMap = new HashMap<>();
        String error = "";
        try {
            for (String fieldStr : fields) {
                error = fieldStr;
                Field field = null;
                try {
                    field = clazz.getField(fieldStr);
                } catch (NoSuchFieldException noSuchFieldException) {
                    try {
                        field = clazz.getDeclaredField(fieldStr);
                    } catch (NoSuchFieldException e) {
                        Class superclass = clazz.getSuperclass();
                        field = superclass.getDeclaredField(fieldStr);
                    }
                }

                field.setAccessible(true);

                Object value1 = field.get(sourceData);
                Object value2 = field.get(newData);

                String value1Str = StringUtils.isEmpty(value1) ? "" : value1.toString();
                String value2Str = StringUtils.isEmpty(value2) ? "" : value2.toString();

                if (!value1Str.equals(value2Str)) {
                    List<String> resultList = new ArrayList(2);
                    resultList.add(value1Str);
                    resultList.add(value2Str);
                    resultMap.put(fieldStr, resultList);
                }
            }
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("履历菜单字段信息页面配置字段【%s】不存在", error)
                    .build();
        }
        return resultMap;
    }

    public static BigDecimal divThenMul100Format2RoundHalfUp(Double d1, Double d2) {
        return format2RounHalfUp(MathOperation.mul(MathOperation.div(d1, d2), 100));
    }

    private static BigDecimal format2RounHalfUp(Double d) {
        if (StringUtils.isEmpty(d)) return new BigDecimal(0);
        BigDecimal bd = new BigDecimal(d);
        bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
        return bd;
    }
}
