Google Code Prettify

[MVC]DbContext 資料處理方法的參考範例



D:\0_Rueije\GCP_20180314\GCP201803Web\BT20180402\Models\GCPModelFunction\cls_t_member.cs
using System;
using System.Linq;
using System.Web;

using BT20180402.Models.GCP;
using BT20180402.Models.GCPViewModel;

using clsWebLibrary;

namespace BT20180402.Models.GCPModelFunction {
    public class cls_t_member
    {
        private clsMessage _cMsg = new clsMessage();
        private GCPDbContext _db = new GCPDbContext();
        public t_member_profile GetMemberProfile() {
            t_member_profile ret = new t_member_profile();
            if(HttpContext.Current.Session == null)
                return ret;
            t_member member = (t_member)HttpContext.Current.Session["member_profile"];
            ret = new t_member_profile()
            {
                member_email = member.member_email,
                member_name = member.member_name,
                member_gender = member.member_gender,
                member_birthday = member.member_birthday
            };
            return ret;
        }
        /// ≪summary>
        /// 是否已經登入系統
        /// ≪/summary>
        /// ≪returns>≪/returns>
        public bool IsLogin() {
            bool ret = false;
            if(HttpContext.Current.Session != null)
                ret = HttpContext.Current.Session["member_profile"] != null;
            return ret;
        }
        //TODO:待測
        public bool IsExitsts(string member_id) {
            bool ret = false;
            try
            {
                t_member m = _db.t_member.Where(r => r.member_id == member_id).SingleOrDefault();
                if(m != null)
                    if(member_id != null)
                        ret = true;
            }
            catch(Exception ex)
            {
                _cMsg.TextDataWrite(this.ToString() + Environment.NewLine + ex.ToString());
            }
            return ret;
        }
        /// ≪summary>
        /// 取得會員資料
        /// ≪/summary>
        /// ≪param name="member_email">≪/param>
        /// ≪returns>≪/returns>
        public t_member GetMemberInfoByEmail(string member_email) {
            t_member ret = new t_member();
            try
            {
                ret = _db.t_member.Where(r => r.member_email == member_email).SingleOrDefault();
            }
            catch(Exception ex)
            {
                _cMsg.TextDataWrite(this.ToString() + Environment.NewLine + ex.ToString());
            }
            return ret;
        }
        public t_member GetMemberInfoById(string member_id) {
            t_member ret = new t_member();
            try
            {
                ret = _db.t_member.Where(r => r.member_id == member_id).SingleOrDefault();
            }
            catch(Exception ex)
            {
                _cMsg.TextDataWrite(this.ToString() + Environment.NewLine + ex.ToString());
            }
            return ret;
        }
        /// ≪summary>
        /// 檢查會員信箱及密碼是否存在
        /// ≪/summary>
        /// ≪param name="vm">≪/param>
        /// ≪param name="member">≪/param>
        /// ≪returns>≪/returns>
        public bool MemberLoginCheck(t_member_login vm, ref t_member member) {
            bool ret = false;
            if(!string.IsNullOrEmpty(vm.member_email))
                if(!string.IsNullOrEmpty(vm.member_password))
                {
                    member = GetMemberInfoByEmail(vm.member_email);
                    if(member != null)
                        if(member.is_using == "Y")
                            if((new msEncode()).deCodeByRnd(member.member_password).Equals(vm.member_password))
                            {
                                HttpContext.Current.Session["member_profile"] = member;
                                ret = true;
                            }
                            else
                                _cMsg.TextLogWrite("密碼解密時發生錯誤![" + vm.member_email + "]");
                        else
                            _cMsg.TextLogWrite("帳戶未啟用![" + vm.member_email + "]");
                    else
                        _cMsg.TextLogWrite("沒有找到會員資料!["+vm.member_email+"]");
                }
                else
                    _cMsg.TextLogWrite("未填寫密碼![" + vm.member_email + "]");
            else
                _cMsg.TextLogWrite("未填寫帳號!");
            return ret;
        }
        public bool Insert(t_member_register vm) {
            bool ret = false;
            try
            {
                t_member m = new t_member()
                {
                    member_email = vm.member_email,
                    member_password = (new msEncode()).enCodeByRnd(vm.member_password),
                    create_date = DateTime.Now
                };
                _db.t_member.Add(m);
                _db.SaveChanges();
                ret = true;
            }
            catch(Exception ex)
            {
                _cMsg.TextLogWrite(ex.ToString());
            }
            return ret;
        }
        public bool Update(t_member_profile vm) {
            bool ret = false;
            try
            {
                t_member m = GetMemberInfoByEmail(vm.member_email);
                m.member_email = vm.member_email;
                m.member_name = vm.member_name;
                m.member_gender = vm.member_gender;
                m.member_birthday = vm.member_birthday;
                m.update_date = vm.update_date;
                m.updater = vm.updater;
                _db.Entry(m).State = System.Data.Entity.EntityState.Modified;
                _db.SaveChanges();
                ret = true;
            }
            catch(Exception ex)
            {
                _cMsg.TextLogWrite(ex.ToString());
            }
            return ret;
        }        
        public bool AccountActivation(string member_id) {
            bool ret = false;
            try
            {
                t_member m = GetMemberInfoById(member_id);
                if(m != null)
                {
                    if(m.is_using != "Y")
                    {
                        m.open_date = DateTime.Now;
                        m.is_using = "Y";
                        _db.Entry(m).State = System.Data.Entity.EntityState.Modified;
                        _db.SaveChanges();
                    }
                    ret = true;
                }
            }
            catch(Exception ex)
            {
                _cMsg.TextLogWrite(ex.ToString());
            }
            return ret;
        }
    }
}