public IHttpActionResult CreateNewChargingItem([FromBody] ChargingItemJsonObj obj)
        {
            UpperVendor vendorInDb = null;

            var newItem = new ChargingItem
            {
                ChargingType = obj.ChargingType,
                Name         = obj.Name,
                Rate         = obj.Rate,
                Description  = obj.Description,
                Unit         = obj.Unit
            };

            //如果customerId等于0,就通过名字和部门代码来找到customer
            if (obj.CustomerId == 0)
            {
                vendorInDb = _context.UpperVendors
                             .SingleOrDefault(x => x.Name == obj.Vendor && x.DepartmentCode == obj.DepartmentCode);

                var sameNameItem = _context.ChargingItems
                                   .Include(x => x.UpperVendor)
                                   .Where(x => x.UpperVendor.Name == obj.Vendor && x.UpperVendor.DepartmentCode == obj.DepartmentCode && x.Name == obj.Name);

                if (sameNameItem.Count() != 0)
                {
                    throw new Exception("The name: " + obj.Name + " has already been taken. Please change it and try again.");
                }

                newItem.UpperVendor = vendorInDb;
                _context.ChargingItems.Add(newItem);
            }
            else
            {
                vendorInDb = _context.UpperVendors.Find(obj.CustomerId);

                //是否将新加的收费项目应用到所有FBA客户
                if (!obj.IsApplyToAll)
                {
                    var sameNameItem = _context.ChargingItems
                                       .Include(x => x.UpperVendor)
                                       .Where(x => x.UpperVendor.Name == obj.Vendor && x.UpperVendor.DepartmentCode == obj.DepartmentCode && x.Name == obj.Name);

                    if (sameNameItem.Count() != 0)
                    {
                        throw new Exception("The name: " + obj.Name + " has already been taken. Please change it and try again.");
                    }

                    newItem.UpperVendor = vendorInDb;
                    _context.ChargingItems.Add(newItem);
                }
                else
                {
                    var fbaCustomersInDb = _context.UpperVendors
                                           .Include(x => x.ChargingItems)
                                           .Where(x => x.DepartmentCode == "FBA");

                    var itemList = new List <ChargingItem>();

                    var customerList = fbaCustomersInDb.ToList();

                    var sameNameItem = _context.ChargingItems
                                       .Include(x => x.UpperVendor)
                                       .Where(x => x.Name == obj.Name)
                                       .ToList();

                    foreach (var s in sameNameItem)
                    {
                        customerList.Remove(customerList.SingleOrDefault(x => x.CustomerCode == s.UpperVendor.CustomerCode));
                    }

                    foreach (var c in customerList)
                    {
                        var customerInDb = fbaCustomersInDb.SingleOrDefault(x => x.CustomerCode == c.CustomerCode);
                        itemList.Add(new ChargingItem
                        {
                            ChargingType = obj.ChargingType,
                            Name         = obj.Name,
                            Rate         = obj.Rate,
                            Description  = obj.Description,
                            Unit         = obj.Unit,
                            UpperVendor  = customerInDb
                        });
                    }

                    _context.ChargingItems.AddRange(itemList);
                }
            }

            _context.SaveChanges();

            var sampleDto = Mapper.Map <ChargingItem, ChargingItemDto>(_context.ChargingItems.OrderByDescending(x => x.Id).First());

            return(Created(Request.RequestUri + "/" + sampleDto.Id, sampleDto));
        }
