Beispiel #1
0
        public void Run()
        {
            string        log            = String.Empty;
            ReplaceString replaceString  = new ReplaceString();
            DirectoryInfo dir            = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent;
            string        dirPath        = dir.FullName;
            string        templateReport = System.IO.Path.Combine(dirPath, @"Json\Province.json");

            using (StreamReader r = new StreamReader(templateReport))
            {
                string          json  = r.ReadToEnd();
                List <Province> items = JsonConvert.DeserializeObject <List <Province> >(json);
                foreach (var item in items)
                {
                    using (var context = new quickbeedev1Context())
                    {
                        var provine = (from p in context.Provinces
                                       select new
                        {
                            Id = p.Id,
                            Name = replaceString.Replace(p.Name, "PROVINCE")
                        }).ToList();

                        var provinceObj = provine.Where(x => x.Name == replaceString.Replace(item.PROVINCE_NAME, "PROVINCE")).FirstOrDefault();

                        if (provinceObj != null)
                        {
                            var translators = new Translators()
                            {
                                DataKey         = "VTP.PROVINCE",
                                OriginalValue   = Convert.ToString(provinceObj.Id),
                                TranslatedValue = Convert.ToString(item.PROVINCE_ID)
                            };
                            var provineExists = context.Translators.Where(x => x.DataKey == translators.DataKey &&
                                                                          x.OriginalValue == translators.OriginalValue && x.TranslatedValue == translators.TranslatedValue);
                            if (provineExists.Count() == 0)
                            {
                                context.Translators.Add(translators);
                                context.SaveChanges();
                                log += provinceObj.Name + "-" + item.PROVINCE_NAME + "\n";
                            }
                        }
                        else
                        {
                            string a        = replaceString.Replace(item.PROVINCE_NAME, "PROVINCE");
                            var    province = new Provinces()
                            {
                                Name         = item.PROVINCE_NAME,
                                ProvinceType = "Tỉnh",
                                CountryId    = 256
                            };
                            context.Provinces.Add(province);
                            context.SaveChanges();
                            var translators = new Translators()
                            {
                                DataKey         = "VTP.PROVINCE",
                                OriginalValue   = Convert.ToString(province.Id),
                                TranslatedValue = Convert.ToString(item.PROVINCE_ID)
                            };
                            context.Translators.Add(translators);
                            context.SaveChanges();
                            log += provinceObj.Name + "-" + item.PROVINCE_NAME + "\n";
                        }
                    }
                }
                Console.WriteLine(log);
            }
        }
Beispiel #2
0
        public void Run()
        {
            string        log            = String.Empty;
            ReplaceString replaceString  = new ReplaceString();
            DirectoryInfo dir            = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent;
            string        dirPath        = dir.FullName;
            string        templateReport = System.IO.Path.Combine(dirPath, @"Json\District.json");

            using (StreamReader r = new StreamReader(templateReport))
            {
                string          json  = r.ReadToEnd();
                List <District> items = JsonConvert.DeserializeObject <List <District> >(json);
                using (var context = new quickbeedev1Context())
                {
                    var translator = context.Translators.Where(x => x.DataKey == "VTP.PROVINCE").ToList();
                    foreach (var i in translator)
                    {
                        int?   provinceOriginalId = Convert.ToInt32(i.OriginalValue);
                        string provinceJsId       = i.TranslatedValue;
                        //List DistrictJS
                        var districtJsList       = items.Where(x => x.PROVINCE_ID == provinceJsId);
                        var districtOriginalList = (from d in context.Districts.Where(x => x.ProvinceId == provinceOriginalId)
                                                    select new
                        {
                            Id = d.Id,
                            Name = replaceString.Replace(d.Name, "DISTRICT")
                        }).ToList();

                        foreach (var itemD in districtJsList)
                        {
                            var districtOriginalObj = districtOriginalList.Where(x => x.Name == replaceString.Replace(itemD.DISTRICT_NAME, "DISTRICT")).FirstOrDefault();
                            if (districtOriginalObj != null)
                            {
                                var translators = new Translators()
                                {
                                    DataKey         = "VTP.DISTRICT",
                                    OriginalValue   = Convert.ToString(districtOriginalObj.Id),
                                    TranslatedValue = Convert.ToString(itemD.DISTRICT_ID)
                                };
                                var districtExists = context.Translators.Where(x => x.DataKey == translators.DataKey &&
                                                                               x.TranslatedValue == translators.TranslatedValue);
                                if (districtExists.Count() == 0)
                                {
                                    context.Translators.Add(translators);
                                    context.SaveChanges();
                                    log += districtOriginalObj.Name + "-" + itemD.DISTRICT_NAME + "\n";
                                }
                            }
                            else
                            {
                                var districtNew = new Districts()
                                {
                                    Name       = replaceString.ConverToUpper(itemD.DISTRICT_NAME),
                                    ProvinceId = provinceOriginalId ?? 0
                                };

                                context.Districts.Add(districtNew);
                                context.SaveChanges();
                                var translators = new Translators()
                                {
                                    DataKey         = "VTP.DISTRICT",
                                    OriginalValue   = Convert.ToString(districtNew.Id),
                                    TranslatedValue = provinceJsId
                                };
                                context.Translators.Add(translators);
                                context.SaveChanges();
                                log += itemD.DISTRICT_NAME + "\n";
                            }
                        }
                    }
                    Console.WriteLine(log);
                }
            }
        }