using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Shared.Phone.UserCenter.DeviceDoorLock
{
///
/// 门锁的历史记录界面
///
public class DoorLockHistoryLogForm : EditorCommonForm
{
#region ■ 变量声明___________________________
///
/// 列表控件
///
private VerticalListControl listView = null;
///
/// 日期From控件
///
private DateSelectControl dateFromControl = null;
///
/// 日期To控件
///
private DateSelectControl dateToControl = null;
///
/// 成员信息
///
private List listMenberInfo = null;
///
/// 设备的Mac地址
///
private string DeviceMac = string.Empty;
///
/// 检索对象的ID(-1:所有 空字符串:其他)
///
private List listSearchUserId = new List() { "-1" };
///
/// 检索开锁方式的ID(-1:所有 0:密码 15:指纹 3:IC卡)
///
private List listSearchLockId = new List() { -1 };
#endregion
#region ■ 初始化_____________________________
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
/// 门锁的Mac地址
public void ShowForm(string i_DeviceMac)
{
this.DeviceMac = i_DeviceMac;
//设置头部信息
base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uHistoryLog));
//删除
var btnDelete = new MostRightIconControl(69, 69);
btnDelete.UnSelectedImagePath = "Item/DeleteIcon2Selected.png";
topFrameLayout.AddChidren(btnDelete);
btnDelete.InitControl();
btnDelete.ButtonClickEvent += (sender, e) =>
{
var form = new TopRightMenuControl(1, 449);
//清空记录
form.AddRowMenu(Language.StringByID(R.MyInternationalizationString.uClearLog), "Item/DeleteIcon2.png", "Item/DeleteIcon2Selected.png", () =>
{
if (this.listView.ChildrenCount == 0)
{
//无数据可删
return;
}
//确认清空当前记录?
this.ShowMassage(ShowMsgType.Confirm, Language.StringByID(R.MyInternationalizationString.uConfirmClearNowLog), () =>
{
//清空所有记录
this.ClearAllHistoryLog();
});
});
};
//初始化中部信息
this.InitMiddleFrame();
}
///
/// 初始化中部信息
///
private void InitMiddleFrame()
{
//清空bodyFrame
this.ClearBodyFrame();
//初始化头部检索控件
this.InitTitleSearchControl();
//主线程的异步会阻塞主线程,导致界面有段时间会白屏,所以这样子让界面先出来
//然后再刷新数据
HdlThreadLogic.Current.RunMainInThread(() =>
{
//初始化记录列表
this.InitLogListInfo();
});
}
///
/// 初始化头部检索控件
///
private void InitTitleSearchControl()
{
var frameTitle = new FrameRowControl();
frameTitle.UseClickStatu = false;
frameTitle.Height = Application.GetRealHeight(115);
frameTitle.BackgroundColor = topFrameLayout.BackgroundColor;
bodyFrameLayout.AddChidren(frameTitle);
var btnDateIcon = frameTitle.AddLeftIcon(69);
btnDateIcon.UnSelectedImagePath = "Item/DateSearchIcon.png";
dateFromControl = new DateSelectControl(Language.StringByID(R.MyInternationalizationString.uStartDate));
dateFromControl.Y = Application.GetRealHeight(35);
dateFromControl.X = Application.GetRealWidth(138);
dateFromControl.dateValue = DateTime.Now.ToString("yyyyMMdd");
frameTitle.AddChidren(dateFromControl, ChidrenBindMode.NotBind);
dateFromControl.DateSelectEvent += (dateValue) =>
{
//初始化记录列表
this.InitLogListInfo();
};
var btnTemp = new NormalViewControl(26, 58, true);
btnTemp.Y = Application.GetRealHeight(29);
btnTemp.X = Application.GetRealWidth(357);
btnTemp.TextColor = UserCenterColor.Current.TextGrayColor1;
btnTemp.Text = "-";
frameTitle.AddChidren(btnTemp, ChidrenBindMode.NotBind);
dateToControl = new DateSelectControl(Language.StringByID(R.MyInternationalizationString.uEndDate));
dateToControl.Y = Application.GetRealHeight(35);
dateToControl.X = Application.GetRealWidth(395);
dateToControl.dateValue = DateTime.Now.ToString("yyyyMMdd");
frameTitle.AddChidren(dateToControl, ChidrenBindMode.NotBind);
dateToControl.DateSelectEvent += (dateValue) =>
{
//初始化记录列表
this.InitLogListInfo();
};
//类型筛选
var btnScreening = frameTitle.AddMostRightView(Language.StringByID(R.MyInternationalizationString.uScreeningType), 145);
frameTitle.ChangedChidrenBindMode(btnScreening, ChidrenBindMode.NotBind);
btnScreening.TextSize = 12;
btnScreening.ButtonClickEvent += (sender, e) =>
{
//显示类型筛选界面
this.ShowDoorLockScreeningTypeForm();
};
//类型筛选的图标
var btnScreeningIcon = new IconViewControl(69);
btnScreeningIcon.X = btnScreening.X - Application.GetRealWidth(12) - btnScreeningIcon.IconSize;
btnScreeningIcon.Gravity = Gravity.CenterVertical;
btnScreeningIcon.UnSelectedImagePath = "Item/ScreeningType.png";
frameTitle.AddChidren(btnScreeningIcon, ChidrenBindMode.NotBind);
btnScreeningIcon.ButtonClickEvent += (sender, e) =>
{
//显示类型筛选界面
this.ShowDoorLockScreeningTypeForm();
};
//底线
frameTitle.AddBottomLine();
this.listView = new VerticalListControl(29);
listView.Y = frameTitle.Bottom - Application.GetRealHeight(3);
listView.BackgroundColor = UserCenterColor.Current.White;
listView.Height = bodyFrameLayout.Height - frameTitle.Bottom + Application.GetRealHeight(3);
bodyFrameLayout.AddChidren(listView);
}
#endregion
#region ■ 初始化记录列表_____________________
///
/// 初始化记录列表
///
private async void InitLogListInfo()
{
this.listView.RemoveAll();
listView.Height = bodyFrameLayout.Height - Application.GetRealHeight(115 - 3);
//如果From>To的时候,不鸟它
if (Convert.ToInt32(dateFromControl.dateValue) > Convert.ToInt32(dateToControl.dateValue))
{
//开始时间大于结束时间
this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uFromTimeIsOverToTime));
return;
}
//打开进度条
this.ShowProgressBar();
//成员列表
var result = await this.GetMemberListInfo();
if (result == false)
{
//关闭进度条
this.CloseProgressBar(ShowReLoadMode.YES);
return;
}
//历史记录
var historyLog = await this.GetHistoryLogInf();
if (historyLog == null)
{
//关闭进度条
this.CloseProgressBar(ShowReLoadMode.YES);
return;
}
//关闭进度条
this.CloseProgressBar();
var dic = new Dictionary();
for (int i = 0; i < listMenberInfo.Count; i++)
{
dic.Add(listMenberInfo[i].SubAccountDistributedMark, string.IsNullOrEmpty(listMenberInfo[i].UserName) == false ? listMenberInfo[i].UserName : listMenberInfo[i].Account);
}
for (int i = 0; i < historyLog.Count; i++)
{
//添加记录行
this.AddLogInfoRow(historyLog[i], dic, i != historyLog.Count - 1);
}
if (listView.ChildrenCount == 0)
{
return;
}
//调整列表控件的高度
var realHeight = listView.ChildrenCount * listView.GetChildren(0).Height;
realHeight += Application.GetRealHeight(23 + 3);
if (realHeight < listView.Height)
{
//缩小控件高度
listView.Height = realHeight;
}
}
///
/// 获取成员列表信息
///
///
private async Task GetMemberListInfo()
{
if (listMenberInfo != null)
{
//只初始化一次
return true;
}
listMenberInfo = new List();
//把自己也加进去
var myInfo = new MemberInfoRes()
{
Account = UserCenterResourse.UserInfo.Account,
UserName = UserCenterResourse.UserInfo.UserName,
SubAccountDistributedMark = Common.Config.Instance.Guid
};
listMenberInfo.Add(myInfo);
if (UserCenterResourse.UserInfo.AuthorityNo == 3)
{
//成员只能看自己的
return true;
}
var pra = new MemberListInfoPra();
string result = await UserCenterLogic.GetResponseDataByRequestHttps("ZigbeeUsers/GetSubAccountByDistributedMark", false, pra);
if (result == null)
{
return false;
}
List listInfo = Newtonsoft.Json.JsonConvert.DeserializeObject>(result);
var checkList = new List();
foreach (MemberInfoRes infoRes in listInfo)
{
if (checkList.Contains(infoRes.DistributedMark) == true)
{
continue;
}
checkList.Add(infoRes.DistributedMark);
listMenberInfo.Add(infoRes);
}
return true;
}
#endregion
#region ■ 添加记录行_________________________
///
/// 添加记录行
///
/// 历史记录
/// 名字
/// 添加底线
private void AddLogInfoRow(HistoryInfo historyInfo, Dictionary dicName, bool addLine = true)
{
var frameTable = new FrameRowControl(listView.rowSpace / 2);
frameTable.UseClickStatu = false;
this.listView.AddChidren(frameTable);
//图标
var btnIcon = frameTable.AddLeftIcon();
if (historyInfo.UnlockIsSuccess == true)
{
btnIcon.UnSelectedImagePath = "Item/UnLockSuccess.png";
}
else
{
btnIcon.UnSelectedImagePath = "Item/UnLockFail.png";
}
//信息
var btnMsg = frameTable.AddLeftCaption("", 600, 60);
btnMsg.TextSize = 15;
//这个坐标有点特殊
btnMsg.Y = Application.GetRealHeight(12) + frameTable.chidrenYaxis;
frameTable.AddChidren(btnMsg, ChidrenBindMode.NotBind);
if (historyInfo.UnlockIsSuccess == true)
{
if (historyInfo.OpenLockMode == 0)
{
//密码开锁成功
btnMsg.TextID = R.MyInternationalizationString.uUnlockByPasswordSuccess;
}
else if (historyInfo.OpenLockMode == 3)
{
//IC卡开锁成功
btnMsg.TextID = R.MyInternationalizationString.uUnlockByICcardSuccess;
}
else if (historyInfo.OpenLockMode == 15)
{
//指纹开锁成功
btnMsg.TextID = R.MyInternationalizationString.uUnlockByFingerPrintSuccess;
}
}
else
{
if (historyInfo.OpenLockMode == 0)
{
//密码开锁失败
btnMsg.TextID = R.MyInternationalizationString.uUnlockByPasswordFail;
}
else if (historyInfo.OpenLockMode == 3)
{
//IC卡开锁失败
btnMsg.TextID = R.MyInternationalizationString.uUnlockByICcardFail;
}
else if (historyInfo.OpenLockMode == 15)
{
//指纹开锁失败
btnMsg.TextID = R.MyInternationalizationString.uUnlockByFingerPrintFail;
}
}
//人物
string perName = dicName.ContainsKey(historyInfo.CloudAccountId) == true ? dicName[historyInfo.CloudAccountId] : Language.StringByID(R.MyInternationalizationString.uOther);
var btnpersion = frameTable.AddLeftCaption(perName, 600, 50, true);
//这个坐标有点特殊
btnpersion.Y = Application.GetRealHeight(72) + frameTable.chidrenYaxis;
btnpersion.TextSize = 12;
btnpersion.TextColor = UserCenterColor.Current.TextGrayColor1;
frameTable.AddChidren(btnpersion, ChidrenBindMode.NotBind);
//时间
frameTable.AddMostRightView(historyInfo.UnlockTime, 400);
if (addLine == true)
{
//底线
frameTable.AddBottomLine();
}
}
#endregion
#region ■ 类型筛选___________________________
///
/// 显示类型筛选界面
///
private void ShowDoorLockScreeningTypeForm()
{
var dic = new Dictionary();
for (int i = 0; i < listMenberInfo.Count; i++)
{
dic.Add(listMenberInfo[i].SubAccountDistributedMark, string.IsNullOrEmpty(listMenberInfo[i].UserName) == false ? listMenberInfo[i].UserName : listMenberInfo[i].Account);
}
//显示底部类型筛选
var form = new DoorLockScreeningTypeForm();
form.AddForm(dic, listSearchUserId, listSearchLockId);
form.FinishSelectEvent += (selectUser, selectLock) =>
{
listSearchUserId.Clear();
listSearchUserId.AddRange(selectUser);
listSearchLockId.Clear();
listSearchLockId.AddRange(selectLock);
//初始化记录列表
this.InitLogListInfo();
};
}
#endregion
#region ■ 获取记录___________________________
///
/// 获取历史记录
///
///
private async Task> GetHistoryLogInf()
{
//获取访问云端接口的启动参数
var pra = this.GetDbInterfacePra();
int nowPage = 0;
var listLog = new List();
while (true)
{
//访问云端
var result = await UserCenterLogic.GetResponseDataByRequestHttps("DoorLock/GetDoorLockHistoryPager", true, pra);
if (result == null)
{
return null;
}
var dataInfo = Newtonsoft.Json.JsonConvert.DeserializeObject(result);
if (dataInfo.HasNextPage == false)
{
break;
}
//获取下一页
nowPage++;
pra.PageSetting.Page = nowPage;
}
//排序一下
var dic = new Dictionary>();
var listSort = new List();
//是否包含其他
bool flage = this.listSearchUserId.Contains("") || this.listSearchUserId.Contains("-1");
foreach (var data in listLog)
{
if (flage == false && string.IsNullOrEmpty(data.CloudAccountId) == true)
{
//没有指定显示其他
continue;
}
if (dic.ContainsKey(data.UnlockTime) == false)
{
dic[data.UnlockTime] = new List();
listSort.Add(data.UnlockTime);
}
dic[data.UnlockTime].Add(data);
}
//执行排序,时间大的在前面
listSort.Sort();
var listSortLog = new List();
for (int i = listSort.Count - 1; i >= 0; i--)
{
listSortLog.AddRange(dic[listSort[i]]);
}
return listSortLog;
}
#endregion
#region ■ 清空记录___________________________
///
/// 清空所有记录
///
private async void ClearAllHistoryLog()
{
//获取访问云端接口的启动参数
var pra = this.GetDbInterfacePra();
//打开进度条
this.ShowProgressBar();
var result = await UserCenterLogic.GetResultStatuByRequestHttps("DoorLock/ClearDoorLockHistory", true, pra);
//关闭进度条
this.CloseProgressBar();
if (result == false)
{
return;
}
this.listView.RemoveAll();
listView.Height = bodyFrameLayout.Height - Application.GetRealHeight(115 - 3);
}
#endregion
#region ■ 一般方法___________________________
///
/// 获取访问云端接口的启动参数
///
///
private GetHistoryDataPra GetDbInterfacePra()
{
DateTime dateFrom = new DateTime(dateFromControl.Year, dateFromControl.Month, dateFromControl.Day, 0, 0, 0);
DateTime dateTo = new DateTime(dateToControl.Year, dateToControl.Month, dateToControl.Day, 23, 59, 59);
//全选则为null
List listUser = null;
if (listSearchUserId.Contains("-1") == false)
{
listUser = new List();
listUser.AddRange(listSearchUserId);
//移除其他,因为云端固定会给
listUser.Remove("");
}
//全选则为null
List listLock = null;
if (listSearchLockId.Contains(-1) == false)
{
listLock = new List();
listLock.AddRange(listSearchLockId);
}
var pra = new GetHistoryDataPra();
pra.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
pra.DoorLockId = this.DeviceMac;
pra.CloudAccountIdArr = listUser;
pra.OpenLockModeArr = listLock;
pra.UnlockTimeBegin = dateFrom;
pra.UnlockTimeEnd = dateTo;
return pra;
}
#endregion
#region ■ 结构体_____________________________
///
/// 获取历史记录数据
///
private class GetHistoryDataPra : IfacePraCommon
{
///
/// RequestVersion
///
public string RequestVersion = Common.CommonPage.RequestVersion;
///
/// Token
///
public string LoginAccessToken = string.Empty;
///
/// 住宅ID
///
public string HomeId = Common.Config.Instance.Home.Id;
///
/// 门锁的Mac地址
///
public string DoorLockId = string.Empty;
///
/// Config.Instance.Guid 或者 MemberInfoRes.SubAccountDistributedMark
///
public List CloudAccountIdArr = new List();
///
/// 开锁方式(0:密码、15:指纹、3:IC卡)
///
public List OpenLockModeArr = null;
///
/// 开锁是否成功
///
public bool? UnlockIsSuccess = null;
///
/// 搜索开锁开始时间
///
public DateTime UnlockTimeBegin;
///
/// 搜索开锁结束时间
///
public DateTime UnlockTimeEnd;
///
/// 页数
///
public PageSettingInfo PageSetting = new PageSettingInfo() { PageSize = 100 };
}
///
/// 获取历史记录的结果
///
private class GetHistoryDataResult
{
///
/// 数据
///
public List PageData = null;
///
/// 是否存在下一页的数据
///
public bool HasNextPage = false;
}
///
/// 历史记录的结果
///
private class HistoryInfo
{
///
/// 开锁方式 0:密码 15:指纹 3:IC卡
///
public int OpenLockMode = -1;
///
/// 开锁时间
///
public string UnlockTime = string.Empty;
///
/// 开锁是否成功
///
public bool UnlockIsSuccess = false;
///
/// 账号的ID
///
public string CloudAccountId = string.Empty;
}
#endregion
}
}