public IActionResult Post([FromBody] InspectionPlanViewModel model)
        {
            var userdata = _context.Users
                           .Where(m => m.Id == model.UserID)
                           //.Select(m => m.Role_id)
                           .FirstOrDefault();

            System.Console.WriteLine("FiscalYearId" + model.FiscalYearId);
            var year = _context.FiscalYearNew
                       .Where(m => m.Year == model.FiscalYearId).FirstOrDefault();

            System.Console.WriteLine("year" + year.Id);
            //if(year == null)
            //{

            //}
            //var test = model.UserID;
            //System.Console.WriteLine(test);
            System.Console.WriteLine("111");
            var date = DateTime.Now;

            System.Console.WriteLine("222" + model.Type);
            var centralpolicydata = new CentralPolicy
            {
                Title = model.Title,
                TypeexaminationplanId = 3,
                FiscalYearNewId       = year.Id,
                StartDate             = model.StartDate,
                EndDate   = model.EndDate,
                Status    = model.Status,
                CreatedAt = date,
                CreatedBy = model.UserID,
                Class     = "แผนการตรวจ",
            };

            System.Console.WriteLine("3");
            _context.CentralPolicies.Add(centralpolicydata);
            _context.SaveChanges();
            System.Console.WriteLine("4");
            //foreach (var id in model.ProvinceId)
            //{
            var centralpolicyprovincedata = new CentralPolicyProvince
            {
                ProvinceId      = model.ProvinceId,
                CentralPolicyId = centralpolicydata.Id,
                Step            = "มอบหมายหน่วยงาน",
                Status          = "ร่างกำหนดการ"
            };

            _context.CentralPolicyProvinces.Add(centralpolicyprovincedata);
            _context.SaveChanges();

            ///////////////////////////////
            var SubjectGroupdata = new SubjectGroup
            {
                CentralPolicyId  = centralpolicydata.Id,
                ProvinceId       = model.ProvinceId,
                Type             = "Master",
                Land             = "Master",
                Status           = "Master",
                StatusSuggestion = "ร่างกำหนดการ",

                ProvincialDepartmentIdCreatedBy = userdata.ProvincialDepartmentId,
                CreatedBy     = userdata.Id,
                RoleCreatedBy = userdata.Role_id,
            };

            _context.SubjectGroups.Add(SubjectGroupdata);
            _context.SaveChanges();
            ///////////////////////////////
            ///
            var subjectdata = new SubjectCentralPolicyProvince
            {
                Name = model.Title,
                CentralPolicyProvinceId = centralpolicyprovincedata.Id,
                Type           = "Master",
                Status         = "ใช้งานจริง",
                SubjectGroupId = SubjectGroupdata.Id,
                CreatedBy      = model.UserID,
            };

            _context.SubjectCentralPolicyProvinces.Add(subjectdata);
            _context.SaveChanges();

            //var inspectionplaneventdata = new InspectionPlanEvent
            //{
            //    StartDate = model.StartDate,
            //    EndDate = model.EndDate,
            //    ProvinceId = model.ProvinceId,
            //    CreatedAt = date,
            //    CreatedBy = model.UserID,
            //};
            //_context.InspectionPlanEvents.Add(inspectionplaneventdata);
            //_context.SaveChanges();

            //var ElectronicBookdata = new ElectronicBook
            //{
            //    CreatedBy = model.UserID,
            //    Status = "ร่างกำหนดการ",
            //};
            //_context.ElectronicBooks.Add(ElectronicBookdata);
            //_context.SaveChanges();
            //System.Console.WriteLine("3");

            //var ElectronicBookGroupdata = new ElectronicBookGroup
            //{
            //    // CentralPolicyProvinceId = centralpolicyprovincedata.Id,
            //    ElectronicBookId = ElectronicBookdata.Id,
            //};
            //_context.ElectronicBookGroups.Add(ElectronicBookGroupdata);
            //_context.SaveChanges();

            var centralpolicyeventdata = new CentralPolicyEvent
            {
                CentralPolicyId       = centralpolicydata.Id,
                InspectionPlanEventId = model.InspectionPlanEventId,
                HaveSubject           = 0,
                StartDate             = model.StartDate,
                EndDate = model.EndDate,
                //ElectronicBookId = ElectronicBookdata.Id,
            };

            _context.CentralPolicyEvents.Add(centralpolicyeventdata);
            _context.SaveChanges();

            var logdata = new Log
            {
                UserId       = model.UserID,
                DatabaseName = "CentralPolicyEvent",
                EventType    = "เพิ่ม",
                EventDate    = date,
                Detail       = "เพิ่มแผนตรวจราชการในกำหนดการตรวจราชการ",
                Allid        = centralpolicyeventdata.Id,
            };

            _context.Logs.Add(logdata);
            _context.SaveChanges();
            //}
            return(Ok(new { status = true }));
        }
