public int Save(GrupCustomer obj)
        {
            var result = 0;

            if (_isUseWebAPI)
            {
                //obj.GrupCustomer_id = Guid.NewGuid().ToString();

                //_unitOfWork = new UnitOfWork(_isUseWebAPI, _baseUrl, _log);
                //result = _unitOfWork.GrupCustomerRepository.Save(obj);
            }
            else
            {
                using (IDapperContext context = new DapperContext())
                {
                    GrupCustomerRepository repo = new GrupCustomerRepository(context, _log);
                    result = repo.Save(obj);
                }
            }

            return(result);
        }
Beispiel #2
0
        public bool Import(string workSheetName, ref int rowCount)
        {
            var result = false;

            try
            {
                var ws = _workbook.Worksheet(workSheetName);

                // Look for the first row used
                var firstRowUsed = ws.FirstRowUsed();

                // Narrow down the row so that it only includes the used part
                var golonganRow = firstRowUsed.RowUsed();

                // First possible address of the company table:
                var firstPossibleAddress = ws.Row(golonganRow.RowNumber()).FirstCell().Address;

                // Last possible address of the company table:
                var lastPossibleAddress = ws.LastCellUsed().Address;

                // Get a range with the remainder of the worksheet data (the range used)
                var golonganRange = ws.Range(firstPossibleAddress, lastPossibleAddress).RangeUsed();

                // Treat the range as a table (to be able to use the column names)
                var grupCustomerTable = golonganRange.AsTable();

                var listOfGrupCustomer = new List <GrupCustomer>();

                listOfGrupCustomer = grupCustomerTable.DataRange.Rows().Select(row => new GrupCustomer
                {
                    nama_grup = row.Field("NAMA GRUP").GetString(),
                    deskripsi = row.Field("DESKRIPSI").GetString()
                }).ToList();

                if (listOfGrupCustomer.Count == 1 && listOfGrupCustomer[0].nama_grup.Length == 0)
                {
                    rowCount = 0;
                    return(false);
                }

                rowCount = listOfGrupCustomer.Count;

                using (IDapperContext context = new DapperContext())
                {
                    GrupCustomerRepository repo = new GrupCustomerRepository(context, _log);

                    foreach (var grupCustomer in listOfGrupCustomer)
                    {
                        if (grupCustomer.nama_grup.Length > 0)
                        {
                            // cek duplikasi data berdasarkan nama grup
                            if (grupCustomer.nama_grup.Length > 30)
                            {
                                grupCustomer.nama_grup = grupCustomer.nama_grup.Substring(0, 50);
                            }

                            var oldGrupCustomer = repo.GetByName(grupCustomer.nama_grup, false)
                                                  .FirstOrDefault();

                            // hanya data yg belum ada akan di import
                            if (oldGrupCustomer == null)
                            {
                                result = Convert.ToBoolean(repo.Save(grupCustomer));
                            }
                        }
                    }
                }

                result = true;
            }
            catch (Exception ex)
            {
                _log.Error("Error:", ex);
            }
            finally
            {
                _workbook.Dispose();
            }

            return(result);
        }