/// <summary> /// 保存表单(新增、修改) /// </summary> /// <param name="keyValue">主键值</param> /// <param name="entity">实体对象</param> /// <returns></returns> public void SaveForm(string keyValue, PayitemEntity entity) { try { PayitemService payservice = new PayitemService(); if (entity.itemcode == null || entity.itemcode == "") { entity.itemcode = payservice.GetKey(10); entity.CreatorId = 1; entity.disable = "1"; entity.CreatorName = Code.OperatorProvider.Provider.Current().UserName; } service.SaveForm(keyValue, entity); } catch (Exception) { throw; } }
/// <summary> /// 保存表单(新增、修改) /// </summary> /// <param name="keyValue">主键值</param> /// <param name="entity">实体对象</param> /// <returns></returns> public void SaveForm(string keyValue, string inport, PayrollEntity entity) { try { string searchColumnName = "员工编号"; string totalColumnName = "总计"; if (inport != "") { // 1.导入功能 DataTable dt = ExcelHelper.ExcelImport(Utils.GetMapPath(inport)); // 2.取出所有得薪资项 PayitemService payservice = new PayitemService(); IEnumerable <PayitemEntity> payitemlist = payservice.GetList(w => w.disable == "1"); #region 3.取出所有的人员信息 List <string> employIds = new List <string>(); foreach (DataRow row in dt.Rows) { if (dt.Columns.Contains(searchColumnName)) { employIds.Add(row[searchColumnName].ToString()); } } EmployinfoService employservice = new EmployinfoService(); IEnumerable <EmployinfoEntity> employlist = employservice.GetAllList(employIds); #endregion #region 4.获取excel中需要遍历的薪资项 List <string> payitems = new List <string>(); foreach (PayitemEntity item in payitemlist) { if (dt.Columns.Contains(item.dispName)) { payitems.Add(item.dispName); } } #endregion PayrollService payrollService = new PayrollService(); int PayrollId = payrollService.FindMaxID() + 1; int employCount = 0; // 本次导入人数 decimal TotalAmount = 0; // 本次应发总金额 #region 5.新增子表 List <PaydetailEntity> detailList = new List <PaydetailEntity>(); PaydetailEntity detailEntity = null; if (dt != null && dt.Rows.Count > 0) { PaydetailService detailService = new PaydetailService(); foreach (DataRow row in dt.Rows) { int?empid = 0; // 员工编号 #region 获取员工信息 if (row[searchColumnName] != null && row[searchColumnName].ToString().Length != 0) { EmployinfoEntity employEntity = employlist.Where(w => w.empid == Convert.ToInt32(row[searchColumnName])).FirstOrDefault(); if (employEntity != null) { empid = employEntity.empid; employCount++; } else { continue; } } #endregion #region 总计列不存在,或者 总计 非数字类型,不记入数据库 if (row[totalColumnName] == null || row[totalColumnName].ToString().Length == 0) { continue; } else { try { TotalAmount += decimal.Round(Convert.ToDecimal(row[totalColumnName]), 2, MidpointRounding.AwayFromZero); } catch (Exception) { continue; } } #endregion #region 遍历行中的项,一项存一条数据 foreach (string item in payitems) { detailEntity = new PaydetailEntity(); detailEntity.empid = empid; PayitemEntity itemEntity = payitemlist.Where(w => w.dispName == item).FirstOrDefault(); if (itemEntity != null) { detailEntity.itemcode = itemEntity.itemcode; detailEntity.amount = Convert.ToDecimal(row[itemEntity.dispName]); } detailEntity.payrollid = PayrollId; detailEntity.CreatorName = Code.OperatorProvider.Provider.Current().UserName;; detailEntity.CreateDate = DateTime.Now; detailList.Add(detailEntity); } #endregion } } #endregion #region 6.新增主表 // 组合主表字段 PayrollEntity parollEntity = new PayrollEntity(); parollEntity.PayrollId = PayrollId; parollEntity.period = entity.period; parollEntity.status = 0; parollEntity.employnum = employCount; parollEntity.Totalamount = decimal.Round(TotalAmount, 2, MidpointRounding.AwayFromZero); parollEntity.CreatorId = 0; parollEntity.CreatorName = Code.OperatorProvider.Provider.Current().UserName; parollEntity.CreateDate = DateTime.Now; #endregion payrollService.SaveUploadData(parollEntity, detailList); } } catch (Exception) { throw; } }