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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.core.framework.utils.FuncWeb;
import com.lideeyunji.tool.framework.yunji.tool.spring.SpringUtils;
import com.lideeyunji.tool.framework.yunji.utils.lideeYunJiUtils;

import java.util.Map;

/**
 * 分页策略
 */
public class PageEnhanceSqlStrategy extends BaseEnhanceSqlStrategy  implements EnhanceSqlStrategy {

    @Override
    public void execute(EnhanceContext context, EnhanceSqlEntity enhanceSqlEntity) {
        Long dbFormId = context.getParam().getDbFormId();
        Map<String, Object> params = context.getParam().getParams();
        Integer pageNo = lideeYunJiUtils.getMap2Integer(params, "pageNo");
        Integer pageSize = lideeYunJiUtils.getMap2Integer(params, "pageSize");
        Page page = FuncWeb.getPage(pageNo, pageSize);
        String executeSql = enhanceSqlEntity.getExecuteSql();

        IFormService dbFormService = SpringUtils.getBean(IFormService.class);
        FormEntity formEntity = dbFormService.getFormEntityById(dbFormId);
        String autoWhereSql = Func.getSqlAutoWhereSql(formEntity, params);
        IPage<Map<String, Object>> pages = this.executeSelectPageSql(executeSql, params, autoWhereSql, page);
        FuncWeb.setPageResult(context, pages);
    }

}
