package com.mortals.xhx.busiz.web;

import com.mortals.framework.model.PageInfo;
import com.mortals.framework.web.BaseJsonBodyController;
import com.mortals.xhx.busiz.BaseReq;
import com.mortals.xhx.busiz.req.PerformReq;
import org.springframework.util.ObjectUtils;

import java.util.Map;

import static com.mortals.framework.ap.SysConstains.*;

public abstract class AbstractBaseController<T extends BaseReq> extends BaseJsonBodyController {



    protected PageInfo buildPageInfo(T query) {
        PageInfo pageInfo = new PageInfo();
        if (!ObjectUtils.isEmpty(query) && !ObjectUtils.isEmpty(query.getPage())) {
            pageInfo.setCurrPage(query.getPage());
        }

        if (!ObjectUtils.isEmpty(query) && !ObjectUtils.isEmpty(query.getSize())) {
            pageInfo.setPrePageResult(query.getSize());
        }

        return pageInfo;
    }


    protected void parsePageInfo(Map<String, Object> model, PageInfo pageInfo) {
        model.put(TOTAL, pageInfo.getTotalResult());
        model.put(PER_PAGE, pageInfo.getPrePageResult());
        model.put(CURRENT_PAGE, pageInfo.getCurrPage());
        model.put(LAST_PAGE, pageInfo.getTotalPage());
        model.put(PAGEINFO_KEY, pageInfo);
    }

}
