pzy
2024-10-03 0092c2dd790c0e41a06e773db937beed932abd7c
task测试
3个文件已添加
306 ■■■■■ 已修改文件
WebAPI/Utility/TreeUtil.cs 74 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
WebAPI/WebBLL/JCJM/GYZL/Gy_CustomerService.cs 25 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
WebAPI/WebBLL/JCJM/IMPL/Gy_CustomerImpl.cs 207 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
WebAPI/Utility/TreeUtil.cs
New file
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
namespace WebAPI.Utility
{
    public class TreeUtil
    {
        public class TreeModel
        {
            public string id { get; set; }
            public string title { get; set; }
            public List<TreeModel> children = new List<TreeModel>();
        }
        #region 递归函数
        /// <summary>
        /// 递归函数
        /// </summary>
        public void digui(DataTable dt, List<TreeModel> tree, int num)
        {
            for (int m = 0; m < tree.Count; m++)
            {
                tree[m].children = new List<TreeModel>();
                for (int i = 0; i < dt.Rows.Count; i++)//第一次循环,得到所有根节点的子集
                {
                    var strLen = dt.Rows[i]["hnumber"].ToString().Split('.');
                    if (strLen.Length == num && dt.Rows[i]["hnumber"].ToString().Contains(tree[m].id + "."))
                    {
                        TreeModel tbjson = new TreeModel();
                        tbjson.id = dt.Rows[i]["hitemid"].ToString();
                        tbjson.title = dt.Rows[i]["hname"].ToString();
                        tree[m].children.Add(tbjson);
                    }
                }
                var strLens = tree[m].id.Split('.');
                for (int i = 0; i < tree[m].children.Count; i++)
                {
                    digui(dt, tree[m].children, strLens.Length + 2);//再次用子集去循环,拿出子集的子集
                }
            }
        }
        #endregion
        #region 分类 树形图(根据代码展开树状图)
        public void getTreeByLevel(DataTable dt, List<TreeModel> tree, int num)
        {
            for (int m = 0; m < tree.Count; m++)
            {
                tree[m].children = new List<TreeModel>();
                for (int i = 0; i < dt.Rows.Count; i++)//第一次循环,得到所有根节点的子集
                {
                    var HLevel = (int)dt.Rows[i]["hlevel"];
                    var HParentID = dt.Rows[i]["hparentid"].ToString();
                    if (HLevel == num && HParentID == tree[m].id)
                    {
                        TreeModel tbjson = new TreeModel();
                        tbjson.id = dt.Rows[i]["hitemid"].ToString();
                        tbjson.title = dt.Rows[i]["hname"].ToString();
                        tree[m].children.Add(tbjson);
                    }
                }
                for (int i = 0; i < tree[m].children.Count; i++)
                {
                    getTreeByLevel(dt, tree[m].children, num + 1);//再次用子集去循环,拿出子集的子集
                }
            }
        }
        #endregion
    }
}
WebAPI/WebBLL/JCJM/GYZL/Gy_CustomerService.cs
New file
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebAPI.WebBLL.JCJM.GYZL
{
    interface Gy_CustomerService
    {
        /// <summary>
        /// 导入excel
        /// </summary>
        void ImportExcel();
        /// <summary>
        /// 上传excel
        /// </summary>
        /// <returns></returns>
        DataTable UploadExcel(DataSet ExcelDs);
    }
}
WebAPI/WebBLL/JCJM/IMPL/Gy_CustomerImpl.cs
New file
@@ -0,0 +1,207 @@
using SyntacticSugar.constant;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using WebAPI.Exceptions;
using WebAPI.WebBLL.JCJM.GYZL;
namespace WebAPI.WebBLL.JCJM.IMPL
{
    public class Gy_CustomerImpl : Gy_CustomerService
    {
        // 线程计数器
        List<Task> taskList = new List<Task>();
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
        #region 导入EXCEL
        public void ImportExcel()
        {
            throw new NotImplementedException();
        }
        #endregion
        /// <summary>
        /// 判断列
        /// </summary>
        /// <param name="provisional"></param>
        /// <returns></returns>
        private static string JudgmentColumns(DataTable provisional)
        {
            var error = "";
            //查询没有的列
            if (!provisional.Columns.Contains("客户代码"))
                error += "没有找到【客户代码】的标题,";
            if (!provisional.Columns.Contains("客户名称"))
                error += "没有找到【客户名称】的标题,";
            return error;
        }
        #region 上传EXCEL
        public DataTable UploadExcel(DataSet ExcelDs)
        {
            //创建临时表
            DataTable provisional = new DataTable("dt2");
            //添加列名
            for (int i = 0; i < ExcelDs.Tables[0].Columns.Count; i++)
            {
                provisional.Columns.Add(ExcelDs.Tables[0].Rows[0][i].ToString());
            }
            //添加数据
            for (int i = 1; i < ExcelDs.Tables[0].Rows.Count; i++)
            {
                DataRow row = provisional.NewRow();
                for (int j = 0; j < ExcelDs.Tables[0].Columns.Count; j++)
                {
                    row[j] = ExcelDs.Tables[0].Rows[i][j].ToString();
                }
                provisional.Rows.Add(row);
            }
            //判断列
            string error = JudgmentColumns(provisional);
            if (error.Length > 0)
            {
                throw new ExcelException($"Excel模板存在错误,{error}\r\n");
            }
            for (int i = 0; i <= provisional.Rows.Count - 1; i++)
            {
                string HRegionNumber = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["地区代码"].ToString());
                string HRegionName = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["地区名称"].ToString());
                string HCurrencyNumber = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["默认货币代码"].ToString());
                string HCurrencyName = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["默认货币名称"].ToString());
                //string HBillingNumber = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["结算方式代码"].ToString());
                //string HBillingName = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["结算方式名称"].ToString());
                string HCusTypeNumber = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["客户分类代码"].ToString());
                string HCusTypeName = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["客户分类名称"].ToString());
                //记录任务
                var tasks = new List<Task>();
                //如果当前地区代码和地区名称不为空则判断是否存在
                if (!String.IsNullOrEmpty(HRegionName) || !String.IsNullOrEmpty(HRegionNumber))
                {
                    tasks.Add(JudgeRegion(HRegionNumber, HRegionName));
                }
                // 如果当前货币代码和货币名称不为空则判断是否存在
                if (!String.IsNullOrEmpty(HCurrencyName) || !String.IsNullOrEmpty(HCurrencyNumber))
                    {
                        tasks.Add(JudgeCurrency(HCurrencyNumber, HCurrencyName));
                    }
                //如果当前客户分类代码和名称
                if (!String.IsNullOrEmpty(HCusTypeName) || !String.IsNullOrEmpty(HCusTypeNumber))
                    {
                        tasks.Add(JudgeCusType(HCusTypeNumber, HCusTypeName));
                    }
                try
                {
                    //等待任务完成
                    Task.WaitAll(tasks.ToArray());
                }
                catch (AggregateException ae)
                {
                    string msg = ae.Message;
                    ae.Handle(ex => {
                        if (ex is EmptyException)
                            msg = ex.Message;
                            Console.WriteLine(ex.Message);
                        return ex is EmptyException;
                    });
                    throw new Exception("第" + i +1 +"有问题: " +msg);
                }
                //获取真实行数
                int line = i + 1;
            }
            return provisional;
        }
        /// <summary>
        /// 判断地区是否正确
        /// </summary>
        private Task<int> JudgeRegion(string HNumber, string HName)
        {
            return Task<int>.Factory.StartNew(() =>
            {
                try
                {
                    SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                    DataSet ds = oCN.RunProcReturn("select * from Gy_AreaSet where HNumber = '" + HNumber + "' and HName = '" + HName + "'", "Gy_AreaSet");
                    if (ds.Tables[0].Rows.Count == EmptyConstant.EMPTY)
                    {
                        throw new EmptyException("当前地区不存在,请重新选择");
                    }
                    return (int)ds.Tables[0].Rows[0][0];
                }
                catch (Exception e)
                {
                    throw new EmptyException(e.Message);
                }
            });
        }
        /// <summary>
        /// 判断货币是否正确
        /// </summary>
        private Task<int> JudgeCurrency(string HNumber, string HName)
        {
            {
                return Task<int>.Factory.StartNew(() =>
                {
                    try
                    {
                        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                        DataSet ds = oCN.RunProcReturn("select * from Gy_Currency where HNumber = '" + HNumber + "' and HName = '" + HName + "'", "Gy_Currency");
                        if (ds.Tables[0].Rows.Count == EmptyConstant.EMPTY)
                        {
                            throw new EmptyException("当前货币不存在,请重新选择");
                        }
                        return (int)ds.Tables[0].Rows[0][0];
                    }
                    catch (Exception e)
                    {
                        throw new EmptyException(e.Message);
                    }
                });
            }
        }
        /// <summary>
        /// 判断客户分类
        /// </summary>
        private Task<int> JudgeCusType(string HNumber, string HName)
        {
            {
                return Task<int>.Factory.StartNew(() =>
                {
                    try
                    {
                        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                        DataSet ds = oCN.RunProcReturn("select * from Gy_CusType where HNumber = '" + HNumber + "' and HName = '" + HName + "'", "Gy_CusType");
                        if (ds.Tables[0].Rows.Count == EmptyConstant.EMPTY)
                        {
                            throw new EmptyException("当前分类不存在,请重新选择");
                        }
                        return (int)ds.Tables[0].Rows[0][0];
                    }
                    catch (Exception e)
                    {
                        throw new EmptyException(e.Message);
                    }
                });
            }
        }
        #endregion
    }
}