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 { // 1. 构建查询条件(WHERE子句) // "1=1" 是万能条件(避免后续拼接条件时需要判断是否加AND),拼接查询参数中的自定义条件(query.keyWhere) var _sbWhere = new StringBuilder(" 1=1" + query.keyWhere); // 2. 构建排序条件(ORDER BY子句) // 拼接排序字段(query.sortName)和排序方向(query.sortOrder,如"ASC"升序、"DESC"降序) var _sbBy = new StringBuilder(query.sortName + " " + query.sortOrder); // 3. 声明总记录数变量(用于接收查询结果的总条数) var totalCount = 0; // 4. 执行分页查询 // 使用数据库访问工具(Db.Queryable)查询MesDefectCode表(别名"a") var itemsList = Db.Queryable("a") .Where(_sbWhere.ToString()) // 应用WHERE条件 .OrderBy(_sbBy.ToString()) // 应用排序条件 // 分页查询:当前页码(query.currentPage)、每页条数(query.everyPageSize),总条数通过ref参数返回 .ToPageList(query.currentPage, query.everyPageSize, ref totalCount); // 5. 组装分页结果对象 // 包含当前页数据(itemsList)、总条数(totalCount)、每页条数(query.everyPageSize) pageList = new PageList(itemsList, totalCount, query.everyPageSize); // 6. 返回成功结果:包含分页数据、成功状态码、提示信息 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.guid"); 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; } } catch (Exception ex) { LogHelper.Debug(ToString(), ex.Message); } if (m != null) return ReturnDto.QuickReturn(m, ReturnCode.Success, "读取成功!"); return ReturnDto.QuickReturn(m, ReturnCode.Default, "读取失败!"); } }