using ICSSoft.Common;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ICSSoft.Entity;
using UFSoft.UBF.Util.Context;
using System.ServiceModel;
using www.ufida.org.EntityData;
using UFSoft.UBF.Exceptions1;
using UFSoft.UBF.Service;
using ICSSoft.APIApproveCompRpt;
using ICSSoft.APIDeleteCompRpt;
using ICSSoft.APICloseCompRpt;
using ICSSoft.DataProject;
namespace ICSSoft.APICreateCompRpt
{
public class CreateCompRpt
{
private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
///
/// 创建完工报告(外部通过API调用)
///
///
///
public string Create(List infos)
{
string msg = "";
DataSet ds = null;
UFIDAU9ISVMOICreateCompRptSrvClient client = new UFIDAU9ISVMOICreateCompRptSrvClient();
//服务返回结果
UFIDAU9ISVMOCompRptKeyDTOData[] returnItem;
//返回异常信息,目前还没有使用此属性
UFSoft.UBF.Exceptions.MessageBase[] returnMsg;
//上下文信息
object context;
//传入 参数
UFIDAU9ISVMOCompRptDTOData[] compRptDTOs;
try
{
if (infos == null || infos.Count == 0)
{
throw new Exception("传入数据不能为空!");
}
string OrgID = "";
string orgsql = @"select ID from Base_Organization
where Code='{0}'";
orgsql = string.Format(orgsql, infos[0].WorkPoint);
DataTable orgdt = DBHelper.Query(orgsql, connString);
if (orgdt.Rows.Count != 0)
{
OrgID = orgdt.Rows[0]["ID"].ToString();
}
else
{
throw new Exception("传入的组织编码有误!");
}
//给上下文信息赋值
context = CreateContextObj(OrgID);
//给传入参数赋值
compRptDTOs = GetDataNew(infos, OrgID);
//服务调用
(client.Endpoint.Binding as BasicHttpBinding).MaxReceivedMessageSize = int.MaxValue;
(client.Endpoint.Binding as BasicHttpBinding).MaxBufferSize = int.MaxValue;
returnItem = client.Do(out returnMsg, context, compRptDTOs);
List DocNoList = new List();
foreach (UFIDAU9ISVMOCompRptKeyDTOData data in returnItem)
{
if (data.m_operateResult == false)
{
throw new Exception(data.m_errorMsg);
}
DocNoList.Add(data.m_docNo);
}
bool ApproveResult = false;
ApproveCompRpt custApproveComRpt = new ApproveCompRpt();
ApproveResult = custApproveComRpt.Approve(DocNoList, OrgID);
if (ApproveResult == false)
{
bool DeleteResult = false;
DeleteCompRpt delete = new DeleteCompRpt();
DeleteResult = delete.Delete(DocNoList, OrgID);
}
else
{
bool CloseResult = false;
CloseCompRpt CloseSV = new CloseCompRpt();
CloseResult = CloseSV.Close(DocNoList, OrgID);
if (CloseResult == true)
{
string strDocNOList = "";
foreach (string DocNo in DocNoList)
{
if (strDocNOList == "")
{
strDocNOList = "'" + DocNo + "'";
}
else
{
strDocNOList += ",'" + DocNo + "'";
}
}
string sql = @"select DISTINCT a.ID as ID,a.ID as IDs,a.DocNo as MRCVCode,c.Code AS cDepCode,d.Name AS cDepName
,e.Code AS cWhCode,f.Name AS cWhName
,g.DocNo as MOCode ,a.CreatedBy as CreateUser ,a.CreatedOn as CreateDateTime
,a.ApproveBy as Checker ,a.ApproveOn as CheckDateTime
from MO_CompleteRpt a
left join CBO_Department c on c.ID=a.HandleDept
left join CBO_Department_Trl d on d.ID=c.ID
left join CBO_Wh e on e.ID=a.RcvWh
left join CBO_Wh_Trl f on f.ID=e.ID
left join MO_MO g on g.ID=a.MO
WHERE A.DocNo in({0}) AND a.Org='{1}'";
sql = string.Format(sql, strDocNOList, OrgID);
DataTable mdt = DBHelper.Query(sql, connString);
sql = @"select DISTINCT a.ID as IDs, b.ID as DetailID,b.LineNum as Sequence,c.Code as InvCode ,
b.RcvQtyByWhUOM as Quantity ,0 as Amount,a.MO as MODetailID,
isnull(d.Code,'') ProjectCode,isnull(e.Code,'') cBatch,'' version ,'' brand,
'' as cFree1,
'' as cFree2,
'' as cFree3,
'' as cFree4,
'' as cFree5,
'' as cFree6,
'' as cFree7,
'' as cFree8,
'' as cFree9,
'' as cFree10
from MO_CompleteRpt a
left join MO_CompleteRptRcvLine b on b.CompleteRpt=a.ID
left join CBO_ItemMaster c on c.ID=a.Item
left join CBO_Project d on d.ID=a.Project
left join Lot_LotMaster e on e.ID=a.ProductLotMaster
WHERE A.DocNo in({0}) AND a.Org='{1}'";
sql = string.Format(sql, strDocNOList, OrgID);
DataTable cdt = DBHelper.Query(sql, connString);
DataTable tempdt1 = mdt.Copy();
tempdt1.TableName = "tempdt1";
ds.Tables.Add(tempdt1);
DataTable tempdt2 = cdt.Copy();
tempdt2.TableName = "tempdt2";
ds.Tables.Add(tempdt2);
}
else
{
throw new Exception("完工报告关闭失败,创建的完工报告已删除!");
}
}
}
catch (Exception ex)
{
log.Error(GetExceptionMessage(ex));
throw new Exception(GetExceptionMessage(ex));
}
msg = JSON.DataSetToJson(ds, "details", "IDs");
return msg;
}
private static long GetTypeID(string docTypeCode, string OrgID)
{
string sql = @"select ID from MO_CompleteRpt where DocNo='" + docTypeCode + "' AND Org='" + OrgID + "' ";
DataTable dt = DBHelper.Query(sql, connString);
if (dt.Rows.Count > 0)
{
return long.Parse(dt.Rows[0][0].ToString());
}
else
{
return 0;
}
}
#region 给上下文信息赋值
///
/// 给上下文信息赋值
///
///
private static ThreadContext CreateContextObj(string OrgID)
{
// 实例化应用上下文对象
ThreadContext thContext = new ThreadContext();
System.Collections.Generic.Dictionary