移动系统liao
2024-04-25 60cfd15db42bb816846072d027121d91ffd013b1
CoreCms.Net.Web.Admin/Controllers/Com/ToolsController.cs
@@ -37,8 +37,12 @@
using COSXML.Auth;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using SKIT.FlurlHttpClient.Wechat.Api;
using SKIT.FlurlHttpClient.Wechat.Api.Models;
using SqlSugar;
@@ -89,6 +93,7 @@
        private readonly IWeChatApiHttpClientFactory _weChatApiHttpClientFactory;
        private readonly SemaphoreSlim _semaphore;
        private readonly ICoreCmsPlanOrderServices _coreCmsPlanOrderServices;
        /// <summary>
        ///     构造函数
@@ -111,7 +116,8 @@
            , ISysUserRoleServices sysUserRoleServices
            , ISysOrganizationServices sysOrganizationServices,
            ICoreCmsLogisticsServices logisticsServices, ISysLoginRecordServices sysLoginRecordServices, ISysNLogRecordsServices sysNLogRecordsServices, ICoreCmsBillPaymentsServices paymentsServices, ICoreCmsBillDeliveryServices billDeliveryServices, ICoreCmsUserServices userServices, ICoreCmsOrderServices orderServices, ICoreCmsBillAftersalesServices aftersalesServices, ICoreCmsSettingServices settingServices, ICoreCmsProductsServices productsServices, ICoreCmsServicesServices servicesServices, ISysRoleMenuServices sysRoleMenuServices
            , IWeChatApiHttpClientFactory weChatApiHttpClientFactory, ICoreCmsPagesServices pagesServices, IToolsServices toolsServices, ICoreCmsReportsServices reportsServices, ICoreCmsGoodsCategoryServices goodsCategoryServices)
            , IWeChatApiHttpClientFactory weChatApiHttpClientFactory, ICoreCmsPagesServices pagesServices, IToolsServices toolsServices, ICoreCmsReportsServices reportsServices, ICoreCmsGoodsCategoryServices goodsCategoryServices
            , ICoreCmsPlanOrderServices  coreCmsPlanOrderServices )
        {
            _user = user;
            _webHostEnvironment = webHostEnvironment;
@@ -148,6 +154,7 @@
            _toolsServices = toolsServices;
            _reportsServices = reportsServices;
            _goodsCategoryServices = goodsCategoryServices;
            _coreCmsPlanOrderServices = coreCmsPlanOrderServices;
            //最多允许2个线程同时访问;
            _semaphore = new SemaphoreSlim(2);
@@ -363,6 +370,258 @@
        //通用操作=========================================================================
        #region 计划订单上传接口====================================================
        /// <summary>
        /// 计划订单上传接口
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public async Task<AdminUiCallBack> UploadFilesXls()
        {
            var jm = new AdminUiCallBack();
            await _semaphore.WaitAsync();
            try
            {
                var filesStorageOptions = await _coreCmsSettingServices.GetFilesStorageOptions();
                //初始化上传参数
                var maxSize = 1024 * 1024 * filesStorageOptions.MaxSize; //上传大小5M
                var file = Request.Form.Files["file"];
                if (file == null)
                {
                    jm.msg = "请选择文件";
                    return jm;
                }
                var fileName = file.FileName;
                var fileExt = Path.GetExtension(fileName).ToLowerInvariant();
                //检查大小
                if (file.Length > maxSize)
                {
                    jm.msg = "上传文件大小超过限制,最大允许上传" + filesStorageOptions.MaxSize + "M";
                    return jm;
                }
                //检查文件扩展名
                if (string.IsNullOrEmpty(fileExt) ||
                    Array.IndexOf(filesStorageOptions.FileTypes.Split(','), fileExt.Substring(1).ToLower()) == -1)
                {
                    jm.msg = "上传文件扩展名是不允许的扩展名,请上传后缀名为:" + filesStorageOptions.FileTypes;
                    return jm;
                }
                var lie = new List<JiHuaOrder>();
                using (var fileStream = file.OpenReadStream())
                {
                    // 使用HSSFWorkbook打开.xls格式的文件
                    IWorkbook workbook = new XSSFWorkbook(fileStream);
                    // 获取第一个工作表
                    ISheet sheet = workbook.GetSheetAt(0);
                    // 迭代工作表中的所有行
                    for (int i = 1; i <= sheet.LastRowNum; i++)
                    {
                        var hang = new JiHuaOrder();
                        IRow row = sheet.GetRow(i);
                        if (row == null) continue; // 跳过空行
                        ICell cell1 = row.GetCell(1);
                        if (cell1 == null)
                        {
                            jm.msg ="第" +(i+1) + "学校不能为空";
                            return jm;
                        }
                        hang.shipAddress = getcellvalue(cell1);
                        ICell cell2 = row.GetCell(2);
                        if (cell2 == null)
                        {
                            jm.msg = "第" + (i + 1) + "联系人不能为空";
                            return jm;
                        }
                        hang.shipName = getcellvalue(cell2);
                        ICell cell3 = row.GetCell(3);
                        if (cell3 == null)
                        {
                            jm.msg = "第" + (i + 1) + "联系电话不能为空";
                            return jm;
                        }
                        hang.shipMobile = getcellvalue(cell3);
                        ICell cell4 = row.GetCell(4);
                        if (cell4 == null)
                        {
                            jm.msg = "第" + (i + 1) + "商品名称不能为空";
                            return jm;
                        }
                        hang.name = getcellvalue(cell4);
                        ICell cell5 = row.GetCell(5);
                        if (cell5 == null)
                        {
                            jm.msg = "第" + (i + 1) + "规格不能为空";
                            return jm;
                        }
                        hang.specification = getcellvalue(cell5);
                        ICell cell6 = row.GetCell(6);
                        if (cell6 == null)
                        {
                            jm.msg = "第" + (i + 1) + "单价(元)不能为空";
                            return jm;
                        }
                        hang.price = decimal.Parse(getcellvalue(cell6));
                        ICell cell7 = row.GetCell(7);
                        if (cell7 == null)
                        {
                            jm.msg = "第" + (i + 1) + "数量不能为空";
                            return jm;
                        }
                        hang.nums = int.Parse(getcellvalue(cell7)); ;
                        lie.Add(hang);
                    }
                }
                lie = lie.OrderBy(x => x.shipAddress).ThenBy(x => x.specification).ToList();
                string url = string.Empty;
                url = "121212";
                if (lie!=null && lie.Count > 0)
                {
                   var  order = new List<JiHuaOrder>();
                    for(int i=0;i< lie.Count; i++)
                    {
                        if (i == 0 )
                        {
                            order.Add(lie[i]);
                            //if(lie.Count == 1)
                            //{
                            //    //保存订单
                            //    jm = await  _coreCmsPlanOrderServices.ToAddJiHua(lie,_user.ID, _user.Name);
                            //    if (jm.code == 1)
                            //    {
                            //        return jm;
                            //    }
                            //}
                        }
                        else
                        {
                            if(lie[i].shipAddress  == lie[i - 1].shipAddress)
                            {
                                order.Add(lie[i]);
                            }
                            else
                            {
                                //保存订单
                                jm = await _coreCmsPlanOrderServices.ToAddJiHua(lie, _user.ID, _user.Name);
                                if (jm.code == 1)
                                {
                                    return jm;
                                }
                                order = new List<JiHuaOrder>();
                                order.Add(lie[i]);
                            }
                        }
                        if (i == lie.Count - 1)
                        {
                            //保存订单
                            jm = await _coreCmsPlanOrderServices.ToAddJiHua(lie, _user.ID, _user.Name);
                            if (jm.code == 1)
                            {
                                return jm;
                            }
                        }
                    }
                }
                else
                {
                    jm.code =1;
                    jm.msg = "表格无数据";
                    jm.data = new
                    {
                        fileUrl = url,
                        src = url
                    };
                }
                var bl = !string.IsNullOrEmpty(url);
                jm.code = bl ? 0 : 1;
                jm.msg = bl ? "上传成功!" : "上传失败";
                jm.data = new
                {
                    fileUrl = url,
                    src = url
                };
            }
            catch (Exception e)
            {
                jm.code = 1;
                jm.msg = "数据异常";
                jm.data = e;
            }
            finally
            {
                _semaphore.Release(); // 释放访问
            }
            return jm;
        }
        /// <summary>
        /// 计划订单上传接口
        /// </summary>
        /// <returns></returns>
        public string getcellvalue(ICell cell)
        {
            string cellvalue = "";
            switch (cell.CellType)
            {
                case CellType.String:
                    // Console.WriteLine(cell.StringCellValue);
                    cellvalue = cell.StringCellValue.Trim();
                    break;
                case CellType.Numeric:
                    //Console.WriteLine(cell.NumericCellValue);
                    cellvalue = cell.NumericCellValue.ToString().Trim();
                    break;
                case CellType.Boolean:
                    //Console.WriteLine(cell.BooleanCellValue);
                    cellvalue = cell.BooleanCellValue.ToString().Trim();
                    break;
                case CellType.Error:
                    // Console.WriteLine(cell.ErrorCellValue);
                    cellvalue = cell.ErrorCellValue.ToString().Trim();
                    break;
                    // 可以添加其他类型的case
            }
            return cellvalue;
        }
        #endregion
        #region 通用上传接口====================================================
        /// <summary>