| | |
| | | package com.xcong.farmer.cms.core.handler; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.xcong.farmer.cms.configurations.properties.CmsProperties; |
| | | import com.xcong.farmer.cms.core.node.AttrNode; |
| | | import com.xcong.farmer.cms.core.node.Template; |
| | | import com.xcong.farmer.cms.core.tag.data.PageChildData; |
| | | import com.xcong.farmer.cms.core.tag.data.PageData; |
| | | import com.xcong.farmer.cms.core.tag.model.Article; |
| | | import com.xcong.farmer.cms.core.tag.model.Column; |
| | | import com.xcong.farmer.cms.core.tag.model.Pagination; |
| | | import com.xcong.farmer.cms.modules.system.entity.ArticleEntity; |
| | | import com.xcong.farmer.cms.modules.system.entity.ColumnEntity; |
| | | import com.xcong.farmer.cms.modules.system.mapper.ArticleMapper; |
| | | import com.xcong.farmer.cms.modules.system.mapper.ColumnMapper; |
| | | import com.xcong.farmer.cms.utils.SpringContextHolder; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * @author wzy |
| | | * @date 2022-07-05 |
| | | **/ |
| | | @Slf4j |
| | | public class PageDataParserHandler implements DataParserHandler { |
| | | |
| | | private ArticleMapper articleMapper = SpringContextHolder.getBean(ArticleMapper.class); |
| | | private ColumnMapper columnMapper = SpringContextHolder.getBean(ColumnMapper.class); |
| | | @Override |
| | | public void dataParser(AttrNode attrNode) { |
| | | log.info("分页解析"); |
| | | synchronized (this) { |
| | | Template.HAS_PAGING = true; |
| | | |
| | | String baseUrl = (String) attrNode.getSystemDataValue("baseUrl"); |
| | | |
| | | Long companyId = (Long) attrNode.getSystemDataValue("companyId"); |
| | | Pagination param = (Pagination) attrNode.getParam(); |
| | | |
| | | Object pageNum = attrNode.getSystemDataValue("page"); |
| | | if (pageNum != null) { |
| | | param.setPage(pageNum.toString()); |
| | | } |
| | | |
| | | Page<ArticleEntity> page = new Page<>(Integer.parseInt(param.getPage()), Integer.parseInt(param.getLimit())); |
| | | ArticleEntity article = new ArticleEntity(); |
| | | article.setColumnCode(param.getCode()); |
| | | article.setCompanyId(companyId); |
| | | IPage<ArticleEntity> pageList = articleMapper.selectArticleInPage(page, article); |
| | | |
| | | PageData pageData = new PageData(); |
| | | pageData.setTotalPage((int) pageList.getPages()); |
| | | pageData.setIndex(Integer.parseInt(param.getPage())); |
| | | pageData.setTotalCnt((int) pageList.getSize()); |
| | | |
| | | if (CollUtil.isNotEmpty(pageList.getRecords())) { |
| | | ColumnEntity parentColumn = columnMapper.selectParentColumnByCodeAndCompanyId(param.getCode(), companyId); |
| | | String path; |
| | | if (parentColumn == null) { |
| | | path = baseUrl + "/" + param.getCode(); |
| | | } else { |
| | | path = baseUrl + "/" + parentColumn.getColumnCode() + "/" + param.getCode(); |
| | | } |
| | | |
| | | String filename = "index_{}.html"; |
| | | |
| | | List<PageChildData> list = new ArrayList<>(); |
| | | int pageNo = Integer.parseInt(param.getPage()); |
| | | |
| | | // 计算出页面显示分页的最大和最小值 |
| | | int[] startAndEnd = pageStartAndEnd(pageNo, (int) pageList.getPages(), Integer.parseInt(param.getSize())); |
| | | for (int i = startAndEnd[0]; i <= startAndEnd[1]; i++) { |
| | | PageChildData child = new PageChildData(); |
| | | child.setIndex(i); |
| | | if (i == 1) { |
| | | child.setPath(path + "/index.html"); |
| | | } else { |
| | | child.setPath(path + "/" + StrUtil.format(filename, i)); |
| | | } |
| | | list.add(child); |
| | | } |
| | | |
| | | pageData.setChildren(list); |
| | | } else { |
| | | pageData.setNext("javascript:void(0)"); |
| | | pageData.setPrev("javascript:void(0)"); |
| | | } |
| | | |
| | | if (Integer.parseInt(param.getPage()) >= pageList.getPages()) { |
| | | Template.HAS_PAGING = false; |
| | | } |
| | | |
| | | attrNode.setData(pageData); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * (x + y)/2 = index |
| | | * y - x= size 解二元一次方程 x-起点 y-终点 |
| | | * |
| | | * @param index 当前页码 |
| | | * @param totalPage 总页数 |
| | | * @param size 分页大小 |
| | | * @return |
| | | */ |
| | | public int[] pageStartAndEnd(int index, int totalPage, int size) { |
| | | int[] page = new int[2]; |
| | | int end = ((index * 2) + size) / 2;; |
| | | |
| | | int start = end - size + 1; |
| | | if (start < 1) { |
| | | start = 1; |
| | | } |
| | | |
| | | if (end > totalPage) { |
| | | end = totalPage; |
| | | } |
| | | |
| | | page[0] = start; |
| | | page[1] = end; |
| | | return page; |
| | | } |
| | | |
| | | } |