package cn.flightfeather.thirdapp.dataanalysis; import android.app.Dialog; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.widget.GridLayoutManager; 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.ImageView; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; import com.github.mikephil.charting.charts.PieChart; import com.ping.greendao.gen.DomainitemDao; import org.greenrobot.greendao.query.QueryBuilder; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; import butterknife.Unbinder; import cn.flightfeather.thirdapp.CommonApplication; import cn.flightfeather.thirdapp.R; import cn.flightfeather.thirdapp.adapter.AllRecyclerViewAdapter; import cn.flightfeather.thirdapp.bean.Domainitem; import cn.flightfeather.thirdapp.bean.vo.RankVo; import cn.flightfeather.thirdapp.bean.vo.StatisticsVo; import cn.flightfeather.thirdapp.bean.vo.TaskFrequencyVo; import cn.flightfeather.thirdapp.bean.vo.TaskVo; import cn.flightfeather.thirdapp.dataanalysis.rank.AnalysisRankActivity; import cn.flightfeather.thirdapp.httpservice.ProblemListService; import cn.flightfeather.thirdapp.httpservice.TaskService; import cn.flightfeather.thirdapp.util.ChartGenerator; import cn.flightfeather.thirdapp.util.CommonUtils; import cn.flightfeather.thirdapp.util.DateFormatter; import cn.flightfeather.thirdapp.util.DialogUtil; import cn.flightfeather.thirdapp.util.Domain; import cn.flightfeather.thirdapp.util.PopupGenerator; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; /** * 2018.12.25 created by riku * 分析总览界面 * 综合简略展示各统计概况: * 任务进度、街镇排名、场景排名、问题类型分布 */ public class AnalysisOverViewFragment extends Fragment { // private final String TITLE = "总览"; private CommonApplication app; private Retrofit retrofit; private Unbinder unbinder; private String curSceneTypeId = "1";//当前场景类型 private String curSceneTypeName = "工地";//当前场景类型 private RankVo rankVo = new RankVo(); private int maxShowItems = 3; private Dialog dialog; private ViewGroup container; // // public AnalysisOverViewFragment() { // Required empty public constructor } public static AnalysisOverViewFragment newInstance() { AnalysisOverViewFragment fragment = new AnalysisOverViewFragment(); return fragment; } // // @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment this.container = container; View view = inflater.inflate(R.layout.fragment_analysis_over_view, container, false); unbinder = ButterKnife.bind(this, view); return view; } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); app = (CommonApplication) getActivity().getApplication(); retrofit = app.getRetrofit(); initPopup(); initToolBar(); initBlock1(); initBlock2(); initBlock3(); initBlock4(); initPannel(); } @Override public void onAttach(Context context) { super.onAttach(context); } @Override public void onDetach() { super.onDetach(); } /** * Called when the fragment is no longer in use. This is called * after {@link #onStop()} and before {@link #onDetach()}. */ @Override public void onDestroy() { super.onDestroy(); if (unbinder != null) { unbinder.unbind(); } } // // @BindView(R.id.action_bar) View action_bar; private void initToolBar() { action_bar.findViewById(R.id.spinner_topclass_task).setVisibility(View.GONE); action_bar.findViewById(R.id.img_left).setVisibility(View.GONE); action_bar.findViewById(R.id.img_right).setVisibility(View.INVISIBLE); action_bar.findViewById(R.id.ll_menu_text).setVisibility(View.VISIBLE); TextView title = action_bar.findViewById(R.id.actionbar_title); title.setText(TITLE); // title.setGravity(Gravity.CENTER_HORIZONTAL); // ViewGroup.LayoutParams layoutParams = title.getLayoutParams(); // layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT; } // // List allTaskVoList = new ArrayList<>(); List showingTasks = new ArrayList<>(); int offset; private void initPannel() { showDialog(); //顶层任务 Call> getTopClassTaskList = retrofit.create(TaskService.class).getTopClassTaskList(0); getTopClassTaskList.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { showingTasks.clear(); allTaskVoList.clear(); if (response.body() != null) { allTaskVoList.addAll(response.body());//返回结果默认按时间降序排序 for (TaskVo t : allTaskVoList) { if (t.getDistrictcode().equals(app.getCurrentUser().getDguid())) {//主管部门的部门id就是区县id showingTasks.add(t); } } } refreshPannel(); } @Override public void onFailure(Call> call, Throwable t) { refreshPannel(); } }); } @BindView(R.id.tab_time_pannel) View tab_time_pannel; TextView text; private void refreshPannel() { showDialog(); text = tab_time_pannel.findViewById(R.id.text_time); if (!showingTasks.isEmpty()) { Date date = showingTasks.get(offset).getStarttime(); String s = DateFormatter.YearMonthFormat_CN.format(date); text.setText(s); refresh(); } } // // @BindView(R.id.rv_task_progress) RecyclerView rv_task_progress; AllRecyclerViewAdapter freAdapter; List frequencyInfos = new ArrayList<>(); private void initBlock1() { freAdapter = new AllRecyclerViewAdapter(frequencyInfos, R.layout.item_common_news, getActivity()) { @Override public void bindView(AllRecyclerViewAdapter.MyViewHolder holder, TaskFrequencyVo.FrequencyInfo obj, boolean isSelected, int position) { StringBuilder totalFrequency = new StringBuilder("总监管频次: " + obj.getTotalFrequency() + "次"); StringBuilder clearFrequency = new StringBuilder("已监管频次: " + obj.getClearFrequency() + "次"); int tmpLeft = obj.getTotalFrequency() - obj.getClearFrequency(); StringBuilder leftFrequency = new StringBuilder("剩余监管频次: " + (tmpLeft >= 0 ? tmpLeft : 0) + "次"); holder.setText(R.id.text_head, obj.getSceneTypeName()) .setText(R.id.text_1_1, totalFrequency) .setText(R.id.text_2_1, clearFrequency) .setText(R.id.text_3_1, leftFrequency) .setVisibility(R.id.text_1_2, View.GONE) .setVisibility(R.id.text_2_2, View.GONE) .setVisibility(R.id.text_3_2, View.GONE) .setImageResource(R.id.image_head, CommonUtils.getIconBySceneType(obj.getSceneTypeId())); } }; LinearLayoutManager lm = new LinearLayoutManager(getActivity()); lm.setOrientation(LinearLayoutManager.VERTICAL); rv_task_progress.setLayoutManager(lm); rv_task_progress.setAdapter(freAdapter); } private void refreshBlock1() { String id = getTopTaskId(); Call getFrequency = retrofit.create(TaskService.class).getFrequency(id); getFrequency.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (response.body() != null) { TaskFrequencyVo frequencyVo = response.body(); frequencyInfos.clear(); frequencyInfos.addAll(frequencyVo.getFrequencyInfos()); freAdapter.notifyDataSetChanged(); } else { loadingOver(false); } } @Override public void onFailure(Call call, Throwable t) { loadingOver(false); } }); } // // @BindView(R.id.rv_rank_town) RecyclerView rv_rank_town; AllRecyclerViewAdapter townRankAdapter; List townRanks = new ArrayList<>(); List allTownRanks = new ArrayList<>(); private void initBlock2() { townRankAdapter = new AllRecyclerViewAdapter(townRanks, R.layout.item_problem_count, getActivity()) { @Override public void bindView(AllRecyclerViewAdapter.MyViewHolder holder, RankVo.TownRank obj, boolean isSelected, int position) { if (position < 3) { int rank = obj.getRankNo(); holder.setText(R.id.text_name, obj.getTownName()) .setText(R.id.text_firstCount, getScore(obj.getAverageScore())) .setVisibility(R.id.text_secondCount, View.GONE) .setVisibility(R.id.image_rank, View.GONE) .setText(R.id.text_no, String.valueOf(rank)); switch (rank) { case 1: holder.setImageResource(R.id.image_no, R.drawable.icon_rank_4); break; case 2: holder.setImageResource(R.id.image_no, R.drawable.icon_rank_3); break; case 3: holder.setImageResource(R.id.image_no, R.drawable.icon_rank_2); break; default: holder.setImageResource(R.id.image_no, R.drawable.icon_rank_1); break; } } } }; LinearLayoutManager lm = new LinearLayoutManager(getActivity()); lm.setOrientation(LinearLayoutManager.VERTICAL); rv_rank_town.setLayoutManager(lm); rv_rank_town.setAdapter(townRankAdapter); } private void refreshBlock2and3() { String id = getTopTaskId(); Call getRank = retrofit.create(TaskService.class).getRank(id, curSceneTypeId, null); getRank.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (response.body() != null) { rankVo = response.body(); rankS(rankVo.getSceneRanks()); rankT(rankVo.getTownRanks()); townRanks.clear(); allTownRanks.clear(); allTownRanks.addAll(rankVo.getTownRanks()); int tmpMax = maxShowItems <= allTownRanks.size() ? maxShowItems : allTownRanks.size(); for (int i = 0; i < tmpMax; i++) { townRanks.add(allTownRanks.get(i)); } townRankAdapter.notifyDataSetChanged(); sceneRanks.clear(); allSceneRanks.clear(); allSceneRanks.addAll(rankVo.getSceneRanks()); int tmpMax1 = maxShowItems <= allSceneRanks.size() ? maxShowItems : allSceneRanks.size(); for (int i = 0; i < tmpMax1; i++) { sceneRanks.add(allSceneRanks.get(i)); } sceneRankAdapter.notifyDataSetChanged(); } else { loadingOver(false); } } @Override public void onFailure(Call call, Throwable t) { loadingOver(false); } }); } // // @BindView(R.id.rv_rank_scene) RecyclerView rv_rank_scene; AllRecyclerViewAdapter sceneRankAdapter; List sceneRanks = new ArrayList<>(); List allSceneRanks = new ArrayList<>(); private void initBlock3() { sceneRankAdapter = new AllRecyclerViewAdapter(sceneRanks, R.layout.item_problem_count, getActivity()) { @Override public void bindView(AllRecyclerViewAdapter.MyViewHolder holder, RankVo.SceneRank obj, boolean isSelected, int position) { if (position < 3) { int rank = obj.getRankNo(); holder.setText(R.id.text_name, obj.getSceneName()) .setText(R.id.text_firstCount, getScore(obj.getScore())) .setVisibility(R.id.text_secondCount, View.GONE) .setVisibility(R.id.image_rank, View.GONE) .setText(R.id.text_no, String.valueOf(rank)); switch (rank) { case 1: holder.setImageResource(R.id.image_no, R.drawable.icon_rank_4); break; case 2: holder.setImageResource(R.id.image_no, R.drawable.icon_rank_3); break; case 3: holder.setImageResource(R.id.image_no, R.drawable.icon_rank_2); break; default: holder.setImageResource(R.id.image_no, R.drawable.icon_rank_1); break; } } } }; LinearLayoutManager lm = new LinearLayoutManager(getActivity()); lm.setOrientation(LinearLayoutManager.VERTICAL); rv_rank_scene.setLayoutManager(lm); rv_rank_scene.setAdapter(sceneRankAdapter); } // // @BindView(R.id.pie_chart_problem) PieChart pieChart; @BindView(R.id.ll_no_data) LinearLayout ll_no_data; HashMap datas = new HashMap<>(); private void initBlock4() { ChartGenerator.quickSetupChart(pieChart); } private void refreshBlock4() { String id = getTopTaskId(); Call> getStatisticalResultbyId = retrofit.create(ProblemListService.class).getStatisticalResultbyId(id, curSceneTypeId); getStatisticalResultbyId.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { datas.clear(); if (response.body() != null) { List statisticsVos = response.body(); for (StatisticsVo s : statisticsVos) { datas.put(s.getName(), (float) s.getCount()); } if (datas.isEmpty()) { pieChart.setVisibility(View.GONE); ll_no_data.setVisibility(View.VISIBLE); } else { pieChart.setVisibility(View.VISIBLE); ll_no_data.setVisibility(View.GONE); } ChartGenerator.quickSetData(pieChart, datas, "问题统计"); loadingOver(true); } else { loadingOver(false); } } @Override public void onFailure(Call> call, Throwable t) { datas.clear(); ChartGenerator.quickSetData(pieChart, datas, "问题统计"); loadingOver(false); } }); } // // private String getTopTaskId() { return showingTasks.get(offset).getTguid();//返回顶层任务id } // // private void refresh() { showDialog(); refreshBlock1(); refreshBlock2and3(); refreshBlock4(); } // // @BindView(R.id.text_type) TextView text_type; @BindView(R.id.image_type) ImageView image_type; RecyclerView rv_popup; AllRecyclerViewAdapter sceneAdapter; private List sceneTypeData = new ArrayList<>();//场景类型 PopupGenerator popup; private void initPopup() { final DomainitemDao domainitemDao = app.getDaoSession().getDomainitemDao(); //场景类型 QueryBuilder queryBuilder = domainitemDao.queryBuilder() .where(DomainitemDao.Properties.Dcguid.eq(Domain.DOMAINGUID_SCENSETYPE)) .orderAsc(DomainitemDao.Properties.Value); sceneTypeData.addAll(queryBuilder.list()); popup = new PopupGenerator(getActivity(), R.layout.popup_scene_selector); rv_popup = popup.findViewById(R.id.rv_popup); sceneAdapter = new AllRecyclerViewAdapter(sceneTypeData, R.layout.item_scene, getActivity()) { @Override public void bindView(AllRecyclerViewAdapter.MyViewHolder holder, final Domainitem obj, boolean isSelected, int position) { String sceneType = String.valueOf(obj.getValue()); holder.setText(R.id.text_scene, obj.getText()) .setImageResource(R.id.image_scene, CommonUtils.getIconBySceneType(sceneType)) .setOnItemClickListener(new View.OnClickListener() { @Override public void onClick(View v) { curSceneTypeId = obj.getValue(); curSceneTypeName = obj.getText(); } }); } }; GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 3); layoutManager.setOrientation(GridLayoutManager.VERTICAL); layoutManager.setAutoMeasureEnabled(true); rv_popup.setLayoutManager(layoutManager); rv_popup.setAdapter(sceneAdapter); popup.setPopupWindowFullScreen(false); } // // //切换顶层任务 @OnClick({R.id.image_left, R.id.image_right}) void onClick(View view) { int tmpOffset = offset; switch (view.getId()) { case R.id.image_left: int tmp = offset + 1; offset = tmp >= showingTasks.size() ? offset : tmp; break; case R.id.image_right: int tmp1 = offset - 1; offset = tmp1 < 0 ? offset : tmp1; break; } if (tmpOffset != offset) { refreshPannel(); } } //场景选择菜单 @OnClick(R.id.ll_menu_text) void onClickMenu() { // if (popup.isShowing()) { // popup.dismiss(); // } else { // popup.showPopupWindow(); // // } } //详细内容按钮 @OnClick({R.id.text_all_2, R.id.text_all_3}) void onClickMoreInfo(View v) { Intent intent = new Intent(getActivity(), AnalysisRankActivity.class); Bundle bundle = new Bundle(); bundle.putSerializable(AnalysisRankActivity.ARG_PARAM1, rankVo); bundle.putString(AnalysisRankActivity.ARG_PARAM2, rankVo.getTopTaskName()); bundle.putString(AnalysisRankActivity.ARG_PARAM3, curSceneTypeName); switch (v.getId()) { case R.id.text_all_2: bundle.putInt(AnalysisRankActivity.ARG_PARAM4, 1); break; case R.id.text_all_3: bundle.putInt(AnalysisRankActivity.ARG_PARAM4, 0); break; } intent.putExtras(bundle); startActivity(intent); } //重新加载按钮 @OnClick(R.id.button_refresh) void onClickRefreshButton() { refresh(); } // // private void refreshCurScene(String id, String name) { curSceneTypeId = id; curSceneTypeName = name; } // // @BindView(R.id.sl_content) ScrollView scrollView;//内容页面 @BindView(R.id.no_data) View no_data;//加载失败页面 private void loadingOver(boolean b) { new Handler().postDelayed(new Runnable() { @Override public void run() { if (dialog.isShowing()) { dialog.dismiss(); } } }, 500); if (b) {//加载成功 scrollView.setVisibility(View.VISIBLE); no_data.setVisibility(View.GONE); } else {//加载失败 scrollView.setVisibility(View.GONE); no_data.setVisibility(View.VISIBLE); } } // private void showDialog() { if (dialog == null) { dialog = DialogUtil.createLoadingDialog(getActivity(), ""); } if (!dialog.isShowing()) { dialog.show(); } } private String getScore(int score) { return String.valueOf(score == -1 ? "未评分" : score); } //刷新排名信息 public static void rankS(List sceneRanks) { if (sceneRanks == null) return; RankVo.SceneRank last = null; RankVo.SceneRank current = null; int rankNo = 1; for (int i = 0; i < sceneRanks.size(); i++) { if (i > 0) { last = sceneRanks.get(i - 1); } current = sceneRanks.get(i); if (last != null) { //同分时并列排名 if (current.getScore() == last.getScore()) { current.setRankNo(last.getRankNo()); } else { current.setRankNo(rankNo); } } else { current.setRankNo(rankNo); } rankNo++; } } //刷新排名信息 private void rankT(List townRanks) { if (townRanks == null) return; RankVo.TownRank last = null; RankVo.TownRank current = null; int rankNo = 1; for (int i = 0; i < townRanks.size(); i++) { if (i > 0) { last = townRanks.get(i - 1); } current = townRanks.get(i); if (last != null) { //同分时并列排名 if (current.getAverageScore() == last.getAverageScore()) { current.setRankNo(last.getRankNo()); } else { current.setRankNo(rankNo); } } else { current.setRankNo(rankNo); } rankNo++; } } }