Files
zszq-trs/YLErpDAL/Modules/UnderlyingModule/SuperviseUndelryingBLL.cs
T
2024-05-09 14:06:26 +08:00

166 lines
5.4 KiB
C#

using System.Data;
using YLErp.Commons;
using YLErp.Core.DBModels;
using YLErp.Model;
using YLErp.Modules;
namespace YLErp.BLL
{
public class SuperviseUndelryingBLL : YLBaseService
{
public SuperviseUndelryingBLL(OptUserInfo userInfo) : base(userInfo)
{
}
private readonly YLContext db = new YLContext();
/// <summary>
/// 查询所有报送标的代码
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public List<supervise_undelrying> GetAll(Supervise_UndelryingReq req)
{
var undelrying = req.undelrying.TrimToNull();
var query = (from source in db.supervise_undelrying
where undelrying == null || source.undelrying.Contains(undelrying)
select source).AsQueryable().ToList();
return query;
}
/// <summary>
/// 批量删除监管报送参数
/// </summary>
public void DeleteSuperviseUndelryings(IEnumerable<int> superviseUndelryingIds)
{
var superviseUndelryingIdList = superviseUndelryingIds.ToList();
var superviseUndelryingList = DbContext.supervise_undelrying.Where(O => superviseUndelryingIdList.Contains(O.id)).ToList();
DbContext.supervise_undelrying.RemoveRange(superviseUndelryingList);
DbContext.SaveChanges();
}
/// <summary>
/// excel导入监管报送参数
/// </summary>
/// <param name="streamIn"></param>
/// <returns></returns>
/// <exception cref="ServiceException"></exception>
public ImportResultModel superviseUndelryingImportFromExcel(Stream streamIn)
{
var importModels = Enumerable.Empty<ImportModel>();
var rowIndex = 0;
try
{
var ds = Office.ExcelHelper.ReadExcelAsDataSet(streamIn);
if (ds.Tables.Count < 1 || ds.Tables[0].Rows.Count < 1)
{
throw new ServiceException("读取导入数据失败:数据为空") { Tag = "111" };
}
var table = ds.Tables[0];
var reader = new DataRowReader(table);
var list = new List<ImportModel>();
rowIndex = 1;
foreach (DataRow row in table.Rows)
{
rowIndex++;
if (row.ItemArray.All(n => string.IsNullOrWhiteSpace(n?.ToString())))
{
continue;
}
reader.SetDataRow(row);
var ul = reader.GetString("代码", false);
//代码为空时,跳过这条数据
if (string.IsNullOrEmpty(ul))
{
continue;
}
var ul_cfmmc = reader.GetString("报送代码CFMMC", false);
var model = list.FirstOrDefault(n => n.undelrying == ul);
if (model == null)
{
model = new ImportModel
{
undelrying = ul,
undelrying_CFMMC = ul_cfmmc,
};
list.Add(model);
}
else
{
model.undelrying = ul;
model.undelrying_CFMMC = ul_cfmmc;
}
}
importModels = list.ToArray();
}
catch (ServiceException se)
{
if (se.Tag != null) throw;
throw new ServiceException($"第{rowIndex}行,发生错误:{se.Message}");
}
catch (Exception ex)
{
LogFactory.GetLogger("导入监管报送参数").Error(ex);
throw new ServiceException($"第{rowIndex}行,发生错误:{ex.Message}", ex);
}
if (!importModels.Any())
{
throw new ServiceException("没有可导入的数据");
}
var result = new ImportResultModel
{
TotalCount = importModels.Count()
};
foreach (var item in importModels)
{
var underlying = item.undelrying;
var superviseUndelrying = DbContext.supervise_undelrying.FirstOrDefault(n => n.undelrying == underlying);
if (superviseUndelrying == null)
{
DbContext.supervise_undelrying.Add(new supervise_undelrying
{
undelrying = item.undelrying,
undelrying_CFMMC = item.undelrying_CFMMC,
CreateTime = DateTime.Now
});
result.SuccessCount++;
}
else
{
superviseUndelrying.undelrying_CFMMC = item.undelrying_CFMMC;
result.SuccessCount++;
}
}
DbContext.SaveChanges();
return result;
}
class ImportModel
{
public string undelrying { get; set; }
public string undelrying_CFMMC { get; set; }
}
}
}