| | |
| | | using System.Collections.Generic; |
| | | using System.Data; |
| | | using System.Data.SqlClient; |
| | | using System.Web; |
| | | using System.Web.Http; |
| | | using WebAPI.Models; |
| | | using WebAPI.Controllers.SCGL.æ¥è®¡å管ç; |
| | | using System.IO; |
| | | using System.Linq; |
| | | using System.Text; |
| | | |
| | | namespace WebAPI.Controllers |
| | | { |
| | |
| | | #endregion |
| | | |
| | | |
| | | |
| | | #region æä»¶ä¸ä¼ |
| | | [Route("CB_ItemMoneyBillController/CB_ItemMoneyBill_Excel")] |
| | | [HttpPost] |
| | | public json CB_ItemMoneyBill_Excel() |
| | | { |
| | | json res = new json(); |
| | | try |
| | | { |
| | | //è·åæä»¶åç§° |
| | | var file = HttpContext.Current.Request.Files[0]; |
| | | //è·åæä»¶ç©çè·¯å¾ |
| | | string ExcelPath = HttpContext.Current.Server.MapPath("~/" + file.FileName); |
| | | //ä¿åæä»¶ |
| | | file.SaveAs(ExcelPath); |
| | | |
| | | SCGL.æ¥è®¡å管ç.NpoiHelper np = new NpoiHelper(); |
| | | DataSet ExcelDs = np.ReadExcel(ExcelPath, 1, 1, "0"); |
| | | |
| | | //å 餿件 |
| | | File.Delete(ExcelPath); |
| | | |
| | | //å建临æ¶è¡¨ |
| | | 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()); |
| | | } |
| | | provisional.Columns.Add("åæ®å·", typeof(string)); |
| | | |
| | | //æ·»å æ°æ® |
| | | 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) |
| | | { |
| | | res.code = "0"; |
| | | res.count = 0; |
| | | res.Message = $"Excel模æ¿åå¨é误,{error}\r\n"; |
| | | res.data = null; |
| | | return res; |
| | | } |
| | | |
| | | for (int i = 0; i <= provisional.Rows.Count - 1; i++) |
| | | { |
| | | string HBillNo= DBUtility.ClsPub.CreateBillCode("1802", ref DBUtility.ClsPub.sExeReturnInfo, true); |
| | | provisional.Rows[i]["åæ®å·"] = HBillNo; |
| | | string HDepNumber = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["é¨é¨ä»£ç "].ToString()); |
| | | string HDepName = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["é¨é¨"].ToString()); |
| | | |
| | | string HEmpNumber = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["èå代ç "].ToString()); |
| | | string HEmpName = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["èå"].ToString()); |
| | | |
| | | string HICMOBillNo = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["ç产订åå·"].ToString()); |
| | | |
| | | string HCostItemNumber = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["ææ¬é¡¹ç®ä»£ç "].ToString()); |
| | | string HCostItemName = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["ææ¬é¡¹ç®"].ToString()); |
| | | |
| | | string HEntryID = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["å表å
ç "].ToString()); |
| | | |
| | | string HDepNumber_sub = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["åé¨é¨ä»£ç "].ToString()); |
| | | string HDepName_sub = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["åé¨é¨"].ToString()); |
| | | |
| | | string HMaterNumber = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["ç©æç¼ç "].ToString()); |
| | | string HMaterName = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["ç©æåç§°"].ToString()); |
| | | |
| | | string HQty = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["æ°é"].ToString()); |
| | | string HPrice = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["åä»·"].ToString()); |
| | | string HMoney = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["éé¢"].ToString()); |
| | | string HWaster = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["æèé"].ToString()); |
| | | string HRelationQty = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["å
³èæ°é"].ToString()); |
| | | string HRelationMoney = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["å
³èéé¢"].ToString()); |
| | | string remark = DBUtility.ClsPub.isStrNull(provisional.Rows[i]["夿³¨"].ToString()); |
| | | |
| | | //è·åçå®è¡æ° |
| | | int line = i + 1; |
| | | } |
| | | |
| | | res.code = "1"; |
| | | res.count = 1; |
| | | res.Message = error; |
| | | res.data = provisional; |
| | | return res; |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | res.code = "0"; |
| | | res.count = 0; |
| | | res.Message = "Exceptionï¼" + e.ToString(); |
| | | res.data = null; |
| | | return res; |
| | | } |
| | | } |
| | | |
| | | /// <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; |
| | | } |
| | | #endregion |
| | | |
| | | #region ä¸ä¼ ä¿å |
| | | [Route("CB_ItemMoneyBillController/Excel_btnSave")] |
| | | [HttpPost] |
| | | public object Excel_btnSave([FromBody] JObject sMainSub) |
| | | { |
| | | |
| | | var _value = sMainSub["sMainSub"].ToString(); |
| | | string msg1 = _value.ToString(); |
| | | string[] sArray = msg1.Split(new string[] { "&å" }, StringSplitOptions.RemoveEmptyEntries); |
| | | string msg2 = sArray[0].ToString(); |
| | | string user = sArray[1].ToString(); |
| | | string organ = sArray[2].ToString(); |
| | | json res = new json(); |
| | | |
| | | try |
| | | { |
| | | oCN.BeginTran(); |
| | | List<object> Excel = Newtonsoft.Json.JsonConvert.DeserializeObject<List<object>>(msg2); |
| | | List<Dictionary<string, string>> list = new List<Dictionary<string, string>>(); |
| | | |
| | | foreach (JObject item in Excel) |
| | | { |
| | | Dictionary<string, string> dic = new Dictionary<string, string>(); |
| | | foreach (var itm in item.Properties()) |
| | | { |
| | | dic.Add(itm.Name, itm.Value.ToString()); |
| | | } |
| | | list.Add(dic); |
| | | } |
| | | |
| | | // 1. æ¶éææéè¦æ¥è¯¢çæ°æ® |
| | | var HBillnos = list.Select(x => x["åæ®å·"].ToString()).Distinct().ToList(); |
| | | var HDeptNames = list.Select(x => x["é¨é¨"].ToString()).Distinct().ToList(); |
| | | var HDeptNumbers = list.Select(x => x["é¨é¨ä»£ç "].ToString()).Distinct().ToList(); |
| | | var HDeptNames_sub = list.Select(x => x["åé¨é¨"].ToString()).Distinct().ToList(); |
| | | var HDeptNumbers_sub = list.Select(x => x["åé¨é¨ä»£ç "].ToString()).Distinct().ToList(); |
| | | var materialNumbers = list.Select(x => x["ç©æç¼ç "].ToString()).Distinct().ToList(); |
| | | var materialNames = list.Select(x => x["ç©æåç§°"].ToString()).Distinct().ToList(); |
| | | var EmpNumbers = list.Select(x => x["èå代ç "].ToString()).Distinct().ToList(); |
| | | var EmpNames = list.Select(x => x["èå"].ToString()).Distinct().ToList(); |
| | | var CostNumbers = list.Select(x => x["ææ¬é¡¹ç®ä»£ç "].ToString()).Distinct().ToList(); |
| | | var CostNames = list.Select(x => x["ææ¬é¡¹ç®"].ToString()).Distinct().ToList(); |
| | | |
| | | |
| | | // 2. æ¹éæ¥è¯¢ä¸»å表é¨é¨ä¿¡æ¯ |
| | | string supplierQuery = $"SELECT HItemID, HName FROM Gy_Department WHERE HName IN ({string.Join(",", HDeptNames.Select(s => $"'{s.Replace("'", "''")}'"))})"; |
| | | DataSet supData = oCN.RunProcReturn(supplierQuery, "Gy_Department"); |
| | | Dictionary<string, int> supplierDict = new Dictionary<string, int>(); |
| | | foreach (DataRow row in supData.Tables[0].Rows) |
| | | { |
| | | supplierDict[row["HName"].ToString()] = (int)row["HItemID"]; |
| | | } |
| | | |
| | | // 2.1 æ¹éæ¥è¯¢ä¸»å表é¨é¨ä¿¡æ¯ |
| | | string supplierQuery_sub = $"SELECT HItemID, HName FROM Gy_Department WHERE HName IN ({string.Join(",", HDeptNames_sub.Select(s => $"'{s.Replace("'", "''")}'"))})"; |
| | | DataSet supData_sub = oCN.RunProcReturn(supplierQuery, "Gy_Department"); |
| | | Dictionary<string, int> supplierDict_sub = new Dictionary<string, int>(); |
| | | foreach (DataRow row in supData_sub.Tables[0].Rows) |
| | | { |
| | | supplierDict_sub[row["HName"].ToString()] = (int)row["HItemID"]; |
| | | } |
| | | |
| | | // 3. æ¹éæ¥è¯¢ç©æä¿¡æ¯ |
| | | string materialQuery = |
| | | $"SELECT HItemID, HNumber, HName FROM Gy_Material WHERE HNumber IN ({string.Join(",", materialNumbers.Select(m => $"'{m.Replace("'", "''")}'"))}) AND HName IN ({string.Join(",", materialNames.Select(m => $"'{m.Replace("'", "''")}'"))})"; |
| | | DataSet materData = oCN.RunProcReturn(materialQuery, "Gy_Material"); |
| | | Dictionary<string, int> materialDict = new Dictionary<string, int>(); |
| | | foreach (DataRow row in materData.Tables[0].Rows) |
| | | { |
| | | string key = $"{row["HNumber"]}_{row["HName"]}"; |
| | | materialDict[key] = (int)row["HItemID"]; |
| | | } |
| | | |
| | | // 3. æ¹éæ¥è¯¢èåä¿¡æ¯ |
| | | string EmployeeQuery = |
| | | $"SELECT HItemID, HNumber, HName FROM Gy_Employee WHERE HNumber IN ({string.Join(",", EmpNumbers.Select(m => $"'{m.Replace("'", "''")}'"))}) AND HName IN ({string.Join(",", EmpNames.Select(m => $"'{m.Replace("'", "''")}'"))})"; |
| | | DataSet EmpData = oCN.RunProcReturn(EmployeeQuery, "Gy_Employee"); |
| | | Dictionary<string, int> EmpDict = new Dictionary<string, int>(); |
| | | foreach (DataRow row in EmpData.Tables[0].Rows) |
| | | { |
| | | string key = $"{row["HName"].ToString()}"; |
| | | EmpDict[key] = (int)row["HItemID"]; |
| | | } |
| | | |
| | | // 3. æ¹éæ¥è¯¢chengbenxiangmuä¿¡æ¯ |
| | | string CostQuery = $"SELECT HItemID, HNumber, HName FROM Gy_ItemMoney_1 WHERE HNumber IN ({string.Join(",", CostNumbers.Select(m => $"'{m.Replace("'", "''")}'"))}) AND HName IN ({string.Join(",", CostNames.Select(m => $"'{m.Replace("'", "''")}'"))})"; |
| | | DataSet CostData = oCN.RunProcReturn(CostQuery, "Gy_ItemMoney_1"); |
| | | Dictionary<string, int> CostDict = new Dictionary<string, int>(); |
| | | foreach (DataRow row in CostData.Tables[0].Rows) |
| | | { |
| | | string key = $"{row["HName"].ToString()}"; |
| | | CostDict[key] = (int)row["HItemID"]; |
| | | } |
| | | |
| | | |
| | | // 4. æ¥è¯¢ç¨æ·ä¿¡æ¯ |
| | | DataSet emp = oCN.RunProcReturn($"SELECT Czybm FROM Gy_Czygl WHERE Czymc = '{user.Replace("'", "''")}'", "Gy_Czygl"); |
| | | if (emp.Tables[0].Rows.Count == 0) |
| | | { |
| | | res.code = "0"; |
| | | res.count = 0; |
| | | res.Message = "ç¨æ·ä¸åå¨ï¼"; |
| | | res.data = null; |
| | | return res; |
| | | } |
| | | string HUserID = emp.Tables[0].Rows[0]["Czybm"].ToString(); |
| | | |
| | | // 5. æ£æ¥ç¼ºå¤±çé¨é¨åç©æï¼å¹¶è®°å½è¡å· |
| | | List<string> missingSupplierMessages = new List<string>(); |
| | | List<string> missingMaterialMessages = new List<string>(); |
| | | |
| | | // é忝ä¸è¡æ°æ®ï¼è®°å½è¡å·ï¼ä»1å¼å§ï¼ |
| | | for (int i = 0; i < list.Count; i++) |
| | | { |
| | | var item = list[i]; |
| | | int lineNumber = i + 1; // è¡å·ä»1å¼å§ |
| | | |
| | | string supplier = item["é¨é¨"]; |
| | | string materialNumber = item["ç©æç¼ç "]; |
| | | string materialName = item["ç©æåç§°"]; |
| | | string materialKey = $"{materialNumber}_{materialName}"; |
| | | |
| | | // æ£æ¥ä¾åºå |
| | | if (!supplierDict.ContainsKey(supplier)) |
| | | { |
| | | missingSupplierMessages.Add($"第{lineNumber}è¡çé¨é¨ã{supplier}ã"); |
| | | } |
| | | |
| | | // æ£æ¥ç©æ |
| | | if (!materialDict.ContainsKey(materialKey)) |
| | | { |
| | | missingMaterialMessages.Add($"第{lineNumber}è¡çç©æã{materialName}(ç¼ç :{materialNumber})ã"); |
| | | } |
| | | } |
| | | |
| | | StringBuilder errorMessage = new StringBuilder(); |
| | | |
| | | if (missingSupplierMessages.Any()) |
| | | { |
| | | errorMessage.Append("以ä¸é¨é¨æªç»´æ¤ï¼"); |
| | | errorMessage.Append(string.Join("ã", missingSupplierMessages)); |
| | | errorMessage.Append("ï¼"); |
| | | } |
| | | |
| | | if (missingMaterialMessages.Any()) |
| | | { |
| | | errorMessage.Append("以ä¸ç©ææªç»´æ¤ï¼"); |
| | | errorMessage.Append(string.Join("ã", missingMaterialMessages)); |
| | | errorMessage.Append("ï¼"); |
| | | } |
| | | |
| | | if (errorMessage.Length > 0) |
| | | { |
| | | res.code = "0"; |
| | | res.count = 0; |
| | | res.Message = errorMessage.ToString(); |
| | | res.data = null; |
| | | return res; |
| | | } |
| | | |
| | | // 6. æ¹éæ¥è¯¢å·²åå¨çè®°å½ |
| | | var materialIds = list.Select(x => materialDict[$"{x["ç©æç¼ç "]}_{x["ç©æåç§°"]}"]).Distinct().ToList(); |
| | | var supplierIds = list.Select(x => supplierDict_sub[x["é¨é¨"]]).Distinct().ToList(); |
| | | |
| | | string existQuery = $"select * from h_v_CB_ItemMoneyBillMain_Edit_excel WHERE HMaterID IN ({string.Join(",", materialIds)}) AND HDeptID_sub IN ({string.Join(",", supplierIds)})"; |
| | | DataSet existData = oCN.RunProcReturn(existQuery, "h_v_CB_ItemMoneyBillMain_Edit_excel"); |
| | | HashSet<string> existRecords = new HashSet<string>(); |
| | | foreach (DataRow row in existData.Tables[0].Rows) |
| | | { |
| | | existRecords.Add($"{row["HMaterID"]}_{row["HDeptID_sub"]}"); |
| | | } |
| | | |
| | | StringBuilder insertSql = new StringBuilder(); |
| | | StringBuilder insertSql_sub = new StringBuilder(); |
| | | StringBuilder updateSql = new StringBuilder(); |
| | | StringBuilder updateSql_sub = new StringBuilder(); |
| | | string currentDate = DateTime.Today.ToString("yyyy-MM-dd"); |
| | | int HEntryID = 1; |
| | | foreach (Dictionary<string, string> item in list) |
| | | { |
| | | int HSupplierID = supplierDict_sub[item["é¨é¨"]]; |
| | | int EmpID = EmpDict[item["èå"]];// |
| | | int HCostID = CostDict[item["ææ¬é¡¹ç®"]] ;//EmpDict[item["èå"]]; |
| | | int HMaterID = materialDict[$"{item["ç©æç¼ç "]}_{item["ç©æåç§°"]}"]; |
| | | string recordKey = $"{HMaterID}_{HSupplierID}"; |
| | | long HInterID = DBUtility.ClsPub.CreateBillID("1802", ref DBUtility.ClsPub.sExeReturnInfo); |
| | | if (!existRecords.Contains(recordKey)) |
| | | { |
| | | |
| | | insertSql.AppendLine($"INSERT INTO CB_ItemMoneyBillMain ([HInterID], [HBillNo], [HDate], [HYear], [HPeriod], [HBillType], [HBillSubType], [HBillStatus], [HCheckItemNowID], [HCheckItemNextID], [HCheckFlowID], [HRemark], [HBacker], [HBackDate], [HBackRemark], [HChecker], [HCheckDate], [HMaker], [HMakeDate], [HUpDater], [HUpDateDate], [HCloseMan], [HCloseDate], [HCloseType], [HDeleteMan], [HDeleteDate], [HMainSourceBillType], [HMainSourceInterID], [HMainSourceEntryID], [HMainSourceBillNo], [HPrintQty], [HEmpID], [HDeptID], [HICMOInterID], [HMaterID], [HICMOBillNo], [HExplanation], [HInnerBillNo], [HBeginDate], [HEndDate], [HOrgID]) VALUES ("); |
| | | insertSql.AppendLine($"'{HInterID}', '{item["åæ®å·"]}', '{DateTime.Now}', {DateTime.Now.Year}, {DateTime.Now.Month}, {"1802"}, '{"1802"}', '{"1"}', '{"0"}', '{"0"}', '{"0"}', '{"excel导å
¥"}', '{""}', '{DateTime.Now}', '{""}', '{""}', '{DateTime.Now}', '{""}', '{DateTime.Now}', '{""}', '{DateTime.Now}', '{""}', '{DateTime.Now}', '{"0"}', '{""}', '{DateTime.Now}', '{""}', '{"0"}', '{"0"}', '{""}', '{"0"}', '{EmpID}', '{HSupplierID}', '{"0"}','{HMaterID}','{"0"}','{""}','{"0"}','{DateTime.Now}','{DateTime.Now}','{"100038"}');"); |
| | | |
| | | insertSql_sub.AppendLine($"INSERT INTO [CB_ItemMoneyBillSub] ([HInterID], [HEntryID], [HCloseMan], [HEntryCloseDate], [HCloseType], [HRemark],[HSourceInterID], [HSourceEntryID], [HSourceBillNo], [HSourceBillType],[HRelationQty], [HRelationMoney], [HBillNo_bak], [HCostItemID],[HDeptID], [HQty], [HPrice], [HMoney],[HMaterID], [HWaster], [HDate], [HBeginBalance], [HEndBalance], [HTimes]) VALUES ("); |
| | | insertSql_sub.AppendLine($"'{HInterID}', '{HEntryID}', '{""}', '{DateTime.Now}', '{"0"}', '{"excel导å
¥"}','{"0"}', '{"0"}', '{"0"}', '{"0"}','{item["å
³èæ°é"]}', '{item["å
³èéé¢"]}', '{""}', '{HCostID}', '{HSupplierID}', '{item["æ°é"]}', '{item["åä»·"]}', '{item["éé¢"]}','{HMaterID}', '{item["æèé"]}', '{DateTime.Now}', '{"0"}', '{"0"}', '{"0"}');"); |
| | | HEntryID++; |
| | | } |
| | | else |
| | | { |
| | | // æ´æ°è¯å¥ |
| | | updateSql.AppendLine($"UPDATE CB_ItemMoneyBillMain SET HDate = '{currentDate}', HRemark = '{item["夿³¨"]}', HEmpID = '{EmpID}', HMaterID = '{HMaterID}' , HDeptID = '{HSupplierID}' WHERE HBillNo = '{item["åæ®å·"]}'"); |
| | | updateSql_sub.AppendLine($"UPDATE CB_ItemMoneyBillSub SET HDate = '{currentDate}', HQty = '{item["æ°é"]}', HDeptID = '{HSupplierID}', HPrice = '{item["åä»·"]}', HRelationQty = '{item["å
³èæ°é"]}', HRelationMoney = '{item["å
³èéé¢"]}', HMoney = '{item["éé¢"]}', HWaster = '{item["æèé"]}', HMaterID = '{HMaterID}' WHERE HInterID = {HInterID};"); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | // æ§è¡æ¹éæä½ |
| | | if (insertSql.Length > 0) |
| | | { |
| | | oCN.RunProc(insertSql.ToString()); |
| | | oCN.RunProc(insertSql_sub.ToString()); |
| | | } |
| | | |
| | | if (updateSql.Length > 0) |
| | | { |
| | | oCN.RunProc(updateSql.ToString()); |
| | | oCN.RunProc(updateSql_sub.ToString()); |
| | | } |
| | | |
| | | |
| | | oCN.Commit(); |
| | | |
| | | res.code = "1"; |
| | | res.count = 1; |
| | | res.Message = "导å
¥æå!"; |
| | | res.data = null; |
| | | return res; |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | //oCN.Rollback(); |
| | | LogService.Write(e); |
| | | res.code = "0"; |
| | | res.count = 0; |
| | | res.Message = "Exceptionï¼" + e.ToString(); |
| | | res.data = null; |
| | | return res; |
| | | } |
| | | } |
| | | #endregion |
| | | |
| | | } |
| | | } |