Example #1
0
        public Message AddTemp(IoT_UserTemp info)
        {
            // 定义执行结果
            Message m;
            string  configName = System.Configuration.ConfigurationManager.AppSettings["defaultDatabase"];
            //Linq to SQL 上下文对象
            DataContext dd = new DataContext(System.Configuration.ConfigurationManager.ConnectionStrings[configName].ConnectionString);

            try
            {
                var count = dd.GetTable <IoT_Meter>().Where(p =>
                                                            p.CompanyID == info.CompanyID && p.MeterNo == info.MeterNo).Count();

                if (count > 0)
                {
                    m = new Message()
                    {
                        Result     = false,
                        TxtMessage = "表号不能重复!"
                    };
                    return(m);
                }

                Table <IoT_UserTemp> tbl = dd.GetTable <IoT_UserTemp>();
                count = tbl.Where(p => p.CompanyID == info.CompanyID && p.MeterNo == info.MeterNo).Count();
                if (count > 0)
                {
                    m = new Message()
                    {
                        Result     = false,
                        TxtMessage = "表号不能重复!"
                    };
                    return(m);
                }

                // 调用新增方法
                tbl.InsertOnSubmit(info);
                // 更新操作
                dd.SubmitChanges();

                m = new Message()
                {
                    Result     = true,
                    TxtMessage = JSon.TToJson <IoT_UserTemp>(info)
                };
            }
            catch (Exception e)
            {
                m = new Message()
                {
                    Result     = false,
                    TxtMessage = "新增临时用户失败!" + e.Message
                };
            }
            return(m);
        }
