using System.Data;
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 static Gs.Toolbox.UtilityHelper;
using Microsoft.AspNetCore.Authorization;
using System.Text;
using System.Dynamic;
using System.Data.SqlClient;
namespace GS.QC.Service
{
///
/// 质保期方案
///
[ApiGroup(ApiGroupNames.QC)]
public class RkqcfaManager : IRomteService
{
private readonly IHttpContextAccessor _http;
private readonly string _userCode, _userGuid, _orgFids;
public RkqcfaManager(IHttpContextAccessor httpContextAccessor)
{
_http = httpContextAccessor;
(_userCode, _userGuid, _orgFids) =
UtilityHelper.GetUserGuidAndOrgGuid(_http);
}
#region
///
/// 读取列表,支持分页
///
///
///
[RequestMethod(RequestMethods.POST)]
public ReturnDto> GetListPage([FromBody] PageQuery model)
{
int currentPage = model.currentPage;
int everyPageSize = model.everyPageSize;
string sortName = string.IsNullOrEmpty(model.sortName) ? "a.fabm" : model.sortName;
string keyWhere = model.keyWhere;
System.Text.StringBuilder sbSql = new StringBuilder();
sbSql.Append("select * from ");
sbSql.Append("( ");
sbSql.Append("select top 100000 ROW_NUMBER() over(order by " + sortName + " " + model.sortOrder + ") as rowIndex,a.* ,(case a.zt when 1 then '正常' when 0 then '禁用' end) as isStatusTxt,('('+org.FNumber+')'+org.NAME) as orgName from MES_CQJY_FA A LEFT JOIN SYS_ORGANIZATION ORG ON ORG.FID=A.fSubsidiary where 1=1" + keyWhere);
sbSql.Append(") as T ");
sbSql.Append(" where T.rowindex>(" + currentPage + "-1)*" + everyPageSize + " and T.rowindex<=" + currentPage + "*" + everyPageSize);
sbSql.Append(" order by rowindex asc ");
sbSql.Append(" select count(1) as intTotal from MES_CQJY_FA a LEFT JOIN SYS_ORGANIZATION ORG ON ORG.FID=A.fSubsidiary where 1=1 " + keyWhere);
DataSet dset = new DataSet();
try
{
dset = Gs.Toolbox.DbHelperSQL.Query(sbSql.ToString());
}
catch (Exception ex)
{
Gs.Toolbox.LogHelper.Debug(this.ToString(), "GetListPage error:" + ex.Message);
return ReturnDto>.QuickReturn(default(PageList), ReturnCode.Exception, "读取失败");
}
PageList _pglist = new PageList
{
total = 0,
everyPageSize = 0,
pages = 0,
list = new List()
};
if (dset != null && dset.Tables.Count > 0 && dset.Tables[0].Rows.Count > 0)//有数据
{
int intTotal = int.Parse(dset.Tables[1].Rows[0]["intTotal"].ToString());
int pages = (intTotal % everyPageSize != 0) ? (intTotal / everyPageSize + 1) : (intTotal / everyPageSize);
_pglist.total = intTotal;
_pglist.everyPageSize = everyPageSize;
_pglist.pages = pages;
List _dy = dset.Tables[0].TableToDynamicList();
_pglist.list = _dy;
}
return ReturnDto>.QuickReturn(_pglist, ReturnCode.Success, "读取成功");
}
///
/// 删除实体
///
///
///
[RequestMethod(RequestMethods.POST)]
public ReturnDto DeleteModel([FromBody] dynamic model)
{
string guid = model.guid;
string mxGuid = model.mxGuid;
int rtnInt = (int)ReturnCode.Default;
int cont = 0;
try
{
cont = isChkOrUnChk(guid, 1);
if (cont > 0)
return ReturnDto.QuickReturn(default(int?), ReturnCode.Exception, "删除失败,该信息已被审核!");
StringBuilder sbCount = new StringBuilder();
sbCount.Append("select COUNT(1) from [dbo].[MES_CQJY_FA_DETAIL] where pGuid='" + guid + "'");
cont = 0;
try
{
cont = int.Parse(DbHelperSQL.GetSingle(sbCount.ToString()).ToString());
}
catch (Exception ex)
{
LogHelper.Debug(ToString(), "DeleteModel error:" + sbCount.ToString());
return ReturnDto.QuickReturn(default(int?), ReturnCode.Exception, "删除失败,请重试!");
}
if (cont > 0)
{
return ReturnDto.QuickReturn(default(int?), ReturnCode.Exception, "请先删除下面的物料列表!");
}
StringBuilder strSql = new StringBuilder();
strSql.Append(" delete from MES_CQJY_FA ");
strSql.Append(" where guid='" + guid + "'");
int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
rtnInt = rows;
}
catch (Exception ex)
{
LogHelper.Debug(this.ToString(), "DeleteModel error:" + ex.Message);
rtnInt = (int)ReturnCode.Exception;
return ReturnDto.QuickReturn(default(int?), ReturnCode.Exception, "删除失败," + ex.Message);
}
if (rtnInt > 0)
return ReturnDto.QuickReturn(default(int?), ReturnCode.Success, "操作成功,共删除" + rtnInt.ToString() + "条数据!");
else
return ReturnDto.QuickReturn(default(int?), ReturnCode.Exception, "删除失败,请重试!");
}
///
/// 增加实体
///
///
///
[RequestMethod(RequestMethods.POST)]
public ReturnDto EditModel([FromBody] dynamic model)
{
string guid = model.guid;
string famc = model.famc;
string bzqx = model.bzqx;
string fjxz = model.fjxz;
string yjrq = model.yjrq;
string bz = model.bz;
int zt = 1;
string fSubsidiary = model.fSubsidiary;
int? rtnInt = (int)ReturnCode.Default;
dynamic mObj = new ExpandoObject();
mObj.outMsg = "";
mObj.outSum = -1;
mObj.outGuid = "";
mObj.outNo = "";
if (UtilityHelper.CheckGuid(guid))
{
int cont = isChkOrUnChk(guid, 1);
if (cont > 0)
{
mObj.outMsg = "删除失败,该信息已被审核!";
mObj.outSum = -1;
return ReturnDto.QuickReturn(mObj, ReturnCode.Exception, mObj.outMsg);
}
}
string fabm = "";//方案编码
StringBuilder strSql = new StringBuilder();
if (!UtilityHelper.CheckGuid(guid))
{
guid = Guid.NewGuid().ToString();
fabm = GetBillNo("JYFA(检验方案)");
strSql.Append(" insert into MES_CQJY_FA");
strSql.Append(" ([guid] ,[fabm] ,[famc] ,[bzqx] ,[fjxz],[yjrq] ,[bz] ,[zt],fSubsidiary,checkStatus)");
strSql.Append(" values(");
strSql.Append(" '" + guid + "','" + fabm + "','" + famc + "'," + bzqx + "," + fjxz + "," + yjrq + ",'" + bz + "'," + zt + "," + fSubsidiary + ",0)");
}
else
{
strSql.Append(" update MES_CQJY_FA set [famc]='" + famc + "' ,[bzqx]=" + bzqx + " ,[fjxz]=" + fjxz + ",[yjrq]=" + yjrq + " ,[bz]='" + bz + "'");
strSql.Append(" where guid='" + guid + "'");
}
try
{
rtnInt = DbHelperSQL.ExecuteSql(strSql.ToString());
mObj.outMsg = "操作成功!";
mObj.outSum = rtnInt;
mObj.outGuid = guid;
mObj.outNo = fabm;
}
catch (Exception ex)
{
LogHelper.Debug(this.ToString(), "EditModel error:" + ex.Message);
rtnInt = (int)ReturnCode.Exception;
mObj.outMsg = ex.Message;
mObj.outSum = -1;
}
if (mObj.outSum <= 0)
return ReturnDto.QuickReturn(mObj, ReturnCode.Exception, mObj.outMsg);
return ReturnDto.QuickReturn(mObj, ReturnCode.Success, mObj.outMsg);
}
///
/// 读取实体
///
///
///
[RequestMethod(RequestMethods.POST)]
[AllowAnonymous]
public ReturnDto GetModel([FromBody] dynamic model)
{
dynamic m = new System.Dynamic.ExpandoObject();
System.Text.StringBuilder sbSql = new StringBuilder();
sbSql.Append(" select top 1 * from MES_CQJY_FA where 1=1 and guid='" + model.guid.ToString() + "' ");
try
{
DataSet dset = new DataSet();
dset = DbHelperSQL.Query(sbSql.ToString());
if (dset != null && dset.Tables.Count > 0 && dset.Tables[0].Rows.Count > 0)
{
System.Data.DataRow dr = dset.Tables[0].Rows[0];
m = dr.RowToDynamic();
return ReturnDto.QuickReturn(m, ReturnCode.Success, "读取成功!");
}
else
return ReturnDto.QuickReturn(m, ReturnCode.Default, "读取失败!");
}
catch (Exception ex)
{
LogHelper.Debug(this.ToString(), "GetModel error:" + ex.Message);
return ReturnDto.QuickReturn(m, ReturnCode.Default, "读取失败!");
}
}
///
/// 判断是否审核
///
///
///
///
private int isChkOrUnChk(string guidList, int status)
{
int cont = 0;
cont = int.Parse(DbHelperSQL.GetSingle("select count(1) from MES_CQJY_FA where guid='" + guidList + "' and isnull(checkstatus,0)=" + status + "").ToString());
return cont;
}
#endregion
///
/// 移出或移入
///
///
///
[RequestMethod(RequestMethods.POST)]
public ReturnDto EditModelOutIn([FromBody] dynamic mode)
{
string _guid = mode.guid;
string _inFieldValue = mode.inFieldValue;
string _inItemsGuids = mode.inItemsGuids;
dynamic m = new ExpandoObject();
m.outSum = -1;
m.outMsg = "";
using (var conn = new SqlConnection(DbHelperSQL.strConn))
{
using (var cmd = new SqlCommand("cqjy_itemsOutIn", conn))
{
try
{
conn.Open();
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] parameters =
{
new("@outMsg", SqlDbType.NVarChar, 300),
new("@outSum", SqlDbType.Int),
new("@inEdtUserGuid", _userGuid),
new("@inOrderGuid", _guid),
new("@inFieldValue",_inFieldValue),
new("@inItemsGuids",_inItemsGuids),
};
parameters[0].Direction = ParameterDirection.Output;
parameters[1].Direction = ParameterDirection.Output;
foreach (var parameter in parameters)
cmd.Parameters.Add(parameter);
cmd.ExecuteNonQuery();
m.outMsg = parameters[0].Value.ToString();
m.outSum = int.Parse(parameters[1].Value.ToString());
}
catch (Exception ex)
{
m.outMsg = ex.Message;
LogHelper.Debug(this.ToString(), "cqjy_itemsOutIn error:" + ex.Message);
return ReturnDto.QuickReturn(m, ReturnCode.Default, ex.Message);
}
finally
{
conn.Close();
}
}
}
return ReturnDto.QuickReturn(m, ReturnCode.Success, "操作成功!");
}
///
/// 读取编码
///
///
///
private static string GetBillNo(string billType, string itemNo = "")
{
var sbSql = new StringBuilder();
sbSql.Append(" DECLARE @hNo nvarchar(50) ");
sbSql.Append(" EXEC [getOrderNo] '" + billType + "',@hNo output,'" +
itemNo + "'");
sbSql.Append(" select @hNo as t1");
var str = "";
try
{
str = DbHelperSQL.GetSingle(sbSql.ToString()).ToString();
}
catch (Exception ex)
{
str = Guid.NewGuid().ToString();
}
return str;
}
}
}