|  |  |  | 
|---|
|  |  |  | import com.doumee.keyCabinet.dao.CabinetGridDo; | 
|---|
|  |  |  | import com.doumee.keyCabinet.dao.DaoManager; | 
|---|
|  |  |  | import com.doumee.keyCabinet.databinding.ManageCabinetActivityBinding; | 
|---|
|  |  |  | import com.doumee.keyCabinet.event.CheckGridStatusEvent; | 
|---|
|  |  |  | import com.doumee.keyCabinet.event.CloseGridOneResultEvent; | 
|---|
|  |  |  | import com.doumee.keyCabinet.event.GetKeyStatusEvent; | 
|---|
|  |  |  | import com.doumee.keyCabinet.event.HttpEvent; | 
|---|
|  |  |  | import com.doumee.keyCabinet.event.KeyResultEvent; | 
|---|
|  |  |  | import com.doumee.keyCabinet.event.ManageOpenGridResultEvent; | 
|---|
|  |  |  | import com.doumee.keyCabinet.event.OpenAllGridEvent; | 
|---|
|  |  |  | import com.doumee.keyCabinet.event.OpenGridEvent; | 
|---|
|  |  |  | import com.doumee.keyCabinet.event.OpenGridListEvent; | 
|---|
|  |  |  | import com.doumee.keyCabinet.event.OpenGridOneResultEvent; | 
|---|
|  |  |  | import com.doumee.keyCabinet.event.TimeClockEvent; | 
|---|
|  |  |  | import com.doumee.keyCabinet.ui.keyCabinet.adapter.ManageCabinetRcvAdapter; | 
|---|
|  |  |  | import com.doumee.keyCabinet.utils.i485.SportUtils; | 
|---|
|  |  |  | import com.doumee.lib_coremodel.bean.event.ActionEventData; | 
|---|
|  |  |  | import com.doumee.lib_coremodel.util.RecyclerHelp; | 
|---|
|  |  |  | import com.doumee.lib_coremodel.util.StringUtil; | 
|---|
|  |  |  | import com.example.datalibrary.db.DBManager; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import org.greenrobot.eventbus.EventBus; | 
|---|
|  |  |  | import org.greenrobot.eventbus.Subscribe; | 
|---|
|  |  |  | 
|---|
|  |  |  | for(ManageKeyCabinetBean d:datas){ | 
|---|
|  |  |  | if(d.getIsSelected().get()){ | 
|---|
|  |  |  | isCanPL = true; | 
|---|
|  |  |  | isCanBJ = true; | 
|---|
|  |  |  | if(keyStatus==-1){ | 
|---|
|  |  |  | keyStatus = getGridStatus(d); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | getDB().tvPlOpen.setBackgroundResource(R.drawable.shape_mb_bt_faile); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(keyStatus==2||keyStatus==4){ | 
|---|
|  |  |  | isCanBJ = true; | 
|---|
|  |  |  | if(isCanBJ){ | 
|---|
|  |  |  | getDB().tvBj.setBackgroundResource(R.drawable.shape_mb_bt_ok); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | getDB().tvBj.setBackgroundResource(R.drawable.shape_mb_bt_faile); | 
|---|
|  |  |  | 
|---|
|  |  |  | for(CabinetGridDo d:gridDos){ | 
|---|
|  |  |  | oldMap.put(d.getGridKey(),d); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<CabinetGridDo> addList = new ArrayList<>(); | 
|---|
|  |  |  | List<CabinetGridDo> updateList = new ArrayList<>(); | 
|---|
|  |  |  | for(ManageKeyCabinetBean bean:beans){ | 
|---|
|  |  |  | String key = SportUtils.intToHex(Integer.parseInt(bean.getBoardCode()))+SportUtils.intToHex(Integer.parseInt(bean.getChannelCode())); | 
|---|
|  |  |  | 
|---|
|  |  |  | gridDo.setKeyStatus(bean.getKeyStatus()); | 
|---|
|  |  |  | gridDo.setWorkingStatus(bean.getWorkingStatus()); | 
|---|
|  |  |  | updateList.add(gridDo); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | //新增 | 
|---|
|  |  |  | CabinetGridDo gridDo = new CabinetGridDo(); | 
|---|
|  |  |  | gridDo.setGridId(bean.getId()); | 
|---|
|  |  |  | gridDo.setCabinetName(bean.getCode()); | 
|---|
|  |  |  | gridDo.setGridKey(key); | 
|---|
|  |  |  | gridDo.setKeyCode(bean.getKeyCode()); | 
|---|
|  |  |  | gridDo.setBindStatus(bean.getBindStatus()); | 
|---|
|  |  |  | gridDo.setGridStatus(bean.getStatus()); | 
|---|
|  |  |  | gridDo.setKeyStatus(bean.getKeyStatus()); | 
|---|
|  |  |  | gridDo.setWorkingStatus(bean.getWorkingStatus()); | 
|---|
|  |  |  | gridDo.setUpdateTime(StringUtil.DateToStr(new Date())); | 
|---|
|  |  |  | addList.add(gridDo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(addList.size()>0){ | 
|---|
|  |  |  | DaoManager.getCabinetGridDao().insert(addList); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(updateList.size()>0){ | 
|---|
|  |  |  | CabinetGridDo[] upList = new CabinetGridDo[updateList.size()]; | 
|---|
|  |  |  | for(int i=0;i<updateList.size();i++){ | 
|---|
|  |  |  | upList[i] = updateList.get(i); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //todo 去掉 | 
|---|
|  |  |  | CabinetGridDo gridDo = oldMap.get("0101"); | 
|---|
|  |  |  | if(gridDo!=null){ | 
|---|
|  |  |  | EventBus.getDefault().post(new HttpEvent(StringUtil.DateToStrSS(new Date())+"=================>更新数据当前钥匙2:"+gridDo.getCurKeyCode())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | DaoManager.getCabinetGridDao().update(upList); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | SportUtils.intToHex(Integer.parseInt(bean.getChannelCode())); | 
|---|
|  |  |  | waitOpenMap.put(key,bean.getCode()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | waitResultCount = 2; | 
|---|
|  |  |  | EventBus.getDefault().post(new OpenAllGridEvent()); | 
|---|
|  |  |  | }break; | 
|---|
|  |  |  | case 10: { | 
|---|
|  |  |  | 
|---|
|  |  |  | keys.add(key); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | waitResultCount = keys.size(); | 
|---|
|  |  |  | EventBus.getDefault().post(new OpenGridListEvent(keys)); | 
|---|
|  |  |  | }break; | 
|---|
|  |  |  | case 4: { | 
|---|
|  |  |  | //标记 | 
|---|
|  |  |  | if(!isCanPL){ | 
|---|
|  |  |  | if(!isCanBJ){ | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | bjIds.clear(); | 
|---|
|  |  |  | 
|---|
|  |  |  | getDB().tvBjgz.setText("标记为故障"); | 
|---|
|  |  |  | getDB().tvBjgz.setTextColor(0xffFF3600); | 
|---|
|  |  |  | getDB().tvBjgz.setBackgroundResource(R.drawable.shape_mb_cz_1_bg); | 
|---|
|  |  |  | getDB().tvBy.setVisibility(View.VISIBLE); | 
|---|
|  |  |  | if(keyStatus==2) { | 
|---|
|  |  |  | //0未绑定 1在位 2借出 3维修保养 | 
|---|
|  |  |  | getDB().tvBy.setVisibility(View.VISIBLE); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | getDB().tvBy.setVisibility(View.GONE); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | getDB().clCz.setVisibility(View.VISIBLE); | 
|---|
|  |  |  | }break; | 
|---|
|  |  |  | 
|---|
|  |  |  | if(tipDownCount==0){ | 
|---|
|  |  |  | isShowTip = false; | 
|---|
|  |  |  | getDB().clTip.setVisibility(View.GONE); | 
|---|
|  |  |  | finish(); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | tipDownCount--; | 
|---|
|  |  |  | getDB().tvDjs.setText(tipDownCount+"s"); | 
|---|
|  |  |  | 
|---|
|  |  |  | return styledText; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void showKeyErrTip(CabinetGridDo gridDo){ | 
|---|
|  |  |  | private void showKeyErrTip(String name){ | 
|---|
|  |  |  | getDB().imgClKm1.setImageResource(R.mipmap.ic_jjfail); | 
|---|
|  |  |  | getDB().tvClKm1.setText("识别失败"); | 
|---|
|  |  |  | getDB().tvClKm2.setText(getTipText2("钥匙柜",gridDo.getCabinetName(),"识别失败"), TextView.BufferType.SPANNABLE); | 
|---|
|  |  |  | getDB().tvClKm2.setText(getTipText2("钥匙柜",name,"识别失败"), TextView.BufferType.SPANNABLE); | 
|---|
|  |  |  | getDB().tvClKmClose.setText("重新存放"); | 
|---|
|  |  |  | getDB().tvClKmClose.setTextColor(0xffffffff); | 
|---|
|  |  |  | getDB().tvClKmClose.setBackgroundResource(R.drawable.shape_mb_bt_ok); | 
|---|
|  |  |  | 
|---|
|  |  |  | return super.onKeyDown(keyCode, event); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private int waitResultCount = 0; | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 开门返回 | 
|---|
|  |  |  | * @param e | 
|---|
|  |  |  | 
|---|
|  |  |  | @Subscribe(threadMode = ThreadMode.MAIN) | 
|---|
|  |  |  | public void ManageOpenGridResultEvent(ManageOpenGridResultEvent e){ | 
|---|
|  |  |  | if(!isFinishing()){ | 
|---|
|  |  |  | if(waitResultCount>0){ | 
|---|
|  |  |  | waitResultCount--; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(waitResultCount>0){ | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | EventBus.getDefault().post(new CheckGridStatusEvent()); | 
|---|
|  |  |  | //校验是否已全开 | 
|---|
|  |  |  | List<CabinetGridDo> gridDos = DaoManager.getCabinetGridDao().loadAll(); | 
|---|
|  |  |  | List<String> msgList = new ArrayList<>(); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private String sygKey; | 
|---|
|  |  |  | @Subscribe | 
|---|
|  |  |  | public void OpenGridOneResultEvent(OpenGridOneResultEvent e){ | 
|---|
|  |  |  | if(!isFinishing()){ | 
|---|
|  |  |  | if(e.getIsOpen()==0){ | 
|---|
|  |  |  | if(e.getKey().equals(sygKey)){ | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | sygKey = e.getKey(); | 
|---|
|  |  |  | //开门失败,重新开门 | 
|---|
|  |  |  | EventBus.getDefault().post(new OpenGridEvent(Integer.parseInt(e.getKey().substring(2,4), 16)+"", | 
|---|
|  |  |  | Integer.parseInt(e.getKey().substring(0,2), 16)+"")); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 关门 | 
|---|
|  |  |  | * @param e | 
|---|
|  |  |  | 
|---|
|  |  |  | @Subscribe(threadMode= ThreadMode.MAIN) | 
|---|
|  |  |  | public void CloseGridOneResultEvent(CloseGridOneResultEvent e){ | 
|---|
|  |  |  | if(!isFinishing()){ | 
|---|
|  |  |  | //获取卡号 | 
|---|
|  |  |  | EventBus.getDefault().post(new GetKeyStatusEvent()); | 
|---|
|  |  |  | if(isShowTip){ | 
|---|
|  |  |  | isShowTip = false; | 
|---|
|  |  |  | getDB().clTip.setVisibility(View.GONE); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //校验卡号 | 
|---|
|  |  |  | CabinetGridDo gridDo = DaoManager.getCabinetGridDao().getGridByKey(e.getKey()); | 
|---|
|  |  |  | if(gridDo!=null){ | 
|---|
|  |  |  | //校验钥匙是否匹配 | 
|---|
|  |  |  | if(gridDo.getGridKey()==null){ | 
|---|
|  |  |  | if(TextUtils.isEmpty(gridDo.getGridKey())){ | 
|---|
|  |  |  | if(!TextUtils.isEmpty(gridDo.getCurKeyCode())){ | 
|---|
|  |  |  | //未绑定的存放了钥匙 | 
|---|
|  |  |  | showKeyErrTip(gridDo); | 
|---|
|  |  |  | showKeyErrTip(gridDo.getCabinetName()+"-"+gridDo.getCurKeyCode()); | 
|---|
|  |  |  | //开门 | 
|---|
|  |  |  | EventBus.getDefault().post(new OpenGridEvent(Integer.parseInt(e.getKey().substring(2,4), 16)+"", | 
|---|
|  |  |  | Integer.parseInt(e.getKey().substring(0,2), 16)+"")); | 
|---|
|  |  |  | 
|---|
|  |  |  | if(!TextUtils.isEmpty(gridDo.getCurKeyCode())){ | 
|---|
|  |  |  | if(!gridDo.getCurKeyCode().equals(gridDo.getKeyCode())){ | 
|---|
|  |  |  | //钥匙不匹配 | 
|---|
|  |  |  | showKeyErrTip(gridDo); | 
|---|
|  |  |  | showKeyErrTip(gridDo.getCabinetName()+"-"+gridDo.getCurKeyCode()); | 
|---|
|  |  |  | EventBus.getDefault().post(new HttpEvent(StringUtil.DateToStrSS(new Date())+"钥匙不匹配==>目标:"+gridDo.getKeyCode()+",当前:"+gridDo.getCurKeyCode())); | 
|---|
|  |  |  | //开门 | 
|---|
|  |  |  | EventBus.getDefault().post(new OpenGridEvent(Integer.parseInt(e.getKey().substring(2,4), 16)+"", | 
|---|
|  |  |  | Integer.parseInt(e.getKey().substring(0,2), 16)+"")); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | EventBus.getDefault().post(new HttpEvent(StringUtil.DateToStrSS(new Date())+"门关闭成功==>目标:"+gridDo.getKeyCode()+",当前:"+gridDo.getCurKeyCode())); | 
|---|
|  |  |  | getVM().closeGrid(gridDo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Subscribe | 
|---|
|  |  |  | public void KeyResultEvent(KeyResultEvent e){ | 
|---|
|  |  |  | if(!isFinishing()){ | 
|---|
|  |  |  | //校验卡号不对的,直接弹出 | 
|---|
|  |  |  | List<CabinetGridDo> gridDos = DaoManager.getCabinetGridDao().loadAll(); | 
|---|
|  |  |  | List<String> keys = new ArrayList<>(); | 
|---|
|  |  |  | StringBuilder sb = new StringBuilder(); | 
|---|
|  |  |  | for(CabinetGridDo gridDo:gridDos){ | 
|---|
|  |  |  | if(TextUtils.isEmpty(gridDo.getGridKey())){ | 
|---|
|  |  |  | if(!TextUtils.isEmpty(gridDo.getCurKeyCode())&&gridDo.getIsOpen()==0){ | 
|---|
|  |  |  | //未绑定的存放了钥匙 | 
|---|
|  |  |  | keys.add(gridDo.getGridKey()); | 
|---|
|  |  |  | if(sb.length()>0){ | 
|---|
|  |  |  | sb.append("、"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | sb.append(gridDo.getCabinetName()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | if(!TextUtils.isEmpty(gridDo.getCurKeyCode())){ | 
|---|
|  |  |  | if(!gridDo.getCurKeyCode().equals(gridDo.getKeyCode())&&gridDo.getIsOpen()==0){ | 
|---|
|  |  |  | //钥匙不匹配 | 
|---|
|  |  |  | keys.add(gridDo.getGridKey()); | 
|---|
|  |  |  | if(sb.length()>0){ | 
|---|
|  |  |  | sb.append("、"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | sb.append(gridDo.getCabinetName()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(sb.length()>0){ | 
|---|
|  |  |  | showKeyErrTip(sb.toString()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(keys.size()>0){ | 
|---|
|  |  |  | //开门 | 
|---|
|  |  |  | EventBus.getDefault().post(new OpenGridListEvent(keys)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|