Example #1
0
        public ActionResult ExportCsOrder(CsOrderView.CsOrderWhere para)
        {
            var data = GetList(para, false);

            if (!data.Data.Any())
            {
                return(Json(new ResModel
                {
                    Data = "没有任何可以导出数据",
                    ResStatus = ResStatue.No
                }));
            }
            var list   = new List <CsOrderView.CsOrderExcel>();
            var detail = _csOrderDetailBll.GetModelList($" AND OrderId IN ({string.Join(",", data.Data.Select(x => x.OrderId))})");

            // 配件/螃蟹/套餐
            var parts    = _csPartsBll.GetModelList($" AND PartId IN ({string.Join(",", detail.Where(x => x.ChoseType == ChoseType.配件.GetHashCode()).Select(x => x.ProductId)).ShowNullOrEmpty("0")}) AND PartType = {PartType.可选配件.GetHashCode()}");
            var products = _csProductsBll.GetModelList($" AND ProductId IN ({string.Join(",", detail.Where(x => x.ChoseType == ChoseType.螃蟹.GetHashCode()).Select(x => x.ProductId)).ShowNullOrEmpty("0")})");
            IEnumerable <CsPackage> packages = null;

            if (detail.Any(x => x.ChoseType == ChoseType.套餐.GetHashCode()))
            {
                packages = _csPackageBll.GetModelList($" AND PackageId IN ({string.Join(",", detail.Where(x => x.ChoseType == ChoseType.套餐.GetHashCode()).Select(x => x.ProductId))})");
            }
            foreach (var order in data.Data)
            {
                var total = string.Empty;
                foreach (var d in detail.Where(x => x.OrderId == order.OrderId).OrderBy(x => x.ChoseType))
                {
                    if (d.ChoseType == ChoseType.套餐.GetHashCode())
                    {
                        total += packages?.FirstOrDefault(x => x.PackageId == d.ProductId)?.PackageNumber + "" + d.ProductNumber;
                        continue;
                    }
                    if (d.ChoseType == ChoseType.螃蟹.GetHashCode())
                    {
                        total += products.FirstOrDefault(x => x.ProductId == d.ProductId)?.ProductNumber + "" + d.ProductNumber;
                        continue;
                    }
                    if (total.IndexOf("P", StringComparison.Ordinal) < 0 &&
                        total.IndexOf("-", StringComparison.Ordinal) < 0)
                    {
                        total += "-";
                    }
                    total += parts.FirstOrDefault(x => x.PartId == d.ProductId)?.PartNumber;
                }
                var sendInfo = order.SendAddress.Split('$');
                var putInfo  = order.OrderAddress.Split('$');
                list.Add(new CsOrderView.CsOrderExcel
                {
                    用户订单号    = order.OrderNumber,
                    寄件公司     = "-",
                    寄联系人     = sendInfo.Length > 0 ? sendInfo[0] : "-",
                    寄联系电话    = sendInfo.Length > 1 ? sendInfo[1] : "-",
                    寄件地址     = sendInfo.Length > 2 ? sendInfo[2] : "-",
                    收件公司     = putInfo.Length > 0 ? putInfo[0] : "-",
                    联系人      = putInfo.Length > 1 ? putInfo[1] : "-",
                    联系电话     = putInfo.Length > 2 ? putInfo[2] : "-",
                    手机号码     = putInfo.Length > 3 ? putInfo[3] : "-",
                    收件详细地址   = putInfo.Length > 4 ? putInfo[4] : "-",
                    付款方式     = "寄付月结",
                    第三方付月结卡号 = "",
                    寄托物品     = "其他",
                    寄托物内容    = total.TrimEnd('-').IsNullOrEmpty() ? "" : $"{total.TrimEnd('-')}-T{detail.Where(x => x.OrderId == order.OrderId && (x.ChoseType == ChoseType.螃蟹.GetHashCode() || x.ChoseType == ChoseType.套餐.GetHashCode())).Sum(x => x.ProductNumber)}",
                    寄托物编号    = "",
                    寄托物数量    = order.CargoNumber.ToString(),
                    件数       = order.OrderCopies.ToString(),
                    实际重量单位KG = "-",
                    计费重量单位KG = "-",
                    业务类型     = "大闸蟹专递",
                    扩展字段1    = detail.Any(x => x.OrderId == order.OrderId && x.ChoseType == ChoseType.套餐.GetHashCode()) ? "企业团购" : "电商代发",
                    扩展字段2    = order.OrderRemarks
                });
            }
            var path = $"excel/{DateTime.Now:yyyyMMddHHmmssffff}.xls";

            try
            {
                NpoiHelper.ExportToExcel(list.ToDataTable(), "D:/" + path);
            }
            catch (Exception e)
            {
                return(Json(new ResModel
                {
                    Data = e.Message,
                    ResStatus = ResStatue.No
                }));
            }
            return(Json(new ResModel
            {
                Data = "../" + path,
                ResStatus = ResStatue.Yes
            }));
        }