Redis应用 大屏叫号序号排序

单纯的保留个笔记,不喜勿喷,后续完善

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class CallQueueVO {

    //调度主键ID
    private String id;

    //访问企业
    private String enterpriseName;

    //姓名
    private String name;

    //身份证
    private String identity;

    //手机号
    private String phone;

    //车牌号码
    private String carNumber;

    //货运单号
    private String waybill;

    //货物种类
    private String goodsType;

    //预计装/卸货时间
    private String estimatedTime;

    //时间戳
    private String timeStamp;

    //序号
    private Integer serial;
}
public class CallQueueUtil {
    public static final String QUEUE_PREFIX = "queue:dispatch";
    private static RedisService redisService;

    static {
        redisService = SpringUtils.getBean(RedisServiceImpl.class);
    }


    /**
     * 添加叫号指令
     *
     * @return
     */
    public static boolean callInsert(CallQueueVO vo) {
        try {
            List<CallQueueVO> list = null;
            String dataList = redisService.get(QUEUE_PREFIX);
            if (StringUtils.isEmpty(dataList)) {
                list = new ArrayList<>();
            } else {
                list = JSON.parseArray(dataList, CallQueueVO.class);
            }
            Integer serial = 1;
            CallQueueVO maxVo = list.stream().max(Comparator.comparing(CallQueueVO::getSerial)).get();
            if (StringUtils.isNotNull(maxVo)) {
                serial = maxVo.getSerial() + 1;
            }
            vo.setSerial(serial);
            list.add(vo);
            redisService.set(QUEUE_PREFIX, JSON.toJSONString(list));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 添加叫号指令
     *
     * @return
     */
    public static boolean batchCallInsert(List<CallQueueVO> rList) {
        try {
            List<CallQueueVO> list = null;
            String dataList = redisService.get(QUEUE_PREFIX);
            if (StringUtils.isEmpty(dataList)) {
                list = new ArrayList();
            } else {
                list = JSON.parseArray(dataList, CallQueueVO.class);
            }
            if (rList.size() == 0) {
                return false;
            }
            Integer serial = 1;
            CallQueueVO maxVo = list.stream().max(Comparator.comparing(CallQueueVO::getSerial)).get();
            if (StringUtils.isNotNull(maxVo)) {
                serial = maxVo.getSerial();
            }
            for (int j = 0; j < rList.size(); j++) {
                serial += 1;
                CallQueueVO item = rList.get(j);
                item.setSerial(serial);
                if (list.stream().noneMatch(w -> w.getId().equals(item.getId()))) {
                    list.add(item);
                }
            }
            redisService.set(QUEUE_PREFIX, JSON.toJSONString(list));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 优先叫号指令
     *
     * @param vo
     * @return
     */
    public static boolean priorityCallInsert(CallQueueVO vo) {
        try {
            String dataList = redisService.get(QUEUE_PREFIX);
            List<CallQueueVO> list = JSON.parseArray(dataList, CallQueueVO.class);
            if (list.size() < 3) {
                return true;
            }
            int subscript = 0;
            for (int i = 0; i < list.size(); i++) {
                CallQueueVO item = list.get(i);
                if (vo.getId().equals(item.getId())) {
                    subscript = i;
                    break;
                }
            }
            if (subscript <= 2) {
                return false;
            }
            list.remove(subscript);
            //前两个位置保持不变
            list.add(2, vo);
            Integer serial = 0;
            for (int i = 0; i < list.size(); i++) {
                CallQueueVO item = list.get(i);
                if (i <= 1) {
                    serial = item.getSerial();
                } else {
                    serial += 1;
                    item.setSerial(serial);
                }
            }
            redisService.set(QUEUE_PREFIX, JSON.toJSONString(list));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 当前排队数量
     *
     * @return
     */
    public static Integer getCurrentQueueNum() {
        String dataList = redisService.get(QUEUE_PREFIX);
        if (StringUtils.isEmpty(dataList)) {
            return 0;
        }
        List<CallQueueVO> list = JSON.parseArray(dataList, CallQueueVO.class);
        return list.size();
    }

    /**
     * 获取排队数据
     *
     * @return
     */
    public static CallQueueAllVO getQueueList() {
        CallQueueAllVO data = new CallQueueAllVO();
        List<CallQueueSerialVO> list = new ArrayList<>();
        String dataList = redisService.get(QUEUE_PREFIX);
        List<CallQueueVO> list_1 = JSON.parseArray(dataList, CallQueueVO.class);
        if (null == list_1 || list_1.size() == 0) {
            return null;
        }
        for (int i = 0; i < list_1.size(); i++) {
            CallQueueVO item = list_1.get(i);
            CallQueueSerialVO item_1 = new CallQueueSerialVO();
            BeanUtils.copyProperties(item, item_1);
            item_1.setGoodsType(StringUtils.isEmpty(item.getGoodsType()) ? "暂无信息" : item.getGoodsType());
            item_1.setSerial(item.getSerial() > 9 ? item.getSerial() + "" : "0" + item.getSerial());
            list.add(item_1);
        }
        data.setList(list);
        CallQueueSerialVO nowInfo = new CallQueueSerialVO();
        BeanUtils.copyProperties(list.get(0), nowInfo);
        data.setNowInfo(nowInfo);
        if (list.size() > 1) {
            CallQueueSerialVO waitInfo = new CallQueueSerialVO();
            BeanUtils.copyProperties(list.get(1), waitInfo);
            data.setWaitInfo(waitInfo);
        }
        return data;
    }

    /**
     * 撤销叫号指令
     *
     * @param plNumber 车牌
     * @return
     */
    public static boolean repealCallInsert(String plNumber) {
        try {
            if (StringUtils.isEmpty(plNumber)) {
                return false;
            }
            String dataList = redisService.get(QUEUE_PREFIX);
            if (StringUtils.isEmpty(dataList)) {
                return true;
            }
            List<CallQueueVO> list_1 = JSON.parseArray(dataList, CallQueueVO.class);
            for (int i = 0; i < list_1.size(); i++) {
                CallQueueVO item = list_1.get(i);
                if (plNumber.equals(item.getCarNumber())) {
                    list_1.remove(i);
                }
            }
            redisService.set(QUEUE_PREFIX, JSON.toJSONString(list_1));
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 刷新排队序号
     *
     * @return
     */
    public static boolean refreshSequenceNumber() {
        List<CallQueueVO> list = null;
        String dataList = redisService.get(QUEUE_PREFIX);
        if (StringUtils.isEmpty(dataList)) {
            list = new ArrayList();
        } else {
            list = JSON.parseArray(dataList, CallQueueVO.class);
        }
        if (list.size() == 0) {
            return true;
        }
        for (int i = 0; i < list.size(); i++) {
            CallQueueVO item = list.get(i);
            item.setSerial(i + 1);
        }
        redisService.set(QUEUE_PREFIX, JSON.toJSONString(list));
        return true;
    }

    /**
     * 删除队列信息
     *
     * @param plNumber
     * @return
     */
    public static boolean delete(String plNumber) {
        try {
            if (StringUtils.isEmpty(plNumber)) {
                return false;
            }
            String dataList = redisService.get(QUEUE_PREFIX);
            if (StringUtils.isEmpty(dataList)) {
                return true;
            }
            List<CallQueueVO> list_1 = JSON.parseArray(dataList, CallQueueVO.class);
            for (int i = 0; i < list_1.size(); i++) {
                CallQueueVO item = list_1.get(i);
                if (plNumber.equals(item.getCarNumber())) {
                    list_1.remove(i);
                }
            }
            redisService.set(QUEUE_PREFIX, JSON.toJSONString(list_1));
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}

需求为 前两位序号不变,后续可变,且每个号码一天只出现一次

你可能感兴趣的:(工具类,Redis,redis,java,数据库)