匿名委派Func應用(共用帳戶申請書狀態更新)

匿名委派Func的用途在於透過一個統一的匿名函式(Func)去執行分別對應的委派函式,以達到統一定義類似功能的函示的參數和回傳值(跟Interface有點類似,定義好函式需要的參數跟回傳值)。這樣就可以把不同的商業邏輯都放到對應的委派函式。而不是把所有的商業邏輯都包成一個函式用if...else來判斷該取得哪段商業邏輯結果。提高程式可讀性

舉例來說[CREATE]和[UPDATE]這兩個功能需要的參數其實都是差不多的,就可以透過匿名函式分別呼叫對應的委派函式。而不用把CREATEA跟UPDATE寫在一起再用if...else...判斷

        //APIModel實際應用
        bool isUpdateSuccess = corporateModel.changeFormStatusforUpdate(source, shareAccNo, departmentNo, departmentName, /*reviewCodeId*/applicationCodeId, funcId);

        /// <summary>
        /// DP. CRM進件申請共用帳戶異動時,要檢查CP是否也已經有做共用帳戶異動。有的話要把CP申請書中止,並變更狀態為S
        /// </summary>
        /// <param name="sysType"></param>
        /// <param name="shareAccNo"></param>
        /// <param name="depId"></param>
        /// <param name="depName"></param>
        /// <param name="paperFuncId"></param>
        /// <param name="funcId"></param>
        /// <returns></returns>
        public bool changeFormStatusforUpdate(string sysType, string shareAccNo, string depId, string depName, string paperFuncId, string funcId)
        {
            return changeFormStatus((p1, p2, p3, p4, p5, p6) => changeFormStatusMainFunforUpdate(p1, p2, p3, p4, p5, p6), sysType, shareAccNo, depId, depName, paperFuncId, funcId);
        }

        /// <summary>
        /// [匿名函式] 依照不同申請書可新增不同委派函式進行申請書狀態變更
        /// </summary>
        /// <param name="myFunc"></param>
        /// <param name="sysType"></param>
        /// <param name="shareAccNo"></param>
        /// <param name="depId"></param>
        /// <param name="depName"></param>
        /// <param name="paperFuncId"></param>
        /// <param name="funcId"></param>
        /// <returns></returns>
        private bool changeFormStatus(Func<string, string, string, string, string, string, bool> myFunc, string sysType, string shareAccNo, string depId, string depName, string paperFuncId, string funcId)
        {
            string para_sysType = !string.IsNullOrEmpty(sysType) ? sysType : "";
            string para_shareAccNo = !string.IsNullOrEmpty(shareAccNo) ? shareAccNo : "";
            string para_depId = !string.IsNullOrEmpty(depId) ? depId : "";
            string para_depName = !string.IsNullOrEmpty(depName) ? depName : "";
            string para_paperFuncId = !string.IsNullOrEmpty(paperFuncId) ? paperFuncId : "";
            string para_funcId = !string.IsNullOrEmpty(funcId) ? funcId : "";

            //return的匿名函式會return給=>的左邊
            return myFunc(para_sysType, para_shareAccNo, para_depId, para_depName, para_paperFuncId, para_funcId);
        }

        /// <summary>
        /// [委派函式主要處理邏輯] DP. CRM進件申請共用帳戶異動時,要檢查CP是否也已經有做共用帳戶異動。有的話要把CP申請書中止,並變更狀態為S
        /// </summary>
        /// <param name="sysType"></param>
        /// <param name="shareAccNo"></param>
        /// <param name="depId"></param>
        /// <param name="depName"></param>
        /// <param name="paperFuncId"></param>
        /// <param name="funcId"></param>
        /// <returns></returns>
        private bool changeFormStatusMainFunforUpdate(string sysType, string shareAccNo, string depId, string depName, string paperFuncId, string funcId)
        {
            bool res = true;
            if (sysType == "CP")
            {//CP進件不用檢查DP.CRM,因為DP.CRM都已經即時做完
                return res;
            }
            var list = getMasterList(sysType, shareAccNo, depId, depName, paperFuncId, funcId).Where(c => c.STATUS == "R");//DP. CRM進件要檢查CP是否有待審核申請書
            if (list != null)
            {
                foreach (var item in list)
                {
                    try
                    {
                        item.STATUS = "S";
                        info.db.SaveChanges();
                    }
                    catch (Exception)
                    {
                        return false;
                    }
                }
            }
            return res;
        }