MsgViewHolderConfirmOrder.java 10.5 KB
Newer Older
1 2 3
package com.yidianling.im.session.viewholder;

import android.annotation.SuppressLint;
YKai committed
4
import android.os.Handler;
5 6 7 8 9 10 11
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

刘鹏 committed
12
import com.google.gson.Gson;
13 14 15
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.msg.MsgService;
16
import com.netease.nimlib.sdk.msg.attachment.MsgAttachment;
17 18
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
19 20
import com.ydl.webview.H5Params;
import com.ydl.webview.NewH5Activity;
21
import com.ydl.ydlcommon.data.http.RxUtils;
刘鹏 committed
22
import com.yidianling.common.tools.LogUtil;
23
import com.yidianling.common.tools.ToastUtil;
24
import com.yidianling.im.R;
刘鹏 committed
25
import com.yidianling.im.bean.OrderStatusBean;
26
import com.yidianling.im.config.constants.ImConstants;
27
import com.yidianling.im.helper.IMChatUtil;
28
import com.yidianling.im.http.ImRetrofitApi;
29 30 31 32
import com.yidianling.im.session.extension.CustomAttachmentConfirmOrder;
import com.yidianling.nimbase.common.ui.recyclerview.adapter.BaseMultiItemFetchLoadAdapter;
import com.yidianling.uikit.business.session.viewholder.MsgViewHolderBase;

33 34 35 36 37
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
刘鹏 committed
38
import io.reactivex.disposables.Disposable;
39
import io.reactivex.schedulers.Schedulers;
刘鹏 committed
40 41
import okhttp3.MediaType;
import okhttp3.RequestBody;
42

刘鹏 committed
43
/**
44
 * 确认服务单  消息类型 38
刘鹏 committed
45
 */
46 47
public class MsgViewHolderConfirmOrder extends MsgViewHolderBase {

48
    private TextView tv_title, tv_date, tv_type, tv_submit;
49 50
    private View lin_root;
    private ImageView img_icon;
51 52 53 54
    /***跳转url*/
    public String url;
    /***订单id*/
    public String orderId;
55 56
    /***服务单id*/
    public String serviceId;
57 58
    /***0 未确认 1已确认*/
    public int flag;
刘鹏 committed
59
    public boolean isSureed = false;//是否已确认
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

    public MsgViewHolderConfirmOrder(BaseMultiItemFetchLoadAdapter adapter) {
        super(adapter);
    }

    @Override
    protected int getContentResId() {
        return R.layout.im_ui_message_custom_confirm_order;
    }

    @Override
    protected void inflateContentView() {
        tv_title = view.findViewById(R.id.tv_title);
        tv_date = view.findViewById(R.id.tv_date);
        tv_type = view.findViewById(R.id.tv_type);
        lin_root = view.findViewById(R.id.lin_root);
        img_icon = view.findViewById(R.id.img_icon);
77
        tv_submit = view.findViewById(R.id.tv_submit);
78

79
        if (isReceivedMessage()) {
80 81
            setAvatarRightInVisibity();
            lin_root.setBackgroundResource(R.drawable.im_bg_radius_no_topleft_white_8);
82
        } else {
83 84 85 86 87 88 89 90
            setAvatarLeftInVisibity();
            lin_root.setBackgroundResource(R.drawable.im_bg_radius_no_topright_white_8);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void bindContentView() {
91
        if (isReceivedMessage()) {
92
            setAvatarRightInVisibity();
93
        } else {
94 95 96 97 98
            setAvatarLeftInVisibity();
        }

        MsgAttachment attachment = message.getAttachment();
        if (attachment instanceof CustomAttachmentConfirmOrder) {
99 100
            CustomAttachmentConfirmOrder customAttachment = (CustomAttachmentConfirmOrder) attachment;
            tv_title.setText(customAttachment.title);
刘鹏 committed
101 102
            tv_date.setText(customAttachment.bookingTime == null ? "" : customAttachment.bookingTime);
            tv_type.setText(customAttachment.servicetype == null ? "" : customAttachment.servicetype);
103 104
            url = customAttachment.url;
            orderId = customAttachment.orderId;
105
            serviceId = customAttachment.serviceId;
106 107 108 109 110 111 112 113
            flag = customAttachment.status;
            if (flag == 1) {
                // 1代表已确认,0代表待确认
                tv_submit.setText("已确认");
                tv_submit.setEnabled(false);
                tv_submit.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_radius_gray_linear_12));
                img_icon.setVisibility(View.VISIBLE);
                tv_submit.setVisibility(View.GONE);
114
                updateLocalMsg();
115 116 117 118 119 120 121 122 123 124 125 126 127
            } else {
                tv_submit.setText("确认");
                tv_submit.setEnabled(true);
                tv_submit.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_radius_green_linear_12));
                img_icon.setVisibility(View.GONE);
                tv_submit.setVisibility(View.VISIBLE);
            }

            tv_submit.setOnClickListener(v -> {
                //确定按钮给服务端发消息发送
                //确认接口
                ToastUtil.toastShort(context, "发送中...");

128
                String str = new Gson().toJson(new OrderStatusBean(serviceId != null ? serviceId : orderId, "3"));
刘鹏 committed
129 130 131 132
                RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), str);

