/* *所有关于WRITER_HEAD类的业务代码应在此处编写 *可使用repository.调用常用方法,获取EF/Dapper等信息 *如果需要事务请使用repository.DbContextBeginTransaction *也可使用DBServerProvider.手动获取数据库相关信息 *用户信息、权限、角色等使用UserContext.Current操作 *WRITER_HEADService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter */ using VOL.Core.BaseProvider; using VOL.Core.Extensions.AutofacManager; using VOL.Entity.DomainModels; using System.Linq; using VOL.Core.Utilities; using System.Linq.Expressions; using VOL.Core.Extensions; using Microsoft.EntityFrameworkCore; using VOL.Order.Repositories; using VOL.Core.Enums; using System.Collections.Generic; using System.Threading.Tasks; //支持Task类的引用 using System; using System.Reflection; using ConvertHelper; namespace VOL.Order.Services { public class TableExtra { /// /// 从表1 /// public List Table1List { get; set; } /// /// 从表2 /// public List Table2List { get; set; } } public partial class WRITER_HEADService { public async Task GetPOEMData(PageDataOptions loadData) { //App_ReportPriceModelBody.vue中loadTableBefore方法查询前给loadData.Value写入的值 try { int total = await WRITER_POEMRepository.Instance.FindAsIQueryable(x => x.WRITER_ID == new System.Guid(loadData.GetParentIdStr("WRITER_ID"))).CountAsync(); var data = await WRITER_POEMRepository.Instance //这里可以自己查询条件,从 loadData.Value找前台自定义传的查询条件 .FindAsIQueryable(x => x.WRITER_ID == new System.Guid(loadData.GetParentIdStr("WRITER_ID"))) //分页 //.TakeOrderByPage(1, 10, x => new Dictionary() { { x.CreateDate, QueryOrderBy.Desc } }) //.Select(s => new { s.GID, s.WRITER_ID, s.POEMNAME, s.CONTENT }) //.Select(s => new { s }) .ToListAsync(); object gridData = new { rows = data, total }; return gridData; } catch (Exception e) { } var data2 = await WRITER_POEMRepository.Instance //这里可以自己查询条件,从 loadData.Value找前台自定义传的查询条件 .FindAsIQueryable(x => 2 == 1) .Select(s => new { s.GID, s.WRITER_ID, s.POEMNAME, s.CONTENT }) .ToListAsync(); object gridData2 = new { rows = data2, total=0 }; return gridData2; } public async Task GetNOVELData(PageDataOptions loadData) { //App_ReportPriceModelBody.vue中loadTableBefore方法查询前给loadData.Value写入的值 int total = await WRITER_NOVELRepository.Instance.FindAsIQueryable(x => x.WRITER_ID == new System.Guid(loadData.GetParentIdStr("WRITER_ID"))).CountAsync(); var data = await WRITER_NOVELRepository.Instance //这里可以自己查询条件,从 loadData.Value找前台自定义传的查询条件 .FindAsIQueryable(x => x.WRITER_ID == new System.Guid(loadData.GetParentIdStr("WRITER_ID"))) //分页 //.TakeOrderByPage(1, 10, x => new Dictionary() { { x.CreateDate, QueryOrderBy.Desc } }) .Select(s => new { s.GID, s.WRITER_ID, s.NOVELNAME }) .ToListAsync(); object gridData = new { rows = data, total }; return gridData; } WebResponseContent _webResponse = new WebResponseContent(); public override PageGridData GetPageData(PageDataOptions options) { //SummaryExpress = (IQueryable queryable) => //{ // return queryable.GroupBy(x => 1).Select(x => new // { // AvgPrice = x.Average(o => o.AvgPrice), // Enable = x.Sum(o => o.Enable) // }) // .FirstOrDefault(); //}; //可以执行任意查询 返回dictionary //var test = repository.Exec("select * from sys_user",null); return base.GetPageData(options); } /// /// 验证提交的从表参数 /// saveModel.Extra在App_ReportPrice.js中addBefore/updateBefore新建与编辑时提交到的后台 /// /// /// private WebResponseContent ValidateExtra(SaveModel saveModel) { if (saveModel == null || saveModel.Extra == null) return _webResponse.Error("请提交参数"); try { TableExtra tableExtra = saveModel.Extra.ToString().DeserializeObject(); //if (tableExtra == null // || tableExtra.Table1List == null // || tableExtra.Table1List.Count == 0 // || tableExtra.Table1List == null // || tableExtra.Table2List.Count == 0) //{ // return _webResponse.Error("请提交从表1与从表2的参数"); //} //校验 从表1字段:Title,CreateDate必填 _webResponse = tableExtra.Table1List.ValidationEntityList(x => new { x.POEMNAME, x.CONTENT }); if (!_webResponse.Status) { _webResponse.Message = "从表1:" + _webResponse.Message; return _webResponse; } //校验 从表2字段:Describe, Name,PhoneNo 必填 _webResponse = tableExtra.Table2List.ValidationEntityList(x => new { x.NOVELNAME }); if (!_webResponse.Status) { _webResponse.Message = "从表2:" + _webResponse.Message; return _webResponse; } //校验成功返回 return _webResponse.OK(null, tableExtra); } catch (Exception ex) { Console.WriteLine(ex.Message); return _webResponse.Error("参数不正确"); } } /// /// 自定义保存从表数据逻辑 /// /// /// public override WebResponseContent Add(SaveModel saveDataModel) { //校验从表配置 ValidateExtra(saveDataModel); if (!_webResponse.Status) { return _webResponse; } //取出校验完成后的从表1.2的数据 TableExtra tableExtra = _webResponse.Data as TableExtra; //保存到数据库前 AddOnExecuting = (WRITER_HEAD head, object obj) => { //获取编码规则编码 //var billno = base.GetBillNo("入库计划");//base.GetBillNo("海运进口","委托编号") //head.BSNO = billno; return WebResponseContent.Instance.OK(); }; //App_ReportPrice 此处已经提交了数据库,处于事务中 AddOnExecuted = (WRITER_HEAD WRITER_HEAD, object obj) => { //在此操作tableExtra从表信息 List POEMList = tableExtra.Table1List;//.Select(s).ToList(); List NOVELList = tableExtra.Table2List; if (!POEMList.isNullorEmpty()) { var dealedbodylist = DealBodyList(WRITER_HEAD.WRITER_ID, POEMList, "WRITER_HEAD"); repository.AddRange(dealedbodylist[0]); repository.UpdateRange(dealedbodylist[1], x => new { x }); repository.SaveChanges(); } if (!NOVELList.isNullorEmpty()) { var dealedbodylist2 = DealBodyList(WRITER_HEAD.WRITER_ID, NOVELList, "WRITER_HEAD"); repository.AddRange(dealedbodylist2[0]); repository.UpdateRange(dealedbodylist2[1], x => new { x }); repository.SaveChanges(); } return WebResponseContent.Instance.OK(); }; var _r= base.Add(saveDataModel); return _r; } /// /// 自定义更新从表操作 /// /// /// public override WebResponseContent Update(SaveModel saveModel) { //校验从表配置 ValidateExtra(saveModel); if (!_webResponse.Status) { return _webResponse; } //取出校验完成后的从表1.2的数据 TableExtra tableExtra = _webResponse.Data as TableExtra; //保存到数据库前 UpdateOnExecuting = (WRITER_HEAD WRITER_HEAD, object obj, object obj2, List list) => { return WebResponseContent.Instance.OK(); }; //App_ReportPrice 此处已经提交了数据库,处于事务中 UpdateOnExecuted = (WRITER_HEAD WRITER_HEAD, object obj, object obj2, List list) => { //在此操作tableExtra从表信息 List POEMList = tableExtra.Table1List;//.Select(s).ToList(); List NOVELList = tableExtra.Table2List; /* //id=0的默认为新增的数据 List addList = POEMList.Where(x => x.GID == Guid.Empty).ToList(); //if (addList.Count > 0) { // foreach (var p in addList) { // //p.GID = Guid.NewGuid(); // } //} //设置默认创建人信息 addList.ForEach(x => { x.SetCreateDefaultVal(); }); //获取所有编辑行 List editIds = POEMList.Where(x => x.GID != Guid.Empty).Select(s => s.GID).ToList(); addList.ForEach(x => { x.SetModifyDefaultVal(); }); //从数据库查询编辑的行是否存在,如果数据库不存在,执行修改操作会异常 List existsIds = WRITER_POEMRepository.Instance.FindAsIQueryable(x => editIds.Contains(x.GID)).Select(s => s.GID).ToList(); //获取实际可以修改的数据 List updateList = POEMList.Where(x => existsIds.Contains(x.GID)).ToList(); //设置默认修改人信息 updateList.ForEach(x => { x.SetModifyDefaultVal(); }); //新增 var keyValue = saveModel.MainData.Keys; PropertyInfo detailMainKey = typeof(WRITER_POEM).GetParentIdProperty(typeof(WRITER_POEM).GetEntityTableName()); PropertyInfo BodyPK = typeof(WRITER_POEM).GetKeyProperty(); addList.ForEach(x => { //设置用户默认值 x.SetCreateDefaultVal(); detailMainKey.SetValue(x, keyValue); if (BodyPK.PropertyType == typeof(Guid)) { BodyPK.SetValue(x, Guid.NewGuid()); } repository.DbContext.Entry(x).State = EntityState.Added; }); */ if (!POEMList.isNullorEmpty()) { var dealedbodylist = DealBodyList(WRITER_HEAD.WRITER_ID, POEMList, "WRITER_HEAD"); repository.AddRange(dealedbodylist[0]); var updatelist_0 = dealedbodylist[1]; List editIds = updatelist_0.Select(s => s.GID).ToList(); List existsIds = WRITER_POEMRepository.Instance.FindAsIQueryable(x => editIds.Contains(x.GID)).Select(s => s.GID).ToList(); //获取实际可以修改的数据 List updatelist = POEMList.Where(x => existsIds.Contains(x.GID)).ToList(); repository.UpdateRange(updatelist, x => new { x }); //最终保存 repository.SaveChanges(); } if (!NOVELList.isNullorEmpty()) { var dealedbodylist2 = DealBodyList(WRITER_HEAD.WRITER_ID, NOVELList, "WRITER_HEAD"); repository.AddRange(dealedbodylist2[0]); var updatelist_0 = dealedbodylist2[1]; List editIds = updatelist_0.Select(s => s.GID).ToList(); List existsIds = WRITER_NOVELRepository.Instance.FindAsIQueryable(x => editIds.Contains(x.GID)).Select(s => s.GID).ToList(); repository.UpdateRange(dealedbodylist2[1], x => new { x }); repository.SaveChanges(); } return WebResponseContent.Instance.OK(); }; var _r = base.Update(saveModel); return _r; } /// /// 删除 /// /// /// /// public override WebResponseContent Del(object[] keys, bool delList = true) { DelOnExecuted = (keys) => { //实现委托 string joinKeys = $"'{string.Join("','", keys)}'"; string sql = $"DELETE FROM {typeof(WRITER_POEM).GetEntityTableName() } where writer_id in ({joinKeys});" + $"DELETE FROM {typeof(WRITER_NOVEL).GetEntityTableName() } where writer_id in ({joinKeys});"; repository.ExecuteSqlCommand(sql); return WebResponseContent.Instance.OK(); }; return base.Del(keys, true); } //public async void DelDetail(object[] keys, string TableName) { // if (TableName == "WRITER_POEM") { // DelDetail(typeof(WRITER_POEM), keys); // } // if (TableName == "WRITER_NOVEL") // { // DelDetail(typeof(WRITER_NOVEL), keys); // } //} //public override WebResponseContent DelDetail(Type DetailType, object[] keys) //{ // return base.DelDetail(DetailType, keys); //} } }