package cn.stylefeng.team.project.modular.businessrender.factory;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.team.meta.api.constants.MetaConstants;
import cn.stylefeng.team.meta.api.entity.MetaModel;
import cn.stylefeng.team.project.modular.businessrender.pojo.MetaModelData;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class ModelDataKeyFactory {

    public static void processCommonDateBeforeSave(MetaModelData metaModelData, List<MetaModel> businessMetaModelList) {

        LoginUser loginUserNullable = LoginContext.me().getLoginUserNullable();

        String dateTime = DateUtil.formatDateTime(new Date());

        Map<String, Object> mainTableData = metaModelData.getMainTableData();

        mainTableData.put(MetaConstants.COMMON_ID_FILED_CODE, metaModelData.getPrimaryKeyId());

        mainTableData.put(MetaConstants.COMMON_CREATE_TIME_FIELD_CODE, dateTime);
        if (loginUserNullable != null) {
            mainTableData.put(MetaConstants.COMMON_CREATE_USER_FIELD_CODE, loginUserNullable.getUserId());
        }

        Map<String, List<Map<String, Object>>> subTableData = metaModelData.getSubTableData();
        if (subTableData == null) {
            return;
        }

        for (Map.Entry<String, List<Map<String, Object>>> subTableDataList : subTableData.entrySet()) {

            String subTableTableName = subTableDataList.getKey();

            List<Map<String, Object>> subTableDataListValue = subTableDataList.getValue();
            if (ObjectUtil.isEmpty(subTableDataListValue)) {
                continue;
            }

            for (Map<String, Object> record : subTableDataListValue) {

                record.put(MetaConstants.COMMON_ID_FILED_CODE, IdWorker.getId());

                mainTableData.put(MetaConstants.COMMON_CREATE_TIME_FIELD_CODE, dateTime);
                if (loginUserNullable != null) {
                    mainTableData.put(MetaConstants.COMMON_CREATE_USER_FIELD_CODE, loginUserNullable.getUserId());
                }

                for (MetaModel model : businessMetaModelList) {
                    if (model.getMetaModelCode().equals(subTableTableName)) {
                        String fieldCode = model.getSubTableFieldCode();
                        record.put(fieldCode, metaModelData.getPrimaryKeyId());
                    }
                }
            }
        }
    }

    public static void processCommonDataBeforeUpdate(MetaModelData metaModelData, List<MetaModel> businessMetaModelList) {

        LoginUser loginUserNullable = LoginContext.me().getLoginUserNullable();

        String dateTime = DateUtil.formatDateTime(new Date());

        Map<String, Object> mainTableData = metaModelData.getMainTableData();

        mainTableData.put(MetaConstants.COMMON_UPDATE_TIME_FIELD_CODE, dateTime);
        if (loginUserNullable != null) {
            mainTableData.put(MetaConstants.COMMON_UPDATE_USER_FIELD_CODE, loginUserNullable.getUserId());
        }

        Map<String, List<Map<String, Object>>> subTableData = metaModelData.getSubTableData();
        if (subTableData == null) {
            return;
        }

        for (Map.Entry<String, List<Map<String, Object>>> subTableDataList : subTableData.entrySet()) {

            String subTableTableName = subTableDataList.getKey();

            List<Map<String, Object>> subTableDataListValue = subTableDataList.getValue();
            if (ObjectUtil.isEmpty(subTableDataListValue)) {
                continue;
            }

            for (Map<String, Object> record : subTableDataListValue) {

                record.put(MetaConstants.COMMON_ID_FILED_CODE, IdWorker.getId());

                mainTableData.put(MetaConstants.COMMON_UPDATE_TIME_FIELD_CODE, dateTime);
                if (loginUserNullable != null) {
                    mainTableData.put(MetaConstants.COMMON_UPDATE_USER_FIELD_CODE, loginUserNullable.getUserId());
                }

                for (MetaModel model : businessMetaModelList) {
                    if (model.getMetaModelCode().equals(subTableTableName)) {
                        String fieldCode = model.getSubTableFieldCode();
                        record.put(fieldCode, metaModelData.getPrimaryKeyId());
                    }
                }
            }
        }

    }
}
