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

📄 contactaccessor.cs

📁 MIED是用于个人帐务管理的小型软件
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System.Data.Common;
using DatabaseUtil;
using Mied.BusinessObject;
using Mied.DAL.TableSchema;
using System;
using Mied.BusinessObject.Bills;
using System.Diagnostics;
using SqlcePlugin;
using Mied.DAL.Exceptions;

namespace Mied.DAL.Accesses
{
    public abstract class ContactAccessor : AccessorCache
    {
        public ContactAccessor(MiedDatabase database, Type contactClassType, ContactType contactType)
            : base(database, ContactSchema.TableName)
        {
            this.ContactClassType = contactClassType;
            this.ContactType = contactType;
        }

        public string SelectContactEmail(ContactKey key)
        {
            DbCommand command = this.Database.Connection.CreateCommand();
            command.CommandText = "SELECT " + ContactSchema.FieldEmail + " FROM " + ContactSchema.TableName;
            this.WhereContactKey(command, key);
            object result = command.ExecuteScalar();
            string email = (string)result;
            return email;
        }

        public override void Delete(int id)
        {
            Contact contact = (Contact)this.Select(id);
            if (contact.ContactType != ContactType.CustomerAndVendor)
            {
                base.Delete(contact.ID.Value);
                return;
            }

            if (contact.ContactType == ContactType.Customer)
            {
                Vendor vendor = this.Database.VendorAccessor.SelectVendor(id);
                vendor.IsCustomer = false;
                this.Database.VendorAccessor.Save(vendor);
            }
            else
            {
                Customer customer = this.Database.CustomerAccessor.SelectCustomer(id);
                customer.IsVendor = false;
                this.Database.CustomerAccessor.Save(customer);
            }

            this.FireDeleted(contact.ID.Value);
        }

        public int VerifyContact(Bill bill)
        {
            Contact contact = this.SelectContact(bill.ContactKey);
            if (contact != null)
                return contact.ID.Value;

            Contact brother = this.BrotherAccessor.SelectContact(bill.ContactKey);
            if (brother != null)
            {
                brother.ContactType = ContactType.CustomerAndVendor;
                brother.BillTo = bill.BillTo;
                brother.ShipTo = bill.ShipTo;
                this.BrotherAccessor.Save(brother);
                return this.VerifyContact(bill);
            }

            contact = this.NewContact();
            contact.BillTo = bill.BillTo;
            contact.ShipTo = bill.ShipTo;
            this.Save(contact);
            return this.VerifyContact(bill);
        }

        protected Contact SelectContact(int id)
        {
            return (Contact)base.Select(id);
        }

        protected Contact SelectContact(ContactKey key)
        {
            DbCommand command = this.CommandSelectEntity();
            this.WhereContactKey(command, key);
            Contact contact = (Contact)CommandHelper.ExecuteReader(command, this.ReadEntity);
            return contact;
        }

        protected void FireSavedByBrother(Contact brother)
        {
            if (brother.ContactType != ContactType.CustomerAndVendor)
            {
                Debug.Fail("Alarm!");
                return;
            }

            Contact contact = this.SelectContact(brother.ID.Value);
            if (contact == null)
            {// may be is new
                contact = this.NewContact();
                brother.ContactRecord.CopyTo(contact.ContactRecord);
            }
            this.FireSaved(contact);
        }

        protected override void SaveBefore(Entity entity, DbTransaction transaction)
        {
            Contact contact = (Contact)entity;
            this.CheckContactNameDuplicate(contact);
            this.UpdateToCustomerAndVender(contact);
        }

        private void CheckContactNameDuplicate(Contact contact)
        {
            if (!contact.IsNew)
                return;

            int? id = this.SelectID(contact.ContactKeyObj);
            if (id != null)
                throw new ContactNameDuplicateException(contact);
        }

        private int? SelectID(ContactKey key)
        {
            DbCommand command = this.CommandSelectID();
            this.WhereContactKey(command, key);
            return this.SelectID(command);
        }

        private void UpdateToCustomerAndVender(Contact contact)
        {
            if (!contact.IsNew)
                return;

            Contact brother = this.BrotherAccessor.SelectContact(contact.ContactKeyObj);
            if (brother == null)
                return;

            if (brother.ContactType == ContactType.CustomerAndVendor)
                return;

            contact.ID = brother.ID;
            contact.ContactType = ContactType.CustomerAndVendor;
        }

        protected override void SaveAfter(Entity entity, Entity entityOld, DbTransaction transaction)
        {
            Contact contact = (Contact)entity;
            if (contact.ContactType == ContactType.CustomerAndVendor)
                this.BrotherAccessor.FireSavedByBrother(contact);
        }

        protected void UpdateLastActivity(int contactID, string lastActivity, string note, DbTransaction transaction)
        {
            DbCommand command = transaction.Connection.CreateCommand();
            command.CommandText = string.Format("UPDATE {0} SET [{1}] = @{1}, [{2}] = @{2}",
                this.TableName, ContactSchema.FieldLastActivity, ContactSchema.FieldNote);
            command.Transaction = transaction;

            CommandFactory.WhereAnd(command, ContactSchema.FieldID, contactID);
            CommandFactory.AddCommandParameter(command, "@" + ContactSchema.FieldLastActivity, lastActivity);

⌨️ 快捷键说明

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