                Disposable subscribe = ImRetrofitApi.Companion.getImRetrofitApi().standardOrderServiceOperation(body)
                        .compose(RxUtils.INSTANCE.resultJavaData())
133 134 135
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(it -> {
刘鹏 committed
136 137
                                    //确认完成,按钮置灰,更新本地消息
                                    updateLocalMsg();
138
                                    // 跳转服务单评价页
YKai committed
139 140 141 142
                                    new Handler().postDelayed(() -> {
                                        H5Params orderParams = new H5Params(ImConstants.Companion.getPLAY_SCORE_URL() + IMChatUtil.doctorId + "/" + serviceId, null);
                                        NewH5Activity.start(context, orderParams);
                                    }, 1000);
刘鹏 committed
143
                                }, throwable ->
144
                                        ToastUtil.toastShort(throwable.getMessage())
刘鹏 committed
145
                        );
146 147 148

            });

149
        }
刘鹏 committed
150
        try {
151 152
            Map<String, Object> map = message.getLocalExtension();
            if (map != null) {
刘鹏 committed
153 154
                Object obj = map.get("isSure");
                Object exp = map.get("isExpired");
155
                if (obj != null) {
刘鹏 committed
156
                    boolean b = (boolean) obj;
157
                    if (flag == 1 || b) {
刘鹏 committed
158 159
                        updateSureView("已确认");
                    }
160
                } else if (exp != null) {
刘鹏 committed
161
                    boolean expb = (boolean) exp;
162
                    if (expb) {
刘鹏 committed
163 164
                        updateSureView("已确认");
                    }
165
                } else {
刘鹏 committed
166 167 168
                    isSureed = false;
                    tv_submit.setText("确认");
                    tv_submit.setEnabled(true);
169
                    tv_submit.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_radius_green_linear_12));
刘鹏 committed
170 171
                }

172
            } else {
刘鹏 committed
173 174 175
                isSureed = false;
                tv_submit.setText("确认");
                tv_submit.setEnabled(true);
176
                tv_submit.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_radius_green_linear_12));
刘鹏 committed
177
            }
178 179
        } catch (Exception e) {
        }
180

181 182
        hideItemBg();
    }
183

184 185
    @Override
    protected void onItemClick() {
刘鹏 committed
186
        if (TextUtils.isEmpty(url) && orderId == null) {
187 188
            return;
        }
189

刘鹏 committed
190
        if (orderId == null || orderId.isEmpty()) {
191 192 193 194
            NewH5Activity.start(context, new H5Params(url, null));
        } else {
            //跳转新订单详情页
            H5Params orderParams = new H5Params(
刘鹏 committed
195
                    ImConstants.Companion.getORDER_DETAIL() + (orderId.isEmpty() ? "" : orderId), null);
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
            NewH5Activity.start(context, orderParams);
        }
    }

    private void updateLocalMsg() {
        updateSureView("已确认");
        //更新本地消息
        Map<String, Object> map = message.getLocalExtension();
        if (map == null) {
            map = new HashMap();
        }
        map.put("isSure", true);
        message.setLocalExtension(map);
        NIMClient.getService(MsgService.class).updateIMMessage(message);
        //同时更新历史同订单未确认的消息状态
211
        NIMClient.getService(MsgService.class).queryMessageList(message.getSessionId(), SessionTypeEnum.P2P, 0, 100).setCallback(new RequestCallback<List<IMMessage>>() {
212 213 214 215 216 217 218 219 220 221
            @Override
            public void onSuccess(List<IMMessage> param) {
                if (param != null && param.size() > 0) {
                    for (IMMessage msg : param) {
                        MsgAttachment attachment = msg.getAttachment();
                        if (attachment instanceof CustomAttachmentConfirmOrder) {
                            CustomAttachmentConfirmOrder customAttachConsultSubScript = (CustomAttachmentConfirmOrder) attachment;
                            if (customAttachConsultSubScript.orderId.equals(orderId)) {
                                //修改历史消息状态为已失效
                                Map map1 = msg.getLocalExtension();
222
                                if (map1 == null) {
223
                                    map1 = new HashMap();
224
                                    map1.put("isExpired", true);
225 226
                                    msg.setLocalExtension(map1);
                                    NIMClient.getService(MsgService.class).updateIMMessage(msg);
227 228
                                    //通知ui刷新
                                    getMsgAdapter().updateItemAtLocalExtension(msg);
229
                                } else {
230
                                    map1 = new HashMap();
231
                                    map1.put("isExpired", true);
232 233 234 235
                                    msg.setLocalExtension(map1);
                                    NIMClient.getService(MsgService.class).updateIMMessage(msg);
                                    //通知ui刷新
                                    getMsgAdapter().updateItemAtLocalExtension(msg);
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
                                }
                            }
                        }
                    }
                }
            }

            @Override
            public void onFailed(int code) {

            }

            @Override
            public void onException(Throwable exception) {

            }
        });
    }

    private void updateSureView(String btnName) {
        tv_submit.setText(btnName);
        tv_submit.setEnabled(false);
        tv_submit.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_radius_gray_linear_12));
259 260
    }
}