Exemplo n.º 1
0
        public HttpResponseMessage Get(int?project, int?department, DateTime startDate, DateTime endDate)
        {
            BillsFilter billsFilter = new BillsFilter();

            billsFilter.Project    = project;
            billsFilter.Department = department;
            billsFilter.StartDate  = startDate;
            billsFilter.EndDate    = endDate;
            billsFilter.LastId     = 0;
            string errMsg = "";
            IList <Model.Export> listExport = new List <Model.Export>();
            int billsCount = BLL.BillManager.GetBillsCountForExport(billsFilter, out errMsg);

            if (billsCount == 0)
            {
                errMsg = "指定条件下没有找到加油单!";
            }
            else if (billsCount > 0)
            {
                try
                {
                    Model.Export export = new Model.Export();
                    export.Id     = 1;
                    export.Length = 0;
                    export.Total  = billsCount;
                    listExport.Add(export);
                }
                catch (Exception ex)
                {
                    errMsg = ex.Message;
                }
            }
            HttpStatusCode status;
            string         json;

            //errMsg = "网络繁忙,请稍稍再度";
            if (errMsg.Length > 0)
            {
                status = (HttpStatusCode)422;
                var msg = new
                {
                    errors = new
                    {
                        ServerSideError = errMsg
                    }
                };
                json = JsonConvert.SerializeObject(msg);
            }
            else
            {
                status = HttpStatusCode.OK;
                var msg2 = new
                {
                    exports = listExport
                };
                json = JsonConvert.SerializeObject(msg2);
            }
            return(new HttpResponseMessage(status)
            {
                Content = new StringContent(json, System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            });
        }
Exemplo n.º 2
0
        public HttpResponseMessage Get(int?count, int?project, int?department, DateTime startDate, DateTime endDate, int?lastId, string name)
        {
            BillsFilter billsFilter = new BillsFilter();

            billsFilter.Project    = project;
            billsFilter.Department = department;
            billsFilter.StartDate  = startDate;
            billsFilter.EndDate    = endDate;
            billsFilter.LastId     = lastId;
            string errMsg = "";
            IList <Model.Export>      listExport = new List <Model.Export>();
            IList <Model.BillPrinter> listBill   = BLL.BillManager.GetBillsForExport(count, billsFilter, out errMsg);

            if (listBill == null)
            {
                errMsg = "数据量过大,请适当缩小时间范围!";
            }
            else if (lastId == 0 && listBill.Count == 0)
            {
                errMsg = "指定条件下没有找到加油单!";
            }
            else
            {
                try
                {
                    Model.Export export    = new Model.Export();
                    int          newLastId = (int)lastId;
                    if (listBill.Count > 0)
                    {
                        newLastId = listBill[listBill.Count - 1].Id;
                        StringBuilder sb   = GetBillTextForExport(listBill, (lastId > 0 ? false : true));
                        byte[]        data = Encoding.GetEncoding("gb2312").GetBytes(sb.ToString());
                        //byte[] data = new UTF8Encoding().GetBytes(sb.ToString());
                        string filePath = HttpContext.Current.Server.MapPath("../download//" + name + ".csv");
                        Function.BytesToFile(data, filePath);
                    }
                    export.Id     = 1;
                    export.Length = listBill.Count;
                    export.LastId = newLastId;
                    listExport.Add(export);
                }
                catch (Exception ex)
                {
                    errMsg = ex.Message;
                }
            }
            HttpStatusCode status;
            string         json;

            //errMsg = "网络繁忙,请稍稍再度";
            if (errMsg.Length > 0)
            {
                status = (HttpStatusCode)422;
                var msg = new
                {
                    errors = new
                    {
                        ServerSideError = errMsg
                    }
                };
                json = JsonConvert.SerializeObject(msg);
            }
            else
            {
                status = HttpStatusCode.OK;
                var msg2 = new
                {
                    exports = listExport
                };
                json = JsonConvert.SerializeObject(msg2);
            }
            return(new HttpResponseMessage(status)
            {
                Content = new StringContent(json, System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            });
        }