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; using System.Data; using System.Dynamic; using Microsoft.CodeAnalysis.CSharp.Syntax; namespace GS.QC.Service; [ApiGroup(ApiGroupNames.QC)] public class MesDefectCodeManager : Repository, IRomteService { private readonly IHttpContextAccessor _http; private readonly string _userCode, _userGuid, _orgFids; public MesDefectCodeManager(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") .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 GetModel([FromBody] MesDefectCode model) { var m = base.GetById(model.Guid); System.Text.StringBuilder sb = new StringBuilder(); sb.Append(" select max(defect_code)+1 from [dbo].[MES_DEFECT_CODE] w "); sb.Append(" where pid='" + m.Guid + "'"); string maxNo = ""; try { var obj = DbHelperSQL.GetSingle(sb.ToString()); if (obj == null) maxNo = m.DefectCode + "01"; else maxNo = obj.ToString(); } catch (Exception ex) { LogHelper.Debug(ToString(), "EditModel error:" + ex.Message); } m.MaxNo = maxNo; if (m != null) return ReturnDto.QuickReturn(m, ReturnCode.Success, "读取成功!"); return ReturnDto.QuickReturn(m, ReturnCode.Default, "读取失败!"); } /// /// 增加 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto EditModel([FromBody] MesDefectCode model) { try { 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; Db.Insertable(model).IgnoreColumns(true).ExecuteCommand(); } else { model.LastupdateBy = _userCode; model.LastupdateDate = DateTime.Now; Db.Updateable(model).IgnoreColumns(true).ExecuteCommand(); } 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 DeleteModel([FromBody] JArray guidList) { var intArray = guidList.ToObject(); string guid = intArray[0]; int it = 0; int? rtnInt = 0; //根项不能删除 string pid = ""; try { pid = DbHelperSQL.GetSingle("select top 1 pid from MES_DEFECT_CODE where guid='" + guid + "' ").ToString(); } catch (Exception ex) { return ReturnDto.QuickReturn(rtnInt, ReturnCode.Exception, "删除失败:" + ex.Message); } if (pid == "00000000-0000-0000-0000-000000000000") { return ReturnDto.QuickReturn(rtnInt, ReturnCode.Exception, "该项目为根目录,不能删除!"); } //有子项不能删除 System.Text.StringBuilder sbSql = new StringBuilder(); sbSql.Append(" select count(1) from [MES_DEFECT_CODE] where pid='" + guid + "' "); try { it = int.Parse(Gs.Toolbox.DbHelperSQL.GetSingle(sbSql.ToString()).ToString()); } catch (Exception ex) { LogHelper.Debug(ToString(), "DeleteModel error:" + ex.Message); } if (it > 0) { return ReturnDto.QuickReturn(rtnInt, ReturnCode.Exception, "删除失败,该项目存在子项!"); } rtnInt = base.DeleteById(intArray) ? intArray.Length : 0; if (rtnInt > 0) return ReturnDto.QuickReturn(rtnInt, ReturnCode.Success, "操作成功,共删除" + rtnInt + "条数据!"); return ReturnDto.QuickReturn(rtnInt, ReturnCode.Exception, "删除失败,请重试!"); } /// /// /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto> SelectCategory(dynamic model) { string strWhere = model.strWhere; var lst = new List(); var dset = new DataSet(); System.Text.StringBuilder sbSql = new StringBuilder(); sbSql.Append("select [type_memo] from [dbo].[MES_DEFECT_TYPE] a where 1=1"); if (!string.IsNullOrEmpty(strWhere)) sbSql.Append(strWhere); sbSql.Append(" order by [type_memo] asc"); try { dset = Gs.Toolbox.DbHelperSQL.Query(sbSql.ToString()); } catch (Exception ex) { LogHelper.Debug(this.ToString(), "EditModel error:" + ex.Message); } if (dset != null && dset.Tables.Count > 0 && dset.Tables[0].Rows.Count > 0) //有数据 lst = dset.Tables[0].TableToDynamicList(); return ReturnDto>.QuickReturn(lst, ReturnCode.Success, "读取成功!"); } /// /// 读取 ,有绑定 /// /// /// [RequestMethod(RequestMethods.POST)] public ReturnDto GetModel2([FromBody] dynamic model) { string guid = model.guid.ToString(); dynamic m = new ExpandoObject(); m.list = new List(); m.list2 = new List(); var dset = new DataSet(); System.Text.StringBuilder sbSql = new StringBuilder(); sbSql.Append(" select * from [dbo].[MES_DEFECT_CODE] where guid='" + guid + "'"); sbSql.Append(" select bind.guid,bind.fType ,u.ACCOUNT,u.USER_NAME,org.NAME as orgName,q.defect_name from [dbo].[SYS_USER_BIND] bind"); sbSql.Append(" left join SYS_USER u on bind.aboutGuid=u.guid"); sbSql.Append(" left join mes_staff f on u.STAFF_ID=f.id left join [dbo].[MES_DEFECT_CODE] q on bind.userGuid=q.guid"); sbSql.Append(" left join [dbo].[SYS_ORGANIZATION] org on org.FID=f.FSubsidiary"); sbSql.Append(" where 1=1 and bind.userGuid='" + guid + "' and bind.fType='员工分区'"); sbSql.Append(" select bind.guid,bind.fType ,f.item_id,f.item_no,f.item_name,org.NAME as orgName,q.defect_name"); sbSql.Append(" from [dbo].[SYS_USER_BIND] bind left join MES_ITEMS f on bind.aboutGuid=f.item_id"); sbSql.Append(" left join [dbo].[MES_DEFECT_CODE] q on bind.userGuid=q.guid"); sbSql.Append(" left join [dbo].[SYS_ORGANIZATION] org on org.FID=f.FSubsidiary"); sbSql.Append(" where 1=1 and bind.userGuid='" + guid + "' and bind.fType='物料分区'"); sbSql.Append(" select bind.guid,bind.fType ,u.depot_section_code,u.depot_section_name"); sbSql.Append(" ,org.NAME as orgName ,q.defect_name from [dbo].[SYS_USER_BIND] bind "); sbSql.Append(" left join MES_DEPOT_SECTIONS u on bind.aboutGuid=u.depot_section_code "); sbSql.Append(" left join MES_DEPOTS f on u.depot_guid=f.depot_id"); sbSql.Append(" left join [dbo].[MES_DEFECT_CODE] q on bind.userGuid=q.guid "); sbSql.Append(" left join [dbo].[SYS_ORGANIZATION] org on org.FID=f.FSubsidiary "); sbSql.Append(" where 1=1 and bind.userGuid='" + guid + "' and bind.fType='库位'"); try { dset = DbHelperSQL.Query(sbSql.ToString()); if (dset != null && dset.Tables.Count > 0 && dset.Tables[0].Rows.Count > 0) { var dr = dset.Tables[0].Rows[0]; m = dr.RowToDynamic(); var _tb = dset.Tables[1].TableToDynamicList(); m.list = _tb; var _tb2 = dset.Tables[2].TableToDynamicList(); m.list2 = _tb2; var _tb3 = dset.Tables[3].TableToDynamicList(); m.list3 = _tb3; } } catch (Exception ex) { LogHelper.Debug(ToString(), ex.Message); } if (m != null) return ReturnDto.QuickReturn(m, ReturnCode.Success, "读取成功!"); return ReturnDto.QuickReturn(m, ReturnCode.Default, "读取失败!"); } }