Ejemplo n.º 1
0
        public async Task <IActionResult> DownloadDataAsync(
            [FromQuery] Dictionary <string, Dictionary <string, List <string> > > filter = default,
            [FromQuery] Dictionary <string, int> sorting = default,
            [FromQuery] Dictionary <string, int> exact   = default,
            CancellationToken cancellationToken          = default)
        {
            InitUserInfo();
            if (!AllowDownload)
            {
                return(ValidationProblem());
            }
            CleanFilter(filter);
            Dictionary <string, List <string> > filterParams = new Dictionary <string, List <string> >();

            foreach (string fieldName in filter.Keys)
            {
                if (filter[fieldName].Count <= 0)
                {
                    continue;
                }
                filterParams.Add(fieldName, new List <string>());
                foreach (var itemValue in filter[fieldName][""])
                {
                    filterParams[fieldName].Add(itemValue);
                }
            }

            int?       id = (filterParams.ContainsKey("id") ? int.Parse(filterParams["id"][0]) : null);
            List <int> purchaseOrderId = null;

            if (filterParams.ContainsKey("purchaseOrderId"))
            {
                purchaseOrderId = new List <int>();
                foreach (var item in filterParams["purchaseOrderId"])
                {
                    var data = int.Parse(item);
                    purchaseOrderId.Add(data);
                }
            }
            List <string> partId    = (filterParams.ContainsKey("partId") ? filterParams["partId"] : null);
            List <double> partPrice = null;

            if (filterParams.ContainsKey("partPrice"))
            {
                partPrice = new List <double>();
                foreach (var item in filterParams["partPrice"])
                {
                    var data = double.Parse(item);
                    partPrice.Add(data);
                }
            }
            List <int> qty = null;

            if (filterParams.ContainsKey("qty"))
            {
                qty = new List <int>();
                foreach (var item in filterParams["qty"])
                {
                    var data = int.Parse(item);
                    qty.Add(data);
                }
            }
            List <double> totalPrice = null;

            if (filterParams.ContainsKey("totalPrice"))
            {
                totalPrice = new List <double>();
                foreach (var item in filterParams["totalPrice"])
                {
                    var data = double.Parse(item);
                    totalPrice.Add(data);
                }
            }


            string fileName  = Guid.NewGuid().ToString() + ".xlsx";
            var    excelFile = _uriComposer.ComposeDownloadPath(fileName);

/*
 *                      string key = await _purchaseOrderDetailService.GenerateExcelBackgroundProcess(excelFile,
 *                              id, purchaseOrderId, partId, partPrice, qty, totalPrice,
 *                              exact, cancellationToken);
 *                      Dictionary<string, string> result = new Dictionary<string, string>() { {"id", key} };
 *                      return Ok(result);
 */
            string generatedFilename = await _purchaseOrderDetailService.GenerateExcel(excelFile, null,
                                                                                       id, purchaseOrderId, partId, partPrice, qty, totalPrice,
                                                                                       exact, cancellationToken);

            fileName = "PurchaseOrderDetail.xlsx";
            byte[] fileBytes = System.IO.File.ReadAllBytes(generatedFilename);
            return(File(fileBytes, "application/xlsx", fileName));
        }