@Arison
2018-06-04T02:15:36.000000Z
字数 19440
阅读 1129
待删除
package com.xzjmyk.pm.activity.ui.erp.presenter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.xzjmyk.pm.activity.MyApplication;
import com.xzjmyk.pm.activity.R;
import com.xzjmyk.pm.activity.bean.User;
import com.xzjmyk.pm.activity.bean.oa.SearchPoiParam;
import com.xzjmyk.pm.activity.db.dao.UserDao;
import com.xzjmyk.pm.activity.db.dao.WorkModelDao;
import com.xzjmyk.pm.activity.ui.erp.activity.oa.SearchLocationActivity;
import com.xzjmyk.pm.activity.ui.erp.model.oa.WorkLocationModel;
import com.xzjmyk.pm.activity.ui.erp.model.oa.WorkModel;
import com.xzjmyk.pm.activity.ui.erp.presenter.imp.IWorkPresenter;
import com.xzjmyk.pm.activity.ui.erp.presenter.imp.IWorkView;
import com.xzjmyk.pm.activity.ui.erp.util.CommonUtil;
import com.xzjmyk.pm.activity.ui.erp.util.ListUtils;
import com.xzjmyk.pm.activity.ui.erp.util.LogUtil;
import com.xzjmyk.pm.activity.ui.erp.util.OACheckUtil;
import com.xzjmyk.pm.activity.ui.erp.util.StringUtils;
import com.xzjmyk.pm.activity.ui.erp.util.oa.CommonInterface;
import com.xzjmyk.pm.activity.ui.erp.util.oa.WorkHandlerUtil;
import com.xzjmyk.pm.activity.ui.erp.util.oa.baidu.BaiduMapUtil;
import com.xzjmyk.pm.activity.ui.erp.util.oa.http.OAHttpHelper;
import com.xzjmyk.pm.activity.ui.erp.util.oa.http.OnHttpResultListener;
import com.xzjmyk.pm.activity.ui.erp.util.oa.http.Request;
import com.xzjmyk.pm.activity.util.PreferenceUtils;
import com.xzjmyk.pm.activity.util.TimeUtils;
import com.xzjmyk.pm.activity.wxapi.ApiConfig;
import com.xzjmyk.pm.activity.wxapi.ApiPlatform;
import com.xzjmyk.pm.activity.wxapi.ApiUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.xzjmyk.pm.activity.ui.erp.util.oa.baidu.BaiduMapUtil.getInstence;
/**
* 班次管理器
* Created by Bitliker on 2016/12/12.
*/
public class WorkPresenter implements OnHttpResultListener, IWorkPresenter {
private final int WORK_LOG = 0x12;
private final int WORK_DATA = 0x11;
private final int MAC_VAL = 0x13;
private final int LOAD_MAC = 0x14;
private final int SIGNING = 0x15;//签到
private final int LOAD_WORK_SET = 0x16;//获取考勤设置
private final int ADDRESS_CHANGE = 0x14;//地址微调
private double distance = -1;
//本地位置
private double longitude;
private double latitude;
private String loaction;
private String address;
private List<WorkLocationModel> locationList;
private WorkLocationModel companyLocation;
private boolean isMacInNet = false;//服务器上是否有mac地址
private boolean isSubmiting = false;
private String macAddress;
private boolean isFree = false;
private String days;
private String name;
private IWorkView iWorkView;
private boolean isB2b;
public WorkPresenter(IWorkView iWorkView, String macAddress) {
if (iWorkView == null) new NullPointerException("iWorkView not be null ");
this.iWorkView = iWorkView;
this.macAddress = macAddress;
}
private void loadLog(ArrayList<WorkModel> models, long time) {
if (iWorkView != null)
iWorkView.showLoading();
String date = TimeUtils.s_long_2_str(time);
Map<String, Object> param = new HashMap<>();
String code = CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_username");
param.put("currentMaster", CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_master"));
if (isB2b)
param.put("pageNumber", 1);
else
param.put("page", 1);
param.put("pageSize", 1000);
if (!isB2b)
param.put("condition", "cl_emcode='" + code + "' and to_char(cl_time,'yyyy-MM-dd')='" + date + "'");
else
param.put("date", TimeUtils.long2str(time, "yyyyMMdd"));
param.put("caller", "CardLog");
param.put("emcode", code);
param.put("master", CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_master"));
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("models", models);
bundle.putLong("time", time);
String url = isB2b ? ApiConfig.getInstance(ApiUtils.getApiModel()).getmApiBase().punch_record_url :
"mobile/oa/workdata.action";
Request request = new Request.Bulider()
.setUrl(url)
.setWhat(WORK_LOG)
.setMode(Request.Mode.GET)
.setParam(param)
.setBundle(bundle)
.bulid();
OAHttpHelper.getInstance().requestHttp(request, this);
}
//判断mac地址是否已经上传
private void getMacByNet() {
if (iWorkView != null) iWorkView.showLoading();
//获取网络数据
Map<String, Object> param = new HashMap<>();
param.put("macaddress", macAddress);
param.put("emcode", CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_username"));
String url = "mobile/queryMobileMac.action";
Request request = new Request.Bulider()
.setUrl(url)
.setWhat(LOAD_MAC)
.setMode(Request.Mode.GET)
.setParam(param)
.bulid();
OAHttpHelper.getInstance().requestHttp(request, this);
}
private void validatorMac(ArrayList<WorkModel> models, String macAddress) {
isSubmiting = true;
if (iWorkView != null) iWorkView.showLoading();
Map<String, Object> param = new HashMap<>();
param.put("macAddress", macAddress);
param.put("emcode", CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_username"));
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("models", models);
String url = "mobile/addMobileMac.action";
Request request = new Request.Bulider()
.setUrl(url)
.setWhat(MAC_VAL)
.setMode(Request.Mode.POST)
.setParam(param)
.setBundle(bundle)
.bulid();
OAHttpHelper.getInstance().requestHttp(request, this);
}
//签到
private void signinWork(Bundle bundle, String code) {
if (iWorkView != null) iWorkView.showLoading();
Map<String, Object> param = new HashMap<>();
Map<String, Object> form = new HashMap<>();
//如果是erp就上传code
if (!isB2b)
form.put("cl_code", code);
if (StringUtils.isEmpty(address))
address = PreferenceUtils.getString("bdaddress");
String phone = MyApplication.getInstance().mLoginUser.getTelephone();
if (StringUtils.isEmpty(phone)) {
String userId = MyApplication.getInstance().mLoginUser.getUserId();
if (!StringUtils.isEmpty(userId)) {
User user = UserDao.getInstance().getUserByUserId(userId);
phone = user.getTelephone();
} else
phone = CommonUtil.getSharedPreferences(MyApplication.getInstance(), "user_phone");
}
String emCode = CommonUtil.getEmcode();
String emname = CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_emname");
if (StringUtils.isEmpty(emname)) {
emname = MyApplication.getInstance().mLoginUser.getNickName().trim();
}
if (StringUtils.isEmpty(loaction))
loaction = PreferenceUtils.getString("bdlocation");
form.put("cl_address", address);//地址
form.put("cl_phone", phone); //电话号码
form.put("cl_distance", distance);//距离
form.put("cl_emname", emname); //名字
form.put("cl_location", loaction); //位置
form.put("cl_emcode", emCode);//编号 erp员工编号 b2b enuu
if (isB2b) {
form.put("enuu", CommonUtil.getSharedPreferences(MyApplication.getInstance().getApplicationContext(), "companyEnUu"));
form.put("emcode", CommonUtil.getSharedPreferences(MyApplication.getInstance().getApplicationContext(), "b2b_uu"));
}
String formStore = StringUtils.mapToJson(form);
param.put("formStore", formStore);
if (!isB2b)
param.put("caller", "CardLog");
String url = isB2b ? ApiConfig.getInstance(ApiUtils.getApiModel()).getmApiBase().punch_worksignin_url : "mobile/saveCardLog.action";
Request request = new Request.Bulider()
.setUrl(url)
.setWhat(SIGNING)
.setMode(Request.Mode.POST)
.setParam(param)
.setBundle(bundle)
.bulid();
OAHttpHelper.getInstance().requestHttp(request, this);
}
/*获取高级设置*/
private void loadWorkSet(long time) {
if (iWorkView != null)
iWorkView.showLoading();
//获取考勤高级设置时间请求
Map<String, Object> param = new HashMap<>();
Bundle bundle = new Bundle();
bundle.putLong("time", time);
if (!isB2b)
param.put("code", 1);
String url = isB2b ? ApiConfig.getInstance(ApiUtils.getApiModel()).getmApiBase().get_plat_senior_setting_url :
"/mobile/getconfigs.action";
Request request = new Request.Bulider()
.setUrl(url)
.setWhat(LOAD_WORK_SET)
.setMode(Request.Mode.GET)
.setParam(param)
.setBundle(bundle)
.bulid();
OAHttpHelper.getInstance().requestHttp(request, this);
}
/*请求班次数据*/
private void loadWorkData(long time) {
Map<String, Object> param = new HashMap<>();
param.put("date", TimeUtils.int_long_2_str(time));
param.put("emcode", CommonUtil.getSharedPreferences(MyApplication.getInstance(), "erp_username"));
Bundle bundle = new Bundle();
bundle.putLong("time", time);
String url = isB2b ? ApiConfig.getInstance(ApiUtils.getApiModel()).getmApiBase().punch_schedule_url :
"mobile/getWorkDate.action";
Request request = new Request.Bulider()
.setUrl(url)
.setWhat(WORK_DATA)
.setMode(Request.Mode.GET)
.setParam(param)
.setBundle(bundle)
.bulid();
OAHttpHelper.getInstance().requestHttp(request, this);
}
public void upDateLocation() {
try {
setBaiduLocation();
} catch (Exception e) {
e.printStackTrace();
}
}
//设置位置信息
private void setBaiduLocation() throws Exception {
boolean isLocation = MyApplication.getInstance().getBdLocationHelper().locationOk();
if (!isLocation) {
if (iWorkView != null) {
iWorkView.showNotLocation();
}
return;
}
longitude = MyApplication.getInstance().getBdLocationHelper().getLongitude();
latitude = MyApplication.getInstance().getBdLocationHelper().getLatitude();
loaction = MyApplication.getInstance().getBdLocationHelper().getName();
address = MyApplication.getInstance().getBdLocationHelper().getAddress();
if (iWorkView == null) return;
distance = -1;
iWorkView.showDistance(getDistance());
iWorkView.showLocation(address);
}
//获取与公司距离
private double getDistance() {
try {
if (ListUtils.isEmpty(locationList)) return -1;
for (WorkLocationModel b : locationList) {
String dis = getInstence().getDistance(
new LatLng(b.getLocation().longitude, b.getLocation().latitude)
, new LatLng(latitude, longitude));
if (StringUtils.isEmpty(dis)) continue;
if (distance == -1 || distance > Double.valueOf(dis)) {
distance = Double.valueOf(dis);
companyLocation = b;
}
}
return distance;
} catch (Exception e) {
return -1;
}
}
public void gotoLocationActivity(Activity ct) {
if (companyLocation == null) {
iWorkView.showToast(R.string.not_addr_to_select, R.color.load_error);
return;
}
Intent intent = new Intent(ct, SearchLocationActivity.class);
SearchPoiParam poiParam = new SearchPoiParam();
poiParam.setType(1);
poiParam.setTitle(MyApplication.getInstance().getResources().getString(R.string.unoffice));
poiParam.setRadius(300);
poiParam.setContrastLatLng(new LatLng(companyLocation.getLocation().longitude, companyLocation.getLocation().latitude));
poiParam.setResultCode(ADDRESS_CHANGE);
poiParam.setDistanceTag(MyApplication.getInstance().getResources().getString(R.string.rice));
intent.putExtra("data", poiParam);
ct.startActivityForResult(intent, ADDRESS_CHANGE);
}
private void getPoi() {
if (companyLocation == null) return;
//公司地址必须反过来
final LatLng compayLng = new LatLng(companyLocation.getLongitude(), companyLocation.getLatitude());
LatLng latLng = new LatLng(latitude, longitude);
BaiduMapUtil.getInstence().getNearInfo(500, 0, latLng,
"公司", new BaiduMapUtil.OnSearchPoiListener() {
@Override
public void result(SearchResult result) {
if (result instanceof PoiResult) {
List<PoiInfo> pois = ((PoiResult) result).getAllPoi();
if (ListUtils.isEmpty(pois) || companyLocation == null) return;
for (int i = 0; i < pois.size(); i++) {
double dis = BaiduMapUtil.getInstence().distance(compayLng, pois.get(i).location);
if (companyLocation.getValidrange() < dis) {
pois.remove(i);
i--;
}
}
if (iWorkView != null) iWorkView.setPois(pois, compayLng);
}
}
});
}
/**
* 处理打卡签到
*
* @param isJSON
* @param message
* @param time
*/
private void handlerWorkData(boolean isJSON, String message, long time) throws Exception {
if (!isJSON) {
if (iWorkView != null) {
iWorkView.showToast(message, R.color.load_error);
showModels(null, time);
}
return;
}
JSONObject object = null;
object = JSON.parseObject(message);
days = OACheckUtil.getJson2Text(object, "wd_day", "day");
name = OACheckUtil.getJson2Text(object, "wd_name", "name");
locationList = WorkHandlerUtil.handerLocation(object, isB2b);
//获取当前最近的位置信息
if (!ListUtils.isEmpty(locationList)) {
float dis = 0;
boolean first = true;
for (WorkLocationModel location : locationList) {
float distance = BaiduMapUtil.getInstence().autoDistance(location.getLocation());
if (first || dis > distance) {
dis = distance;
companyLocation = location;
}
}
} else {
companyLocation = null;
}
ArrayList<WorkModel> models = WorkHandlerUtil.handlerWorkData(object, isB2b);
try {
if (ListUtils.isEmpty(locationList) && iWorkView != null && !ListUtils.isEmpty(models))
iWorkView.showToast(R.string.not_addr_message, R.color.load_warning);
else getPoi();
setBaiduLocation();
} catch (ClassCastException e) {
if (e != null)
LogUtil.i("handlerWorkData ClassCastException=" + e.getMessage());
} catch (Exception e) {
if (e != null)
LogUtil.i("handlerWorkData Exception=" + e.getMessage());
}
//b2b平台接口出现异常
Object ifNeedSignCard = object.get("ifNeedSignCard");
if (ifNeedSignCard instanceof Boolean)
isFree = !((boolean) ifNeedSignCard);
else if (ifNeedSignCard instanceof String)
isFree = !"是".equals(ifNeedSignCard);
if (isFree) {
loadLog(null, time);
return;
}
if (ListUtils.isEmpty(models)) {
if (iWorkView != null) {
iWorkView.showToast(R.string.not_work_message, R.color.load_error);
showModels(null, time);
}
return;
}
loadLog(models, time);
}
/**
* 处理打卡签到列表,建议在线程钟使用
*
* @param isJSON 是否是json数据
* @param message 返回信息
* @param logTime 日期时间戳
* @throws Exception
*/
private void handlerWorkLog(boolean isJSON, String message, final ArrayList<WorkModel> workModels, long logTime) throws Exception {
if (!isJSON || (!isFree && ListUtils.isEmpty(workModels))) {
showModels(workModels, logTime);
return;
}
JSONObject object = JSON.parseObject(message);
ArrayList<WorkModel> models;
if (!isFree) {
models = WorkHandlerUtil.handlerWorkLog(object, workModels);
} else {
models = WorkHandlerUtil.handlerFreeLog(object);
}
showModels(models, logTime);
}
private void showModels(List<WorkModel> models, long longTime) {
if (ListUtils.isEmpty(models)) {
WorkModelDao.getInstance().clearByDate(longTime);
} else {
WorkModelDao.getInstance().createOrUpdata(models, longTime);
}
List<WorkModel> chche = WorkModelDao.getInstance().query(true, longTime);
Collections.sort(chche, new Comparator<WorkModel>() {
@Override
public int compare(WorkModel lhs, WorkModel rhs) {
return lhs.getId() - rhs.getId();
}
});
iWorkView.showModel(isFree, chche);
iWorkView.dimssLoading();
}
private long subMitTime;
/**
* 判断是否符合签到
*
* @param models
* @return
*/
private boolean isSubmitAble(ArrayList<WorkModel> models) {
if (ListUtils.isEmpty(models) && !isFree) {
if (iWorkView != null)
iWorkView.showToast(R.string.not_work_message, R.color.load_warning);
return false;
}
if (isSubmiting) {
if (iWorkView != null)
iWorkView.showToast(R.string.not_signin_agin, R.color.load_warning);
return false;
} else if (companyLocation == null || companyLocation.getValidrange() < distance) {
if (iWorkView != null)
iWorkView.showToast(R.string.too_long, R.color.load_warning);
return false;
} else if (subMitTime != 0 && (System.currentTimeMillis() - subMitTime) < (20 * 1000)) {
if (iWorkView != null)
iWorkView.showToast(R.string.not_signin_agin, R.color.load_warning);
return false;
} else if (!MyApplication.getInstance().isNetworkActive()) {
if (iWorkView != null)
iWorkView.showToast(R.string.networks_out, R.color.load_warning);
return false;
}
return true;
}
/********************
* 对外接口
*********************************/
@Override
public void start(boolean canShowTocat) {
// 判断当前为平台用户;
isB2b = ApiUtils.getApiModel() instanceof ApiPlatform;
// isB2b = false;
MyApplication.getInstance().getBdLocationHelper().requestLocation();
if (MyApplication.getInstance().isNetworkActive()) {
if (!isB2b)
getMacByNet();
loadWorkData(System.currentTimeMillis(), canShowTocat);
} else if (canShowTocat) {
iWorkView.showToast(R.string.networks_out, R.color.load_warning);
}
}
public void loadWorkData(long time, boolean canShowTocat) {
if (!MyApplication.getInstance().isNetworkActive() && canShowTocat) {
iWorkView.showToast(R.string.networks_out, R.color.load_warning);
return;
}
if (isB2b)
loadWorkData(time);//TODO 如果是b2b的情况就先下拉打卡,因为没有排班设计只有默认班次
else
loadWorkSet(time);
}
@Override
public void submit(ArrayList<WorkModel> models, String macAddress) {
if (isSubmitAble(models)) {
if (isB2b) {
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("models", models);
signinWork(bundle, "");
} else if (isMacInNet) {
submitWork(models, macAddress);
} else {
if (iWorkView != null)
iWorkView.showFristMac();
}
}
}
public void submitByFrist(ArrayList<WorkModel> models, String macAddress) {
if (isSubmitAble(models)) {
submitWork(models, macAddress);
}
}
/**
* 提交打卡
*
* @param models 班次以及打卡记录数据
* @param macAddress mac地址
*/
private void submitWork(final ArrayList<WorkModel> models, String macAddress) {
subMitTime = System.currentTimeMillis();
if (isB2b) {
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("models", models);
signinWork(bundle, "");
} else
validatorMac(models, macAddress);
}
/**
* 更改位置信息
*
* @param poi 位置信息
*/
public void changPoi(PoiInfo poi) {
loaction = StringUtils.isEmpty(poi.name) ? "" : poi.name;//位置名称
address = StringUtils.isEmpty(poi.address) ? "" : poi.address;
latitude = poi.location.latitude;
longitude = poi.location.longitude;
if (iWorkView == null) return;
iWorkView.showLocation(address);
distance = -1;
iWorkView.showDistance(getDistance());
}
@Override
public void result(int what, boolean isJSON, String message, final Bundle bundle) {
try {
ArrayList<WorkModel> models = null;
if (iWorkView != null) iWorkView.dimssLoading();
switch (what) {
case LOAD_WORK_SET://获取打卡设置
long t = System.currentTimeMillis();
if (bundle != null) t = bundle.getLong("time");
if (isJSON)
WorkHandlerUtil.handlerWorkSet(JSON.parseObject(message));
loadWorkData(t);
break;
case WORK_DATA://打卡班次
long time = System.currentTimeMillis();
if (bundle != null) time = bundle.getLong("time");
handlerWorkData(isJSON, message, time);
break;
case WORK_LOG://打卡列表
long logTime = System.currentTimeMillis();
if (bundle != null) models = bundle.getParcelableArrayList("models");
if (bundle != null) logTime = bundle.getLong("time");
handlerWorkLog(isJSON, message, models, logTime);
break;
case MAC_VAL://判断mac地址
if (isJSON && JSON.parseObject(message).containsKey("success") &&
JSON.parseObject(message).getBoolean("success")) {
CommonInterface.getInstance().getCodeByNet("CardLog", new CommonInterface.OnResultListener() {
@Override
public void result(boolean isOk, int result, String message) {
signinWork(bundle, message);
}
});
}
break;
case LOAD_MAC://判断是否是第一次请求数据
if (isJSON) {
JSONObject object = JSON.parseObject(message);
if (object.containsKey("success") && object.getBoolean("success")) {
//返回成功
JSONArray array = object.getJSONArray("macaddress");
if (array != null && array.getJSONObject(0).containsKey("MACADDRESS")) {
if ("0".equals(array.getJSONObject(0).getString("MACADDRESS"))) {
//获取到新的mac地址
isMacInNet = false;
} else isMacInNet = true;
}
}
} else isMacInNet = false;
break;
case SIGNING:
//签到成功
isSubmiting = false;
if (isJSON && JSON.parseObject(message).containsKey("success") && JSON.parseObject(message).getBoolean("success")) {
if (iWorkView != null)
iWorkView.showToast(R.string.signin_ok, R.color.load_submit);
if (bundle != null) models = bundle.getParcelableArrayList("models");
loadLog(models, System.currentTimeMillis());
} else if (iWorkView != null)
iWorkView.showToast(R.string.signin_error, R.color.load_error);
break;
default:
break;
}
} catch (Exception e) {
if (e != null)
LogUtil.i("result=" + e.getMessage());
}
}
@Override
public void error(int what, String message, Bundle bundle) {
try {
isSubmiting = false;
subMitTime = 0;
if (iWorkView != null) iWorkView.dimssLoading();
if (StringUtils.isInclude(message, "该设备不是考勤打卡常用设备")) {
if (iWorkView != null) iWorkView.showErrorMac();
} else if (StringUtils.isInclude(message, "设备正处于申请变更绑定阶段")) {
if (iWorkView != null) {
long time = 0;
if (bundle != null) time = bundle.getLong("time");
if (time != 0 && !TimeUtils.s_long_2_str(System.currentTimeMillis()).equals(TimeUtils.s_long_2_str(time))) {
} else {
iWorkView.showToast(R.string.mac_changing, R.color.load_warning);
iWorkView.setErrorMac(MyApplication.getInstance().getResources().getString(R.string.mac_changing));
}
}
} else if (StringUtils.isInclude(message, MyApplication.getInstance().getResources().getString(R.string.mac_other))) {
iWorkView.showToast(R.string.mac_other, R.color.load_warning);
iWorkView.setErrorMac(MyApplication.getInstance().getResources().getString(R.string.mac_other));
} else {
String mes = StringUtils.getChinese(message);
if (!StringUtils.isEmpty(mes) && iWorkView != null)
iWorkView.showToast(mes, R.color.load_warning);
}
} catch (Exception e) {
if (e != null) LogUtil.i("Error Exception =" + e.getMessage());
}
}
/*调转显示我的考勤*/
public void showMyRele(Intent intent, List<WorkModel> models) {
if (models instanceof ArrayList)
intent.putParcelableArrayListExtra("data", (ArrayList<WorkModel>) models);
intent.putExtra("isFree", isFree);
intent.putExtra("day", days);
intent.putExtra("name", name);
}
}