Example #2
0
        /// <summary>
        /// 批量导入用户
        /// </summary>
        /// <param name="meterNo"></param>
        /// <returns></returns>
        public Message BatchImport(string meterNo)
        {
            // 定义执行结果
            Message m;
            string  configName = System.Configuration.ConfigurationManager.AppSettings["defaultDatabase"];
            //Linq to SQL 上下文对象
            DataContext dd = new DataContext(System.Configuration.ConfigurationManager.ConnectionStrings[configName].ConnectionString);

            try
            {
                Table <IoT_UserTemp> tbl    = dd.GetTable <IoT_UserTemp>();
                IoT_UserTemp         dbInfo = tbl.Where(p => p.MeterNo == meterNo).SingleOrDefault();

                IoT_User user = new IoT_User()
                {
                    UserName  = dbInfo.UserName,
                    CompanyID = dbInfo.CompanyID,
                    Address   = dbInfo.Address,
                    Phone     = dbInfo.Phone,
                    UserType  = dbInfo.UserType,
                    State     = '1'
                };
                bool      result = DateTime.TryParse(dbInfo.InstallDate, out DateTime installDate);
                IoT_Meter meter  = new IoT_Meter()
                {
                    MeterNo     = dbInfo.MeterNo,
                    TotalAmount = dbInfo.MeterNum,
                    InstallType = dbInfo.InstallType,
                    Direction   = dbInfo.Direction,
                    InstallDate = result ? installDate : DateTime.Now,
                    CompanyID   = dbInfo.CompanyID
                };

                m = BatchAddUserMeter(user, meter);
                tbl.DeleteOnSubmit(dbInfo);
                dd.SubmitChanges();
            }
            catch (Exception e)
            {
                m = new Message()
                {
                    Result     = false,
                    TxtMessage = "新增用户失败!" + e.Message
                };
            }
            return(m);
        }
        public override void DoLoginedHandlerWork(HttpContext context)
        {
            Message jsonMessage;

            jsonMessage = new Message()
            {
                Result     = false,
                TxtMessage = "权限验证失败,可能原因:\n1、数据中心通讯失败。\n2、系统管理员未与您分配对应操作权限。"
            };
            //获取操作类型AType:ADD,EDIT,DELETE
            string AjaxType = context.Request.QueryString["AType"] == null ? string.Empty : context.Request.QueryString["AType"].ToString().ToUpper();


            WCFServiceProxy <IUserManage> proxy = null;

            try
            {
                proxy = new WCFServiceProxy <IUserManage>();
                if (AjaxType == "UPLOAD")
                {
                    if (context.Request.Files != null && context.Request.Files.Count == 1)
                    {
                        HttpPostedFile postedFile = context.Request.Files[0];

                        DataTable           dt = null;
                        Message             m;
                        List <IoT_UserTemp> list = new List <IoT_UserTemp>();

                        NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook(postedFile.InputStream);

                        int sheetCount = book.NumberOfSheets;
                        for (int sheetIndex = 0; sheetIndex < sheetCount; sheetIndex++)
                        {
                            NPOI.SS.UserModel.ISheet sheet = book.GetSheetAt(sheetIndex);
                            if (sheet == null)
                            {
                                continue;
                            }

                            NPOI.SS.UserModel.IRow row = sheet.GetRow(0);
                            if (row == null)
                            {
                                continue;
                            }
                            int firstCellNum = row.FirstCellNum;
                            int lastCellNum  = row.LastCellNum;
                            if (firstCellNum == lastCellNum)
                            {
                                continue;
                            }

                            dt = new DataTable(sheet.SheetName);
                            for (int i = firstCellNum; i < lastCellNum; i++)
                            {
                                dt.Columns.Add(row.GetCell(i).StringCellValue, typeof(string));
                            }

                            for (int i = 1; i <= sheet.LastRowNum; i++)
                            {
                                DataRow newRow = dt.NewRow();
                                for (int j = firstCellNum; j < lastCellNum; j++)
                                {
                                    NPOI.SS.UserModel.ICell cell = sheet.GetRow(i).GetCell(j);
                                    if (cell == null)
                                    {
                                        newRow[j] = "";
                                    }
                                    else
                                    {
                                        switch (cell.CellType)
                                        {
                                        case NPOI.SS.UserModel.CellType.Blank:
                                        case NPOI.SS.UserModel.CellType.Unknown:
                                            newRow[j] = "";
                                            break;

                                        case NPOI.SS.UserModel.CellType.Numeric:
                                            if (HSSFDateUtil.IsCellDateFormatted(cell))
                                            {
                                                newRow[j] = cell.DateCellValue.ToString("yyyy-MM-dd");
                                            }
                                            else
                                            {
                                                newRow[j] = cell.NumericCellValue;
                                            }
                                            break;

                                        case NPOI.SS.UserModel.CellType.String:
                                            newRow[j] = cell.StringCellValue;
                                            break;

                                        case NPOI.SS.UserModel.CellType.Formula:
                                            newRow[j] = cell.CellFormula;
                                            break;

                                        case NPOI.SS.UserModel.CellType.Boolean:
                                            newRow[j] = cell.BooleanCellValue;
                                            break;

                                        case NPOI.SS.UserModel.CellType.Error:
                                            newRow[j] = "";
                                            break;

                                        default:
                                            newRow[j] = "";
                                            break;
                                        }
                                    }
                                }
                                if (lastCellNum < 8)
                                {
                                    continue;
                                }
                                decimal meterNum = decimal.TryParse(newRow[3].ToString(), out meterNum) ? meterNum : 0;

                                IoT_UserTemp gas = new IoT_UserTemp()
                                {
                                    UserName  = newRow[0].ToString(),
                                    UserID    = newRow[1].ToString(),
                                    MeterNo   = newRow[2].ToString(),
                                    MeterNum  = meterNum,
                                    Street    = newRow[4].ToString(),
                                    Community = newRow[5].ToString(),
                                    Door      = newRow[6].ToString(),
                                    Address   = newRow[7].ToString(),
                                    CompanyID = loginOperator.CompanyID
                                };

                                /*
                                 *                                   Direction = newRow[8].ToString(),
                                 *  InstallType = newRow[9].ToString(),
                                 *  Phone = newRow[10].ToString(),
                                 *  UserType = newRow[11].ToString() =="0"?"0":"1",
                                 *  InstallDate = newRow[12].ToString(),                                 */

                                if (lastCellNum >= 9)
                                {
                                    gas.Direction = newRow[8].ToString();
                                }
                                if (lastCellNum >= 10)
                                {
                                    gas.InstallType = newRow[9].ToString();
                                }
                                if (lastCellNum >= 11)
                                {
                                    gas.Phone = newRow[10].ToString();
                                }
                                if (lastCellNum >= 12)
                                {
                                    gas.UserType = newRow[11].ToString();
                                }

                                if (lastCellNum >= 13)
                                {
                                    try
                                    {
                                        gas.InstallDate = Convert.ToDateTime(newRow[12].ToString()).ToString("yyyy-MM-dd");
                                    }
                                    catch { }
                                }

                                m = proxy.getChannel.AddTemp(gas);
                                if (!m.Result)
                                {
                                    list.Add(gas);
                                }
                            }
                        }
                        jsonMessage = new Message()
                        {
                            Result     = true,
                            TxtMessage = Newtonsoft.Json.JsonConvert.SerializeObject(list)
                        };
                    }
                }
            }
            catch (Exception ex) {
                jsonMessage = new Message()
                {
                    Result     = false,
                    TxtMessage = ex.Message
                };
            }
            finally
            {
                proxy.CloseChannel();
            }
            context.Response.Write(JSon.TToJson <Message>(jsonMessage));
        }