riku
2021-02-25 e102578ebfc95c27aeb13dce13fb82af53a2bead
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
package cn.flightfeather.thirdapp.activity;
 
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CalendarView;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
 
import com.ping.greendao.gen.DomainitemDao;
import com.ping.greendao.gen.TaskDao;
 
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import cn.flightfeather.thirdapp.CommonApplication;
import cn.flightfeather.thirdapp.R;
import cn.flightfeather.thirdapp.adapter.RecyclerItemClickListener;
import cn.flightfeather.thirdapp.adapter.TaskListAdapter;
import cn.flightfeather.thirdapp.adapter.TopClassTaskAdapter;
import cn.flightfeather.thirdapp.bean.entity.Domainitem;
import cn.flightfeather.thirdapp.bean.entity.Subtask;
import cn.flightfeather.thirdapp.bean.entity.Task;
import cn.flightfeather.thirdapp.bean.vo.TaskPack;
import cn.flightfeather.thirdapp.bean.entity.Taskvertion;
import cn.flightfeather.thirdapp.bean.entity.Userinfo;
import cn.flightfeather.thirdapp.bean.vo.TaskVo;
import cn.flightfeather.thirdapp.httpservice.SubTaskService;
import cn.flightfeather.thirdapp.httpservice.TaskService;
import cn.flightfeather.thirdapp.httpservice.TaskVersionService;
import cn.flightfeather.thirdapp.httpservice.UserInfoService;
import cn.flightfeather.thirdapp.util.Constant;
import cn.flightfeather.thirdapp.util.DateFormatter;
import cn.flightfeather.thirdapp.util.Domain;
import cn.flightfeather.thirdapp.util.UUIDGenerator;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
 
public class TaskActivity extends AppCompatActivity implements View.OnClickListener
        , RadioGroup.OnCheckedChangeListener, AdapterView.OnItemSelectedListener
        , CalendarView.OnDateChangeListener{
 
    private static final String TAG = "TaskActivity";
    private static final int TYPE_SUBTASK = 1;
    private static final int TYPE_TOPCLASS_TASK = 2;
    private TaskService mTaskService;
    private SubTaskService mSubTaskService;
    private boolean requestAgain = true;
    private boolean firstLoad = true;
    private Map<String, List<TaskVo>> taskAllMapList = new HashMap<>();
    private List<Integer> dayTaskTimeList;
    private Map<Integer, List<Subtask>> subTaskMapCurrent;
    private List<TaskVo> monthTaskListCurrent;
    private List<Subtask> subTaskListCurrent = new ArrayList<>();
    private ArrayList<Task> mTopClassTaskList = new ArrayList<>();
    private HashMap<String, TaskVo> mTopClassTaskVoMap = new HashMap<>();
    private java.util.Calendar calendarCurrent;
    private Spinner sp_selectMonthTask;
    private int mCurrentTaskPeriod;
    private RadioButton mRadio_subtask, mRadio_topclasstask;
    private TextView tv_no_data;
    private TextView tv_title;
    private com.haibin.calendarview.CalendarView mCalendarView;
    private TaskListAdapter subTaskListAdapter;
    private TopClassTaskAdapter mTopClassTaskAdapter;
    private RecyclerView rv_subTaskList;
    private String mCurYearMonth;
    private TextView tv_adjusttask_method;
    private TextView tv_adjusttask_attr_key;
    private TextView tv_adjusttask_attr_value;
    private TextView tv_adjusttask_reason;
    private EditText et_adjusttask_description;
    private List<Domainitem> mTaskAdjustMethodList;
    private List<Domainitem> mTaskAdjustReasonList;
    private Taskvertion mCurrentTaskVersion;
    private int mTaskAdjustMethodChoice;
    private int mTaskAdjustReasonChoice;
    private Subtask mSelectedSubtask;
    private boolean[] mCheckedArray;
    private CommonApplication mApplication;
    private ArrayList<Userinfo> mUserList = new ArrayList<>();
    private ArrayList<Userinfo> mSelectedUserList = new ArrayList<>();
    private boolean isLoadedUsers;
    private Date mPlanStartTime;
    private Date mPlanEndTime;
    private String mExecutors;
    private String mExecutors_un;
    private String mExecutors_rn;
    private boolean mShowMyTasks;
    private int mCurrentType = TYPE_SUBTASK;
    private View mCalendarLayout;
    private RecyclerView rv_topclass_task;
    private TaskDao mTaskDao;
    private int mUpdateTaskPosition;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task);
        initData();
        initUI();
    }
 
    private void initData() {
        mApplication = (CommonApplication) getApplication();
        mTaskService = mApplication.getRetrofit().create(TaskService.class);
        mSubTaskService = mApplication.getRetrofit().create(SubTaskService.class);
        DomainitemDao domainitemDao = mApplication.getDaoSession().getDomainitemDao();
        mTaskAdjustMethodList = domainitemDao.queryBuilder()
                .where(DomainitemDao.Properties.Dcguid.eq(Domain.DOMAINGUID_TASKADJUST))
                .orderAsc(DomainitemDao.Properties.Index).list();
        mTaskAdjustReasonList = domainitemDao.queryBuilder()
                .where(DomainitemDao.Properties.Dcguid.eq(Domain.DOMAINGUID_TASKADJUST_REASON))
                .orderAsc(DomainitemDao.Properties.Index).list();
        Intent intent = getIntent();
        mCurrentTaskPeriod = intent.getIntExtra(Constant.KEY_INTENT_TASK_PERIOD, R.id.day_task);
        calendarCurrent = Calendar.getInstance();
        mCurYearMonth = DateFormatter.YearMonthFormat.format(calendarCurrent.getTime());
        getThreeMonthTask(mCurYearMonth, Constant.TASK_GETINFOTYPE_MIDDLE);
        loadUserInfo();
        loadTopClassTask();
    }
 
    private void initUI() {
        tv_title = (TextView) findViewById(R.id.actionbar_title);
        CheckBox cb_subtaskExecutor = (CheckBox) findViewById(R.id.cb_subtaskExecutor);
        cb_subtaskExecutor.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.getId() == R.id.cb_subtaskExecutor) {
                    mShowMyTasks = isChecked;
                    if (mCurrentType == TYPE_SUBTASK) {
                        showSubTaskByDay(calendarCurrent.get(Calendar.DAY_OF_MONTH));
                    } else {
                        showTopClassTaskList();
                    }
                }
            }
        });
        findViewById(R.id.img_right).setVisibility(View.GONE);
        mCalendarLayout = findViewById(R.id.calendarLayout);
        findViewById(R.id.img_left).setOnClickListener(this);
        findViewById(R.id.tv_add).setOnClickListener(this);
        rv_subTaskList = (RecyclerView) findViewById(R.id.recyclerView);
        mCalendarView = (com.haibin.calendarview.CalendarView) findViewById(R.id.calendarView);
        tv_title.setText(String.valueOf(mCalendarView.getSelectedCalendar().getYear()));
