All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.luoshu.open.id.ui.controller.IdController Maven / Gradle / Ivy

package com.luoshu.open.id.ui.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.luoshu.open.id.*;
import com.luoshu.open.id.ui.IdUiViewPermission;
import com.luoshu.open.id.ui.exception.IdUIPermException;
import com.luoshu.open.id.ui.model.*;
import lombok.extern.slf4j.Slf4j;
import org.luoshu.util.Assert;
import org.luoshu.util.exception.NoticeException;
import org.luoshu.util.io.IOUtils;
import org.luoshu.util.json.JSON;
import org.luoshu.util.web.RequestResourceServlet;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author inme
 */
@Slf4j
public class IdController {

    private JdbcIdDao jdbcIdDao;
    private IdUiViewPermission uiViewPermission;
    private ObjectMapper objectMapper = new ObjectMapper();

    public IdController(JdbcIdDao jdbcDao, IdUiViewPermission uiViewPermission) {
        this.jdbcIdDao = jdbcDao;
        this.uiViewPermission = uiViewPermission;
    }


    /**
     * 查询列表
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @RequestResourceServlet.Path("listIds")
    private Page listIds(HttpServletRequest request , HttpServletResponse response) throws IOException {
        if(!IdUiViewPermission.PermTypeEnum.hasSelectPerm(uiViewPermission.getPerm())){
            throw new NoticeException("没有查询权限");
        }
        ConditionDTO conditionDTO = null;
        try(InputStream in = request.getInputStream()){
            String text = IOUtils.toString(in);
            conditionDTO = objectMapper.readValue(text, ConditionDTO.class);
        }
        conditionDTO.setPageStart((conditionDTO.getPageNum() - 1) * conditionDTO.getPageSize());
        List idPOS = jdbcIdDao.listByCondition(conditionDTO);
        long count = jdbcIdDao.countByCondition(conditionDTO);
        return new Page(idPOS , (int) count, conditionDTO.getPageSize());
    }

    /**
     * 跳过区间
     * @param request
     * @param response
     * @return
     */
    @RequestResourceServlet.Path("skipNum")
    private Object skipNum(HttpServletRequest request , HttpServletResponse response){
        if(!IdUiViewPermission.PermTypeEnum.hasEditPerm(uiViewPermission.getPerm())){
            throw new NoticeException("没有操作权限");
        }

        SkipRangeDTO skipRangeDTO = null;
        try(InputStream in = request.getInputStream()){
            skipRangeDTO = JSON.parseObject(in, SkipRangeDTO.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        Assert.assertNotBlank(skipRangeDTO.getCategory() , "category is empty");

        int maxSkipNum = 10000;
        if(skipRangeDTO.getSkipNum() > maxSkipNum){
            throw new NoticeException("跳过的区间不能大于 " + maxSkipNum);
        }
        if(skipRangeDTO.getSkipNum() <= 0){
            throw new NoticeException("跳过的区间数量必须大于 0");
        }
        String category = skipRangeDTO.getCategory();

        IdPO idPO = jdbcIdDao.findByCategory(category);
        if(idPO == null){
            throw new IllegalArgumentException("找不到 category : " + category);
        }
        IdGenerate idGenerate = IdUtil.createCategory(category);
        if(idGenerate instanceof JdbcIdGenerate){
            RangeInfo rangeInfo = ((JdbcIdGenerate) idGenerate).readRangeByFixStep(skipRangeDTO.getSkipNum());
            return rangeInfo;
        }else{
            throw new RuntimeException("未实现");
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy