package cn.flightfeather.thirdapp.module.inspectioninfo;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
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.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.ping.greendao.gen.DomainitemDao;
import com.ping.greendao.gen.ScenseDao;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import butterknife.BindView;
import butterknife.ButterKnife;
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.Scense;
import cn.flightfeather.thirdapp.bean.vo.ProblemCategoryVo;
import cn.flightfeather.thirdapp.bean.vo.ProblemlistVo;
import cn.flightfeather.thirdapp.httpservice.ProblemListService;
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 retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* 2019.1.22
* @author riku
* 监管情况界面,展示用户(工地、码头等)本月的问题分布情况
*/
public class InspectionInfoFragment extends Fragment implements Serializable{
//
private final static String TITLE = "问题";
private static final long serialVersionUID = 6072583863506447855L;
private CommonApplication application;
//场景类型
private String sceneType = Constant.SCENE_TYPE_SITE;
private Unbinder unbinder;
//
//
public InspectionInfoFragment() {
// Required empty public constructor
}
public static InspectionInfoFragment newInstance() {
InspectionInfoFragment fragment = new InspectionInfoFragment();
// Bundle args = new Bundle();
// fragment.setArguments(args);
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
View view = inflater.inflate(R.layout.fragment_inspection_info, container, false);
this.unbinder = ButterKnife.bind(this, view);
return view;
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.application = (CommonApplication) getActivity().getApplication();
initToolBar();
initData();
initView(this.sceneType);
Date now = new Date();
String date = DateFormatter.dateTimeFormat3.format(now);
refresh(application.getCurrentUser().getDguid(), date);//对于客户来说,D_GUID就是场景id
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
}
@Override
public void onDestroy() {
super.onDestroy();
if (this.unbinder != null) {
this.unbinder.unbind();
}
}
//
//
@BindView(R.id.action_bar)
View action_bar;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void initToolBar() {
// action_bar.setElevation(0);
// action_bar.setBackgroundColor(Color.alpha(0));
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.GONE);
TextView title = action_bar.findViewById(R.id.actionbar_title);
title.setText(TITLE);
title.setGravity(Gravity.CENTER_HORIZONTAL);
}
//
//
@BindView(R.id.text_total) TextView text_total;
@BindView(R.id.text_changed) TextView text_changed;
@BindView(R.id.text_unchanged) TextView text_unchanged;
private int total_problems;//总问题数
private int changed_problems;//已整改问题数
private void refreshStatisticBar() {
text_total.setText(String.valueOf(total_problems));
text_changed.setText(String.valueOf(changed_problems));
text_unchanged.setText(String.valueOf(total_problems-changed_problems));
}
//
//
private ArrayList problemCategories = new ArrayList<>();//列表中展示的数据,按问题类型分类
private DomainitemDao domainitemDao;
private ScenseDao scenseDao;
private void initData() {
//region 查询当前“企业”用户的场景类型
String sceneGuid = this.application.getCurrentUser().getDguid();//登录用户为“企业”时,该变量表示场景guid
this.scenseDao = this.application.getDaoSession().getScenseDao();
List scenses = scenseDao.queryBuilder()
.where(
ScenseDao.Properties.Guid.eq(
sceneGuid
)
)
.limit(1).list();
if (scenses != null && !scenses.isEmpty()) {
this.sceneType = String.valueOf(scenses.get(0).getTypeid());
}
//endregion
//region 查询当前场景类型下的问题类型值域
this.domainitemDao = this.application.getDaoSession().getDomainitemDao();
List domainitems = domainitemDao.queryBuilder()
.where(
DomainitemDao.Properties.Dcguid.eq(
CommonUtils.getProblemTypeGUID(this.sceneType)
)
)
.orderAsc(DomainitemDao.Properties.Index).list();
for (Domainitem d :
domainitems) {
ProblemCategoryVo p = new ProblemCategoryVo();
p.setProblemTypeId(d.getValue());
p.setProblemTypeName(d.getText());
this.problemCategories.add(p);
}
//endregion
}
//
//
//问题按分类进行个数统计
@BindView(R.id.rv_problem_type) RecyclerView rv_problem_type;
AllRecyclerViewAdapter adapter;
private void initView(final String sceneType) {
adapter = new AllRecyclerViewAdapter(this.problemCategories, R.layout.item_problem_category, getActivity()) {
@Override
public void bindView(AllRecyclerViewAdapter.MyViewHolder holder, final ProblemCategoryVo obj, boolean isSelected, int position) {
holder.setImageResource(R.id.image_problem_category, CommonUtils.getIconByProblemType(sceneType, obj.getProblemTypeId()))
.setText(R.id.text_problem_category, obj.getProblemTypeName())
.setText(R.id.text_rectified_problem, String.valueOf(obj.getIsRectifiedCount()))
.setText(R.id.text_total_problem, String.valueOf(obj.getTotalCount()));
if (obj.getTotalCount() > 0) {
holder.setItemSelected(true)
.setOnItemClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ProblemChangeActivity.class);
intent.putExtra(ProblemChangeActivity.ARG_PROBLEM, obj.getProblemlistVos());
intent.putExtra(ProblemChangeActivity.ARG_TOTAL_PROBLEM, problemCategories);
startActivity(intent);
}
});
}
}
};
GridLayoutManager gl = new GridLayoutManager(getActivity(), 3);
gl.setOrientation(LinearLayoutManager.VERTICAL);
rv_problem_type.setLayoutManager(gl);
rv_problem_type.setAdapter(adapter);
}
//
//
private void refresh(String sceneId, String date) {
Call> getProblemByScene = this.application.getRetrofit().create(ProblemListService.class).getProblemByScene(sceneId, date);
getProblemByScene.enqueue(new Callback>() {
@Override
public void onResponse(Call> call, Response> response) {
if (response.body() != null) {
updateData(response.body());
adapter.notifyDataSetChanged();
refreshStatisticBar();
}
}
@Override
public void onFailure(Call> call, Throwable t) {
}
});
}
private void updateData(List problemlistVos) {
this.total_problems = 0;
this.changed_problems = 0;
for (ProblemCategoryVo p : this.problemCategories) {
p.setTotalCount(0);
p.setIsRectifiedCount(0);
p.getProblemlistVos().clear();
for (int i = 0; i < problemlistVos.size(); i++) {
ProblemlistVo problemlistVo = problemlistVos.get(i);
if (problemlistVo.getTypeid()!=null && p.getProblemTypeId().equals(problemlistVo.getTypeid().toString())) {
p.getProblemlistVos().add(problemlistVo);
//总问题数累加
p.setTotalCount(p.getTotalCount() + 1 );
this.total_problems++;
//已整改问题数累加
if (problemlistVo.getIschanged() && Objects.equals(problemlistVo.getExtension3(), Domain.CHANGE_CHECK_PASS)) {
p.setIsRectifiedCount(p.getIsRectifiedCount() + 1);
this.changed_problems++;
}
problemlistVos.remove(i);
i--;
}
}
}
//问题列表不为空则表示有异常的问题(可能问题类型不存在或为空), 都归类为"其他"类型
if (!problemlistVos.isEmpty()) {
ProblemCategoryVo problemCategoryVo = this.problemCategories.get(this.problemCategories.size() - 1);//得到“其他”这个问题类型(查询时已排序)
problemCategoryVo.getProblemlistVos().addAll(problemlistVos);
//总问题数累加
problemCategoryVo.setTotalCount(problemCategoryVo.getTotalCount() + problemlistVos.size());
this.total_problems += problemlistVos.size();
for (ProblemlistVo p : problemlistVos) {
//补充问题类型id和类型名称为"其他"类型
try {
p.setTypeid(Byte.valueOf(problemCategoryVo.getProblemTypeId()));
} catch (NumberFormatException e) {
e.printStackTrace();
}
p.setTypename(problemCategoryVo.getProblemTypeName());
if (p.getIschanged()) {
//已整改问题数累加
problemCategoryVo.setIsRectifiedCount(problemCategoryVo.getIsRectifiedCount() + 1);
this.changed_problems++;
}
}
}
}
//
//
public void updateData() {
Date now = new Date();
String date = DateFormatter.dateTimeFormat3.format(now);
refresh(application.getCurrentUser().getDguid(), date);//对于客户来说,D_GUID就是场景id
}
//
}