package com.ym.mec.biz.service.impl;

import com.ym.mec.biz.dal.dao.DataChangeLogDao;
import com.ym.mec.biz.dal.dto.VipGroupActivityAddDto;
import com.ym.mec.biz.dal.entity.DataChangeLog;
import com.ym.mec.biz.dal.enums.VipGroupActivityTypeEnum;
import com.ym.mec.biz.service.DataChangeLogService;
import com.ym.mec.biz.service.LogService;
import com.ym.mec.util.date.DateUtil;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * @Author qnc99
 * @Date 2020/12/15 0015
 */
@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private DataChangeLogDao dataChangeLogDao;

    private Field[] getAllFields(Object object){
        Class clazz = object.getClass();
        List<Field> fieldList = new ArrayList<>();
        while (clazz != null){
            fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
            clazz = clazz.getSuperclass();
        }
        Field[] fields = new Field[fieldList.size()];
        fieldList.toArray(fields);
        return fields;
    }

    @Override
    public void entityVariationAnalysis(Object oldEntity, Object newEntity, Integer operatorId) {
        List<String> changes = new ArrayList<>();
        Field[] allFields = getAllFields(oldEntity);
        Long dataId = Long.valueOf(0);
        for (Field field : allFields) {
            if(field.getName().equals("createTime")||field.getName().equals("updateTime")){
                continue;
            }
            ApiModelProperty annotation = field.getAnnotation(ApiModelProperty.class);
            if(Objects.isNull(annotation)){
                continue;
            }
            field.setAccessible(true);
            try {
                if(field.getName().equals("id")){
                    dataId = Long.valueOf(String.valueOf(field.get(oldEntity)));
                }
                String oldValue = "", newValue = "";
                if(field.getType().equals(Date.class)){
                    if(Objects.nonNull(field.get(oldEntity))){
                        oldValue = DateUtil.dateToString((Date) field.get(oldEntity), DateUtil.EXPANDED_DATE_TIME_FORMAT);
                    }
                    if(Objects.nonNull(field.get(newEntity))){
                        newValue = DateUtil.dateToString((Date) field.get(newEntity), DateUtil.EXPANDED_DATE_TIME_FORMAT);
                    }
                }else{
                    oldValue = String.valueOf(field.get(oldEntity));
                    newValue = String.valueOf(field.get(newEntity));
                }
                if(!oldValue.equals(newValue)){
                    changes.add(annotation.value() + "：" + oldValue + " -> " + newValue);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        if(CollectionUtils.isEmpty(changes)){
            return;
        }
        DataChangeLog dataChangeLog = new DataChangeLog();
        dataChangeLog.setClassName(oldEntity.getClass().getName());
        dataChangeLog.setDataId(dataId);
        dataChangeLog.setChangeInfo(StringUtils.join(changes, "；"));
        dataChangeLog.setOperatorId(operatorId);
        dataChangeLogDao.insert(dataChangeLog);
    }
}
