// 创建SO
        /// <summary>
        /// 创建SO
        /// </summary>
        /// <param name="bpObj"></param>
        /// <returns></returns>
        private List <SoBackDTO> CreateSO(CreateApprovedSaleOrderSV bpObj)
        {
            System.Collections.Generic.List <SoBackDTO> results = new System.Collections.Generic.List <SoBackDTO>();
            SoBackDTO result = new SoBackDTO();

            //object result2;
            try
            {
                if (bpObj.SoLineDto == null || bpObj.SoLineDto.Count == 0)
                {
                    result.IsSuccess = false;
                    result.ErrorInfo = "传入参数不可为空";
                    result.Timestamp = System.DateTime.Now.ToString();
                    HBHCommon.LoggerError(result.ErrorInfo);
                    results.Add(result);
                    //result2 = results;
                }
                else
                {
                    string usercode     = bpObj.SoLineDto[0].UserCode;
                    string enterprise   = bpObj.SoLineDto[0].EnterpriseCode;
                    string errormessage = this.ValidateParamNullOrEmpty(bpObj);
                    if (!string.IsNullOrEmpty(errormessage))
                    {
                        result.IsSuccess = false;
                        result.ErrorInfo = errormessage;
                        result.Timestamp = System.DateTime.Now.ToString();
                        HBHCommon.LoggerError(result.ErrorInfo);
                        results.Add(result);
                        //result2 = results;
                    }
                    else
                    {
                        List <string> lstProjectCode = new List <string>();

                        System.Collections.Generic.List <SOStatusDTOData> statusDTOs = null;
                        //using (UBFTransactionScope trans = new UBFTransactionScope(TransactionOption.Required))
                        {
                            using (ISession session = Session.Open())
                            {
                                foreach (SoLineDTO line in bpObj.SoLineDto)
                                {
                                    if (!string.IsNullOrEmpty(line.DmsSaleNo))
                                    {
                                        Project project = Project.Finder.Find(string.Format("Org={0} and Code='{1}'", Context.LoginOrg.ID, line.DmsSaleNo), new OqlParam[0]);
                                        if (project == null)
                                        {
                                            Project p = Project.Create();
                                            p.Org                     = (Context.LoginOrg);
                                            p.StartDate               = (System.DateTime.Now);
                                            p.EndDate                 = (System.DateTime.Now);
                                            p.Code                    = (line.DmsSaleNo);
                                            p.Name                    = (line.DmsSaleNo);
                                            p.Effective               = (new Effective());
                                            p.Effective.IsEffective   = (true);
                                            p.Effective.EffectiveDate = (System.Convert.ToDateTime("2000.01.01 00:00:00"));
                                            p.Effective.DisableDate   = (System.Convert.ToDateTime("9999.12.31"));
                                        }

                                        if (!lstProjectCode.Contains(line.DmsSaleNo))
                                        {
                                            lstProjectCode.Add(line.DmsSaleNo);
                                        }
                                    }
                                }
                                session.Commit();
                            }
                            // 先不下发,下发直接死掉了

                            //foreach (string projCode in lstProjectCode)
                            //{
                            //    Project project2 = Project.Finder.Find(string.Format("Org={0} and Code='{1}'", Context.LoginOrg.ID, projCode), new OqlParam[0]);
                            //    if (project2 != null)
                            //    {
                            //        try
                            //        {
                            //            OnlineSendObjsProxy onlineSendObjsProxy = new OnlineSendObjsProxy();
                            //            onlineSendObjsProxy.FullName = ("UFIDA.U9.CBO.SCM.ProjectTask.Project");
                            //            onlineSendObjsProxy.IDs = (new System.Collections.Generic.List<long>());
                            //            onlineSendObjsProxy.IDs.Add(project2.ID);
                            //            onlineSendObjsProxy.FromOrg = (Context.LoginOrg.ID);
                            //            onlineSendObjsProxy.ToOrgList = (new System.Collections.Generic.List<long>());
                            //            //Organization org = Organization.Finder.Find("Code='10'", new OqlParam[0]);
                            //            //if (org != null)
                            //            //{
                            //            //    onlineSendObjsProxy.ToOrgList.Add(org.ID);
                            //            //}
                            //            //Organization org2 = Organization.Finder.Find("Code='30'", new OqlParam[0]);
                            //            //if (org2 != null)
                            //            //{
                            //            //    onlineSendObjsProxy.ToOrgList.Add(org2.ID);
                            //            //}
                            //            if (HBHCommon.ProjectSendOrgCode.Count > 0)
                            //            {
                            //                foreach (string orgCode in HBHCommon.ProjectSendOrgCode)
                            //                {
                            //                    Organization org = Organization.Finder.Find("Code=@OrgCode"
                            //                        , new OqlParam(orgCode)
                            //                        );
                            //                    if (org != null)
                            //                    {
                            //                        onlineSendObjsProxy.ToOrgList.Add(org.ID);
                            //                    }
                            //                }
                            //            }
                            //            if (onlineSendObjsProxy.ToOrgList.Count > 0)
                            //            {
                            //                onlineSendObjsProxy.Do();
                            //            }
                            //        }
                            //        catch (System.Exception e)
                            //        {
                            //            //throw new System.ApplicationException(string.Format("{0}项目下发失败:{1}", project2.Code, ex.Message));
                            //            result.IsSuccess = false;
                            //            result.ErrorInfo = e.Message;
                            //            result.Timestamp = System.DateTime.Now.ToString();
                            //            results.Add(result);
                            //            //result2 = results;
                            //            //return result2;
                            //            return results;
                            //        }
                            //    }
                            //}

                            // 如果已经生成了订单,则看看是否审核、没有审核 则 审核之;


                            List <SO> lstSO;

                            CommonCreateSOSRVProxy proxy = new CommonCreateSOSRVProxy();
                            proxy.SOs = this.GetSaleOrderDTODataList(bpObj, out lstSO);

                            if (proxy.SOs != null &&
                                proxy.SOs.Count > 0
                                )
                            {
                                try
                                {
                                    //proxy.ContextDTO = (new ContextDTOData());
                                    //proxy.ContextDTO.OrgID = (Context.LoginOrg.ID);
                                    //proxy.ContextDTO.OrgCode = (Context.LoginOrg.Code);
                                    //proxy.ContextDTO.EntCode = (enterprise);
                                    //proxy.ContextDTO.UserCode = (usercode);
                                    //proxy.ContextDTO.CultureName = (Context.LoginLanguageCode);
                                    System.Collections.Generic.List <CommonArchiveDataDTOData> resultsolist = proxy.Do();
                                    if (resultsolist == null || resultsolist.Count == 0)
                                    {
                                        result.IsSuccess = false;
                                        result.ErrorInfo = "没有生成销售订单";
                                        result.Timestamp = System.DateTime.Now.ToString();
                                        results.Add(result);
                                        //result2 = results;

                                        HBHCommon.LoggerError(result.ErrorInfo);
                                        return(results);
                                    }
                                    SOStatusTransferBPProxy bp = new SOStatusTransferBPProxy();
                                    bp.SOKeyDTOList = new System.Collections.Generic.List <SM.SO.SOKeyDTOData>();
                                    foreach (CommonArchiveDataDTOData d in resultsolist)
                                    {
                                        SM.SO.SOKeyDTOData dto = new SM.SO.SOKeyDTOData();
                                        dto.SOkey        = (d.ID);
                                        dto.TargetStatus = (2);
                                        bp.SOKeyDTOList.Add(dto);
                                    }
                                    statusDTOs      = bp.Do();
                                    bp              = new SOStatusTransferBPProxy();
                                    bp.SOKeyDTOList = (new System.Collections.Generic.List <SM.SO.SOKeyDTOData>());
                                    foreach (SOStatusDTOData dt in statusDTOs)
                                    {
                                        SM.SO.SOKeyDTOData dto = new SM.SO.SOKeyDTOData();
                                        dto.SOkey        = (dt.SOID);
                                        dto.SOSysVersion = (dt.SysVersion);
                                        dto.TargetStatus = (3);
                                        bp.SOKeyDTOList.Add(dto);
                                    }
                                    statusDTOs = bp.Do();
                                    //trans.Commit();
                                }
                                catch (System.Exception e)
                                {
                                    //trans.Rollback();
                                    result.IsSuccess = false;
                                    result.ErrorInfo = e.Message;
                                    result.Timestamp = System.DateTime.Now.ToString();
                                    results.Add(result);
                                    //result2 = results;
                                    //return result2;
                                    HBHCommon.LoggerError(result.ErrorInfo + "/r/n" + e.StackTrace);
                                    return(results);
                                }
                            }

                            // 已经存在的订单,试着审核
                            if (lstSO != null &&
                                lstSO.Count > 0
                                )
                            {
                                SOStatusTransferBPProxy bp = new SOStatusTransferBPProxy();
                                bp.SOKeyDTOList = new System.Collections.Generic.List <SM.SO.SOKeyDTOData>();
                                foreach (SO so in lstSO)
                                {
                                    if (so != null &&
                                        so.Status == SODocStatusEnum.Open
                                        )
                                    {
                                        SM.SO.SOKeyDTOData dto = new SM.SO.SOKeyDTOData();
                                        dto.SOkey        = so.ID;
                                        dto.TargetStatus = (2);
                                        bp.SOKeyDTOList.Add(dto);
                                    }
                                }
                                if (bp.SOKeyDTOList != null &&
                                    bp.SOKeyDTOList.Count > 0
                                    )
                                {
                                    bp.Do();

                                    // 重新取订单,可以取到最新的订单状态
                                    foreach (SM.SO.SOKeyDTOData dto in bp.SOKeyDTOList)
                                    {
                                        if (dto != null &&
                                            dto.SOkey > 0
                                            )
                                        {
                                            for (int i = 0; i < lstSO.Count; i++)
                                            {
                                                SO so = lstSO[i];

                                                if (so != null &&
                                                    so.ID == dto.SOkey
                                                    )
                                                {
                                                    lstSO[i] = SO.Finder.FindByID(dto.SOkey);
                                                }
                                            }
                                        }
                                    }
                                }

                                bp = new SOStatusTransferBPProxy();
                                bp.SOKeyDTOList = (new System.Collections.Generic.List <SM.SO.SOKeyDTOData>());
                                foreach (SO so in lstSO)
                                {
                                    if (so != null &&
                                        so.Status == SODocStatusEnum.Open
                                        )
                                    {
                                        SM.SO.SOKeyDTOData dto = new SM.SO.SOKeyDTOData();
                                        dto.SOkey        = so.ID;
                                        dto.SOSysVersion = (so.SysVersion);
                                        dto.TargetStatus = (3);
                                        bp.SOKeyDTOList.Add(dto);
                                    }
                                }
                                if (bp.SOKeyDTOList != null &&
                                    bp.SOKeyDTOList.Count > 0
                                    )
                                {
                                    bp.Do();
                                }

                                if (statusDTOs == null)
                                {
                                    statusDTOs = new List <SOStatusDTOData>();
                                }

                                // 组织创建结果DTO
                                foreach (SO so in lstSO)
                                {
                                    if (so != null
                                        )
                                    {
                                        SOStatusDTOData resultDTO = new SOStatusDTOData();
                                        resultDTO.SOID = so.ID;

                                        statusDTOs.Add(resultDTO);
                                    }
                                }
                            }
                        }
                        if (statusDTOs != null && statusDTOs.Count > 0)
                        {
                            foreach (SOStatusDTOData dt in statusDTOs)
                            {
                                SO so = SO.Finder.FindByID(dt.SOID);
                                if (so != null)
                                {
                                    result.IsSuccess = true;
                                    result.ErrorInfo = "生单成功";
                                    result.DMSDocNo  = so.DescFlexField.PubDescSeg5;
                                    result.ERPDocNo  = so.DocNo;
                                    results.Add(result);
                                }
                            }
                        }
                        //result2 = results;
                    }
                }
            }
            catch (System.Exception e)
            {
                result.IsSuccess = false;
                result.ErrorInfo = e.Message;
                result.Timestamp = System.DateTime.Now.ToString();
                HBHCommon.LoggerError(result.ErrorInfo + "/r/n" + e.StackTrace);
                results.Add(result);
                //result2 = results;
            }
            //return result2;
            return(results);
        }
        // 创建SO
        /// <summary>
        /// 创建SO
        /// </summary>
        /// <param name="bpObj"></param>
        /// <returns></returns>
        private List<SoBackDTO> CreateSO(CreateApprovedSaleOrderSV bpObj)
        {
            System.Collections.Generic.List<SoBackDTO> results = new System.Collections.Generic.List<SoBackDTO>();
            SoBackDTO result = new SoBackDTO();
            //object result2;
            try
            {
                if (bpObj.SoLineDto == null || bpObj.SoLineDto.Count == 0)
                {
                    result.IsSuccess = false;
                    result.ErrorInfo = "传入参数不可为空";
                    result.Timestamp = System.DateTime.Now.ToString();
                    HBHCommon.LoggerError(result.ErrorInfo);
                    results.Add(result);
                    //result2 = results;
                }
                else
                {
                    string usercode = bpObj.SoLineDto[0].UserCode;
                    string enterprise = bpObj.SoLineDto[0].EnterpriseCode;
                    string errormessage = this.ValidateParamNullOrEmpty(bpObj);
                    if (!string.IsNullOrEmpty(errormessage))
                    {
                        result.IsSuccess = false;
                        result.ErrorInfo = errormessage;
                        result.Timestamp = System.DateTime.Now.ToString();
                        HBHCommon.LoggerError(result.ErrorInfo);
                        results.Add(result);
                        //result2 = results;
                    }
                    else
                    {
                        List<string> lstProjectCode = new List<string>();

                        System.Collections.Generic.List<SOStatusDTOData> statusDTOs = null;
                        //using (UBFTransactionScope trans = new UBFTransactionScope(TransactionOption.Required))
                        {
                            using (ISession session = Session.Open())
                            {
                                foreach (SoLineDTO line in bpObj.SoLineDto)
                                {
                                    if (!string.IsNullOrEmpty(line.DmsSaleNo))
                                    {
                                        Project project = Project.Finder.Find(string.Format("Org={0} and Code='{1}'", Context.LoginOrg.ID, line.DmsSaleNo), new OqlParam[0]);
                                        if (project == null)
                                        {
                                            Project p = Project.Create();
                                            p.Org = (Context.LoginOrg);
                                            p.StartDate = (System.DateTime.Now);
                                            p.EndDate = (System.DateTime.Now);
                                            p.Code = (line.DmsSaleNo);
                                            p.Name = (line.DmsSaleNo);
                                            p.Effective = (new Effective());
                                            p.Effective.IsEffective = (true);
                                            p.Effective.EffectiveDate = (System.Convert.ToDateTime("2000.01.01 00:00:00"));
                                            p.Effective.DisableDate = (System.Convert.ToDateTime("9999.12.31"));
                                        }

                                        if (!lstProjectCode.Contains(line.DmsSaleNo))
                                        {
                                            lstProjectCode.Add(line.DmsSaleNo);
                                        }
                                    }
                                }
                                session.Commit();
                            }
                            // 先不下发,下发直接死掉了

                            foreach (string projCode in lstProjectCode)
                            {
                                Project project2 = Project.Finder.Find(string.Format("Org={0} and Code='{1}'", Context.LoginOrg.ID, projCode), new OqlParam[0]);
                                if (project2 != null)
                                {
                                    try
                                    {
                                        OnlineSendObjsProxy onlineSendObjsProxy = new OnlineSendObjsProxy();
                                        onlineSendObjsProxy.FullName = ("UFIDA.U9.CBO.SCM.ProjectTask.Project");
                                        onlineSendObjsProxy.IDs = (new System.Collections.Generic.List<long>());
                                        onlineSendObjsProxy.IDs.Add(project2.ID);
                                        onlineSendObjsProxy.FromOrg = (Context.LoginOrg.ID);
                                        onlineSendObjsProxy.ToOrgList = (new System.Collections.Generic.List<long>());
                                        //Organization org = Organization.Finder.Find("Code='10'", new OqlParam[0]);
                                        //if (org != null)
                                        //{
                                        //    onlineSendObjsProxy.ToOrgList.Add(org.ID);
                                        //}
                                        //Organization org2 = Organization.Finder.Find("Code='30'", new OqlParam[0]);
                                        //if (org2 != null)
                                        //{
                                        //    onlineSendObjsProxy.ToOrgList.Add(org2.ID);
                                        //}
                                        if (HBHCommon.ProjectSendOrgCode.Count > 0)
                                        {
                                            foreach (string orgCode in HBHCommon.ProjectSendOrgCode)
                                            {
                                                Organization org = Organization.Finder.Find("Code=@OrgCode"
                                                    , new OqlParam(orgCode)
                                                    );
                                                if (org != null)
                                                {
                                                    onlineSendObjsProxy.ToOrgList.Add(org.ID);
                                                }
                                            }
                                        }
                                        if (onlineSendObjsProxy.ToOrgList.Count > 0)
                                        {
                                            onlineSendObjsProxy.Do();
                                        }
                                    }
                                    catch (System.Exception e)
                                    {
                                        //throw new System.ApplicationException(string.Format("{0}项目下发失败:{1}", project2.Code, ex.Message));
                                        result.IsSuccess = false;
                                        result.ErrorInfo = e.Message;
                                        result.Timestamp = System.DateTime.Now.ToString();
                                        results.Add(result);
                                        //result2 = results;
                                        //return result2;
                                        return results;
                                    }
                                }
                            }

                            // 如果已经生成了订单,则看看是否审核、没有审核 则 审核之;

                            List<SO> lstSO;

                            CommonCreateSOSRVProxy proxy = new CommonCreateSOSRVProxy();
                            proxy.SOs = this.GetSaleOrderDTODataList(bpObj, out lstSO);

                            if (proxy.SOs != null
                                && proxy.SOs.Count > 0
                                )
                            {
                                try
                                {
                                    //proxy.ContextDTO = (new ContextDTOData());
                                    //proxy.ContextDTO.OrgID = (Context.LoginOrg.ID);
                                    //proxy.ContextDTO.OrgCode = (Context.LoginOrg.Code);
                                    //proxy.ContextDTO.EntCode = (enterprise);
                                    //proxy.ContextDTO.UserCode = (usercode);
                                    //proxy.ContextDTO.CultureName = (Context.LoginLanguageCode);
                                    System.Collections.Generic.List<CommonArchiveDataDTOData> resultsolist = proxy.Do();
                                    if (resultsolist == null || resultsolist.Count == 0)
                                    {
                                        result.IsSuccess = false;
                                        result.ErrorInfo = "没有生成销售订单";
                                        result.Timestamp = System.DateTime.Now.ToString();
                                        results.Add(result);
                                        //result2 = results;

                                        HBHCommon.LoggerError(result.ErrorInfo);
                                        return results;
                                    }
                                    SOStatusTransferBPProxy bp = new SOStatusTransferBPProxy();
                                    bp.SOKeyDTOList = new System.Collections.Generic.List<SM.SO.SOKeyDTOData>();
                                    foreach (CommonArchiveDataDTOData d in resultsolist)
                                    {
                                        SM.SO.SOKeyDTOData dto = new SM.SO.SOKeyDTOData();
                                        dto.SOkey = (d.ID);
                                        dto.TargetStatus = (2);
                                        bp.SOKeyDTOList.Add(dto);
                                    }
                                    statusDTOs = bp.Do();
                                    bp = new SOStatusTransferBPProxy();
                                    bp.SOKeyDTOList = (new System.Collections.Generic.List<SM.SO.SOKeyDTOData>());
                                    foreach (SOStatusDTOData dt in statusDTOs)
                                    {
                                        SM.SO.SOKeyDTOData dto = new SM.SO.SOKeyDTOData();
                                        dto.SOkey = (dt.SOID);
                                        dto.SOSysVersion = (dt.SysVersion);
                                        dto.TargetStatus = (3);
                                        bp.SOKeyDTOList.Add(dto);
                                    }
                                    statusDTOs = bp.Do();
                                    //trans.Commit();
                                }
                                catch (System.Exception e)
                                {
                                    //trans.Rollback();
                                    result.IsSuccess = false;
                                    result.ErrorInfo = e.Message;
                                    result.Timestamp = System.DateTime.Now.ToString();
                                    results.Add(result);
                                    //result2 = results;
                                    //return result2;
                                    HBHCommon.LoggerError(result.ErrorInfo + "/r/n" + e.StackTrace);
                                    return results;
                                }
                            }

                            // 已经存在的订单,试着审核
                            if (lstSO != null
                                && lstSO.Count > 0
                                )
                            {

                                SOStatusTransferBPProxy bp = new SOStatusTransferBPProxy();
                                bp.SOKeyDTOList = new System.Collections.Generic.List<SM.SO.SOKeyDTOData>();
                                foreach (SO so in lstSO)
                                {
                                    if (so != null
                                        && so.Status == SODocStatusEnum.Open
                                        )
                                    {
                                        SM.SO.SOKeyDTOData dto = new SM.SO.SOKeyDTOData();
                                        dto.SOkey = so.ID;
                                        dto.TargetStatus = (2);
                                        bp.SOKeyDTOList.Add(dto);
                                    }
                                }
                                if (bp.SOKeyDTOList != null
                                    && bp.SOKeyDTOList.Count > 0
                                    )
                                {
                                    bp.Do();

                                    // 重新取订单,可以取到最新的订单状态
                                    foreach (SM.SO.SOKeyDTOData dto in bp.SOKeyDTOList)
                                    {
                                        if (dto != null
                                            && dto.SOkey > 0
                                            )
                                        {
                                            for (int i = 0; i < lstSO.Count; i++)
                                            {
                                                SO so = lstSO[i];

                                                if (so != null
                                                    && so.ID == dto.SOkey
                                                    )
                                                {
                                                    lstSO[i] = SO.Finder.FindByID(dto.SOkey);
                                                }
                                            }
                                        }
                                    }
                                }

                                bp = new SOStatusTransferBPProxy();
                                bp.SOKeyDTOList = (new System.Collections.Generic.List<SM.SO.SOKeyDTOData>());
                                foreach (SO so in lstSO)
                                {
                                    if (so != null
                                        && so.Status == SODocStatusEnum.Open
                                        )
                                    {
                                        SM.SO.SOKeyDTOData dto = new SM.SO.SOKeyDTOData();
                                        dto.SOkey = so.ID;
                                        dto.SOSysVersion = (so.SysVersion);
                                        dto.TargetStatus = (3);
                                        bp.SOKeyDTOList.Add(dto);
                                    }
                                }
                                if (bp.SOKeyDTOList != null
                                    && bp.SOKeyDTOList.Count > 0
                                    )
                                {
                                    bp.Do();
                                }

                                if (statusDTOs == null)
                                {
                                    statusDTOs = new List<SOStatusDTOData>();
                                }

                                // 组织创建结果DTO
                                foreach (SO so in lstSO)
                                {
                                    if (so != null
                                        )
                                    {
                                        SOStatusDTOData resultDTO = new SOStatusDTOData();
                                        resultDTO.SOID = so.ID;

                                        statusDTOs.Add(resultDTO);
                                    }
                                }
                            }
                        }
                        if (statusDTOs != null && statusDTOs.Count > 0)
                        {
                            foreach (SOStatusDTOData dt in statusDTOs)
                            {
                                SO so = SO.Finder.FindByID(dt.SOID);
                                if (so != null)
                                {
                                    result.IsSuccess = true;
                                    result.ErrorInfo = "生单成功";
                                    result.DMSDocNo = so.DescFlexField.PubDescSeg5;
                                    result.ERPDocNo = so.DocNo;
                                    results.Add(result);
                                }
                            }
                        }
                        //result2 = results;
                    }
                }
            }
            catch (System.Exception e)
            {
                result.IsSuccess = false;
                result.ErrorInfo = e.Message;
                result.Timestamp = System.DateTime.Now.ToString();
                HBHCommon.LoggerError(result.ErrorInfo + "/r/n" + e.StackTrace);
                results.Add(result);
                //result2 = results;
            }
            //return result2;
            return results;
        }