コード例 #1
0
        protected override void BeforeSave(Dictionary <string, string> dic, Base.Logic.Domain.S_UI_Form formInfo, bool isNew)
        {
            var list        = new List <Dictionary <string, object> >();
            var id          = dic.GetValue("ID");
            var normalValue = 0m;

            if (!string.IsNullOrEmpty(dic.GetValue("NormalValue")))
            {
                normalValue = Convert.ToDecimal(dic.GetValue("NormalValue"));
            }
            var extraValue = 0m;

            if (!string.IsNullOrEmpty(dic.GetValue("AdditionalValue")))
            {
                extraValue = Convert.ToDecimal(dic.GetValue("AdditionalValue"));
            }
            if ((normalValue == 0 && extraValue == 0))
            {
                throw new Formula.Exceptions.BusinessException("每日工时不能为0");
            }
            var user     = FormulaHelper.GetUserInfoByID(dic.GetValue("UserID"));
            var employee = this.ComprehensiveDbContext.Set <S_HR_Employee>().FirstOrDefault(d => d.UserID == user.UserID);
            var fo       = FormulaHelper.CreateFO <WorkHourFO>();
            //根据日期段判断
            var startDate      = dic.GetValue("WorkHourDateStart").Replace("T", " ");
            var endDate        = dic.GetValue("WorkHourDateEnd").Replace("T", " ");
            var projectID      = dic.GetValue("ProjectID");
            var WorkTimeMajor  = dic.GetValue("WorkTimeMajor");
            var SubProjectCode = dic.GetValue("SubProjectCode");
            var MajorCode      = dic.GetValue("MajorCode");
            var WorkContent    = dic.GetValue("WorkContent");
            var dateList       = getWorkHourDate(startDate, endDate);
            //已存在工时数据
            var userID    = dic.GetValue("UserID");
            var existList = this.ComprehensiveDbContext.Set <S_W_UserWorkHour>().Where(a => a.UserID == userID && dateList.Contains(a.WorkHourDate)).ToList();

            foreach (var item in dateList)
            {
                var existNormalValue = existList.Where(a => a.WorkHourDate == item && a.SupplementID != id).Sum(a => a.NormalValue);
                var existExtraValue  = existList.Where(a => a.WorkHourDate == item && a.SupplementID != id).Sum(a => a.AdditionalValue);
                if (!existNormalValue.HasValue)
                {
                    existNormalValue = 0;
                }
                if (!existExtraValue.HasValue)
                {
                    existExtraValue = 0;
                }
                //自动补全正班工日,加班工日不补全
                if ((normalValue + existNormalValue) > NormalHoursMax)
                {
                    normalValue = NormalHoursMax - existNormalValue.Value;
                }
                //throw new Formula.Exceptions.BusinessException("【" + item.ToShortDateString() + "】正常工时不能大于" + NormalHoursMax.ToString());
                if ((extraValue + existExtraValue) > maxExtraHour)
                {
                    throw new Formula.Exceptions.BusinessException("【" + item.ToShortDateString() + "】加班工时不能大于" + maxExtraHour.ToString());
                }

                if ((normalValue == 0 && extraValue == 0))
                {
                    continue;
                }

                var userWorkHour = existList.FirstOrDefault(d => d.SupplementID == id &&
                                                            d.UserID == user.UserID && d.WorkHourDate == item);
                if (userWorkHour == null || ComprehensiveDbContext.Entry <S_W_UserWorkHour>(userWorkHour).State == System.Data.EntityState.Deleted)
                {
                    userWorkHour              = ComprehensiveDbContext.Set <S_W_UserWorkHour>().Create();
                    userWorkHour.ID           = FormulaHelper.CreateGuid();
                    userWorkHour.UserID       = user.UserID;
                    userWorkHour.UserName     = user.UserName;
                    userWorkHour.UserDeptID   = user.UserOrgID;
                    userWorkHour.UserDeptName = user.UserOrgName;
                    userWorkHour.WorkHourDate = item;
                    userWorkHour.UserCode     = user.Code;
                    if (employee != null)
                    {
                        userWorkHour.EmployeeID = employee.ID;
                    }
                    userWorkHour.State         = "Create";
                    userWorkHour.IsConfirm     = "0";
                    userWorkHour.IsStep1       = "0";
                    userWorkHour.IsStep2       = "0";
                    userWorkHour.CreateUser    = CurrentUserInfo.UserName;
                    userWorkHour.CreateUserID  = CurrentUserInfo.UserID;
                    userWorkHour.SupplementID  = id;
                    userWorkHour.BelongMonth   = item.Month;
                    userWorkHour.BelongYear    = item.Year;
                    userWorkHour.BelongQuarter = ((item.Month - 1) / 3) + 1;
                    ComprehensiveDbContext.Set <S_W_UserWorkHour>().Add(userWorkHour);
                }
                if (userWorkHour.State != "Create")
                {
                    continue;
                }
                userWorkHour.ProjectChargerUser     = dic.GetValue("ProjectChargerUser");
                userWorkHour.ProjectChargerUserName = dic.GetValue("ProjectChargerUserName");
                userWorkHour.ProjectCode            = dic.GetValue("ProjectCode");
                userWorkHour.ProjectDept            = dic.GetValue("ProjectDept");
                userWorkHour.ProjectDeptName        = dic.GetValue("ProjectDeptName");
                userWorkHour.ProjectID       = projectID;
                userWorkHour.ProjectName     = dic.GetValue("ProjectName");
                userWorkHour.WorkTimeMajor   = dic.GetValue("WorkTimeMajor");
                userWorkHour.SubProjectCode  = dic.GetValue("SubProjectCode");
                userWorkHour.SubProjectName  = dic.GetValue("SubProjectName");
                userWorkHour.TaskWorkCode    = dic.GetValue("TaskWorkCode");
                userWorkHour.TaskWorkName    = dic.GetValue("TaskWorkName");
                userWorkHour.WorkContent     = dic.GetValue("WorkContent");
                userWorkHour.MajorCode       = dic.GetValue("MajorCode");
                userWorkHour.MajorName       = dic.GetValue("MajorName");
                userWorkHour.WorkHourType    = dic.GetValue("WorkHourType");
                userWorkHour.NormalValue     = normalValue;
                userWorkHour.AdditionalValue = extraValue;
                userWorkHour.WorkHourValue   = normalValue + extraValue;
                userWorkHour.WorkHourDay     = fo.ConvertHourToDay(userWorkHour.WorkHourValue, workHourType, NormalHoursMax);
            }
        }