using System.Text; using Gs.BaseInfo.Models; using Gs.Entity.Sys; 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; using SqlSugar; namespace Gs.BaseInfo.Services; [ApiGroup(ApiGroupNames.BaseInfo)] public class MesWorkshopManager : Repository, IRomteService { private readonly IHttpContextAccessor _http; private readonly string _userCode, _orgCode, _userGuid, _orgGuid, _userId, _orgId; public MesWorkshopManager(IHttpContextAccessor httpContextAccessor) { _http = httpContextAccessor; (_userCode, _orgCode, _userGuid, _orgGuid, _userId, _orgId) = UtilityHelper.GetUserGuidAndOrgGuid(_http); } #region /// /// 查询列表,支持分页 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto> GetListPage(PageQuery query) { var pageList = new PageList(); try { var _sbWhere = new StringBuilder(" 1=1" + query.keyWhere); var _sbBy = new StringBuilder(query.sortName + " " + query.sortOrder); var totalCount = 0; var itemsList = Db .Queryable( (a, org) => new object[] { JoinType.Left, a.FSubsidiary == org.Fid, }).Select((a, org) => new MesWorkshop { FSubsidiary = "(" + org.FNumber + ")" + org.Name, }, true) .Where(_sbWhere.ToString()) .OrderBy(_sbBy.ToString()) .ToPageList(query.currentPage, query.everyPageSize, ref totalCount); pageList = new PageList(itemsList, totalCount, query.everyPageSize); return ReturnDto>.QuickReturn(pageList, ReturnCode.Success, "读取成功"); } catch (Exception ex) { return ReturnDto>.QuickReturn(pageList, ReturnCode.Default, ex.Message); } } /// /// 删除功能菜单 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto DeleteModel([FromBody] JArray guidList) { var intArray = guidList.ToObject(); var guid = intArray[0]; int? rtnInt = (int)ReturnCode.Default; var cont = 0; var it1 = 0; var it2 = 0; cont = IsChkOrUnChk(guid, true); if (cont > 0) return ReturnDto.QuickReturn(default(int?), ReturnCode.Exception, "删除失败,该信息已被审核!"); cont = Db.Queryable() .Where(c => c.WorkshopGuid == Guid.Parse(guid)).Count(); if (cont > 0) return ReturnDto.QuickReturn(rtnInt, ReturnCode.Exception, "删除失败,该条目下面有子项,不可删除!"); try { Db.Ado.BeginTran(); it1 = Db.Deleteable().In(guid).ExecuteCommand(); it2 = Db.Deleteable() .Where(it => it.WorkshopGuid == Guid.Parse(guid)) .ExecuteCommand(); Db.Ado.CommitTran(); } catch (Exception ex) { LogHelper.Debug(ToString(), "DeleteModel error:" + ex.Message); Db.Ado.RollbackTran(); return ReturnDto.QuickReturn(rtnInt, ReturnCode.Exception, "删除失败,请重试!"); } rtnInt = it2 + it1; return ReturnDto.QuickReturn(rtnInt, ReturnCode.Success, "操作成功,共删除" + rtnInt + "条数据!"); } /// /// 增加 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto EditModel([FromBody] MesWorkshop 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 _manager = new MesWorkshopLineManager(); 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.WorkshopGuid = model.Guid; m.CreateDate = DateTime.Now; m.CreateBy = _userCode; _addLst.Add(m); } // _manager.UpdateRange(_upLst); Db.Updateable(_upLst).IgnoreColumns(true).ExecuteCommand(); _manager.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 GetModel([FromBody] MesWorkshop model) { var m = base.GetById(model.Guid); var lst = new MesWorkshopLineManager(); m.list = lst.GetList(it => it.WorkshopGuid == m.Guid); if (m != null) return ReturnDto.QuickReturn(m, ReturnCode.Success, "读取成功!"); return ReturnDto.QuickReturn(m, ReturnCode.Default, "读取失败!"); } /// /// 删除明细实体,支持批量删除 /// /// /// [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 = (int)ReturnCode.Default; var _manager = new MesWorkshopLineManager(); rtnInt = _manager.DeleteById(intArray) ? intArray.Length : 0; if (rtnInt > 0) return ReturnDto.QuickReturn(rtnInt, ReturnCode.Success, "操作成功,共删除" + rtnInt + "条数据!"); return ReturnDto.QuickReturn(rtnInt, ReturnCode.Exception, "删除失败,请重试!"); } private int IsChkOrUnChkByMx(string _mxGuid, bool status) { var _manger = new MesWorkshopLineManager(); var _md = _manger.GetById(_mxGuid); return IsChkOrUnChk(_md.WorkshopGuid.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; } #endregion }