//        mCalendarView.setOnMonthChangeListener(this);
//        mCalendarView.setOnCalendarSelectListener(this);
        RadioGroup mRadioGroup = (RadioGroup) findViewById(R.id.rGroup_taskperiod);
        mRadioGroup.setOnCheckedChangeListener(this);
        mRadio_subtask = (RadioButton) findViewById(R.id.rb_subtask);
        mRadio_topclasstask = (RadioButton) findViewById(R.id.rb_topclasstask);
        tv_no_data = (TextView) findViewById(R.id.tv_no_data);
        sp_selectMonthTask = (Spinner) findViewById(R.id.spinner_topclass_task);
        sp_selectMonthTask.setOnItemSelectedListener(this);
        initRecyclerView();
        initTopClassTaskRecyclerView();
        initRadioButton();
    }
 
    private void showTopClassTaskList() {
        mTopClassTaskList.clear();
        if (mTaskDao == null) {
            mTaskDao = mApplication.getDaoSession().getTaskDao();
        }
        ArrayList<Task> list = (ArrayList<Task>) mTaskDao.loadAll();
        for (Task task : list) {
            if (mShowMyTasks) {
                if (task.getTsguid() == null
                        && task.getExecutorguids().contains(mApplication.getCurrentUser().getGuid())) {
                    mTopClassTaskList.add(task);
                }
            } else {
                mTopClassTaskList.add(task);
            }
        }
        mTopClassTaskAdapter.notifyDataSetChanged();
    }
 
    private void initRadioButton() {
        switch (mCurrentTaskPeriod) {
            case R.id.day_task:
                mRadio_subtask.setChecked(true);
                mCurrentType = TYPE_SUBTASK;
//                mCalendarView.setHighLightMode(CalendarView.HIGHLIGHT_MODE_DAY);
                break;
            case R.id.week_task:
//                mCalendarView.setHighLightMode(CalendarView.HIGHLIGHT_MODE_WEEK);
                mCurrentType = TYPE_SUBTASK;
                break;
            case R.id.month_task:
                mRadio_topclasstask.setChecked(true);
                mCurrentType = TYPE_TOPCLASS_TASK;
                break;
            default:
                mRadio_subtask.setChecked(true);
                mCurrentType = TYPE_SUBTASK;
        }
    }
 
    //初始化子任务列表
    private void initRecyclerView() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rv_subTaskList.setLayoutManager(manager);
 
        subTaskListAdapter = new TaskListAdapter(this, subTaskListCurrent);
        rv_subTaskList.setAdapter(subTaskListAdapter);
        rv_subTaskList.addOnItemTouchListener(new RecyclerItemClickListener(this, rv_subTaskList, new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Subtask subTaskSelected = subTaskListCurrent.get(position);
            }
 
            @Override
            public void onItemLongClick(View view, int position) {
                Subtask subtask = subTaskListCurrent.get(position);
                if (subtask.getStatus().equals(Domain.TASK_STATUS_WAITING)) {
                    mSelectedSubtask = subtask;
                    showAdjustTaskDialog();
                } else {
                    Toast.makeText(TaskActivity.this, "此任务" + subtask.getStatus() + ", 不可变更。", Toast.LENGTH_SHORT).show();
                }
            }
        }));
    }
 
    private void initTopClassTaskRecyclerView() {
        rv_topclass_task = (RecyclerView) findViewById(R.id.rv_topclass_task);
        rv_topclass_task.setVisibility(View.GONE);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rv_topclass_task.setLayoutManager(manager);
        mTopClassTaskAdapter = new TopClassTaskAdapter(this, mTopClassTaskList);
        rv_topclass_task.setAdapter(mTopClassTaskAdapter);
        rv_topclass_task.addOnItemTouchListener(new RecyclerItemClickListener(this, rv_topclass_task, new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(TaskActivity.this, MOVMapActivity.class);
                intent.putExtra(Constant.KEY_INTENT_MODE, MOVMapActivity.MODE_UPDATE_MOV);
                intent.putExtra(Constant.KEY_INTENT_TOPCLASS_TASK, mTopClassTaskList.get(position));
                startActivity(intent);
            }
 
            @Override
            public void onItemLongClick(View view, int position) {
                mUpdateTaskPosition = position;
                Intent intent = new Intent(TaskActivity.this, NewMonthTaskActivity.class);
                intent.putExtra(Constant.KEY_INTENT_TOPCLASS_TASK, mTopClassTaskList.get(position));
                startActivityForResult(intent, Constant.REQUESTCODE_FROM_TASKACTIVITY_UPDATE);
            }
        }));
    }
 
    private void showAdjustTaskDialog() {
        mCurrentTaskVersion = new Taskvertion();
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_adjust_task, null);
        view.findViewById(R.id.tv_item_task_list_time).setVisibility(View.GONE);
        view.findViewById(R.id.tv_date).setVisibility(View.GONE);
        view.findViewById(R.id.ll_adjusttask_method).setOnClickListener(this);
        view.findViewById(R.id.ll_adjusttask_attr).setOnClickListener(this);
        view.findViewById(R.id.ll_adjusttask_reason).setOnClickListener(this);
        ((TextView) view.findViewById(R.id.tv_item_task_list_name)).setText(mSelectedSubtask.getName());
        String name = mSelectedSubtask.getExecutorrealtimes();
        name = name.replaceAll(Constant.CONNECTOR, Constant.CONNECTOR_FOR_VIEW);
        ((TextView) view.findViewById(R.id.tv_item_task_list_address)).setText(name);
        ((TextView) view.findViewById(R.id.tv_item_task_list_status)).setText(DateFormatter.dateFormat2.format(mSelectedSubtask.getPlanstarttime()));
        tv_adjusttask_method = (TextView) view.findViewById(R.id.tv_adjusttask_method);
        tv_adjusttask_attr_key = (TextView) view.findViewById(R.id.tv_adjusttask_attr_key);
        tv_adjusttask_attr_value = (TextView) view.findViewById(R.id.tv_adjusttask_attr_value);
        tv_adjusttask_reason = (TextView) view.findViewById(R.id.tv_adjusttask_reason);
        mTaskAdjustMethodChoice = 0;
        mTaskAdjustReasonChoice = 0;
        Domainitem adjustTaskType = mTaskAdjustMethodList.get(mTaskAdjustMethodChoice);
        Domainitem adjustTaskReason = mTaskAdjustReasonList.get(mTaskAdjustReasonChoice);
        tv_adjusttask_method.setText(adjustTaskType.getText());
        tv_adjusttask_attr_value.setText(DateFormatter.dateFormat2.format(mSelectedSubtask.getPlanstarttime()));
        tv_adjusttask_reason.setText(adjustTaskReason.getText());
        mCurrentTaskVersion.setTypecode(adjustTaskType.getValue());
        mCurrentTaskVersion.setTypename(adjustTaskType.getText());
        mCurrentTaskVersion.setModifyreasonid(Byte.parseByte(adjustTaskReason.getValue()));
        mCurrentTaskVersion.setModifyreason(adjustTaskReason.getText());
        et_adjusttask_description = (EditText) view.findViewById(R.id.et_adjusttask_description);
        new AlertDialog.Builder(this).setTitle("任务调整")
                .setView(view)
                .setNegativeButton(R.string.cancel, null)
                .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        callAdjustTaskTask();
                    }
                }).show();
    }
 
    private void showAdjustTaskMethodDialog() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final String[] items = new String[mTaskAdjustMethodList.size()];
        for (int i = 0; i < mTaskAdjustMethodList.size(); i++) {
            items[i] = mTaskAdjustMethodList.get(i).getText();
        }
        builder.setSingleChoiceItems(items, mTaskAdjustMethodChoice,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mTaskAdjustMethodChoice = which;
                        Domainitem domainitem = mTaskAdjustMethodList.get(mTaskAdjustMethodChoice);
                        mCurrentTaskVersion.setTypecode(domainitem.getValue());
                        mCurrentTaskVersion.setTypename(domainitem.getText());
                        tv_adjusttask_method.setText(domainitem.getText());
                        if (domainitem.getValue().equals(Domain.ADJUSTTASK_TYPE_EXECUTORS)) {
                            tv_adjusttask_attr_key.setText("任务执行人");
                            String names = mSelectedSubtask.getExecutorrealtimes().replaceAll(Constant.CONNECTOR, Constant.CONNECTOR_FOR_VIEW);
                            tv_adjusttask_attr_value.setText(names);
                        } else {
                            tv_adjusttask_attr_key.setText("执行日期");
                            tv_adjusttask_attr_value.setText(DateFormatter.dateFormat.format(mSelectedSubtask.getPlanstarttime()));
                        }
                        dialog.dismiss();
                    }
                });
        builder.create().show();
    }
 
    private void showAdjustTaskExecDateDialog(Calendar cal) {
        final int year, month, day;
        year = cal.get(Calendar.YEAR);
        month = cal.get(Calendar.MONTH);
        day = cal.get(Calendar.DAY_OF_MONTH);
        //日历控件
        DatePickerDialog dp = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int iyear, int monthOfYear, int dayOfMonth) {
                Calendar execDate = Calendar.getInstance();
                execDate.setTime(mSelectedSubtask.getPlanstarttime());
                execDate.set(Calendar.YEAR, iyear);
                execDate.set(Calendar.MONTH, monthOfYear);
                execDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                execDate.set(Calendar.HOUR_OF_DAY, 0);
                execDate.set(Calendar.MINUTE, 0);
                execDate.set(Calendar.SECOND, 0);
                mPlanStartTime = execDate.getTime();
                mCurrentTaskVersion.setBeforealterstarttime(mSelectedSubtask.getPlanstarttime());
                mCurrentTaskVersion.setBeforealterendtime(mSelectedSubtask.getPlanendtime());
                execDate.set(Calendar.HOUR_OF_DAY, 23);
                execDate.set(Calendar.MINUTE, 59);
                execDate.set(Calendar.SECOND, 59);
                mPlanEndTime = execDate.getTime();
                tv_adjusttask_attr_value.setText(DateFormatter.dateFormat.format(mPlanStartTime));
            }
        }, year, month, day);
        DatePicker datePicker = dp.getDatePicker();
        long minDate, maxDate;
        if (mCurrentTaskVersion.getTypecode().equals(Domain.ADJUSTTASK_TYPE_EXECDATE_AHEAD)) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, -7);
            minDate = calendar.getTimeInMillis();
            maxDate = mSelectedSubtask.getPlanstarttime().getTime();
        } else {
            minDate = mSelectedSubtask.getPlanstarttime().getTime();
            Calendar date = Calendar.getInstance();
            date.setTimeInMillis(minDate);
            date.add(Calendar.DAY_OF_YEAR, 7);
            maxDate = date.getTimeInMillis();
        }
        datePicker.setMinDate(minDate);
        datePicker.setMaxDate(maxDate);
        dp.show();
    }
 
    /**
     * 显示任务执行者多选对话框
     */
    private void showAdjustTaskExectorsDialog() {
        mSelectedUserList.clear();
//         设置默认选中的选项,全为false默认均未选中
        if (mCheckedArray == null) {
            mCheckedArray = new boolean[mUserList.size()];
            for (int i = 0; i < mCheckedArray.length; i++) {
                mCheckedArray[i] = false;
            }
        }
        AlertDialog.Builder multiChoiceDialog = new AlertDialog.Builder(this);
        multiChoiceDialog.setTitle("选择任务执行者");
        final String[] items = new String[mUserList.size()];
        for (int i = 0; i < items.length; i++) {
            items[i] = mUserList.get(i).getRealname();
        }
        multiChoiceDialog.setMultiChoiceItems(items, mCheckedArray,
                new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which,
                                        boolean isChecked) {
                        mCheckedArray[which] = isChecked;
                    }
                });
        multiChoiceDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int i = 0;
                        StringBuilder sb = new StringBuilder();
                        StringBuilder sb_un = new StringBuilder(); //用户名字符串
                        StringBuilder sb_rn = new StringBuilder(); //真实姓名字符串,用于界面显示
                        for (boolean isChecked : mCheckedArray) {
                            if (isChecked) {
                                Userinfo user = mUserList.get(i);
                                sb.append(user.getGuid()).append(Constant.CONNECTOR);
                                sb_un.append(user.getAcountname()).append(Constant.CONNECTOR);
                                sb_rn.append(user.getRealname()).append(Constant.CONNECTOR);
                            }
                            i++;
                        }
                        if (sb.length() > 0) {
                            mExecutors = sb.deleteCharAt(sb.length() - 1).toString();
                            mExecutors_un = sb_un.deleteCharAt(sb_un.length() - 1).toString();
                            mExecutors_rn = sb_rn.deleteCharAt(sb_rn.length() - 1).toString();
                            mCurrentTaskVersion.setExecutorguid(mExecutors);
                            mCurrentTaskVersion.setExecutorusername(mExecutors_un);
                            mCurrentTaskVersion.setExecutorrealname(mExecutors_rn);
                            tv_adjusttask_attr_value.setText(mExecutors_rn.replaceAll(Constant.CONNECTOR, Constant.CONNECTOR_FOR_VIEW));
                        }
                    }
                });
        multiChoiceDialog.show();
    }
 
    public void showAdjustTaskReasonDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final String[] items = new String[mTaskAdjustReasonList.size()];
        for (int i = 0; i < mTaskAdjustReasonList.size(); i++) {
            items[i] = mTaskAdjustReasonList.get(i).getText();
        }
        builder.setSingleChoiceItems(items, mTaskAdjustReasonChoice,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mTaskAdjustReasonChoice = which;
                    }
                });
        builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Domainitem domainitem = mTaskAdjustReasonList.get(mTaskAdjustReasonChoice);
                mCurrentTaskVersion.setModifyreasonid(Byte.parseByte(domainitem.getValue()));
                mCurrentTaskVersion.setModifyreason(domainitem.getText());
                tv_adjusttask_reason.setText(domainitem.getText());
            }
        });
        builder.create().show();
    }
 
    /**
     * 调用调整任务的接口
     */
    private void callAdjustTaskTask() {
        String desc = et_adjusttask_description.getText().toString();
        if ((desc == null || desc.equals(Constant.EMPTY_STRING))
                && mCurrentTaskVersion.getModifyreasonid().equals(Domain.ADJUSTTASK_REASON_OTHERS)) {
            Toast.makeText(this, "请填写任务调整原因。", Toast.LENGTH_SHORT).show();
            return;
        }
        mCurrentTaskVersion.setTvguid(UUIDGenerator.generateShortUUID());
        mCurrentTaskVersion.setTguid(mSelectedSubtask.getTsguid());
        mCurrentTaskVersion.setStguid(mSelectedSubtask.getStguid());
        mCurrentTaskVersion.setSubtaskname(mSelectedSubtask.getName());
        Userinfo user = mApplication.getCurrentUser();
        mCurrentTaskVersion.setModifierguid(user.getGuid());
        mCurrentTaskVersion.setModifierusername(user.getAcountname());
        mCurrentTaskVersion.setModifierrealname(user.getRealname());
        if (!desc.equals(Constant.EMPTY_STRING)) {
            mCurrentTaskVersion.setModifydescription(desc);
        }
        if (mCurrentTaskVersion.getTypecode().equals(Domain.ADJUSTTASK_TYPE_EXECUTORS)) {
            mSelectedSubtask.setExecutorguids(mExecutors);
            mSelectedSubtask.setExecutorusernames(mExecutors_un);
            mSelectedSubtask.setExecutorrealtimes(mExecutors_rn);
        } else {
            mSelectedSubtask.setPlanstarttime(mPlanStartTime);
            mSelectedSubtask.setPlanendtime(mPlanEndTime);
        }
        hadDayTask();
    }
 
    private void hadDayTask() {
        boolean hadDayTask = false;
        List<TaskVo> currentTopClassDayTaskVoList = mTopClassTaskVoMap.get(mSelectedSubtask.getTguid()).getDaytaskList();
        for (TaskVo taskVo : currentTopClassDayTaskVoList) {
            if (taskVo.getStarttime().getDay() == mSelectedSubtask.getPlanstarttime().getDay()) {
                hadDayTask = true;
                mSelectedSubtask.setTsguid(taskVo.getTguid());
                updateSubTask();
                break;
            }
        }
        if (!hadDayTask) {
            callPutDayTask();
        }
    }
 
    private void callPutDayTask() {
        Task task = mTopClassTaskVoMap.get(mSelectedSubtask.getTguid()).taskVo2Task();
        final Task dayTask = task.clone();
        dayTask.setTguid(UUIDGenerator.generate16ShortUUID());
        dayTask.setTsguid(task.getTguid());
        dayTask.setLevelnum(null);
        dayTask.setExtension2(null);
        dayTask.setDeadlinetype(null);
        dayTask.setStarttime(mSelectedSubtask.getPlanstarttime());
        dayTask.setEndtime(mSelectedSubtask.getPlanendtime());
        Calendar cal = Calendar.getInstance();
        Date now = cal.getTime();
        dayTask.setSettime(now);
        dayTask.setPlannerguid(mApplication.getCurrentUser().getGuid());
        dayTask.setPlannerusername(mApplication.getCurrentUser().getAcountname());
        dayTask.setPlannerrealname(mApplication.getCurrentUser().getRealname());
        dayTask.setT1stverifierguid(mApplication.getCurrentUser().getGuid());
        dayTask.setT1stverifierusername(mApplication.getCurrentUser().getAcountname());
        dayTask.setT1stverifierrealname(mApplication.getCurrentUser().getRealname());
        dayTask.setT1stverifytime(now);
        dayTask.setT1stisverify(true);
        String area = dayTask.getCityname()+dayTask.getDistrictname();
        cal.setTime(dayTask.getStarttime());
        String name = (cal.get(Calendar.MONTH)+1)+"月"+cal.get(Calendar.DAY_OF_MONTH)+"日"+area+dayTask.getTypename();
        dayTask.setName(name);
        Call<ResponseBody> call = mTaskService.putTask(dayTask);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
                if (response.body() != null) {
                    Toast.makeText(TaskActivity.this, "日任务上传成功。", Toast.LENGTH_SHORT).show();
                    mSelectedSubtask.setTsguid(dayTask.getTguid());
                    updateSubTask();
                } else if (response.errorBody() != null) {
                    Toast.makeText(TaskActivity.this, "日任务上传失败。", Toast.LENGTH_SHORT).show();
                }
            }
 
            @Override
            public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
                Toast.makeText(TaskActivity.this, "日任务上传接口调用失败。", Toast.LENGTH_SHORT).show();
            }
        });
    }
 
    private void updateSubTask() {
        Call<ResponseBody> call = mSubTaskService.postSubTask(mSelectedSubtask);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
                if (response.body() != null) {
                    Toast.makeText(TaskActivity.this, "子任务修改成功。", Toast.LENGTH_SHORT).show();
                    callPutTaskVersion();
                } else if (response.errorBody() != null) {
                    Toast.makeText(TaskActivity.this, "子任务修改失败。", Toast.LENGTH_SHORT).show();
                }
            }
 
            @Override
            public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
 
            }
        });
    }
 
    /**
     * 调用新增任务版本接口
     */
    private void callPutTaskVersion() {
        Call<ResponseBody> call = mApplication.getRetrofit().create(TaskVersionService.class).putTaskVersion(mCurrentTaskVersion);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
                if (response.body() != null) {
                    Toast.makeText(TaskActivity.this, "任务版本上传成功。", Toast.LENGTH_SHORT).show();
                } else if (response.errorBody() != null) {
                    Toast.makeText(TaskActivity.this, "任务版本上传失败。", Toast.LENGTH_SHORT).show();
                }
            }
 
            @Override
            public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
 
            }
        });
    }
 
    /**
     * 获取当前月及前后月的任务信息
     *
     * @param yearMonth 年月:例:2018-01
     * @param type      Middle、Left、Right
     */
    private void getThreeMonthTask(final String yearMonth, final String type) {
        Call<List<TaskPack>> loadThreeMonthTasks = mTaskService.loadThreeMonthTasks(yearMonth, "0", type);
        loadThreeMonthTasks.enqueue(new Callback<List<TaskPack>>() {
            @Override
            public void onResponse(@NonNull Call<List<TaskPack>> call, @NonNull Response<List<TaskPack>> response) {
 
                if (response.body() != null) {
                    requestAgain = false;
                    List<TaskPack> taskPackList = response.body();
                    if (taskPackList.size() > 0) {
                        for (TaskPack taskPack : taskPackList) {
                            taskAllMapList.put(taskPack.getDate(), taskPack.getUpperTaskList());
                        }
                        if (firstLoad) {
                            firstLoad = false;
                        }
                        showMonthAllTask(calendarCurrent);
                    }
                } else if (response.errorBody() != null) {
                    if (!requestAgain) {
                        requestAgain = true;
                        getThreeMonthTask(yearMonth, type);
                    } else {
                        Toast.makeText(TaskActivity.this, "获取任务信息出错", Toast.LENGTH_SHORT).show();
//                        showMonthAllTask(calendarCurrent);
                    }
                    System.out.println("ThreeMonthTask:" + response.errorBody());
                }
            }
 
            @Override
            public void onFailure(@NonNull Call<List<TaskPack>> call, @NonNull Throwable t) {
                if (!requestAgain) {
                    requestAgain = true;
                    getThreeMonthTask(yearMonth, type);
                } else {
                    Toast.makeText(TaskActivity.this, "网络链接失败", Toast.LENGTH_SHORT).show();
//                    showMonthAllTask(calendarCurrent);
                }
                System.out.println("ThreeMonthTask:" + t.toString());
            }
        });
    }
 
    //显示当前月的月任务列表(日历上的标注交给spinner操作)
    private void showMonthAllTask(java.util.Calendar calendarCurrent) {
        monthTaskListCurrent = new ArrayList<>();
        List<String> monthTaskStringListCurrent = new ArrayList<>();
        dayTaskTimeList = new ArrayList<>();
        subTaskMapCurrent = new HashMap<>();
        String currentMonth = DateFormatter.YearMonthFormat.format(calendarCurrent.getTime());
        List<TaskVo> taskVoList = taskAllMapList.get(currentMonth);
        if (taskVoList != null && taskVoList.size() > 0) {
            monthTaskStringListCurrent.add("全部任务");
            for (TaskVo monthTask : taskVoList) {
                monthTaskListCurrent.add(monthTask);
                monthTaskStringListCurrent.add(monthTask.getName());
            }
        } else {
            monthTaskStringListCurrent.add("无任务");
        }
        ArrayAdapter selectMonthTaskAdapter = new ArrayAdapter<>(this, R.layout.item_spinner_drop_down, monthTaskStringListCurrent);
        sp_selectMonthTask.setAdapter(selectMonthTaskAdapter);
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_left:
                this.finish();
                break;
            case R.id.tv_add:
                Intent intent = new Intent(this, MOVMapActivity.class);
                intent.putExtra(Constant.KEY_INTENT_MODE, MOVMapActivity.MODE_ADD_SUBTASK);
                startActivityForResult(intent, Constant.REQUESTCODE_FROM_TASKACTIVITY_ADD);
                break;
            case R.id.ll_adjusttask_method:
                showAdjustTaskMethodDialog();
                break;
            case R.id.ll_adjusttask_attr:
                if (mCurrentTaskVersion.getTypecode().equals(Domain.ADJUSTTASK_TYPE_EXECUTORS)) {
                    showAdjustTaskExectorsDialog();
                } else {
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(mSelectedSubtask.getPlanstarttime());
                    showAdjustTaskExecDateDialog(cal);
                }
                break;
            case R.id.ll_adjusttask_reason:
                showAdjustTaskReasonDialog();
                break;
        }
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == Constant.REQUESTCODE_FROM_TASKACTIVITY_ADD) {
                getThreeMonthTask(mCurYearMonth, Constant.TASK_GETINFOTYPE_MIDDLE);
            } else if (requestCode == Constant.REQUESTCODE_FROM_TASKACTIVITY_UPDATE) {
                Task task = (Task) data.getSerializableExtra(Constant.KEY_INTENT_TOPCLASS_TASK);
                mTopClassTaskList.remove(mUpdateTaskPosition);
                mTopClassTaskList.add(mUpdateTaskPosition, task);
                mTopClassTaskAdapter.notifyDataSetChanged();
            }
        }
    }
 
    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.rb_subtask:
                mCalendarLayout.setVisibility(View.VISIBLE);
                sp_selectMonthTask.setVisibility(View.VISIBLE);
                rv_topclass_task.setVisibility(View.GONE);
                mCurrentType = TYPE_SUBTASK;
                break;
            case R.id.rb_topclasstask:
                rv_topclass_task.setVisibility(View.VISIBLE);
                mCalendarLayout.setVisibility(View.GONE);
                sp_selectMonthTask.setVisibility(View.GONE);
                mCurrentType = TYPE_TOPCLASS_TASK;
                break;
        }
    }
 
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//将选中的文字变为白色
        CheckedTextView tv = (CheckedTextView) view.findViewById(view.getId());
        tv.setTextColor(getResources().getColor(R.color.white));
        //选择后清空数据重新读取
        dayTaskTimeList = new ArrayList<>();
        subTaskMapCurrent = new HashMap<>();
        if (position == 0) {
            showOneMonthAllTask2(calendarCurrent);
            showSubTaskByDay(calendarCurrent.get(java.util.Calendar.DAY_OF_MONTH));
        } else {
            TaskVo monthTaskSelected = monthTaskListCurrent.get(position - 1);
            showOneMonthTask(monthTaskSelected);
            showSubTaskByDay(calendarCurrent.get(java.util.Calendar.DAY_OF_MONTH));
        }
    }
 
    //显示一个月任务的数据和日历标注(Spinner选择一个月任务后的操作)
    private void showOneMonthTask(TaskVo monthTask) {
        getOneMonthTaskAllData(monthTask);
        addScheme(dayTaskTimeList);
    }
 
    //显示当月日数据和历标注
    private void showOneMonthAllTask2(java.util.Calendar calendarCurrent) {
        String currentMonth = DateFormatter.YearMonthFormat.format(calendarCurrent.getTime());
        List<TaskVo> taskVoList = taskAllMapList.get(currentMonth);
        if (taskVoList != null && taskVoList.size() > 0) {
            for (TaskVo monthTask : taskVoList) {
                getOneMonthTaskAllData(monthTask);
            }
        }
        addScheme(dayTaskTimeList);
    }
 
    //跟据日期显示那天的子任务
    private void showSubTaskByDay(int day) {
        List<Subtask> subTaskList = subTaskMapCurrent.get(day);
        subTaskListCurrent.clear();
        if (subTaskList != null && subTaskList.size() > 0) {
            if (mShowMyTasks) {
                String executor = mApplication.getCurrentUser().getGuid();
                for (Subtask subtask : subTaskList) {
                    if (subtask.getExecutorguids().contains(executor)) {
                        subTaskListCurrent.add(subtask);
                    }
                }
            } else {
                subTaskListCurrent.addAll(subTaskList);
            }
            tv_no_data.setVisibility(View.GONE);
        } else {
            tv_no_data.setVisibility(View.VISIBLE);
        }
        subTaskListAdapter.notifyDataSetChanged();
    }
 
    //获取一个月任务的全部数据
    private void getOneMonthTaskAllData(TaskVo monthTask) {
        List<TaskVo> dayTaskList = monthTask.getDaytaskList();
        if (dayTaskList != null && dayTaskList.size() > 0) {
            for (TaskVo dayTask : dayTaskList) {
                java.util.Calendar calendar = java.util.Calendar.getInstance();
                calendar.setTime(dayTask.getStarttime());
                int day = calendar.get(java.util.Calendar.DAY_OF_MONTH);
                dayTaskTimeList.add(day);
                List<Subtask> subTaskList = dayTask.getSubtaskList();
                if (subTaskList != null && subTaskList.size() > 0) {
                    if (subTaskMapCurrent.containsKey(day)) {
                        subTaskMapCurrent.get(day).addAll(subTaskList);
                    } else {
                        subTaskMapCurrent.put(day, subTaskList);
                    }
                }
            }
        }
    }
 
    //根据日期列表添加全部标记点
    private void addScheme(List<Integer> dayTaskTimeList) {
        Map<String, com.haibin.calendarview.Calendar> mSchemes = new HashMap<>();
        if (dayTaskTimeList != null && dayTaskTimeList.size() > 0) {
            for (Integer day : dayTaskTimeList) {
                com.haibin.calendarview.Calendar c = getSchemeCalendar(calendarCurrent.get(java.util.Calendar.YEAR), (calendarCurrent.get(java.util.Calendar.MONTH) + 1), day, 0xFFdf1356, false);
                mSchemes.put(c.toString(), c);
            }
        }
        mCalendarView.setSchemeDate(mSchemes);
    }
 
    //添加标记点
    private com.haibin.calendarview.Calendar getSchemeCalendar(int year, int month, int day, int color, boolean schemeCompleted) {
        com.haibin.calendarview.Calendar calendar = new com.haibin.calendarview.Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setSchemeColor(color);//如果单独标记颜色、则会使用这个颜色
        calendar.setScheme("记");
        return calendar;
    }
 
    @Override
    public void onNothingSelected(AdapterView<?> parent) {
 
    }
 
