Ejemplo n.º 1
0
        /// <summary>
        /// 设置model,如果不为空就设置,如果为空则和之前一样
        /// </summary>
        /// <param name="target"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        public GuYuanKaoQin SetGuYuanKaoQin(GuYuanKaoQin model, GuYuanKaoQinReq data)
        {
            if (data.guyuanId != null)
            {
                model.guyuanId = data.guyuanId.Value;
            }
            if (!string.IsNullOrEmpty(data.guyuanName))
            {
                model.guyuanName = data.guyuanName;
            }
            if (data.workDate != null && data.workDate != DateTime.MinValue && data.workDate != SqlDateTime.MinValue.Value)
            {
                model.workDate = data.workDate.Value;
            }
            if (!string.IsNullOrEmpty(data.checkType))
            {
                model.checkType = data.checkType;
            }
            if (!string.IsNullOrEmpty(data.checkResult))
            {
                model.checkResult = data.checkResult;
            }
            if (data.checkTime != null && data.checkTime != DateTime.MinValue && data.checkTime != SqlDateTime.MinValue.Value)
            {
                model.checkTime = data.checkTime.Value;
            }

            return(model);
        }
Ejemplo n.º 2
0
        public GuYuanKaoQin UpdateSingle(int id, GuYuanKaoQinReq data)
        {
            GuYuanKaoQin model = db.GuYuanKaoQin.Find(id);

            SetGuYuanKaoQin(model, data);
            db.SaveChanges();
            return(model);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 同步考勤数据
        /// </summary>
        public void SyncKaoQin()
        {
            //获取上个月第一天
            var dateFrom = DateTime.Today;

            dateFrom = new DateTime(dateFrom.Year, dateFrom.Month, 1).AddDays(-1);
            dateFrom = new DateTime(dateFrom.Year, dateFrom.Month, 1);

            var config = db.DingTalkKaoQin.FirstOrDefault(n => n.id == 0);

            if (config == null)
            {
                config = new DingTalkKaoQin {
                    id = 0, workDate = dateFrom
                };
                db.DingTalkKaoQin.Add(config);
            }
            else
            {
                if (config.workDate > dateFrom)
                {
                    //前次同步的最后一天可以重复,防止数据不完整
                    dateFrom = config.workDate.Value.Date;
                }
            }

            //-------获取钉钉数据(每次只能取7天范围内的)-----------------

            var unixExpoch = new DateTime(1970, 1, 1).AddHours(8);
            var corpClient = GetCorpClient();

            for (var i = 0; i < 10 && dateFrom < DateTime.Today; i++)
            {
                var dateTo = dateFrom.AddDays(6);

                if (dateTo >= DateTime.Today)
                {
                    dateTo = DateTime.Today.AddDays(-1);
                }

                var results = corpClient.GetAttendanceList(dateFrom, dateTo).OrderBy(n => n.id);

                foreach (var r in results)
                {
                    if (db.DingTalkKaoQin.Any(n => n.id == r.id))
                    {
                        continue;
                    }

                    switch (r.checkType)
                    {
                    case "OnDuty":
                        r.checkType = "上班";
                        break;

                    case "OffDuty":
                        r.checkType = "下班";
                        break;
                    }
                    switch (r.timeResult)
                    {
                    case "Normal":
                        r.timeResult = "正常";
                        break;

                    case "Early":
                        r.timeResult = "早退";
                        break;

                    case "Late":
                        r.timeResult = "迟到";
                        break;

                    case "SeriousLate":
                        r.timeResult = "严重迟到";
                        break;

                    case "NotSigned":
                        r.timeResult = "未打卡";
                        break;
                    }
                    switch (r.locationResult)
                    {
                    case "Normal":
                        r.locationResult = "范围内";
                        break;

                    case "Outside":
                        r.locationResult = "范围外";
                        break;

                    case "NotSigned":
                        r.locationResult = "未打卡";
                        break;
                    }
                    //---钉钉考勤---
                    var dkqData = new DingTalkKaoQin
                    {
                        id             = r.id,
                        approveId      = r.approveId,
                        approveResult  = r.approveResult,
                        baseCheckTime  = unixExpoch.AddMilliseconds(r.baseCheckTime),
                        checkType      = r.checkType,
                        groupId        = r.groupId,
                        locationResult = r.locationResult,
                        planId         = r.planId,
                        recordId       = r.recordId,
                        sourceType     = r.sourceType,
                        timeResult     = r.timeResult,
                        userCheckTime  = unixExpoch.AddMilliseconds(r.userCheckTime),
                        userId         = r.userId,
                        workDate       = unixExpoch.AddMilliseconds(r.workDate)
                    };
                    db.DingTalkKaoQin.Add(dkqData);

                    //---考勤雇员---
                    var query = from a in db.GuYuanUser
                                join b in db.DingTalkUser on a.Guid equals b.Guid
                                where b.userid == r.userId
                                select a;
                    var gyUser = query.FirstOrDefault() ?? SyncUser(r.userId);

                    //---雇员考勤表---
                    var gykqData = new GuYuanKaoQin
                    {
                        checkResult = r.timeResult,
                        checkTime   = dkqData.userCheckTime,
                        checkType   = r.checkType,
                        guyuanId    = gyUser.id,
                        guyuanName  = gyUser.Name,
                        workDate    = dkqData.workDate,
                        externalId  = r.id.ToString()
                    };

                    db.GuYuanKaoQin.Add(gykqData);
                }

                config.workDate = dateTo;
                db.SaveChanges();

                dateFrom = dateTo.AddDays(1);
            }
        }