package com.lideeyunji.core.framework.config.aspect.enhance.strategy.sql;

import com.lideeyunji.core.framework.config.aspect.enhance.model.EnhanceContext;
import com.lideeyunji.core.framework.entity.EnhanceSqlEntity;
import com.lideeyunji.core.framework.entity.FormEntity;
import com.lideeyunji.core.framework.service.IFormService;
import com.lideeyunji.core.framework.utils.Func;
import com.lideeyunji.tool.framework.yunji.model.ResultDataModel;
import com.lideeyunji.tool.framework.yunji.tool.spring.SpringUtils;

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

/**
 * 详情策略
 */
public class DetailEnhanceSqlStrategy extends BaseEnhanceSqlStrategy  implements EnhanceSqlStrategy {

    @Override
    public void execute(EnhanceContext context, EnhanceSqlEntity enhanceSqlEntity) {
        IFormService dbFormService = SpringUtils.getBean(IFormService.class);

        Long dbFormId = context.getParam().getDbFormId();
        Long id = (Long) context.getParam().getDataId();
        Map<String, Object> params = context.getParam().getParams();
        params.put("id", id);//把id放入参数集

        FormEntity formEntity = dbFormService.getFormEntityById(dbFormId);
        String autoWhereSql = Func.getSqlAutoWhereSql(formEntity, params);

        String executeSql = enhanceSqlEntity.getExecuteSql();
        List<Map<String, Object>> dataMapList = super.executeSelectListSql(executeSql, params, autoWhereSql);
        context.setResultModel(ResultDataModel.fomatList(dataMapList));
    }

}
