using System.Data; using System.Text; using Gs.Entity.BaseInfo; 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 MesWorkshopLineManager : Repository, IRomteService { private readonly IHttpContextAccessor _http; private readonly string _userCode, _userGuid, _orgFids; public MesWorkshopLineManager(IHttpContextAccessor httpContextAccessor) { _http = httpContextAccessor; (_userCode, _userGuid, _orgFids) = UtilityHelper.GetUserGuidAndOrgGuid(_http); } /// /// 查询列表,支持分页 /// /// /// [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, c, org) => new object[] { JoinType.Left, a.DptId == c.Id.ToString(), JoinType.Left, c.FSubsidiary == org.Fid.ToString() }).Select((a, c, org) => new MesWorkshopLine { FSubsidiary = "(" + org.FNumber + ")" + org.Name, departName = c.Departmentname, }, 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) { LogHelper.Debug(this.ToString(), ex.Message); return ReturnDto>.QuickReturn(pageList, ReturnCode.Default, ex.Message); } } /// /// 检查是否被使用,使用后不能进行修改和删除 /// /// /// private bool checkLine(string strGuid) { System.Text.StringBuilder sbSql = new StringBuilder(); sbSql.Append(" select top 1 daa015 as xt from WOMDAA daa left join MES_WORKSHOP_LINE line on daa.daa015=line.id where line.guid='" + strGuid + "'"); sbSql.Append(" union all"); sbSql.Append(" select top 1 lineId as xt from MES_WORK_PROD daa left join MES_WORKSHOP_LINE line on daa.lineId=line.id where line.guid='" + strGuid + "'"); try { DataSet dset = new DataSet(); dset = Gs.Toolbox.DbHelperSQL.Query(sbSql.ToString()); if (dset != null && dset.Tables.Count > 0 && dset.Tables[0].Rows.Count > 0) return true; } catch (Exception ex) { LogHelper.Debug(ToString(), "EditModel error:" + ex.Message); } return false; } /// /// 增加或编辑实体 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto EditModel([FromBody] MesWorkshopLine model) { bool _blChk = checkLine(model.Guid.ToString()); if (_blChk) { return ReturnDto.QuickReturn("", ReturnCode.Exception, "操作失败,该线体已产生了数据,无法进行修改,删除等操作!"); } var _bl = false; try { if (!UtilityHelper.CheckGuid(model.Guid)) { model.Guid = Guid.NewGuid(); model.CreateBy = _userCode; model.CreateDate = DateTime.Now; model.LastupdateBy = _userCode; model.LastupdateDate = DateTime.Now; _bl = base.Insert(model); } else { model.LastupdateBy = _userCode; model.LastupdateDate = DateTime.Now; } //_bl = base.Update(model); _bl = Db.Updateable(model).IgnoreColumns(true).ExecuteCommand() > 0 ? true : false; } catch (Exception ex) { LogHelper.Debug(ToString(), "EditModel error:" + ex.Message); return ReturnDto.QuickReturn("", ReturnCode.Exception, ex.Message); } if (_bl) return ReturnDto.QuickReturn(model.Guid.ToString(), ReturnCode.Success, "操作成功!"); return ReturnDto.QuickReturn("", ReturnCode.Exception, "增加失败,请重试!"); } /// /// 读取 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto GetModel([FromBody] MesWorkshopLine model) { var m = base.GetById(model.Guid); if (m != null) return ReturnDto.QuickReturn(m, ReturnCode.Success, "读取成功!"); return ReturnDto.QuickReturn(m, ReturnCode.Default, "读取失败!"); } /// /// 删除明细实体,支持批量删除 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto DeleteModel([FromBody] JArray guidList) { var intArray = guidList.ToObject(); int? rtnInt = (int)ReturnCode.Default; bool _blChk = checkLine(intArray[0]); if (_blChk) { return ReturnDto.QuickReturn(rtnInt, ReturnCode.Exception, "操作失败,该线体已产生了数据,无法进行修改,删除等操作!"); } var _manager = new MesWorkshopLineManager(_http); rtnInt = _manager.DeleteById(intArray) ? intArray.Length : 0; if (rtnInt > 0) return ReturnDto.QuickReturn(rtnInt, ReturnCode.Success, "操作成功,共删除" + rtnInt + "条数据!"); return ReturnDto.QuickReturn(rtnInt, ReturnCode.Exception, "删除失败,请重试!"); } }