Esempio n. 2
0
        public async Task <IActionResult> Post([FromForm] CentralPolicyProvinceViewModel model)
        {
            //var eiei = model.StartDate;
            //System.Console.WriteLine("test: " + eiei);
            //return Ok(model);
            var date = DateTime.Now;
            var centralpolicydata = new CentralPolicy
            {
                Title        = model.Title,
                Type         = model.Type,
                FiscalYearId = model.FiscalYearId,
                StartDate    = model.StartDate,
                EndDate      = model.EndDate,
                Status       = model.Status,
                CreatedAt    = date,
                CreatedBy    = model.UserID,
                Class        = model.Class,
            };

            _context.CentralPolicies.Add(centralpolicydata);
            _context.SaveChanges();

            foreach (var id in model.ProvinceId)
            {
                var centralpolicyprovincedata = new CentralPolicyProvince
                {
                    ProvinceId      = id,
                    CentralPolicyId = centralpolicydata.Id,
                    Step            = "มอบหมายเขต"
                };
                _context.CentralPolicyProvinces.Add(centralpolicyprovincedata);
            }
            _context.SaveChanges();

            int index    = 0;
            int indexend = 0;

            foreach (var item in model.StartDate2)
            {
                var CentralPolicyDate = new CentralPolicyDate
                {
                    CentralPolicyId = centralpolicydata.Id,
                    StartDate       = item,
                    //EndDate = item.EndDate,
                };
                _context.CentralPolicyDates.Add(CentralPolicyDate);
                _context.SaveChanges();

                var id = _context.CentralPolicyDates.Find(CentralPolicyDate.Id);

                indexend = 0;

                foreach (var itemend in model.EndDate2)
                {
                    if (index == indexend)
                    {
                        id.EndDate = itemend;

                        System.Console.WriteLine("END: " + indexend);
                    }
                    indexend++;
                }

                index++;

                System.Console.WriteLine("Start: " + index);
            }



            //int maxSize = Int32.Parse(ConfigurationManager.AppSettings["MaxFileSize"]);
            //var size = data.files.Sum(f => f.Length);

            //ตรวจสอบว่ามี Folder Upload ใน wwwroot มั้ย
            if (!Directory.Exists(_environment.WebRootPath + "//Uploads//"))
            {
                Directory.CreateDirectory(_environment.WebRootPath + "//Uploads//"); //สร้าง Folder Upload ใน wwwroot
            }

            //var BaseUrl = url.ActionContext.HttpContext.Request.Scheme;
            // path ที่เก็บไฟล์
            var filePath = _environment.WebRootPath + "//Uploads//";


            if (model.files != null)
            {
                foreach (var formFile in model.files.Select((value, index) => new { Value = value, Index = index }))
                //foreach (var formFile in data.files)
                {
                    var    random    = RandomString(10);
                    string filePath2 = formFile.Value.FileName;
                    string filename  = Path.GetFileName(filePath2);
                    string ext       = Path.GetExtension(filename);

                    if (formFile.Value.Length > 0)
                    {
                        // using (var stream = System.IO.File.Create(filePath + formFile.Value.FileName))
                        using (var stream = System.IO.File.Create(filePath + random + filename))
                        {
                            await formFile.Value.CopyToAsync(stream);
                        }

                        var CentralPolicyFile = new CentralPolicyFile
                        {
                            CentralPolicyId = centralpolicydata.Id,
                            Name            = random + filename,
                        };
                        _context.CentralPolicyFiles.Add(CentralPolicyFile);
                        _context.SaveChanges();
                    }
                }
            }
            return(Ok(centralpolicydata.Id));
        }
Esempio n. 3
0
        public void Post([FromBody] InspectionPlanViewModel model)
        {
            //var test = model.UserID;
            //System.Console.WriteLine(test);
            System.Console.WriteLine("1" + model.Title);
            var date = DateTime.Now;

            System.Console.WriteLine("2" + model.Type);
            var centralpolicydata = new CentralPolicy
            {
                Title        = model.Title,
                Type         = model.Type,
                FiscalYearId = model.FiscalYearId,
                StartDate    = model.StartDate,
                EndDate      = model.EndDate,
                Status       = model.Status,
                CreatedAt    = date,
                CreatedBy    = model.UserID,
                Class        = "แผนการตรวจ",
            };

            System.Console.WriteLine("3");
            _context.CentralPolicies.Add(centralpolicydata);
            _context.SaveChanges();
            System.Console.WriteLine("4");
            //foreach (var id in model.ProvinceId)
            //{
            var centralpolicyprovincedata = new CentralPolicyProvince
            {
                ProvinceId      = model.ProvinceId,
                CentralPolicyId = centralpolicydata.Id,
            };

            _context.CentralPolicyProvinces.Add(centralpolicyprovincedata);
            _context.SaveChanges();

            //var inspectionplaneventdata = new InspectionPlanEvent
            //{
            //    StartDate = model.StartDate,
            //    EndDate = model.EndDate,
            //    ProvinceId = model.ProvinceId,
            //    CreatedAt = date,
            //    CreatedBy = model.UserID,
            //};
            //_context.InspectionPlanEvents.Add(inspectionplaneventdata);
            //_context.SaveChanges();
            var ElectronicBookdata = new ElectronicBook
            {
                CreatedBy = model.UserID,
                Status    = "ร่างกำหนดการ",
            };

            _context.ElectronicBooks.Add(ElectronicBookdata);
            _context.SaveChanges();
            System.Console.WriteLine("3");

            var ElectronicBookGroupdata = new ElectronicBookGroup
            {
                CentralPolicyProvinceId = centralpolicyprovincedata.Id,
                ElectronicBookId        = ElectronicBookdata.Id,
            };

            _context.ElectronicBookGroups.Add(ElectronicBookGroupdata);
            _context.SaveChanges();

            var centralpolicyeventdata = new CentralPolicyEvent
            {
                CentralPolicyId       = centralpolicydata.Id,
                InspectionPlanEventId = model.InspectionPlanEventId,
                ElectronicBookId      = ElectronicBookdata.Id,
            };

            _context.CentralPolicyEvents.Add(centralpolicyeventdata);
            _context.SaveChanges();
            //}
        }