⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customeraccessor.cs

📁 MIED是用于个人帐务管理的小型软件
💻 CS
字号:
using System.Collections;
using System.Data.Common;
using DatabaseUtil;
using Mied.BusinessObject;
using Mied.BusinessObject.Bills;
using Mied.BusinessObject.Emails;
using Mied.DAL.Cache;
using System;
using Mied.DAL.TableSchema;
using CommonUtil;

namespace Mied.DAL.Accesses
{
    public class CustomerAccessor : ContactAccessor
    {
        public CustomerAccessor(MiedDatabase database)
            : base(database, typeof(Customer), ContactType.Customer)
        {
        }

        public Customer SelectCustomer(int id)
        {
            return (Customer)base.Select(id);
        }

        public Customer SelectCustomer(ContactKey contactKey)
        {
            return (Customer)base.SelectContact(contactKey);
        }

        public CustomerList SelectCustomerList()
        {
            return (CustomerList)this.SelectList();
        }

        protected override IList CreateEntityList()
        {
            return new CustomerList();
        }

        protected override IList CreateCacheListObject()
        {
            return new CustomerCacheList(this);
        }

        public Aging SelectAging(int contactID)
        {
            Aging aging = new Aging();

            BillSummaryList billList = this.Database.InvoiceAccessor.SelectSummaryListUnVoidByContact(contactID);
            foreach (BillSummary bill in billList)
            {
                aging.Aging0_30.Value += bill.D0_30;
                aging.Aging31_60.Value += bill.D31_60;
                aging.Aging61_90.Value += bill.D61_90;
                aging.Aging90up.Value += bill.D90up;
            }

            return aging;
        }

        public Aging SelectAging(ContactKey contactKey)
        {
            Contact contact = this.SelectContact(contactKey);
            if (contact == null || contact.ID == null)
            {
                return new Aging();
            }

            return SelectAging(contact.ID.Value);
        }

        public EmailStatementStuff SelectEmailStatement(ContactKey key)
        {
            EmailStatementStuff statement = new EmailStatementStuff();
            Contact contact = this.SelectContact(key);

            statement.ContactKey = key;
            statement.ContactEmail = contact.Email;

            return statement;
        }

        public EmailStatementStuffList SelectEmailStatementList()
        {
            EmailStatementStuffList list = new EmailStatementStuffList();
            IList customerList = this.SelectListFromCache();
            foreach (Customer customer in customerList)
            {
                EmailStatementStuff statement = new EmailStatementStuff();
                statement.ContactKey = customer.BillTo.ContactKey;
                statement.ContactEmail = customer.Email;
                list.Add(statement);
            }

            return list;
        }

        protected override Entity ReadEntity(RecordReader reader)
        {
            Customer customer = (Customer)base.ReadEntity(reader);

            if (!customer.CreditCard.IsNew)
            {
                CreditCardInfo info = this.Database.CreditCardAccessor.Select(customer.CreditCard.ID.Value);
                info.CopyTo(customer.CreditCard);
            }

            return customer;
        }

        protected override void SaveBefore(Entity entity, DbTransaction transaction)
        {
            base.SaveBefore(entity, transaction);

            Customer customer = (Customer)entity;
            if (customer.IsNew)
            {
                if (!customer.CreditCard.IsEmpty)
                    this.Database.CreditCardAccessor.Insert(customer.CreditCard, transaction);
                return;
            }

            if (customer.CreditCard.IsNew)
            {
                if (!customer.CreditCard.IsEmpty)
                    this.Database.CreditCardAccessor.Insert(customer.CreditCard, transaction);
                return;
            }

            if (customer.CreditCard.IsEmpty)
            {
                this.Database.CreditCardAccessor.Delete(customer.CreditCard.ID.Value);
                customer.CreditCard.ID = null;
                return;
            }

            this.Database.CreditCardAccessor.Save(customer.CreditCard);
        }

        protected override void SaveAfter(Entity entity, Entity entityOld, DbTransaction transaction)
        {
            base.SaveAfter(entity, entityOld, transaction);

            if (null == entityOld) //insert action
            {
                Customer customer = (Customer)entity;

                string strLastActivity = CommonHelper.LastActivity_CustomerCreated;
                string strNote = customer.Note;

                CommonHelper.ConstructLastActivityAndNote(ref strLastActivity, ref strNote);                               

                if (customer.CustomerCreditLimit != 0)
                {
                    strLastActivity = string.Format(CommonHelper.LastActivity_CustomerCreditLimit, customer.CustomerCreditLimit);
                    CommonHelper.ConstructLastActivityAndNote(ref strLastActivity, ref strNote);                               
                }

                if (customer.CustomerDiscount != 0)
                {
                    strLastActivity = string.Format(CommonHelper.LastActivity_CustomerDiscount, customer.CustomerDiscount);
                    CommonHelper.ConstructLastActivityAndNote(ref strLastActivity, ref strNote);                               
                }

                this.UpdateLastActivity(customer.ID.Value, strLastActivity, strNote, transaction);
            }
            else  //update action
            {
                Customer customer = (Customer)entity;
                Customer customerOld = (Customer)entityOld;

                string strLastActivity = "";
                string strNote = customer.Note;

                if (customer.CustomerCreditLimit != customerOld.CustomerCreditLimit)
                {
                    strLastActivity = string.Format(CommonHelper.LastActivity_CustomerCreditLimit, customer.CustomerCreditLimit);
                    CommonHelper.ConstructLastActivityAndNote(ref strLastActivity, ref strNote);                               
                }

                if (customer.CustomerDiscount != customerOld.CustomerDiscount)
                {
                    strLastActivity = string.Format(CommonHelper.LastActivity_CustomerDiscount, customer.CustomerDiscount);
                    CommonHelper.ConstructLastActivityAndNote(ref strLastActivity, ref strNote);
                }

                if (!string.IsNullOrEmpty(strLastActivity))
                {
                    this.UpdateLastActivity(customer.ID.Value, strLastActivity, strNote, transaction);
                }
            }
        }        
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -