mac
2024-05-22 4598b51c90c695c9ccbe5350a84e4a46b9d8e587
app/src/main/java/com/hdl/photovoltaic/other/HdlMessageLogic.java
@@ -11,6 +11,7 @@
import com.hdl.photovoltaic.internet.api.HttpApi;
import com.hdl.photovoltaic.listener.CloudCallBeak;
import com.hdl.photovoltaic.ui.bean.MessageBean;
import com.hdl.photovoltaic.ui.bean.UnCountBean;
import java.util.ArrayList;
import java.util.Comparator;
@@ -23,7 +24,9 @@
public class HdlMessageLogic {
    private static volatile HdlMessageLogic sHdlMessageLogic;
    private int pageSize = 50;//页数
    private final int pageSize = 50;//页数
    private List<MessageBean> mListMessage = new ArrayList<>();
    /**
     * 获取当前对象
@@ -40,6 +43,15 @@
        }
        return sHdlMessageLogic;
    }
    public void clearListMessage() {
        if (mListMessage == null) {
            return;
        }
        if (mListMessage.size() > 0) {
            mListMessage.clear();
        }
    }
    /**
@@ -61,6 +73,21 @@
        }
        return sortList(newList);
    }
    /**
     * 消息全部已读(本地缓存)
     *
     * @param isRead 是否读取(true已读取)
     */
    public void setAllRead(boolean isRead) {
        for (int i = 0; i < mListMessage.size(); i++) {
            MessageBean messageBean = mListMessage.get(i);
            if (messageBean == null) {
                continue;
            }
            messageBean.setRead(isRead);
        }
    }
@@ -104,6 +131,18 @@
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            unReadList.sort(new Comparator<MessageBean>() {
                @Override
                public int compare(MessageBean o1, MessageBean o2) {
                    if (o1 == null && o2 == null) {
                        return -1;
                    }
                    //倒序(顺序 Long.compare(o2.getCreateTime(), o1.getCreateTime()))
                    return Long.compare(o2.getCreateTime(), o1.getCreateTime());
                }
            });
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            readList.sort(new Comparator<MessageBean>() {
                @Override
                public int compare(MessageBean o1, MessageBean o2) {
@@ -111,11 +150,10 @@
                        return -1;
                    }
                    //倒序(顺序 Long.compare(o2.getCreateTime(), o1.getCreateTime()))
                    return Long.compare(o1.getCreateTime(), o2.getCreateTime());
                    return Long.compare(o2.getCreateTime(), o1.getCreateTime());
                }
            });
        }
        if (unReadList.size() > 0) {
            newList.addAll(unReadList);
        }
@@ -126,7 +164,7 @@
    }
    public List<MessageBean> getAllMessageList() {
        return mListMessage;
        return sortList(mListMessage);
    }
@@ -155,7 +193,8 @@
                    break;
                }
            }
        } catch (Exception ignored) {
        } catch (Exception e) {
            String ss = e.getMessage();
        }
    }
@@ -185,14 +224,14 @@
            }
        } catch (Exception e) {
            String mes = e.getMessage();
            HdlLogLogic.print("--->" + mes);
            HdlLogLogic.print("---" + mes);
        }
    }
    /**
     * 添加消息到列表里面
     * 添加消息到列表里面(单个插入)
     *
     * @param messageBean -
     * @param messageBean 单个
     * @param position    表示加到列表的位置
     */
    public void setListMessage(MessageBean messageBean, int position) {
@@ -216,14 +255,14 @@
            }
        } catch (Exception e) {
            String mes = e.getMessage();
            HdlLogLogic.print("--->" + mes);
            HdlLogLogic.print("---" + mes);
        }
    }
    /**
     * 添加消息到列表里面
     *
     * @param list -
     * @param list 列表
     */
    public void setListMessage(List<MessageBean> list) {
        try {
@@ -242,22 +281,54 @@
    }
    private List<MessageBean> mListMessage = new ArrayList<>();
    /**
     * 获取未处理缓存消息数量
     */
    public String getMessageUntreatedMemoryCount() {
        if (this.mListMessage == null || this.mListMessage.size() == 0) {
            return "0";
        }
        List<MessageBean> newList = new ArrayList<>();
        for (int i = 0; i < mListMessage.size(); i++) {
            MessageBean messageBean = mListMessage.get(i);
            if (messageBean == null) {
                continue;
            }
            if (!messageBean.getStatus().equals(MessageStateType.untreated)) {
                continue;
            }
            if (!messageBean.isRead()) {
                newList.add(messageBean);
            }
        }
        return newList.size() + "";
    }
    /**
     * 获取未处理消息数量
     *
     * @param cloudCallBeak -
     */
    public void getMessageUntreatedCount(CloudCallBeak<String> cloudCallBeak) {
    public void getMessageUntreatedCount(String homeId, CloudCallBeak<UnCountBean> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Message_UntreatedCount;
        JsonObject json = new JsonObject();
        if (!TextUtils.isEmpty(homeId)) {
            json.addProperty("homeId", homeId);
        }
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(String str) {
                if (TextUtils.isEmpty(str)) {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(new UnCountBean());
                    }
                    return;
                }
                UnCountBean unCountBean = new Gson().fromJson(str, UnCountBean.class);
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(str);
                    cloudCallBeak.onSuccess(unCountBean);
                }
            }
@@ -314,7 +385,7 @@
            json.addProperty("status", messageStateType);
        }
        json.addProperty("pageNo", 1);//页码
        json.addProperty("pageSize", 50);//页数
        json.addProperty("pageSize", pageSize);//页数
        List<MessageBean> list = new ArrayList<>();
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
@@ -384,74 +455,36 @@
    }
    /**
     * 获取消息列表
     * 获取指定页的获取消息列表(用于B端)
     *
     * @param pageNo           页码
     * @param messageStateType all:全部,UNTREATED:未处理,PROCESSED:已处理;
     * @param cloudCallBeak    -
     */
    private void getMessageList(int pageNo, String messageStateType, CloudCallBeak<List<MessageBean>> cloudCallBeak) {
    public void getPageNoMessageList(int pageNo, String messageStateType, CloudCallBeak<MessageListClass> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Message_List;
        JsonObject json = new JsonObject();
        if (!MessageStateType.all.equals(messageStateType)) {
            json.addProperty("status", messageStateType);
        }
        json.addProperty("pageNo", 1);//页码
        json.addProperty("pageSize", 50);//页数
        json.addProperty("pageNo", pageNo);//页码
        json.addProperty("pageSize", pageSize);//页数
        List<MessageBean> list = new ArrayList<>();
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(String jsonStr) {
                if (TextUtils.isEmpty(jsonStr)) {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(new ArrayList<>());
                        cloudCallBeak.onSuccess(new MessageListClass());
                    }
                    return;
                }
                Gson gson = new Gson();
                MessageListClass oldMessageListClass = gson.fromJson(jsonStr, MessageListClass.class);
                list.addAll(oldMessageListClass.getList());
                setListMessage(oldMessageListClass.getList());
                //总共有多少页
                long totalPage = oldMessageListClass.totalPage;
                if (totalPage < 2) {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(list);
                    }
                    return;
                }
                //记录请求条数
                AtomicInteger atomicInteger = new AtomicInteger(0);
                //从第二页获取数据
                for (int i = 2; i <= totalPage; i++) {
                    json.addProperty("pageNo", i);//更新页码
                    HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
                        @Override
                        public void onSuccess(String jsonStr) {
                            atomicInteger.set(atomicInteger.get() + 1);
                            MessageListClass newMessageListClass = gson.fromJson(jsonStr, MessageListClass.class);
                            list.addAll(newMessageListClass.getList());
                            setListMessage(newMessageListClass.getList());
                            if (atomicInteger.get() == totalPage - 1) {
                                //最后一条退出
                                if (cloudCallBeak != null) {
                                    cloudCallBeak.onSuccess(list);
                                }
                            }
                        }
                        @Override
                        public void onFailure(HDLException e) {
                            atomicInteger.set(atomicInteger.get() + 1);
                            if (atomicInteger.get() == totalPage - 1) {
                                //最后一条退出
                                if (cloudCallBeak != null) {
                                    cloudCallBeak.onSuccess(list);
                                }
                            }
                        }
                    });
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(oldMessageListClass);
                }
            }
@@ -466,6 +499,81 @@
    }
    /**
     * 获取指定页的获取消息列表
     *
     * @param homeId                  电站id(填""默认账号下所有消息)
     * @param pageNo                  页码
     * @param messageStateType        all:全部,UNTREATED:未处理,PROCESSED:已处理;
     * @param deviceType_filter_key   类型(设备,等级,时间)
     * @param deviceType_filter_value deviceType(INV :逆变器;BMS :BMS控制盒;BATTERY :电池单元);
     *                                type(FAULT :故障; WARN :告警; EVENT :事件(提示));
     *                                timeType(TODAY :今天;LAST_3_DAYS: 近3天;LAST_7_DAYS :近7天;LAST_30_DAYS :近30天);
     * @param search_title            报警名称
     * @param cloudCallBeak           -
     */
    public void getPageNoMessageList(
            String homeId,
            int pageNo,
            String messageStateType,
            String deviceType_filter_key,
            String deviceType_filter_value,
            String type_filter_key,
            String type_filter_value,
            String timeType_filter_key,
            String timeType_filter_value,
            String search_title, CloudCallBeak<MessageListClass> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Message_List;
        JsonObject json = new JsonObject();
        if (!TextUtils.isEmpty(homeId)) {
            json.addProperty("homeId", homeId);
        }
        if (!MessageStateType.all.equals(messageStateType)) {
            json.addProperty("status", messageStateType);
        }
        if (!TextUtils.isEmpty(deviceType_filter_key) && !TextUtils.isEmpty(deviceType_filter_value)) {
            json.addProperty(deviceType_filter_key, deviceType_filter_value);
        }
        if (!TextUtils.isEmpty(type_filter_key) && !TextUtils.isEmpty(type_filter_value)) {
            json.addProperty(type_filter_key, type_filter_value);
        }
        if (!TextUtils.isEmpty(timeType_filter_key) && !TextUtils.isEmpty(timeType_filter_value)) {
            json.addProperty(timeType_filter_key, timeType_filter_value);
        }
        if (!TextUtils.isEmpty(search_title)) {
            json.addProperty("title", search_title);
        }
        json.addProperty("pageNo", pageNo);//页码
        json.addProperty("pageSize", pageSize);//页数
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(String jsonStr) {
                if (TextUtils.isEmpty(jsonStr)) {
                    if (cloudCallBeak != null) {
                        cloudCallBeak.onSuccess(new MessageListClass());
                    }
                    return;
                }
                Gson gson = new Gson();
                MessageListClass oldMessageListClass = gson.fromJson(jsonStr, MessageListClass.class);
                setListMessage(oldMessageListClass.getList());
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(oldMessageListClass);
                }
            }
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
    /**
     * 获取消息详情
@@ -532,8 +640,39 @@
    }
    /**
     * 消息全部已读
     *
     * @param homeId        消息id(不传已读所有电站的,传值已读指定电站的)
     * @param cloudCallBeak -
     */
    public void messageAllRead(String homeId, CloudCallBeak<Boolean> cloudCallBeak) {
        String requestUrl = HttpApi.POST_Message_AllRead;
        JsonObject json = new JsonObject();
        if (!TextUtils.isEmpty(homeId)) {
            json.addProperty("homeId", homeId);
        }
        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), new CloudCallBeak<String>() {
            @Override
            public void onSuccess(String str) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onSuccess(true);
                }
            }
    static class MessageListClass {
            @Override
            public void onFailure(HDLException e) {
                if (cloudCallBeak != null) {
                    cloudCallBeak.onFailure(e);
                }
            }
        });
    }
    public static class MessageListClass {
        //总条数
        private long totalCount;