using System.Text; using Gs.Entity.BaseInfo; using Gs.Entity.Sys; 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 Masuit.Tools; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; using SqlSugar; using System.Data; namespace GS.QC.Service; [ApiGroup(ApiGroupNames.QC)] public class MesQaMjManager : Repository, IRomteService { private readonly IHttpContextAccessor _http; private readonly string _userCode, _userGuid, _orgFids; public MesQaMjManager(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, b, org, d) => new JoinQueryInfos( JoinType.Inner, a.ItemId == b.Id, JoinType.Inner, b.FSubsidiary == org.Fid, JoinType.Left, a.SuppId == d.Id.ToString() )) .Where(_sbWhere.ToString()) .OrderBy(_sbBy.ToString()) .Select((a, b, org, d) => new MesQaMj { FSubsidiary = "(" + org.FNumber + ") " + org.Name, Guid = a.Guid.SelectAll(), gysName = d.SuppName, itemName = b.ItemName, suppNo = d.SuppNo }) .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); } } /// /// 根据主表id读取主表 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto GetModel( [FromBody] MesQaMj model) { var m = base.GetById(model.Guid); System.Text.StringBuilder sbSql = new StringBuilder(); sbSql.Append("select i.FSubsidiary,i.[item_name],i.[item_model],i.item_no,mj.SUPP_ID,sup.supp_name,sup.supp_no"); sbSql.Append(" from MES_ITEMS i right join [dbo].[MES_QA_MJ] mj on i.id=mj.ITEM_ID left join [dbo].[MES_SUPPLIER] sup on sup.id=mj.SUPP_ID "); sbSql.Append(" where i.id=" + m.ItemId); try { DataSet dset = new DataSet(); dset = Gs.Toolbox.DbHelperSQL.Query(sbSql.ToString()); System.Data.DataRow r = dset.Tables[0].Rows[0]; m.FSubsidiary = r["FSubsidiary"].ToString(); m.ItemNo = r["item_no"].ToString(); m.suppNo = r["supp_no"].ToString(); } catch (Exception ex) { LogHelper.Debug(ToString(), "GetModel error:" + ex.Message); } if (m != null) return ReturnDto.QuickReturn(m, ReturnCode.Success, "读取成功!"); return ReturnDto.QuickReturn(m, ReturnCode.Default, "读取失败!"); } /// /// 增加或编辑实体 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto EditModel([FromBody] MesQaMj model) { if (UtilityHelper.CheckGuid(model.Guid)) { var cont = 0; cont = IsChkOrUnChk(model.Guid.ToString()); if (cont > 0) return ReturnDto.QuickReturn("", ReturnCode.Exception, "修改失败,该信息已被审核!"); } try { //防呆是不是属于同一组织 string supOrg = "", itemOrg = ""; supOrg = Gs.Toolbox.DbHelperSQL.GetSingle("select top 1 FSubsidiary from MES_SUPPLIER a where a.id="+model.SuppId.ToString()).ToString(); itemOrg = Gs.Toolbox.DbHelperSQL.GetSingle("select top 1 FSubsidiary from MES_ITEMS a where a.id=" + model.ItemId.ToString()).ToString(); if (supOrg != itemOrg) { return ReturnDto.QuickReturn("", ReturnCode.Default, "供应商和物料不在同一组织下,请重新选择!"); } Db.Ado.BeginTran(); if (!UtilityHelper.CheckGuid(model.Guid)) { model.Guid = Guid.NewGuid(); model.CreateBy = _userCode; model.CreateDate = DateTime.Now; model.LastupdateBy = _userCode; model.LastupdateDate = DateTime.Now; model.Status = false; base.Insert(model); } else { model.LastupdateBy = _userCode; model.LastupdateDate = DateTime.Now; Db.Updateable(model).IgnoreColumns(true) .ExecuteCommand(); } Db.Ado.CommitTran(); } catch (Exception ex) { LogHelper.Debug(this.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 DeleteModel([FromBody] JArray guidList) { var intArray = guidList.ToObject(); var cont = IsChkOrUnChk(intArray[0]); 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, "删除失败,请重试!"); } /// /// 查看免检物料是否重复维护 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto GetCount([FromBody] string? itemIdStr) { if (itemIdStr.IsNullOrEmpty()) { int? rtnInt = 0; return ReturnDto.QuickReturn(rtnInt, ReturnCode.Success, ""); } var itemId = Convert.ToInt32(itemIdStr); int? count = Db.Queryable() .Where(s => s.ItemId == itemId) .Count(); return ReturnDto.QuickReturn(count, ReturnCode.Success, ""); } private int IsChkOrUnChk(string? _guid) { var cont = Db.Queryable() .Where(it => it.Guid == Guid.Parse(_guid) && it.Status == true) .Count(); return cont; } }