Esempio n. 1
0
 public static void Save_ChargeMeter_Project(ChargeMeter_Project data, ChargeMeter meter, SqlHelper helper)
 {
     data.MeterName        = meter.MeterName;
     data.MeterNumber      = meter.MeterNumber;
     data.MeterCategoryID  = meter.MeterCategoryID;
     data.MeterType        = meter.MeterType;
     data.MeterSpec        = meter.MeterSpec;
     data.MeterCoefficient = meter.MeterCoefficient;
     data.MeterRemark      = meter.MeterRemark;
     data.MeterChargeID    = meter.MeterChargeID;
     data.MeterHouseNo     = meter.MeterHouseNo;
     data.MeterLocation    = meter.MeterLocation;
     data.SortOrder        = meter.SortOrder;
     data.WriteStatus      = 0;
     data.FeeStatus        = 0;
     data.Save(helper);
 }
        private void importmeterpointdata(HttpContext context)
        {
            HttpFileCollection uploadFiles = context.Request.Files;

            if (uploadFiles.Count == 0)
            {
                context.Response.Write("请选择一个文件");
                return;
            }
            if (string.IsNullOrEmpty(uploadFiles[0].FileName))
            {
                context.Response.Write("请选择一个文件");
                return;
            }
            string msg          = string.Empty;
            bool   ImportFailed = false;
            int    TotalNumber  = 0;

            using (SqlHelper helper = new SqlHelper())
            {
                try
                {
                    helper.BeginTransaction();
                    #region 导入处理
                    for (int j = 0; j < uploadFiles.Count; j++)
                    {
                        HttpPostedFile postedFile = uploadFiles[j];
                        string         filepath   = HttpContext.Current.Server.MapPath("~/upload/ImportChaoBiao/" + DateTime.Now.ToString("yyyyMMdd"));
                        if (!System.IO.Directory.Exists(filepath))
                        {
                            System.IO.Directory.CreateDirectory(filepath);
                        }
                        string filename = DateTime.Now.ToLocalTime().ToString("yyyyMMddHHmmss") + "_" + postedFile.FileName;
                        string fullpath = Path.Combine(filepath, filename);
                        postedFile.SaveAs(fullpath);
                        DataTable table = ExcelExportHelper.NPOIReadExcel(fullpath);
                        for (int i = 0; i < table.Rows.Count; i++)
                        {
                            int ID = 0;
                            int.TryParse(table.Rows[i]["仪表ID"].ToString(), out ID);
                            Foresight.DataAccess.ChargeMeter_Project projectbiao = null;
                            if (ID > 0)
                            {
                                projectbiao = Foresight.DataAccess.ChargeMeter_Project.GetChargeMeter_Project(ID, helper);
                            }
                            if (projectbiao == null)
                            {
                                msg         += "<p>第" + (i + 2) + "行上传失败。原因:仪表不存在</p>";
                                ImportFailed = true;
                                break;
                            }
                            projectbiao.MeterName       = table.Rows[i]["仪表名称"].ToString();
                            projectbiao.MeterNumber     = table.Rows[i]["仪表编号"].ToString();
                            projectbiao.MeterCategoryID = ChargeMeter_Project.GetMeterCategoryID(table.Rows[i]["仪表种类"].ToString());
                            if (projectbiao.MeterCategoryID == 0)
                            {
                                msg         += "<p>第" + (i + 2) + "行上传失败。原因:仪表种类不存在</p>";
                                ImportFailed = true;
                                break;
                            }
                            projectbiao.MeterType = ChargeMeter_Project.GetMeterTypeID(table.Rows[i]["仪表类型"].ToString());
                            if (projectbiao.MeterType == 0)
                            {
                                msg         += "<p>第" + (i + 2) + "行上传失败。原因:仪表类型不存在</p>";
                                ImportFailed = true;
                                break;
                            }
                            projectbiao.MeterSpec = ChargeMeter_Project.GetMeterSpec(table.Rows[i]["仪表规格"].ToString());
                            if (projectbiao.MeterSpec == 0)
                            {
                                msg         += "<p>第" + (i + 2) + "行上传失败。原因:仪表规格不存在</p>";
                                ImportFailed = true;
                                break;
                            }
                            projectbiao.MeterCoefficient = WebUtil.GetDecimalByStr(table.Rows[i]["倍率"].ToString());
                            projectbiao.MeterHouseNo     = table.Rows[i]["缴费户号"].ToString();
                            projectbiao.MeterLocation    = table.Rows[i]["仪表位置"].ToString();
                            projectbiao.MeterRemark      = table.Rows[i]["备注"].ToString();
                            projectbiao.SortOrder        = WebUtil.GetIntByStr(table.Rows[i]["排序序号"].ToString());
                            projectbiao.MeterStartPoint  = WebUtil.GetDecimalByStr(table.Rows[i]["上次读数"].ToString());
                            projectbiao.MeterEndPoint    = WebUtil.GetDecimalByStr(table.Rows[i]["本次读数"].ToString());
                            projectbiao.Save(helper);
                            TotalNumber = i;
                        }
                    }
                    #endregion
                    if (!ImportFailed)
                    {
                        helper.Commit();
                        msg += "<p>导入完成</p>";
                    }
                    else
                    {
                        helper.Rollback();
                        msg += "<p>导入失败</p>";
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.WriteError("ImportGongTanHandler", "visit: importmeterdata", ex);
                    helper.Rollback();
                    msg += "<p>第" + (TotalNumber + 2) + "行上传失败。原因:" + ex.Message + "</p>";
                }
                context.Response.Write(msg);
            }
        }