Przeglądaj źródła

获取省市县乡镇导入excel

duhongyu 6 lat temu
rodzic
commit
650b3b31b1

+ 109 - 40
codegit/CallCenterApi/CallCenterApi.Interface/CallCenterApi.Interface/Controllers/customer/RegionCategoryController.cs

@@ -2,9 +2,11 @@
2 2
 using CallCenterApi.Common;
3 3
 using CallCenterApi.Interface.Controllers.Base;
4 4
 using CallCenterApi.Interface.Models.Input;
5
+using CallCenterApi.Model;
5 6
 using System;
6 7
 using System.Collections.Generic;
7 8
 using System.Data;
9
+using System.IO;
8 10
 using System.Linq;
9 11
 using System.Web;
10 12
 using System.Web.Mvc;
@@ -14,66 +16,134 @@ namespace CallCenterApi.Interface.Controllers.customer
14 16
     public class RegionCategoryController : BaseController
15 17
     {
16 18
         private BLL.T_RegionCategory regionBLL = new BLL.T_RegionCategory();
17
-
18 19
         /// <summary>
19
-        /// 获取地市列表
20
+        /// 获取
20 21
         /// </summary>
21 22
         /// <returns></returns>
22
-        public ActionResult GetAllList()
23
+        public ActionResult GetProvinceList()
23 24
         {
25
+        
26
+            return NoToken("未知错误,请重新登录");
24 27
             if (Request.IsAuthenticated)
25 28
             {
26 29
                 DataTable dt = new DataTable();
27
-                dt = new BLL.T_RegionCategory().GetList(" F_DeleteFlag=0 ").Tables[0];
30
+                dt = new BLL.T_RegionCategory().GetList(" F_Layer=1 ").Tables[0];
28 31
                 return Success("加载成功", dt);
29 32
             }
30 33
             return NoToken("未知错误,请重新登录");
31 34
         }
32
-
33 35
         /// <summary>
34
-        /// 获取地市列表
36
+        /// 获取省市县乡镇
35 37
         /// </summary>
36 38
         /// <returns></returns>
37
-        public ActionResult GetList()
39
+        public ActionResult GetAllList(int F_Layer, int F_RegionId = 0)
38 40
         {
39
-            ActionResult res = NoToken("未知错误,请重新登录");
40
-                DataTable dt = new DataTable();
41
-                string pid = HttpUtility.UrlDecode(RequestString.GetQueryString("pid"));
42
-                string strpageindex = RequestString.GetQueryString("page");
43
-                int pageindex = 1;
44
-                string strpagesize = RequestString.GetQueryString("pagesize");
45
-                int pagesize = 10;
46
-
47
-                string sql = "";
48
-                if (strpageindex.Trim() != "")
41
+            var entityJson = new BLL.T_RegionCategory().GetModelList("F_Layer = " + F_Layer + "AND F_ParentId=" + F_RegionId);
42
+            if (entityJson.Count > 0)
43
+            {
44
+                return Success("加载成功", entityJson);
45
+            }
46
+            else
47
+            {
48
+                return Success("获取失败,内容为空");
49
+            }
50
+        }
51
+        /// <summary>
52
+        /// 导入excel
53
+        /// </summary>
54
+        /// <returns></returns>
55
+        public ActionResult ImportExcel(int F_Layer, int F_RegionId = 0)
56
+        {
57
+            string userscode = CurrentUser.UserData.F_UserCode;
58
+            HttpPostedFile _upFile = RequestString.GetFile("upFile");
59
+            if (_upFile != null)
60
+            {
61
+                int headrow = 0;
62
+                #region 上传文件
63
+                string filepath = "";
64
+                string datepath = DateTime.Now.ToString("yyyyMMddHHMMss");
65
+                string aLastName = Path.GetExtension(_upFile.FileName);
66
+                string oriname = Path.GetFileNameWithoutExtension(_upFile.FileName);
67
+                if (aLastName != "xls" && aLastName != "xlsx")
49 68
                 {
50
-                    pageindex = Convert.ToInt32(strpageindex);
69
+                    return Error("文件类型错误,请选择Excel文件");
51 70
                 }
52
-
53
-                if (strpagesize.Trim() != "")
71
+                string newpath = datepath + "_" + _upFile.FileName;
72
+                if (!Directory.Exists(Server.MapPath(this.Request.ApplicationPath + "\\ExcelData")))
54 73
                 {
55
-                    pagesize = Convert.ToInt32(strpagesize);
74
+                    Directory.CreateDirectory(Server.MapPath(this.Request.ApplicationPath + "\\ExcelData"));
56 75
                 }
57
-                int recordCount = 0;
58
-                dt = BLL.PagerBLL.GetListPager(
59
-                    "T_RegionCategory",
60
-                    "F_RegionId",
61
-                    "*",
62
-                    sql,
63
-                    "ORDER BY F_RegionId desc",
64
-                    pagesize,
65
-                    pageindex,
66
-                    true,
67
-                    out recordCount);
68
-
69
-                var obj = new
76
+                filepath = this.Request.ApplicationPath + "/ExcelData/" + newpath;
77
+                string PhysicalPath = Server.MapPath(filepath);
78
+                _upFile.SaveAs(PhysicalPath);
79
+                #endregion
80
+                #region 添加附件日志
81
+                Model.T_Sys_Accessories model_T_Sys_Accessories = new Model.T_Sys_Accessories();
82
+                model_T_Sys_Accessories.F_AddTime = DateTime.Now;//上传时间
83
+                model_T_Sys_Accessories.F_FileName = newpath;//附件名称
84
+                model_T_Sys_Accessories.F_FileType = aLastName;//附件类型
85
+                model_T_Sys_Accessories.F_FileUrl = filepath;//附件地址
86
+                model_T_Sys_Accessories.F_UserCode = userscode;//上传人  
87
+                model_T_Sys_Accessories.F_OriName = oriname;
88
+                int fid = new BLL.T_Sys_Accessories().Add(model_T_Sys_Accessories);
89
+                #endregion
90
+                NPOIHelper np = new NPOIHelper();
91
+                DataTable dt = np.ExcelToTable(_upFile, headrow);
92
+                string msg = string.Empty;
93
+                if (dt == null || dt.Rows.Count == 0)
94
+                    return Error("文件没有数据");
95
+                else
70 96
                 {
71
-                    rows = dt,
72
-                    total = recordCount
73
-                };
97
+                    foreach (DataRow dr in dt.Rows)
98
+                    {
99
+                        #region 数据入库
100
+                        headrow = headrow + 1;
74 101
 
75
-                res = Content(obj.ToJson());
76
-            return res;
102
+                        if (dr["地区名称"].ToString() != "" )
103
+                        {
104
+                           
105
+                            Model.T_RegionCategory dModel = new Model.T_RegionCategory();
106
+                            dModel.F_RegionName = dr["地区名称"].ToString();//地区名称
107
+                            dModel.F_Layer = F_Layer;
108
+                            dModel.F_ParentId = F_RegionId;
109
+                            dModel.F_CreateBy = int .Parse (userscode);
110
+                            dModel.F_CreateOn = DateTime.Now;
111
+                            dModel.F_DeleteFlag = 0;
112
+                            var res = regionBLL.Add(dModel);
113
+                            if (res > 0)
114
+                            {
115
+                                addoptlog(userscode, DTRequest.GetIP(), EnumOpt.import.ToString(), res.ToString(), "导入成功,导入的文件为:" + newpath + ",当前为第" + headrow + "行");
116
+                            }
117
+                            else
118
+                            {
119
+
120
+                                msg = msg + "第" + headrow + "行,导入失败<br>";
121
+                            }
122
+                        }
123
+                        else
124
+                            msg = msg + "第" + headrow + "行,物料编号为空或格式不正确,未导入<br>";
125
+                        #endregion
126
+                    }
127
+                    if (string.IsNullOrEmpty(msg))
128
+                        return Success("导入成功 ");
129
+                    else
130
+                        return Error(msg);
131
+                }
132
+
133
+            }
134
+            return Error("文件不能为空");
135
+        }
136
+        /// <summary>
137
+        /// 操作日志
138
+        /// </summary>
139
+        /// <param name="usercode">操作人工号</param>
140
+        /// <param name="ip">操作人IP</param>
141
+        /// <param name="optcode">操作编号</param>
142
+        /// <param name="optid">操作id</param>
143
+        /// <param name="des">操作描述</param>
144
+        private void addoptlog(string usercode, string ip, string optcode, string optid, string des)
145
+        {
146
+            new BLL.T_Sys_OperateLogs().AddOptLog("地区管理", usercode, ip, des, optcode, "T_RegionCategory", optid);
77 147
         }
