package cn.stylefeng.team.project.modular.businessrender.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.team.meta.api.MetaModelServiceApi;
import cn.stylefeng.team.meta.api.entity.MetaModel;
import cn.stylefeng.team.meta.api.enums.SubTableTypeEnum;
import cn.stylefeng.team.project.api.SqlProviderApi;
import cn.stylefeng.team.project.api.pojo.SqlProviderResult;
import cn.stylefeng.team.project.modular.business.enums.BusinessExceptionEnum;
import cn.stylefeng.team.project.modular.businessrender.service.ModelDataService;
import cn.stylefeng.team.project.modular.businessrender.sql.ProjectSqlExecuteApi;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ModelDataServiceImpl implements ModelDataService {

    @Resource
    private MetaModelServiceApi metaModelServiceApi;

    @Resource
    private SqlProviderApi sqlProviderApi;

    @Resource
    private ProjectSqlExecuteApi projectSqlExecuteApi;

    @Override
    public Map<String, List<Map<String, Object>>> getBusinessDataByPrimaryKey(Long businessId, String primaryKeyId) {

        Map<String, List<Map<String, Object>>> result = new HashMap<>();

        if (ObjectUtil.isEmpty(businessId) || ObjectUtil.isEmpty(primaryKeyId)) {
            return result;
        }

        List<MetaModel> businessMetaModelList = metaModelServiceApi.getBusinessMetaModelList(businessId, null);
        if (ObjectUtil.isEmpty(businessMetaModelList)) {
            throw new ServiceException(BusinessExceptionEnum.CANT_FIND_TABLES);
        }

        for (MetaModel oneModel : businessMetaModelList) {

            String tableName = oneModel.getMetaModelCode();

            if (YesOrNotEnum.Y.getCode().equals(oneModel.getMainFlag())) {
                SqlProviderResult sqlProviderResult = sqlProviderApi.selectById(tableName, primaryKeyId);
                Map<String, Object> mainTable = projectSqlExecuteApi.findOne(sqlProviderResult.getMybatisSqlTemplate(), sqlProviderResult.getSqlTemplateParams());
                result.put(tableName, ListUtil.list(true, mainTable));
            }

            else if (oneModel.getSubTableType().equals(SubTableTypeEnum.ONE_TO_ONE.getCode())) {

                String fieldCode = oneModel.getSubTableFieldCode();
                SqlProviderResult sqlProviderResult = sqlProviderApi.selectByField(tableName, fieldCode, primaryKeyId);
                Map<String, Object> subTable = projectSqlExecuteApi.findOne(sqlProviderResult.getMybatisSqlTemplate(), sqlProviderResult.getSqlTemplateParams());
                result.put(tableName, ListUtil.list(true, subTable));
            }

            else if (oneModel.getSubTableType().equals(SubTableTypeEnum.ONE_TO_MANY.getCode())) {

                String fieldCode = oneModel.getSubTableFieldCode();
                SqlProviderResult sqlProviderResult = sqlProviderApi.selectByField(tableName, fieldCode, primaryKeyId);
                List<Map<String, Object>> subTableRecordList = projectSqlExecuteApi.findList(sqlProviderResult.getMybatisSqlTemplate(), sqlProviderResult.getSqlTemplateParams());
                result.put(tableName, subTableRecordList);
            }
        }

        return result;
    }

}
