Ejemplo n.º 1
0
        //// <summary>
        /// 这里保存上传的文件到服务器上面
        /// </summary>
        /// <param name="file">上传文件实例,名称应该和前台页面上的上传控件name属性一致</param>
        /// <returns></returns>
        public ActionResult Order_PL(HttpPostedFileBase file)
        {
            retValue ret = new retValue();

            ret.result = true;
            if (file == null)
            {
                ret.result = false;
                ret.reason = "请选择上传文件!";
            }
            else
            {
                PMS.Models.UserModel userModel = Session["UserModel"] as PMS.Models.UserModel;
                int    userid   = userModel._ID;
                Random r        = new Random();
                var    fileExt  = System.IO.Path.GetExtension(file.FileName).Substring(1);
                var    filename = DateTime.Now.ToString("yyyyMMddHHmmss") + r.Next(10000) + "." + fileExt;
                var    filepath = System.IO.Path.Combine(Server.MapPath("~/Images"), filename);
                var    savepath = "/Images/" + filename;
                file.SaveAs(filepath);
                BLL.OrderInfoBLL _bll = new OrderInfoBLL();
                BLL.OrgInfoBLL   _Org = new OrgInfoBLL();
                DataTable        dt   = OpenFile(filepath);
                //删除文件
                FileInfo fileInfo = new FileInfo(filepath);
                fileInfo.Delete();
                if (dt == null || dt.Rows.Count == 0)
                {
                    ret.result = false;
                    ret.reason = "Excel中没有数据";
                }
                else
                {
                    int           count    = 0;
                    SqlHelp       dbhelper = new SqlHelp();
                    SqlConnection conn     = new SqlConnection(dbhelper.SqlConnectionString);
                    conn.Open();
                    using (SqlTransaction tran = conn.BeginTransaction())
                    {
                        try
                        {
                            foreach (DataRow item in dt.Rows)
                            {
                                int orgid = _Org.GetIDByName(item["OrgName"]._ToStrTrim());
                                if (orgid == 0)
                                {
                                    ret.result = false;
                                    ret.reason = "第" + (dt.Rows.IndexOf(item) + 1) + "行数据机构填写不正确";
                                    break;
                                }
                                else if (string.IsNullOrEmpty(item["BKDH"]._ToStrTrim()))
                                {
                                    ret.result = false;
                                    ret.reason = "第" + (dt.Rows.IndexOf(item) + 1) + "行数据报刊代号不能为空"; break;
                                }
                                else if (item["OrderNum"]._ToInt32() <= 0)
                                {
                                    ret.result = false;
                                    ret.reason = "第" + (dt.Rows.IndexOf(item) + 1) + "行数据订购数必须大于0"; break;
                                }
                                else if (item["OrderMonths"]._ToInt32() <= 0)
                                {
                                    ret.result = false;
                                    ret.reason = "第" + (dt.Rows.IndexOf(item) + 1) + "行数据订购月数必须大于0"; break;
                                }
                                else if (string.IsNullOrEmpty(item["Indate"]._ToStrTrim()))
                                {
                                    ret.result = false;
                                    ret.reason = "第" + (dt.Rows.IndexOf(item) + 1) + "行数据订购起始日期不能为空"; break;
                                }
                                else if (string.IsNullOrEmpty(item["OrderNo"]._ToStrTrim()))
                                {
                                    ret.result = false;
                                    ret.reason = "第" + (dt.Rows.IndexOf(item) + 1) + "行数据订户单位编号不能为空"; break;
                                }
                                else
                                {
                                    ret = _bll.Insert(item["BKDH"]._ToStrTrim(), item["OrderNum"]._ToInt32(), item["OrderMonths"]._ToInt32(), item["Indate"]._ToStrTrim(), userid._ToStr(),
                                                      0, item["OrderNo"]._ToStrTrim(), item["UnitName"]._ToStrTrim(), item["Address"]._ToStrTrim(), item["Name"]._ToStrTrim(), item["Phone"]._ToStrTrim(), orgid._ToStr(), tran, userid._ToStr());
                                    count++;
                                    if (!ret.result)
                                    {
                                        tran.Rollback();
                                        return(Json(JsonConvert.SerializeObject(ret), JsonRequestBehavior.AllowGet));
                                    }
                                }
                            }
                            if (!ret.result)
                            {
                                tran.Rollback();
                            }
                            else
                            {
                                tran.Commit();
                                ret.result = true;
                                ret.data   = "导入成功";
                            }
                        }
                        catch (Exception ex)
                        {
                            ret.result = false;
                            ret.reason = ex.Message;
                            tran.Rollback();
                        }
                    }
                    conn.Close();
                }
            }
            var js = JsonConvert.SerializeObject(ret);

            return(Json(js, JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 2
0
        public ActionResult Order_AddEdits(string str)
        {
            retValue ret = new retValue();

            PMS.Models.UserModel userModel = Session["UserModel"] as PMS.Models.UserModel;
            int userid = userModel._ID;

            BLL.OrderInfoBLL _BLL = new OrderInfoBLL();
            JObject          o    = null;

            if (!string.IsNullOrEmpty(str))
            {
                o = JObject.Parse(str);

                string ID         = o["ID"]._ToStrTrim();
                string NGUID      = o["NGUID"]._ToStrTrim();
                string CostID     = o["CostID"]._ToStrTrim();
                string Month      = o["Month"]._ToStrTrim();
                string OrderNum   = o["OrderNumber"]._ToStrTrim();
                string PersonID   = o["OrderPeople"]._ToStrTrim();
                string Poster     = o["Poster"]._ToStrTrim();
                string BKDH       = o["NewspaperName"]._ToStrTrim();
                string FullPrice  = o["FullPrice"]._ToStrTrim();
                string MoneyPayed = o["Pay"]._ToStrTrim();
                string test1      = o["test1"]._ToStrTrim();
                if (string.IsNullOrEmpty(ID))
                {
                    SqlHelp       dbhelper = new SqlHelp();
                    SqlConnection conn     = new SqlConnection(dbhelper.SqlConnectionString);
                    conn.Open();
                    using (SqlTransaction tran = conn.BeginTransaction())
                    {
                        try
                        {
                            ret = _BLL.Insert(tran, BKDH, PersonID._ToInt32(), OrderNum._ToInt32(), Month._ToInt32(),
                                              test1, userid._ToStr(), Poster._ToInt32(), FullPrice._ToDecimal());
                            tran.Commit();
                        }
                        catch (Exception ex)
                        {
                            ret.result = false;
                            ret.reason = ex.Message;
                            tran.Rollback();
                            throw;
                        }
                    }
                }
                else
                {
                    ret = _BLL.UpdateByPK(ID._ToInt32(), Month._ToInt32(), OrderNum._ToInt32(), BKDH,
                                          PersonID._ToInt32(), userid, Poster._ToInt32(), test1, NGUID);
                    if (ret.result)
                    {
                        CostBLL costBLL = new CostBLL();
                        costBLL.UpdateByPK(CostID._ToInt32(), FullPrice._ToDecimal(), MoneyPayed._ToDecimal(), userid);
                    }
                }
            }
            var js = JsonConvert.SerializeObject(ret);

            return(Json(js, JsonRequestBehavior.AllowGet));
        }