//    @Override
//    public void onDateChange(com.haibin.oldcalendarview.Calendar calendar) {
//        if (calendar.getMonth() != calendarCurrent.get(Calendar.MONTH) + 1) {
//            tv_title.setText(calendar.getCurrentYearMonth());
//            calendarCurrent.set(Calendar.YEAR, calendar.getYear());
//            calendarCurrent.set(Calendar.MONTH, calendar.getMonth() - 1);
//            calendarCurrent.set(Calendar.DAY_OF_MONTH, calendar.getDay());
//            calendarCurrent.set(Calendar.HOUR_OF_DAY, 0);
//            showMonthAllTask(calendarCurrent);
//        } else {
//            calendarCurrent.set(Calendar.DAY_OF_MONTH, calendar.getDay());
//        }
//        showSubTaskByDay(calendarCurrent.get(java.util.Calendar.DAY_OF_MONTH));
//    }
//
//    @Override
//    public void onYearChange(int year) {
//
//    }
//
//    @Override
//    public void onDateSelected(com.haibin.oldcalendarview.Calendar calendar) {
//
//    }
 
    /**
     * 获取用户信息
     * <p>
     * 注:目前没有接口,通过获取全部数据来过滤我要的数据.不合理
     */
    private void loadUserInfo() {
        UserInfoService userInfoService = mApplication.getRetrofit().create(UserInfoService.class);
        Call<List<Userinfo>> userinfoCall = userInfoService.getAllUser();
        userinfoCall.enqueue(new Callback<List<Userinfo>>() {
            @Override
            public void onResponse(@NonNull Call<List<Userinfo>> call, @NonNull Response<List<Userinfo>> response) {
                if (response != null && response.body() != null) {
                    List<Userinfo> list = response.body();
//                        daoSession.getUserinfoDao().deleteAll();
                    mApplication.getDaoSession().getUserinfoDao().insertOrReplaceInTx(list);
                    mUserList.clear();
                    for (Userinfo userinfo : list) {
                        if (userinfo.getUsertypeid() == 1) {
                            mUserList.add(userinfo);
                        }
                    }
                    isLoadedUsers = true;
                }
            }
 
            @Override
            public void onFailure(@NonNull Call<List<Userinfo>> call, @NonNull Throwable t) {
                Log.e(TAG, "onFailure()" + t.getMessage());
            }
        });
    }
 
    private void loadTopClassTask() {
        mTopClassTaskList.clear();
        mTopClassTaskVoMap.clear();
        TaskService taskService = mApplication.getRetrofit().create(TaskService.class);
        Call<ArrayList<TaskVo>> call = taskService.getTopClassTaskList(Constant.TOPCLASS_TASK_TYPE_UNFINISHED);
        call.enqueue(new Callback<ArrayList<TaskVo>>() {
            @Override
            public void onResponse(@NonNull Call<ArrayList<TaskVo>> call, @NonNull Response<ArrayList<TaskVo>> response) {
                if (response.body() != null) {
                    ArrayList<TaskVo> list = response.body();
                    for (TaskVo taskVo : list) {
                        mTopClassTaskVoMap.put(taskVo.getTguid(), taskVo);
                        mTopClassTaskList.add(taskVo.taskVo2Task());
                    }
                    if (mTaskDao == null) {
                        mTaskDao = mApplication.getDaoSession().getTaskDao();
                    }
                    mTaskDao.insertOrReplaceInTx(mTopClassTaskList);
                    mTopClassTaskAdapter.notifyDataSetChanged();
                } else if (response.errorBody() != null) {
                    Toast.makeText(TaskActivity.this, "获取顶层任务失败,请重试。", Toast.LENGTH_SHORT).show();
                }
            }
 
            @Override
            public void onFailure(@NonNull Call<ArrayList<TaskVo>> call, @NonNull Throwable t) {
 
            }
        });
    }
 
    @Override
    public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
 
    }
}