using System.Text; using GS.QC.Models; using Gs.Toolbox; using Gs.Toolbox.ApiCore.Abstract.Mvc; using Gs.Toolbox.ApiCore.Common.Mvc; using Gs.Toolbox.ApiCore.Group; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; namespace GS.QC.Service; [ApiGroup(ApiGroupNames.QC)] public class MesSysLookupTypesManager : Repository, IRomteService { private readonly IHttpContextAccessor _http; private readonly MesSysLookupsManager _mesSysLookupsManager; private readonly string _userCode,_userGuid,_orgFids; public MesSysLookupTypesManager(IHttpContextAccessor httpContextAccessor) { _http = httpContextAccessor; _mesSysLookupsManager = new MesSysLookupsManager(); (_userCode, _userGuid, _orgFids) = UtilityHelper.GetUserGuidAndOrgGuid(_http); } /// /// 查询列表,支持分页 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto> GetListPage(PageQuery query) { var _sbWhere = new StringBuilder(" 1=1 " + query.keyWhere); var _sbBy = new StringBuilder(query.sortName + " " + query.sortOrder); var totalCount = 0; var itemsList = Db.Queryable() .Where(_sbWhere.ToString()) .OrderBy(_sbBy.ToString()) .ToPageList(query.currentPage, query.everyPageSize, ref totalCount); var pageList = new PageList(itemsList, totalCount, query.everyPageSize); return ReturnDto>.QuickReturn(pageList, ReturnCode.Success, "读取成功"); } /// /// 根据主表id读取主表和子表 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto GetModel( [FromBody] MesSysLookupTypes model) { var m = base.GetById(model.Guid); m.list = _mesSysLookupsManager.GetList(it => it.LookupGuid == model.Guid).OrderBy(it => it.LookupValue).ToList() ; if (m != null) return ReturnDto.QuickReturn(m, ReturnCode.Success, "读取成功!"); return ReturnDto.QuickReturn(m, ReturnCode.Default, "读取失败!"); } /// /// 增加或编辑实体 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto EditModel([FromBody] MesSysLookupTypes model) { if (UtilityHelper.CheckGuid(model.Guid)) { var cont = 0; cont = IsChkOrUnChk(model.Guid.ToString(), true); if (cont > 0) return ReturnDto.QuickReturn("", ReturnCode.Exception, "修改失败,该信息已被审核!"); } try { Db.Ado.BeginTran(); if (!UtilityHelper.CheckGuid(model.Guid)) { model.Guid = Guid.NewGuid(); model.CreateBy = _userCode; model.CreateDate = DateTime.Now; base.Insert(model); } else { model.LastupdateBy = _userCode; model.LastupdateDate = DateTime.Now; // base.Update(model); Db.Updateable(model).IgnoreColumns(true) .ExecuteCommand(); } var _upLst = new List(); var _addLst = new List(); foreach (var m in model.list) if (UtilityHelper.CheckGuid(m.Guid)) { m.LastupdateBy = _userCode; m.LastupdateDate = DateTime.Now; _upLst.Add(m); } else { m.Guid = Guid.NewGuid(); m.LookupGuid = model.Guid; m.CreateDate = DateTime.Now; m.CreateBy = _userCode; _addLst.Add(m); } Db.Updateable(_upLst).IgnoreColumns(true) .ExecuteCommand(); _mesSysLookupsManager.InsertRange(_addLst); Db.Ado.CommitTran(); } catch (Exception ex) { LogHelper.Debug(ToString(), "EditModel error:" + ex.Message); Db.Ado.RollbackTran(); return ReturnDto.QuickReturn("", ReturnCode.Exception, ex.Message); } return ReturnDto.QuickReturn(model.Guid.ToString(), ReturnCode.Success, "操作成功!"); } /// /// 删除明细实体,支持批量删除 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto DeleteModelMx([FromBody] JArray guidList) { var intArray = guidList.ToObject(); var cont = 0; cont = IsChkOrUnChkByMx(intArray[0], true); if (cont > 0) return ReturnDto.QuickReturn(default(int?), ReturnCode.Exception, "删除失败,该信息已被审核!"); int? rtnInt = _mesSysLookupsManager.DeleteByIds(intArray) ? intArray.Length : 0; return rtnInt > 0 ? ReturnDto.QuickReturn(rtnInt, ReturnCode.Success, "操作成功,共删除" + rtnInt + "条数据!") : ReturnDto.QuickReturn(rtnInt, ReturnCode.Exception, "删除失败,请重试!"); } /// /// 删除主表 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto DeleteModel([FromBody] JArray guidList) { var intArray = guidList.ToObject(); var cont = 0; cont = IsChkOrUnChk(intArray[0], true); if (cont > 0) return ReturnDto.QuickReturn(default(int?), ReturnCode.Exception, "删除失败,该信息已被审核!"); int? rtnInt = base.DeleteByIds(intArray) ? intArray.Length : 0; return rtnInt > 0 ? ReturnDto.QuickReturn(rtnInt, ReturnCode.Success, "操作成功,共删除" + rtnInt + "条数据!") : ReturnDto.QuickReturn(rtnInt, ReturnCode.Exception, "删除失败,请重试!"); } /// /// 根据编码获取结果集 /// /// /// List [RequestMethod(RequestMethods.POST)] public ReturnDto> GetItemList([FromBody] string code) { var mesSysLookupTypes = Db.Queryable() .Where(s => s.LookupTypeCode == code).First(); var list = new List(); if (mesSysLookupTypes == null) return ReturnDto>.QuickReturn(list, ReturnCode.Exception, "读取成功"); list = Db.Queryable() .Where(a => a.LookupGuid == mesSysLookupTypes.Guid).ToList(); return ReturnDto>.QuickReturn(list, ReturnCode.Exception, "读取成功"); } private int IsChkOrUnChkByMx(string _mxGuid, bool status) { var m = _mesSysLookupsManager.GetById(_mxGuid); return IsChkOrUnChk(m.LookupGuid.ToString(), status); } private int IsChkOrUnChk(string? _guid, bool status) { var cont = 0; cont = base.GetList(it => it.Guid == Guid.Parse(_guid) && it.CheckStatus == status).Count; return cont; } }