Example #2
0
        // parameters:
        //      bAdvanceXml 是否为 AdvanceXml 情况
        static void OutputBorrowHistory(LibraryChannel channel,
                                        XmlDocument reader_dom,
                                        ChargingHistoryLoader history_loader,
                                        CacheableBiblioLoader summary_loader,
                                        ref int nRowIndex,
                                        ref Patron patron)
        {
            int nStartRow = nRowIndex;

            // 第一笔借书的时间
            string firstBorrowDate = "";


            string history = "<table>"
                             + "<tr>"
                             + "<td>序号</td><td>借书日期</td><td>册条码号</td>"
                             + "<td>书刊名称</td><td>索取号</td>"
                             + "</tr>";

            Hashtable clcHash  = new Hashtable();
            Hashtable yearHash = new Hashtable();

            int nItemIndex = 0;

            foreach (ChargingItemWrapper wrapper in history_loader)
            {
                ChargingItem item = wrapper.Item;
                ChargingItem rel  = wrapper.RelatedItem;

                string strItemBarcode = item.ItemBarcode;
                string strBorrowDate  = rel == null ? "" : rel.OperTime;
                if (strBorrowDate.Length > 10)
                {
                    strBorrowDate = strBorrowDate.Substring(0, 10);
                }

                // ==加到每年借书数量hashtable
                if (strBorrowDate.Length > 4)
                {
                    string year       = strBorrowDate.Substring(0, 4);
                    int    yearCouter = 0;
                    if (yearHash.ContainsKey(year) == true)
                    {
                        yearCouter = (int)yearHash[year];
                    }
                    yearCouter++;
                    yearHash[year] = yearCouter;
                }


                // ==加入书目摘要,一次一条记录
                string        strSummary    = "";
                List <string> item_barcodes = new List <string>();
                item_barcodes.Add("@itemBarcode:" + strItemBarcode);
                summary_loader.RecPaths = item_barcodes;
                foreach (BiblioItem biblio in summary_loader)
                {
                    strSummary = biblio.Content;
                    strSummary = GetShortSummary(strSummary);
                }


                // ==获取索取号
                string accessNo = "";

                // 获取册记录
                string strItemXml = "";
                string strBiblio  = "";
                string strError   = "";
                long   lRet       = channel.GetItemInfo(//null,
                    strItemBarcode,
                    "xml",
                    out strItemXml,
                    "xml",
                    out strBiblio,
                    out strError);
                if (-1 >= lRet)
                {
                    accessNo = "获得'" + strItemBarcode + "'发生错误: " + strError;
                }
                else if (0 == lRet)
                {
                    accessNo = strItemBarcode + " 记录不存在";
                }
                else if (1 < lRet)
                {
                    accessNo = strItemBarcode + " 记录重复,需馆员处理";
                }
                else
                {
                    // 获取索取号
                    XmlDocument itemDom = new XmlDocument();
                    try
                    {
                        itemDom.LoadXml(strItemXml);
                        //accessNo
                        accessNo = DomUtil.GetElementInnerText(itemDom.DocumentElement, "accessNo");

                        string bigClass = "";
                        if (string.IsNullOrEmpty(accessNo) == true)
                        {
                            bigClass = "[空]";
                        }
                        else
                        {
                            bigClass = accessNo.Substring(0, 1);
                        }

                        int value = 0;
                        if (clcHash.ContainsKey(bigClass) == true)
                        {
                            value = (int)clcHash[bigClass];
                        }
                        value++;
                        clcHash[bigClass] = value;
                    }
                    catch (Exception ex)
                    {
                        accessNo = strItemBarcode + " 加载到dom出错:" + ex.Message;
                    }
                }


                nItemIndex++;

                string uiClass = "";
                if (nItemIndex % 2 == 1)
                {
                    uiClass = " class='grayline' ";
                }


                history += "<tr " + uiClass + ">"
                           + "<td>" + nItemIndex + "</td><td>" + strBorrowDate + "</td><td>" + strItemBarcode + "</td>"
                           + "<td>" + strSummary + "</td><td>" + accessNo + "</td>"
                           + "</tr>";

                // 由于是倒序,集合中最后一笔记录是第1次的借书记录
                firstBorrowDate = strBorrowDate;
            }

            history += "</table>";

            // 借书历史
            patron.historyTable    = history;
            patron.firstBorrowDate = firstBorrowDate;

            // ==每个类别数量==
            // 先借助ArrayList排序一下
            List <string> list = new List <string>();

            foreach (System.Collections.DictionaryEntry item in clcHash)
            {
                list.Add(item.Key.ToString());
            }
            list.Sort();
            string classTable = "<table class='clcTable'><tr><td>图书种类</td><td>借阅数量</td></tr>";

            foreach (string k in list)
            {
                classTable += "<tr>"
                              + "<td>" + k + "</td><td>" + (int)clcHash[k] + "</td>"
                              + "</tr>";
            }
            classTable           += "</table>";
            patron.clcTable       = classTable;
            patron.covertClcCount = list.Count;

            //==每年借书数量==
            ArrayList al = new ArrayList(yearHash.Keys);

            al.Sort();
            string yearTable = "<table class='yearTable'><tr><td>借阅时段</td><td>借阅数量</td></tr>";

            foreach (string k in al)
            {
                yearTable += "<tr>"
                             + "<td>" + k + "</td><td>" + (int)yearHash[k] + "</td>"
                             + "</tr>";
            }
            yearTable       += "</table>";
            patron.yearTable = yearTable;
        }