package com.casic.missiles.utils;

import com.casic.missiles.annotation.DictCodeField;
import com.casic.missiles.core.application.service.AbstractDictService;
import com.casic.missiles.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.Objects;

/**
 * 字典utils
 */
public class DictCodeUtils {
    /**
     * 字典code转换成名称
     */
    public static void convertDictCodeToName(Object object) throws Exception {
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (!field.isAccessible()) {
                field.setAccessible(true);
            }
            if (field.isAnnotationPresent(DictCodeField.class) && field.get(object) != null) {
                DictCodeField dictCodeField = field.getAnnotationsByType(DictCodeField.class)[0];
                if (StringUtils.isEmpty(dictCodeField.cacheName())) {
                    throw new BusinessException(500, "缓存名不能为空");
                }
                AbstractDictService dictService = SpringContextUtil.getBean(AbstractDictService.class);
                if (Objects.isNull(field.get(object)) || StringUtils.isEmpty(field.get(object).toString())) {
                    continue;
                }
                String name = dictService.getDictNameByCode(dictCodeField.cacheName(), field.get(object).toString());
                if (StringUtils.isEmpty(name)) {
                    throw new BusinessException(500, dictCodeField.message());
                }
                String destFieldName = field.getName() + "Name";
                Field destField = object.getClass().getDeclaredField(destFieldName);
                if (destField == null) {
                    throw new BusinessException(500, "字典名称字段不存在");
                }
                if (!destField.isAccessible()) {
                    destField.setAccessible(true);
                }
                destField.set(object, name);
            }
        }
    }


    /**
     * 多个字典code（逗号隔开）,转换成名称
     */
    public static void convertDictMultiCodeToName(Object object) throws Exception {
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (!field.isAccessible()) {
                field.setAccessible(true);
            }
            if (field.isAnnotationPresent(DictCodeField.class) && field.get(object) != null) {

                DictCodeField dictCodeField = field.getAnnotationsByType(DictCodeField.class)[0];
                if (StringUtils.isEmpty(dictCodeField.cacheName())) {
                    throw new BusinessException(500, "缓存名不能为空");
                }
                AbstractDictService dictService = SpringContextUtil.getBean(AbstractDictService.class);
                if (Objects.isNull(field.get(object)) || StringUtils.isEmpty(field.get(object).toString())) {
                    continue;
                }
                String name = "";
                //进行字典值得获取
                if (field.get(object).toString().contains(",")) {
                    String[] filedObjects = field.get(object).toString().split(",");
                    for (String filedObject : filedObjects) {
                        name += dictService.getDictNameByCode(dictCodeField.cacheName(), filedObject) + ",";
                        if (StringUtils.isEmpty(name)) {
                            throw new BusinessException(500, dictCodeField.message());
                        }
                    }
                    if (StringUtils.isEmpty(name)) {
                        throw new BusinessException(500, dictCodeField.message());
                    }
                } else {
                    name = dictService.getDictNameByCode(dictCodeField.cacheName(), field.get(object).toString());
                    if (StringUtils.isEmpty(name)) {
                        throw new BusinessException(500, dictCodeField.message());
                    }
                }
                String destFieldName = field.getName() + "Name";
                Field destField = object.getClass().getDeclaredField(destFieldName);
                if (destField == null) {
                    throw new BusinessException(500, "字典名称字段不存在");
                }
                if (!destField.isAccessible()) {
                    destField.setAccessible(true);
                }
                destField.set(object, name);
            }
        }
    }

    /**
     * 校验字典code是否合法
     *
     * @param object
     * @return
     */
    public static boolean validDictCode(Object object) throws Exception {
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (!field.isAccessible()) {
                field.setAccessible(true);
            }
            if (field.isAnnotationPresent(DictCodeField.class) && field.get(object) != null && !field.get(object).equals("")) {
                DictCodeField dictCodeField = field.getAnnotationsByType(DictCodeField.class)[0];
                // 判断字典code是否需要校验
                if (!dictCodeField.needValid()) {
                    continue;
                }
                AbstractDictService dictService = SpringContextUtil.getBean(AbstractDictService.class);
                if (StringUtils.isEmpty(dictCodeField.cacheName())) {
                    throw new BusinessException(500, "缓存名不能为空");
                }
                String name = dictService.getDictNameByCode(dictCodeField.cacheName(), field.get(object).toString());
                if (StringUtils.isEmpty(name)) {
                    throw new BusinessException(500, dictCodeField.message());
                }
                dictCodeField.message();
            }
        }
        return true;
    }

}