/// <summary>
 /// 根据公司Code取得CostCenter集合
 /// </summary>
 /// <param name="companyCode"></param>
 /// <returns></returns>
 public static List <Model.Common.SingleField> GetActCostCenter(Model.Common.SingleField param)
 {
     try
     {
         List <Model.Common.SingleField> lstCostCenter = factory.GetActCostCenter(param);
         if (lstCostCenter == null || lstCostCenter.Count == 0)
         {
             throw new Exception("DAL.SalesOrder.CompanyFactory.GetActCostCenter()==null");
         }
         return(lstCostCenter);
     }
     catch (Exception ex)
     {
         LogHelper.WriteLog(new Log()
         {
             message = ex.Message
         }, "GetActCostCenter");
         return(null);
     }
 }
        /// <summary>
        /// 根据公司Code取得CostCenter集合
        /// </summary>
        /// <param name="companyCode"></param>
        /// <returns></returns>
        public List <Model.Common.SingleField> GetActCostCenter(Model.Common.SingleField param)
        {
            List <Model.Common.SingleField> lstCostCenter = new List <Model.Common.SingleField>();
            List <Model.Common.SingleField> lstCCOrder    = new List <Model.Common.SingleField>();
            List <Model.Common.SingleField> lstCCWhs      = new List <Model.Common.SingleField>();

            // 当前时间
            string strDateTime = DateTime.Now.ToString("yyyy-MM-dd");
            // Sql文(SalesOrderLine)
            string strSqlOrder = " SELECT DISTINCT L.COSTCENTERCODE AS CODE " +
                                 " FROM SALESORDERITEM (nolock) AS L " +
                                 " INNER JOIN SALESORDERHEAD (nolock) AS H " +
                                 " ON L.HEADGUID = H.HEADGUID " +
                                 " AND H.OWNERCOMPANYCODE = '{0}' " +
                                 " AND H.STATUS <> '0' " +
                                 " AND L.STATUS <> '0' " +
                                 " INNER JOIN PRODUCTTYPEDATA AS P " +
                                 " ON L.PRODUCTCODE = P.ID " +
                                 " AND P.PRODUCTTYPE = 'SalesMeals' " +
                                 " WHERE H.VALIDDATE <= '{1}' AND H.STARTDATE <= '{1}' " +
                                 " AND ISNULL(H.EXPIRYDATE, H.ENDDATE) >= '{1}' " +
                                 " AND L.STARTDATE <= '{1}' " +
                                 " AND L.ENDDATE >= '{1}' " +
                                 " AND ISNULL(L.EXPIRYDATE,'2222-12-12') >= '{1}' ";

            lstCCOrder = SqlServerHelper.GetEntityList <Model.Common.SingleField>(SqlServerHelper.salesorderConn(), string.Format(strSqlOrder,
                                                                                                                                  param.code, strDateTime));

            // Sql文(CCWhs)
            string strSqlCCWhs = " SELECT COSTCENTERCODE AS CODE " +
                                 " FROM CCMast " +
                                 " WHERE DBNAME = '{0}' ";

            lstCCWhs = SqlServerHelper.GetEntityList <Model.Common.SingleField>(SqlServerHelper.salesorderConn(), string.Format(strSqlCCWhs,
                                                                                                                                param.code));

            //lstCostCenter = lstCCOrder.Concat(lstCCWhs).ToList();
            //lstCostCenter = lstCostCenter.Distinct().ToList();

            lstCostCenter = (from so in lstCCOrder
                             join wh in lstCCWhs
                             on so.code equals wh.code into cc
                             select new Model.Common.SingleField()
            {
                code = so.code
            }).ToList();

            if (lstCostCenter.Count == 0)
            {
                return(lstCostCenter);
            }

            // 如果Flag不为空则继续获取中/英文名称
            if (string.IsNullOrWhiteSpace(param.flag))
            {
                // 成本中心主档
                CompanyAddress addr = new CompanyAddress();
                addr.ip      = GetCompanyInfoByCode(param.code).ip;
                addr.erpCode = param.code;
                List <CostCenter> lstCCMastData = (new MastDataFactory()).GetCostCenter(addr);

                lstCostCenter.GroupJoin(lstCCMastData,
                                        cc => new { code = cc.code },
                                        ma => new { code = ma.costCenterCode },
                                        (cc, ma) =>
                {
                    var first  = ma.FirstOrDefault();
                    bool check = first != null;
                    cc.name1   = check ? first.costCenterName_ZH : string.Empty;
                    cc.name2   = check ? first.costCenterName_EN : string.Empty;

                    return(cc);
                }).ToList();
            }

            lstCostCenter = lstCostCenter.OrderBy(r => r.code).ToList();

            return(lstCostCenter);
        }