78 148
 
79 149
         /// <summary>
@@ -109,7 +179,6 @@ namespace CallCenterApi.Interface.Controllers.customer
109 179
                 dModel.F_ParentId = input.F_ParentId;
110 180
                 dModel.F_Sort = input.F_Sort;
111 181
                 dModel.F_RegionName = input.F_RegionName;
112
-
113 182
                 dModel.F_DeleteFlag = 0;
114 183
                 dModel.F_CreateBy = CurrentUser.UserData.F_UserId;
115 184
                 dModel.F_CreateOn = DateTime.Now;

+ 17 - 0
codegit/CallCenterApi/CallCenterApi.Interface/CallCenterApi.Interface/Models/Input/RegionCategoryInput.cs

@@ -39,5 +39,22 @@ namespace CallCenterApi.Interface.Models.Input
39 39
             set;
40 40
             get;
41 41
         }
42
+        public class RegionCategoryList
43
+        {
44
+        
45
+            public int regionid { get; set; }
46
+            /// <summary>
47
+            ///
48
+            /// </summary>
49
+            public string regionname { get; set; }
50
+            /// <summary>
51
+            ///
52
+            /// </summary>
53
+            public int? layer { get; set; }
54
+            /// <summary>
55
+            /// 
56
+            /// </summary>
57
+            public int ?  parentid{ get; set; }
58
+        }
42 59
     }
43 60
 }