Example #1
0
        /// <summary>
        /// 保存页面内容
        /// </summary>
        /// <param name="bookid"></param>
        /// <param name="userid"></param>
        /// <param name="pagedata"></param>
        /// <returns></returns>
        public ActionResult SavePageData(int bookid, int userid, string pdata)
        {
            try
            {
                Inpinke_Book model    = DBBookBLL.GetBookByID(bookid);
                PageDataObjs pageObjs = (PageDataObjs)JsonConvert.DeserializeObject(pdata, typeof(PageDataObjs));
                if (pageObjs == null)
                {
                    return(Content("{\"success\":false,\"msg\":\"保存失败,内容为空\"}"));
                }
                if (model.BookStauts == (int)BookStatus.Making)
                {
                    return(Content("{\"success\":false,\"msg\":\"对不起当前印品已下单印制不能再编辑。\"}"));
                }
                string     strReturn    = "{\"success\":true,\"pviews\":[";
                string     noneused     = "";
                string     usedimgs     = "";
                List <int> noneUseImage = new List <int>();
                List <int> usedimg      = new List <int>();
                foreach (PageDataObj pageObj in pageObjs.pdatas)
                {
                    int pnum = pageObj.pagenum;
                    Inpinke_Book_Page bookPage = DBBookBLL.GetBookPageByPNum(pnum, bookid);
                    XElement          root     = new XElement("layout", new XAttribute("pnum", pnum), new XAttribute("styleid", pageObj.styleid),
                                                              new XAttribute("isskip", pageObj.isskip), new XAttribute("opnum", pageObj.opagenum), new XAttribute("bgcolor", pageObj.bgcolor));
                    bool isCreate = false;
                    if (bookPage == null)
                    {
                        bookPage = new Inpinke_Book_Page()
                        {
                            BookID  = bookid,
                            PageNum = pnum
                        };
                        isCreate = true;
                    }
                    else
                    {
                        //原页面不是跨页,现在是跨页的,需要删除掉一个原页面
                        if (!bookPage.IsSkip && pageObj.isskip.ToLower() == "true" && pageObj.opagenum != "-1_0")
                        {
                            int opnum = int.Parse(pageObj.opagenum.Replace(pnum + "_", ""));
                            Inpinke_Book_Page oBookPage = DBBookBLL.GetBookPageByPNum(opnum, bookid);
                            if (oBookPage != null)
                            {
                                oBookPage.PageStatus = (int)PageStatus.Delete;
                                DBBookBLL.UpdateBookPage(oBookPage);
                                //修改页面图片使用次数
                                DBImageBLL.ChangeImageUsedNum(oBookPage.PageData, bookid, true, ref noneUseImage);
                            }
                        }
                        //修改页面图片使用次数
                        DBImageBLL.ChangeImageUsedNum(bookPage.PageData, bookid, true, ref noneUseImage);
                    }
                    bookPage.IsSkip = pageObj.isskip.ToLower() == "false" ? false : true;

                    //添加图片节点
                    foreach (PageImage image in pageObj.image)
                    {
                        XElement       imageItem = new XElement("image");
                        PropertyInfo[] pis       = image.GetType().GetProperties();
                        int            urlIndex  = image.src.ToLower().IndexOf("/userfile");
                        if (urlIndex > 0)
                        {
                            image.src = image.src.Substring(urlIndex);
                        }
                        foreach (PropertyInfo property in pis)
                        {
                            //设置节点属性
                            imageItem.SetAttributeValue(property.Name, property.GetValue(image, null));
                        }
                        root.Add(imageItem);
                    }
                    //添加文字节点
                    foreach (PageText text in pageObj.text)
                    {
                        XElement       textItem = new XElement("text");
                        PropertyInfo[] pis      = text.GetType().GetProperties();
                        foreach (PropertyInfo property in pis)
                        {
                            //设置节点属性
                            textItem.SetAttributeValue(property.Name, property.GetValue(text, null));
                        }
                        if (pageObj.pagenum == 0 && text.issingle == "true")
                        {
                            if (!string.IsNullOrEmpty(text.content) && text.conid == "txt_1")
                            {
                                model.BookName = text.content;
                            }
                            if (!string.IsNullOrEmpty(text.content) && text.conid == "txt_2")
                            {
                                model.SubBookName = text.content;
                            }
                        }

                        root.Add(textItem);
                    }
                    bookPage.PageData = root;
                    //修改页面图片使用次数

                    DBImageBLL.ChangeImageUsedNum(bookPage.PageData, bookid, false, ref usedimg);

                    bool istxtO = false; //文字是否过多
                    if (isCreate)
                    {
                        int pID = DBBookBLL.GetMaxBookPageID() + 1;
                        bookPage.PageImg = SavePageView(pageObj, userid, model, ref istxtO, pID);
                    }
                    else
                    {
                        bookPage.PageImg = SavePageView(pageObj, userid, model, ref istxtO, bookPage.ID);
                    }

                    strReturn          += "{\"pnum\":" + pageObj.pagenum + ",\"src\":\"" + bookPage.PageImg + "\",\"isskip\":" + pageObj.isskip + ",\"istxtover\":" + istxtO.ToString().ToLower() + "},";
                    bookPage.PageStatus = (int)PageStatus.Normal;
                    if (pageObj.pagenum == 0)
                    {
                        model.BookCover = "/UserFile/" + UserSession.CurrentUser.ID + "/" + bookid + "/cover200.jpg";
                    }

                    if (isCreate)
                    {
                        DBBookBLL.AddBookPage(bookPage);
                        //更新已完成的页数
                        DBBookBLL.UpdateDonePage(bookid);
                    }
                    else
                    {
                        DBBookBLL.UpdateBookPage(bookPage);
                    }
                }
                noneused = string.Join(",", noneUseImage);
                usedimgs = string.Join(",", usedimg);

                strReturn = strReturn.TrimEnd(',') + "],\"noneusedimg\":[" + noneused + "],\"usedimg\":[" + usedimgs + "]}";
                return(Content(strReturn));
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("SavePageData BookID:{0},UserID:{1},Error:{2}", bookid, userid, ex.ToString()));
                return(Content("{\"success\":false,\"msg\":\"服务器异常保存失败,请稍后再试\"}"));
            }
        }