/// <summary>
        /// 手动添加考勤记录
        /// </summary>
        public int AddStuAttManually(long stuId, byte attType, string attTime, string attTemp, string attRemark, byte isSendWxMsg = 1)
        {
            var res = 0;
            List <SYS_Student> allStus = (List <SYS_Student>)GetSchoolEntities("stu");
            SYS_Student        sObj    = allStus.Where(x => x.ID == stuId).FirstOrDefault();

            if (sObj != null)
            {
                DateTime             aTime = DateTime.Parse(attTime);
                SYS_StudentAttRecord sar   = new SYS_StudentAttRecord()
                {
                    SchoolId    = mlUser.School.ID,
                    MasterId    = stuId,
                    AttType     = attType,
                    AttTime     = aTime,
                    AttTimeStr  = aTime.ToString("yyyyMMddHHmmssfff"),
                    AttWay      = (byte)AttWay.手动添加,
                    Temperature = attTemp,
                    Remark      = attRemark
                };
                UnitOfWork.Repository <SYS_StudentAttRecord>().AddEntity(sar);
                var addOk = UnitOfWork.CommitAsync().Result;
                if (addOk > 0)
                {
                    //修改考勤状态
                    if (attType == (byte)AttType.签出)
                    {
                        sObj.AttStatus = (byte)CurrentAttStatus.离校;
                    }
                    else
                    {
                        sObj.AttStatus = (byte)CurrentAttStatus.在校;
                    }
                    UnitOfWork.Repository <SYS_Student>().UpdateEntity(sObj);

                    //推送
                    if (isSendWxMsg == 1)
                    {
                        //查找学生家长
                        List <FK_Stu_Parent> fks = UnitOfWork.Repository <FK_Stu_Parent>().GetEntitiesAsync(x => x.SchoolId == sObj.SchoolId && x.StuId == sObj.ID).Result;
                        if (null != fks && fks.Count > 0)
                        {
                            var pWxPInfo  = UnitOfWork.Repository <Wx_PublicInfo>().GetEntitiesAsync(x => x.Type == 1).Result.FirstOrDefault();
                            var wxpInfoId = pWxPInfo == null ? 0 : pWxPInfo.ID;
                            if (mlUser.School.WxPublicInfoId != null && mlUser.School.WxPublicInfoId > 0)
                            {
                                wxpInfoId = (int)mlUser.School.WxPublicInfoId;
                            }
                            var wxPubInfo = UnitOfWork.Repository <Wx_PublicInfo>().GetEntitiesAsync(x => x.ID == wxpInfoId).Result.FirstOrDefault();
                            foreach (var fk in fks)
                            {
                                //推送消息
                                if (null != wxPubInfo)
                                {
                                    // 需要从Cookie中获取
                                    ModelWxMsg <ModelWmAttendance> wxMsg = new ModelWxMsg <ModelWmAttendance>();
                                    wxMsg = JsonFileProvider.Instance.GetSettings <ModelWxMsg <ModelWmAttendance> >();
                                    wxMsg.data.first.value = string.Format(wxMsg.data.first.value, (AttType)attType);
                                    wxMsg.touser           = fk.OpenId;
                                    wxMsg.url = mWxSetting.PubUrl_Host + "/WxRelated/Center/AttDetail?attId=" + sar.ID;
                                    wxMsg.data.keyword1.value = sObj.StuName;
                                    wxMsg.data.keyword2.value = DateTime.Now.ToString();
                                    wxMsg.data.keyword3.value = mlUser.School.SchoolName;
                                    ModelWmResult wmResult = WXOAuthApiHelper.SendTmplMessage(wxPubInfo.AccessToken, wxMsg);
                                    if (null == wmResult || wmResult.msgid <= 0)
                                    {
                                        LogHelper.Error("推送错误:" + wmResult.errmsg);
                                    }
                                }
                            }
                        }
                    }
                }
                res = addOk + UnitOfWork.CommitAsync().Result;
            }
            return(res);
        }
        public ModelJsonRet UploadAttFromApp(int schoolId, long stuId, byte attType, byte attWay, byte isSendMsg)
        {
            _mjr.errMsg = "未找到匹配的项";
            var school = UnitOfWork.Repository <SYS_School>().GetEntitiesAsync(x => x.ID == schoolId).Result.FirstOrDefault();
            var stuObj = UnitOfWork.Repository <SYS_Student>().GetEntitiesAsync(x => x.SchoolId == schoolId && x.ID == stuId && x.Status == (byte)StuStatus.正常).Result.FirstOrDefault();

            if (null == stuObj)
            {
                _mjr.errMsg = "未找到学生ID";
                return(_mjr);
            }
            var currentAttStatus = CurrentAttStatus.在校;

            //判断是签入还是签出
            if (attType == (byte)AttType.签出)
            {
                currentAttStatus = CurrentAttStatus.离校;
            }

            SYS_StudentAttRecord stuAttRecord = new SYS_StudentAttRecord
            {
                SchoolId   = stuObj.SchoolId,
                MasterId   = stuId,
                AttWay     = attWay,
                AttType    = (byte)attType,
                AttTime    = DateTime.Now,
                AttTimeStr = DateTime.Now.ToString("yyyyMMddHHmmssfff"),
            };

            UnitOfWork.Repository <SYS_StudentAttRecord>().AddEntity(stuAttRecord);
            var addOk = UnitOfWork.CommitAsync().Result;

            if (addOk > 0)
            {
                _mjr.code    = 1;
                _mjr.errMsg  = "";
                _mjr.content = "OK";

                //更新学生当前考勤状态
                stuObj.AttStatus = (byte)currentAttStatus;
                UnitOfWork.Repository <SYS_Student>().UpdateEntity(stuObj);

                if (isSendMsg > 0)
                {
                    //推送考勤消息
                    List <FK_Stu_Parent> fks = UnitOfWork.Repository <FK_Stu_Parent>().GetEntitiesAsync(x => x.SchoolId == stuObj.SchoolId && x.StuId == stuObj.ID).Result;//查找学生家长
                    if (null != fks && fks.Count > 0)
                    {
                        var pWxPInfo  = UnitOfWork.Repository <Wx_PublicInfo>().GetEntitiesAsync(x => x.Type == 1).Result.FirstOrDefault();
                        var wxpInfoId = pWxPInfo == null ? 0 : pWxPInfo.ID;
                        if (school.WxPublicInfoId != null && school.WxPublicInfoId > 0)
                        {
                            wxpInfoId = (int)school.WxPublicInfoId;
                        }
                        var wxPubInfo = UnitOfWork.Repository <Wx_PublicInfo>().GetEntitiesAsync(x => x.ID == wxpInfoId).Result.FirstOrDefault();
                        foreach (var fk in fks)
                        {
                            //推送消息
                            if (null != wxPubInfo)
                            {
                                // 需要从Cookie中获取
                                ModelWxMsg <ModelWmAttendance> wxMsg = new ModelWxMsg <ModelWmAttendance>();
                                wxMsg = JsonFileProvider.Instance.GetSettings <ModelWxMsg <ModelWmAttendance> >();
                                wxMsg.data.first.value = string.Format(wxMsg.data.first.value, (AttType)attType);
                                wxMsg.url    = mWxSetting.PubUrl_Host + "/WxRelated/Center/AttDetail?attId=" + stuAttRecord.ID;
                                wxMsg.touser = fk.OpenId;
                                wxMsg.data.keyword1.value = stuObj.StuName;
                                wxMsg.data.keyword2.value = DateTime.Now.ToString();
                                wxMsg.data.keyword3.value = school.SchoolName;
                                ModelWmResult wmResult = WXOAuthApiHelper.SendTmplMessage(wxPubInfo.AccessToken, wxMsg);
                                if (null == wmResult || wmResult.msgid <= 0)
                                {
                                    LogHelper.Error("推送错误:" + wmResult.errmsg);
                                }
                            }
                        }
                    }
                }
            }
            return(_mjr);
        }
        /// <summary>
        /// 上传考勤数据
        /// </summary>
        /// <param name="token"></param>
        /// <param name="personType">人员类型1:学生,2:职工</param>
        /// <param name="attWay">考勤方式1:刷卡,2:二维码</param>
        public ModelJsonRet UploadAtt(int schoolId, byte personType, long personId, byte attWay, string cardNo, string dateTime, string deviceId, out long attId)
        {
            attId = 0;
            var res = 0;
            //byte[] byteArray = Convert.FromBase64String(token);
            //string tokenStr= Encoding.Default.GetString(byteArray);
            //var schoolId = int.Parse(tokenStr.Split(',')[0]);
            var school = UnitOfWork.Repository <SYS_School>().GetEntitiesAsync(x => x.ID == schoolId && x.Status == 1).Result.FirstOrDefault();

            if (school == null)
            {
                _mjr.errMsg = "验证失败或学校id不存在或该学校已注销";
                return(_mjr);
            }
            var attTime    = DateTime.ParseExact(dateTime, "yyyyMdHHmmssfff", System.Globalization.CultureInfo.InvariantCulture);
            var todayStart = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));

            if (personType == 1)//学生考勤
            {
                var stuObj = UnitOfWork.Repository <SYS_Student>().GetEntitiesAsync(x => x.SchoolId == schoolId && x.ID == personId).Result.FirstOrDefault();
                if (null == stuObj)
                {
                    _mjr.errMsg = "未找到学生ID";
                    return(_mjr);
                }
                var attType          = AttType.签入;
                var currentAttStatus = CurrentAttStatus.在校;
                //判断是签入还是签出
                var todayAttRecords = UnitOfWork.Repository <SYS_StudentAttRecord>().GetEntitiesAsync(x => x.AttTime > todayStart).Result;
                if (todayAttRecords.Count > 0 && todayAttRecords.Count % 2 == 1)
                {
                    attType          = AttType.签出;
                    currentAttStatus = CurrentAttStatus.离校;
                }

                SYS_StudentAttRecord stuAttRecord = new SYS_StudentAttRecord
                {
                    SchoolId   = school.ID,
                    MasterId   = personId,
                    AttWay     = attWay,
                    AttType    = (byte)attType,
                    AttTime    = attTime,
                    AttTimeStr = dateTime,
                };
                UnitOfWork.Repository <SYS_StudentAttRecord>().AddEntity(stuAttRecord);
                var addOk = UnitOfWork.CommitAsync().Result;
                if (addOk > 0)
                {
                    res   = addOk;
                    attId = stuAttRecord.ID;
                    //更新学生当前考勤状态
                    stuObj.AttStatus = (byte)currentAttStatus;
                    UnitOfWork.Repository <SYS_Student>().UpdateEntity(stuObj);

                    //推送考勤消息
                    List <FK_Stu_Parent> fks = UnitOfWork.Repository <FK_Stu_Parent>().GetEntitiesAsync(x => x.SchoolId == stuObj.SchoolId && x.StuId == stuObj.ID).Result;//查找学生家长
                    if (null != fks && fks.Count > 0)
                    {
                        var pWxPInfo  = UnitOfWork.Repository <Wx_PublicInfo>().GetEntitiesAsync(x => x.Type == 1).Result.FirstOrDefault();
                        var wxpInfoId = pWxPInfo == null?0: pWxPInfo.ID;
                        if (school.WxPublicInfoId != null && school.WxPublicInfoId > 0)
                        {
                            wxpInfoId = (int)school.WxPublicInfoId;
                        }
                        var wxPubInfo = UnitOfWork.Repository <Wx_PublicInfo>().GetEntitiesAsync(x => x.ID == wxpInfoId).Result.FirstOrDefault();
                        foreach (var fk in fks)
                        {
                            //推送消息
                            if (null != wxPubInfo)
                            {
                                // 需要从Cookie中获取
                                ModelWxMsg <ModelWmAttendance> wxMsg = new ModelWxMsg <ModelWmAttendance>();
                                wxMsg = JsonFileProvider.Instance.GetSettings <ModelWxMsg <ModelWmAttendance> >();
                                wxMsg.data.first.value = string.Format(wxMsg.data.first.value, (AttType)attType);
                                wxMsg.url    = mWxSetting.PubUrl_Host + "/WxRelated/Center/AttDetail?attId=" + stuAttRecord.ID;
                                wxMsg.touser = fk.OpenId;
                                wxMsg.data.keyword1.value = stuObj.StuName;
                                wxMsg.data.keyword2.value = attTime.ToString();
                                wxMsg.data.keyword3.value = school.SchoolName;
                                ModelWmResult wmResult = WXOAuthApiHelper.SendTmplMessage(wxPubInfo.AccessToken, wxMsg);
                                if (null == wmResult || wmResult.msgid <= 0)
                                {
                                    LogHelper.Error("推送错误:" + wmResult.errmsg);
                                }
                            }
                        }
                    }
                }
            }
            else if (personType == 2)//职工考勤
            {
                var staffObj = UnitOfWork.Repository <SYS_Staff>().GetEntitiesAsync(x => x.SchoolId == schoolId && x.ID == personId).Result.FirstOrDefault();
                if (null == staffObj)
                {
                    _mjr.errMsg = "未找到职工ID";
                    return(_mjr);
                }
                var attType          = AttType.签入;
                var currentAttStatus = CurrentAttStatus.在校;
                //判断是签入还是签出

                var todayAttRecords = UnitOfWork.Repository <SYS_StaffAttRecord>().GetEntitiesAsync(x => x.AttTime > todayStart).Result.ToList();
                if (todayAttRecords.Count > 0 && todayAttRecords.Count % 2 == 1)
                {
                    attType          = AttType.签出;
                    currentAttStatus = CurrentAttStatus.离校;
                }
                SYS_StaffAttRecord stuAttRecord = new SYS_StaffAttRecord
                {
                    SchoolId   = school.ID,
                    MasterId   = (int)personId,
                    AttWay     = attWay,
                    AttType    = (byte)attType,
                    AttTime    = attTime,
                    AttTimeStr = dateTime,
                };
                UnitOfWork.Repository <SYS_StaffAttRecord>().AddEntity(stuAttRecord);
                var addOk = UnitOfWork.CommitAsync().Result;
                if (addOk > 0)
                {
                    res   = addOk;
                    attId = stuAttRecord.ID;
                    //更新当前考勤状态
                    staffObj.AttStatus = (byte)currentAttStatus;
                    UnitOfWork.Repository <SYS_Staff>().UpdateEntity(staffObj);

                    //推送考勤消息
                    var pWxPInfo  = UnitOfWork.Repository <Wx_PublicInfo>().GetEntitiesAsync(x => x.Type == 1).Result.FirstOrDefault();
                    var wxpInfoId = pWxPInfo == null ? 0 : pWxPInfo.ID;
                    if (school.WxPublicInfoId != null && school.WxPublicInfoId > 0)
                    {
                        wxpInfoId = (int)school.WxPublicInfoId;
                    }
                    var wxPubInfo = UnitOfWork.Repository <Wx_PublicInfo>().GetEntitiesAsync(x => x.ID == wxpInfoId).Result.FirstOrDefault();
                    if (null != wxPubInfo && staffObj.OpenId != null && staffObj.OpenId.Length > 6)
                    {
                        // 需要从Cookie中获取
                        ModelWxMsg <ModelWmAttendance> wxMsg = new ModelWxMsg <ModelWmAttendance>();
                        wxMsg = JsonFileProvider.Instance.GetSettings <ModelWxMsg <ModelWmAttendance> >();
                        wxMsg.data.first.value = (AttType)attType + " 成功";
                        //wxMsg.url = mWxSetting.PubUrl_Host + "/WxRelated/Center/AttDetail?attId=" + stuAttRecord.ID;
                        wxMsg.touser = staffObj.OpenId;
                        wxMsg.data.keyword1.value = staffObj.StaffName;
                        wxMsg.data.keyword2.value = attTime.ToString();
                        wxMsg.data.keyword3.value = school.SchoolName;
                        ModelWmResult wmResult = WXOAuthApiHelper.SendTmplMessage(wxPubInfo.AccessToken, wxMsg);
                        if (null == wmResult || wmResult.msgid <= 0)
                        {
                            LogHelper.Error("推送错误:" + wmResult.errmsg);
                        }
                    }
                }
            }
            if (res > 0)
            {
                _mjr.code    = 1;
                _mjr.content = "ok";
                //RefreshEntities("stu");
                //RefreshEntities("staff");
            }
            return(_mjr);
        }