Пример #1
0
        public JsonResult GetByPhongBan(string id, string kcn)
        {
            try
            {
                var watch         = System.Diagnostics.Stopwatch.StartNew();
                var elapsedMs     = watch.ElapsedMilliseconds;
                var khoichucnangE = new KhoiChucNang();
                //if (!string.IsNullOrEmpty(kcn))
                //{
                //    khoichucnangE = dbContext.KhoiChucNangs.Find(m => m.Enable.Equals(true) && m.Id.Equals(kcn)).FirstOrDefault();
                //    if (khoichucnangE == null)
                //    {
                //        khoichucnangE = dbContext.KhoiChucNangs.Find(m => m.Enable.Equals(true)).FirstOrDefault();
                //    }
                //}
                //else
                //{
                //    khoichucnangE = dbContext.KhoiChucNangs.Find(m => m.Enable.Equals(true)).FirstOrDefault();
                //}

                //var phongbans = dbContext.PhongBans.Find(m => m.Enable.Equals(true) && m.KhoiChucNangId.Equals(khoichucnangE.Id)).SortBy(m => m.Order).ToList();
                var phongBanE = new PhongBan();
                if (!string.IsNullOrEmpty(id))
                {
                    phongBanE = dbContext.PhongBans.Find(m => m.Id.Equals(id)).FirstOrDefault();
                    if (phongBanE != null)
                    {
                        var phongbans = dbContext.PhongBans.Find(m => m.KhoiChucNangId.Equals(phongBanE.KhoiChucNangId)).ToList();

                        var bophans = dbContext.BoPhans.Find(m => m.Enable.Equals(true) && m.PhongBanId.Equals(phongBanE.Id)).ToList();

                        var managers = dbContext.ChucVus.Find(m => m.Enable.Equals(true) && m.PhongBanId.Equals(phongBanE.Id)).ToList();

                        watch.Stop();
                        elapsedMs = watch.ElapsedMilliseconds;
                        return(Json(new { elapsedMs = elapsedMs + "ms", result = true, phongban = phongBanE, phongbans, bophans, managers }));
                    }
                }

                var lastestE    = dbContext.PhongBans.Find(m => m.Enable.Equals(true)).SortByDescending(m => m.Order).Limit(1).FirstOrDefault();
                var lastestCode = lastestE != null ? lastestE.Order + 1 : 1;
                phongBanE = new PhongBan()
                {
                    Order  = lastestCode,
                    Enable = true
                };
                watch.Stop();
                elapsedMs = watch.ElapsedMilliseconds;
                return(Json(new { elapsedMs = elapsedMs + "ms", result = true, phongban = phongBanE }));
            }
            catch (Exception ex)
            {
                return(Json(new { result = false, message = ex.Message }));
            }
        }
Пример #2
0
        public IActionResult KhoiChucNang(KhoiChucNang entity)
        {
            try
            {
                if (string.IsNullOrEmpty(entity.Name))
                {
                    return(Json(new { result = false, source = "create", entity, message = "Tên không để trống! Vui lòng kiểm tra tên." }));
                }

                entity.Alias = Utility.AliasConvert(entity.Name);
                if (string.IsNullOrEmpty(entity.Id))
                {
                    var existE = dbContext.KhoiChucNangs.Find(m => m.Alias.Equals(entity.Alias)).FirstOrDefault();
                    if (existE == null)
                    {
                        //var lastestE = dbContext.KhoiChucNangs.Find(m => m.Enable.Equals(true)).SortByDescending(m => m.Order).Limit(1).FirstOrDefault();
                        //var lastestCode = lastestE != null ? lastestE.Order + 1 : 1;
                        entity.Code  = "KHOI" + entity.Order;
                        entity.Order = entity.Order;
                        dbContext.KhoiChucNangs.InsertOne(entity);
                        return(Json(new { result = true, source = "create", entity, message = Constants.DataSuccess }));
                    }
                    entity.Id = existE.Id;
                }

                var filter = Builders <KhoiChucNang> .Filter.Eq(m => m.Id, entity.Id);

                var update = Builders <KhoiChucNang> .Update
                             .Set(m => m.CongTyChiNhanhId, entity.CongTyChiNhanhId)
                             .Set(m => m.Name, entity.Name)
                             .Set(m => m.Alias, entity.Alias)
                             .Set(m => m.Description, entity.Description)
                             .Set(m => m.Order, entity.Order)
                             .Set(m => m.Code, "KHOI" + entity.Order)
                             .Set(m => m.Enable, entity.Enable);

                dbContext.KhoiChucNangs.UpdateOne(filter, update);

                #region Relations
                var filterEmployee = Builders <Employee> .Filter.Eq(m => m.KhoiChucNang, entity.Id);

                var updateEmployee = Builders <Employee> .Update
                                     .Set(m => m.KhoiChucNangName, entity.Name);

                dbContext.Employees.UpdateMany(filterEmployee, updateEmployee);
                #endregion

                return(Json(new { result = true, source = "create", entity, message = Constants.DataSuccess }));
            }
            catch (Exception ex)
            {
                return(Json(new { result = false, source = "create", entity, message = ex.Message }));
            }
        }