package cn.flightfeather.thirdapp.module.task; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import com.ping.greendao.gen.ScenseDao; import org.greenrobot.greendao.query.QueryBuilder; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import cn.flightfeather.thirdapp.CommonApplication; import cn.flightfeather.thirdapp.R; import cn.flightfeather.thirdapp.activity.NewMonthTaskActivity; import cn.flightfeather.thirdapp.activity.ScenseMapActivity; import cn.flightfeather.thirdapp.activity.TaskActivity; import cn.flightfeather.thirdapp.adapter.RecyclerItemClickListener; import cn.flightfeather.thirdapp.adapter.TaskListAdapter; import cn.flightfeather.thirdapp.bean.entity.Scense; import cn.flightfeather.thirdapp.bean.entity.Subtask; import cn.flightfeather.thirdapp.bean.entity.Userinfo; import cn.flightfeather.thirdapp.bean.vo.TaskPack; import cn.flightfeather.thirdapp.bean.vo.TaskVo; import cn.flightfeather.thirdapp.httpservice.SubTaskService; import cn.flightfeather.thirdapp.httpservice.TaskService; import cn.flightfeather.thirdapp.util.CommonUtils; 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 cn.flightfeather.thirdapp.view.CircleProgressView; import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; /** * */ public class TaskFragment extends Fragment implements RecyclerItemClickListener.OnItemClickListener , View.OnClickListener { private int taskKindChoice = 1; private CommonApplication application; private Retrofit mRetrofit; private boolean requestAgain; private Map> taskAllMapList = new HashMap<>(); private boolean firstLoad = true; private String mCurYearMonth; private String mToday; private ArrayList mTopClassTaskList = new ArrayList<>(); private ArrayList mSubtaskList = new ArrayList<>(); private ArrayList mDayTaskList = new ArrayList<>(); private CircleProgressView daytask_circleprogress; private CircleProgressView weektask_circleprogress; private CircleProgressView monthtask_circleprogress; private TaskListAdapter mAdapter; private View tv_no_task; private View view_waiting; private AlertDialog mCreateUnfixedScenseTaskDialog; private int mTopClassTaskChoice; public TaskFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_task, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); initData(); initUI(view); } /** * 获取数据 */ private void initData() { application = (CommonApplication) getActivity().getApplication(); mRetrofit = application.getRetrofit(); Date now = Calendar.getInstance().getTime(); mCurYearMonth = DateFormatter.YearMonthFormat.format(now); mToday = DateFormatter.dateFormat2.format(now); getThreeMonthTask(mCurYearMonth, Constant.TASK_GETINFOTYPE_MIDDLE); } private void initUI(View view) { ViewGroup rl_addTask = view.findViewById(R.id.rl_add_task); view.findViewById(R.id.rl_scense_map).setOnClickListener(this); view.findViewById(R.id.rl_add_scense).setOnClickListener(this); view.findViewById(R.id.day_task).setOnClickListener(this); view.findViewById(R.id.week_task).setOnClickListener(this); view.findViewById(R.id.month_task).setOnClickListener(this); view.findViewById(R.id.ll_container_taskadjust).setOnClickListener(this); view_waiting = view.findViewById(R.id.view_waiting); tv_no_task = view.findViewById(R.id.tv_no_task); RecyclerView mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_container); LinearLayoutManager manager = new LinearLayoutManager(getActivity()); mRecyclerView.setLayoutManager(manager); mAdapter = new TaskListAdapter(getActivity(), mSubtaskList); mAdapter.setShowDate(false); mRecyclerView.setAdapter(mAdapter); mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), mRecyclerView, this)); daytask_circleprogress = (CircleProgressView) view.findViewById(R.id.daytask_circleprogress); weektask_circleprogress = (CircleProgressView) view.findViewById(R.id.weektask_circleprogress); monthtask_circleprogress = (CircleProgressView) view.findViewById(R.id.monthtask_circleprogress); daytask_circleprogress.setRGB(83, 230, 149); weektask_circleprogress.setRGB(107, 191, 245); monthtask_circleprogress.setRGB(236, 120, 101); daytask_circleprogress.setProgress(0, 0); weektask_circleprogress.setProgress(0, 0); monthtask_circleprogress.setProgress(0, 0); rl_addTask.setOnClickListener(this); } /** * 获取当前月及前后月的任务信息 * * @param yearMonth 年月:例:2018-01 * @param type Middle、Left、Right */ private void getThreeMonthTask(final String yearMonth, final String type) { Call> loadThreeMonthTasks = mRetrofit.create(TaskService.class).loadThreeMonthTasks(yearMonth, application.getCurrentUser().getGuid(), type); loadThreeMonthTasks.enqueue(new Callback>() { @Override public void onResponse(@NonNull Call> call, @NonNull Response> response) { view_waiting.setVisibility(View.GONE); if (response.body() != null) { requestAgain = false; List taskPackList = response.body(); if (taskPackList != null && taskPackList.size() > 0) { for (TaskPack taskPack : taskPackList) { taskAllMapList.put(taskPack.getDate(), taskPack.getUpperTaskList()); } if (firstLoad) { firstLoad = false; // showMonthAllTask(calendarCurrent); } invalidate(); } } else if (response.errorBody() != null) { if (!requestAgain) { requestAgain = true; getThreeMonthTask(yearMonth, type); } else { Toast.makeText(application, "获取任务信息出错", Toast.LENGTH_SHORT).show(); // showMonthAllTask(calendarCurrent); } System.out.println("ThreeMonthTask:" + response.errorBody()); } } @Override public void onFailure(@NonNull Call> call, @NonNull Throwable t) { if (!requestAgain) { requestAgain = true; getThreeMonthTask(yearMonth, type); } else { Toast.makeText(application, "网络链接失败", Toast.LENGTH_SHORT).show(); // showMonthAllTask(calendarCurrent); } System.out.println("ThreeMonthTask:" + t.toString()); } }); } /** * 遍历获取的数据,更新界面 */ private void invalidate() { mTopClassTaskList.clear(); mDayTaskList.clear(); mSubtaskList.clear(); int daytaskTotalNumThisWeek = 0; int daytaskFinishedNumThisWeek = 0; int daytaskTotalNumThisMonth = 0; int daytaskFinishedNumThisMonth = 0; int subtaskTotalNumToday = 0; int subtaskFinishedNumToday = 0; for (TaskVo taskVo : taskAllMapList.get(mCurYearMonth)) { mTopClassTaskList.add(taskVo); for (TaskVo dayTaskVo : taskVo.getDaytaskList()) { daytaskTotalNumThisMonth++; mDayTaskList.add(dayTaskVo); long taskTime = dayTaskVo.getStarttime().getTime(); long monday = CommonUtils.getTimesWeekmorning().getTime(); long sunday = CommonUtils.getTimesWeeknight().getTime(); if (taskTime >= monday && taskTime <= sunday) { daytaskTotalNumThisWeek++; if (dayTaskVo.getRuningstatus().equals(Domain.TASK_STATUS_FINISHED)) { daytaskFinishedNumThisWeek++; } } if (dayTaskVo.getRuningstatus().equals(Domain.TASK_STATUS_FINISHED)) { daytaskFinishedNumThisMonth++; } List subTaskList = dayTaskVo.getSubtaskList(); for (Subtask subtask : subTaskList) { String subtaskStartTime = DateFormatter.dateFormat2.format(subtask.getPlanstarttime()); if (subtaskStartTime.equals(mToday)) { subtaskTotalNumToday++; if (subtask.getStatus().equals(Domain.TASK_STATUS_FINISHED)) { subtaskFinishedNumToday++; } mSubtaskList.add(subtask); } } } } daytask_circleprogress.setProgress(subtaskFinishedNumToday, subtaskTotalNumToday); weektask_circleprogress.setProgress(daytaskFinishedNumThisWeek, daytaskTotalNumThisWeek); monthtask_circleprogress.setProgress(daytaskFinishedNumThisMonth, daytaskTotalNumThisMonth); if (mSubtaskList.size() > 0) { mAdapter.notifyDataSetChanged(); tv_no_task.setVisibility(View.GONE); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.rl_add_task: showTaskKindDialog(); break; case R.id.rl_scense_map: Intent intent1 = new Intent(getActivity(), ScenseMapActivity.class); intent1.putExtra(Constant.KEY_INTENT_TODAY_TASKLIST, mSubtaskList); getActivity().startActivityForResult(intent1, Constant.REQUESTCODE_FROM_TASKFRAGMENT); break; case R.id.rl_add_scense: startActivity(new Intent(getActivity(), SceneDetailActivity.class)); break; case R.id.day_task: case R.id.week_task: case R.id.month_task: Intent intent = new Intent(getActivity(), TaskActivity.class); intent.putExtra(Constant.KEY_INTENT_TASK_PERIOD, v.getId()); startActivity(intent); break; case R.id.ll_container_taskadjust: Intent intent2 = new Intent(getActivity(), MonthTaskManagementActivity.class); startActivity(intent2); break; } } /** * 显示新增任务类型:顶层任务、子任务 * @deprecated Use {@link #showTaskKindDialog_newType()} */ @Deprecated private void showTaskKindDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); //无固定场景巡查任务是当任务类型为巡查时才使用,故此次放该选项不合适,暂时先放此处 final String[] items = {"顶层任务", "子任务", "无固定场景巡查任务"}; builder.setTitle("选择任务类型"); builder.setSingleChoiceItems(items, taskKindChoice, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { taskKindChoice = which; } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); Intent intent; if (taskKindChoice < 2) { if (taskKindChoice == 0) { intent = new Intent(getActivity(), NewMonthTaskActivity.class); } else { // intent = new Intent(getActivity(), MOVMapActivity.class); // intent.putExtra(Constant.KEY_INTENT_MODE, MOVMapActivity.MODE_ADD_SUBTASK); intent = new Intent(getActivity(), NewSubTaskActivity.class); } startActivity(intent); } else { showCreateUnfixedScenseTaskDialog(); } } }); builder.create().show(); } /** * 更换新样式的弹出框,功能未修改 * 显示新增任务类型:顶层任务、子任务 */ private void showTaskKindDialog_newType() { } private void showCreateUnfixedScenseTaskDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); final ArrayList list = new ArrayList<>(); for (TaskVo taskVo : mTopClassTaskList) { if (taskVo.getExtension2() != null && taskVo.getExtension2().equals(Domain.UNFIXED_SCNESE_ALLOWED)) { list.add(taskVo); } } final String[] items = new String[list.size()]; for (int i = 0; i < items.length; i++) { TaskVo taskVo = list.get(i); items[i] = taskVo.getName(); } builder.setSingleChoiceItems(items, mTopClassTaskChoice, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mTopClassTaskChoice = which; } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (list.isEmpty()) { return; } showCheckTaskInfoDialog(list.get(mTopClassTaskChoice)); } }); mCreateUnfixedScenseTaskDialog = builder.create(); mCreateUnfixedScenseTaskDialog.show(); } private void showCheckTaskInfoDialog(final TaskVo taskVo) { AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); builder.setTitle("提示"); builder.setMessage("当前选中的顶层任务为" + taskVo.getName()); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { createSubtask(taskVo); } }); builder.create().show(); } private void createSubtask(TaskVo topClassTask) { SubTaskService subtaskService = mRetrofit.create(SubTaskService.class); Subtask subtask = new Subtask(); TaskVo currentDayTask = null; Calendar now = Calendar.getInstance(); for (TaskVo taskVo : mDayTaskList) { if (taskVo.getTsguid().equals(topClassTask.getTguid())) { Calendar cal = Calendar.getInstance(); cal.setTime(taskVo.getStarttime()); if (now.get(Calendar.YEAR) == cal.get(Calendar.YEAR) && now.get(Calendar.MONTH) == cal.get(Calendar.MONTH) && now.get(Calendar.DAY_OF_MONTH) == cal.get(Calendar.DAY_OF_MONTH)) { currentDayTask = taskVo; break; } } } if (currentDayTask == null) { currentDayTask = createDayTask(topClassTask); callPutDayTask(topClassTask, subtaskService, subtask, currentDayTask, now); } else { callPutSubtask(topClassTask, subtaskService, subtask, currentDayTask, now); } } private void callPutSubtask(TaskVo topClassTask, SubTaskService subtaskService, final Subtask subtask, TaskVo currentDayTask, Calendar now) { subtask.setStguid(UUIDGenerator.generate16ShortUUID()); subtask.setTguid(topClassTask.getTguid()); subtask.setTsguid(currentDayTask.getTguid()); subtask.setTypeno(topClassTask.getTypeno()); subtask.setType(topClassTask.getTypename()); subtask.setProvincecode(topClassTask.getProvincecode()); subtask.setProvincename(topClassTask.getProvincename()); subtask.setCitycode(topClassTask.getCitycode()); subtask.setCityname(topClassTask.getCityname()); subtask.setDistrictcode(topClassTask.getDistrictcode()); subtask.setDistrictname(topClassTask.getDistrictname()); subtask.setTowncode(topClassTask.getTowncode()); subtask.setTownname(topClassTask.getTownname()); QueryBuilder qb = application.getDaoSession().getScenseDao().queryBuilder(); Scense scense; if (subtask.getTowncode() == null) { scense = qb.where(ScenseDao.Properties.Provincecode.eq(subtask.getProvincecode()) , ScenseDao.Properties.Citycode.eq(subtask.getCitycode()) , ScenseDao.Properties.Districtcode.eq(subtask.getDistrictcode()) , ScenseDao.Properties.Typeid.eq(Domain.SCENSETYPE_UNFIXED_SCENSE)).unique(); } else { scense = qb.where(ScenseDao.Properties.Provincecode.eq(subtask.getProvincecode()) , ScenseDao.Properties.Citycode.eq(subtask.getCitycode()) , ScenseDao.Properties.Districtcode.eq(subtask.getDistrictcode()) , ScenseDao.Properties.Towncode.eq(subtask.getTowncode()) , ScenseDao.Properties.Typeid.eq(Domain.SCENSETYPE_UNFIXED_SCENSE)).unique(); } subtask.setScenseid(scense.getGuid()); subtask.setScensename(scense.getName()); subtask.setDeploytime(now.getTime()); subtask.setPlanstarttime(now.getTime()); now.set(Calendar.HOUR_OF_DAY, 23); now.set(Calendar.MINUTE, 59); now.set(Calendar.SECOND, 59); subtask.setPlanendtime(now.getTime()); Userinfo currentUser = application.getCurrentUser(); subtask.setDeployerguid(currentUser.getGuid()); subtask.setDeployerrealname(currentUser.getRealname()); subtask.setDeployerusername(currentUser.getAcountname()); subtask.setExecutorguids(currentUser.getGuid()); subtask.setExecutorrealtimes(currentUser.getRealname()); subtask.setExecutorusernames(currentUser.getAcountname()); subtask.setStatus(Domain.TASK_STATUS_WAITING); String template = "%s%s的%s无固定场景巡查"; String area; if (subtask.getTownname() == null) { area = subtask.getCityname() + subtask.getDistrictname(); } else { area = subtask.getCityname() + subtask.getDistrictname() + subtask.getTownname(); } subtask.setName(String.format(Locale.CHINA, template , DateFormatter.dateTimeFormat_month_2_minute.format(subtask.getPlanstarttime()) , subtask.getExecutorrealtimes(), area)); Call call = subtaskService.putSubTask(subtask); call.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.body() != null) { Toast.makeText(getActivity(), "子任务创建成功。", Toast.LENGTH_SHORT).show(); showTurnToInspectionDialog(subtask); } else if (response.errorBody() != null) { Toast.makeText(getActivity(), "子任务创建失败。", Toast.LENGTH_SHORT).show(); } } @Override public void onFailure(@NonNull Call call, @NonNull Throwable t) { Toast.makeText(getActivity(), "接口调用失败。", Toast.LENGTH_SHORT).show(); } }); } private void showTurnToInspectionDialog(final Subtask subtask) { new AlertDialog.Builder(getActivity()).setTitle("提示") .setMessage("子任务创建成功,前往执行?") .setNegativeButton(R.string.cancel, null) .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); if (subtask != null) { Intent intent = new Intent("cn.flightfeather.thirdapp.broadcasereceiver.MainReceiver"); try { intent.putExtra("subtask", subtask); // Log.e("数据!!", intent.getStringExtra("subtask")); intent.setAction(Constant.GOTOSUBTASK); // getActivity().switchSelectedFragment(ll_inspection); getActivity().sendBroadcast(intent); } catch (NullPointerException e) { e.printStackTrace(); } } } }).show(); } private TaskVo createDayTask(TaskVo topClassTask) { TaskVo dayTask = topClassTask.clone(); dayTask.setTguid(UUIDGenerator.generate16ShortUUID()); dayTask.setTsguid(topClassTask.getTguid()); dayTask.setDeadlinetype(null); dayTask.setExtension1(null); dayTask.setLevelnum(null); Calendar cal = Calendar.getInstance(); dayTask.setSettime(cal.getTime()); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); dayTask.setStarttime(cal.getTime()); cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); dayTask.setEndtime(cal.getTime()); dayTask.setPlannerguid(application.getCurrentUser().getGuid()); dayTask.setPlannerusername(application.getCurrentUser().getAcountname()); dayTask.setPlannerrealname(application.getCurrentUser().getRealname()); dayTask.setT1stverifierguid(application.getCurrentUser().getGuid()); dayTask.setT1stverifierusername(application.getCurrentUser().getAcountname()); dayTask.setT1stverifierrealname(application.getCurrentUser().getRealname()); String template = "%d月%d日%s无固定场景巡查任务"; cal.setTime(dayTask.getStarttime()); String area = dayTask.getCityname()+dayTask.getDistrictname(); dayTask.setName(String.format(Locale.CHINA, template, cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH), area)); return dayTask; } private void callPutDayTask(final TaskVo topClassTask, final SubTaskService subtaskService, final Subtask subtask, final TaskVo currentDayTask, final Calendar now) { Call call = mRetrofit.create(TaskService.class).putTask(currentDayTask.taskVo2Task()); call.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.body() != null) { Toast.makeText(getActivity(), "日任务创建成功。", Toast.LENGTH_SHORT).show(); callPutSubtask(topClassTask, subtaskService, subtask, currentDayTask, now); } else if (response.errorBody() != null) { Toast.makeText(getActivity(), "日任务创建失败。", Toast.LENGTH_SHORT).show(); } } @Override public void onFailure(@NonNull Call call, @NonNull Throwable t) { Toast.makeText(getActivity(), "日任务创建失败。", Toast.LENGTH_SHORT).show(); } }); } @Override public void onItemClick(View view, int position) { } @Override public void onItemLongClick(View view, int position) { } }