/// <summary>
        /// 获取能源结算用量汇总
        /// </summary>
        /// <param name="year">年度</param>
        /// <param name="energyType">能源类型</param>
        /// <param name="department">部门</param>
        /// <returns></returns>
        public SettlementQuantumSummary GetDepartmentQuantumSummary(int year, EnergyType energyType, Department department)
        {
            SettlementQuantumSummary summary = new SettlementQuantumSummary();

            summary.DepartmentName = department.Name;
            summary.Year           = year;
            summary.EnergyType     = energyType.DisplayName();

            SettlementRecordBusiness srBusiness = new SettlementRecordBusiness();
            var settlements = FindByYear(year).ToList();

            if (settlements.Count == 0)
            {
                return(null);
            }

            bool flag = false;

            for (int i = 0; i < settlements.Count; i++)
            {
                var settle = settlements[i];
                var record = srBusiness.FindByDepartment(settle.Id, department.Id, energyType);
                if (record == null)
                {
                    continue;
                }

                switch (i)
                {
                case 0:
                    summary.PlanQuantum  = record.BeginQuantum;
                    summary.FirstQuarter = record.Quantum;
                    break;

                case 1:
                    summary.SecondQuarter = record.Quantum;
                    break;

                case 2:
                    summary.ThirdQuarter = record.Quantum;
                    break;

                case 3:
                    summary.FourthQuarter = record.Quantum;
                    break;
                }
                flag = true;
            }

            summary.TotalQuantum  = summary.FirstQuarter + summary.SecondQuarter + summary.ThirdQuarter + summary.FourthQuarter;
            summary.RemainQuantum = summary.PlanQuantum - summary.TotalQuantum;

            if (!flag)
            {
                return(null);
            }

            return(summary);
        }
        /// <summary>
        /// 删除能源结算
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public override bool Delete(Settlement entity)
        {
            // delete the records
            SettlementRecordBusiness recordBusiness = new SettlementRecordBusiness();

            recordBusiness.DeleteBySettlement(entity.Id);

            return(base.Delete(entity));
        }
        /// <summary>
        /// 获取能源结算金额汇总
        /// </summary>
        /// <param name="year">年度</param>
        /// <param name="energyType">能源类型</param>
        /// <param name="department">部门</param>
        /// <returns></returns>
        public SettlementAmountSummary GetDepartmentAmountSummary(int year, EnergyType energyType, Department department)
        {
            SettlementAmountSummary summary = new SettlementAmountSummary();

            summary.DepartmentName = department.Name;
            summary.Year           = year;
            summary.EnergyType     = energyType.DisplayName();

            TargetBusiness targetBusiness = new TargetBusiness();
            var            target         = targetBusiness.FindByYear(year);

            SettlementRecordBusiness srBusiness = new SettlementRecordBusiness();
            var settlements = FindByYear(year).ToList();

            if (settlements.Count == 0)
            {
                return(null);
            }

            bool flag = false;

            for (int i = 0; i < settlements.Count; i++)
            {
                var settle = settlements[i];
                var record = srBusiness.FindByDepartment(settle.Id, department.Id, energyType);
                if (record == null)
                {
                    continue;
                }

                switch (i)
                {
                case 0:
                    summary.UnitPrice    = record.UnitPrice;
                    summary.PlanAmount   = record.BeginAmount;
                    summary.FirstQuarter = record.Amount;
                    break;

                case 1:
                    summary.SecondQuarter = record.Amount;
                    break;

                case 2:
                    summary.ThirdQuarter = record.Amount;
                    break;

                case 3:
                    summary.FourthQuarter = record.Amount;
                    break;
                }
                flag = true;
            }

            summary.TotalAmount  = summary.FirstQuarter + summary.SecondQuarter + summary.ThirdQuarter + summary.FourthQuarter;
            summary.RemainAmount = summary.PlanAmount - summary.TotalAmount;

            if (summary.RemainAmount < 0)
            {
                TargetRecordBusiness trBusiness = new TargetRecordBusiness();
                var targetRecord = trBusiness.FindByDepartment(target.Id, department.Id, (int)energyType);

                summary.SchoolTake = Math.Round(-summary.RemainAmount * targetRecord.SchoolTake);
                summary.SelfTake   = -summary.RemainAmount - summary.SchoolTake;
            }

            if (!flag)
            {
                return(null);
            }

            return(summary);
        }
        /// <summary>
        /// 获取部门结算汇总
        /// </summary>
        /// <param name="year">年度</param>
        /// <param name="energyType">能源类型</param>
        /// <param name="department">部门</param>
        /// <returns></returns>
        public DepartmentSettlementSummary GetDepartmentSummary(int year, EnergyType energyType, Department department)
        {
            DepartmentSettlementSummary data = new DepartmentSettlementSummary();

            data.Year           = year;
            data.DepartmentId   = department.Id;
            data.DepartmentName = department.Name;
            data.EnergyType     = energyType.DisplayName();
            data.SettleQuantum  = 0;
            data.SettleAmount   = 0;

            TargetBusiness targetBusiness = new TargetBusiness();
            var            target         = targetBusiness.FindByYear(year);

            SettlementRecordBusiness srBusiness = new SettlementRecordBusiness();
            var settlements = FindByYear(year).ToList();

            if (settlements.Count == 0)
            {
                return(null);
            }

            bool flag = false;

            for (int i = 0; i < settlements.Count; i++)
            {
                var settle = settlements[i];
                var record = srBusiness.FindByDepartment(settle.Id, department.Id, energyType);
                if (record == null)
                {
                    continue;
                }

                if (i == 0)
                {
                    data.PlanQuantum = record.BeginQuantum;
                    data.PlanAmount  = record.BeginAmount;
                    data.UnitPrice   = record.UnitPrice;
                }

                data.SettleQuantum += record.Quantum;
                data.SettleAmount  += record.Amount;
                flag = true;
            }

            if (!flag)
            {
                return(null);
            }

            data.RemainQuantum = data.PlanQuantum - data.SettleQuantum;
            data.RemainAmount  = data.PlanAmount - data.SettleAmount;

            if (data.RemainAmount < 0)
            {
                TargetRecordBusiness trBusiness = new TargetRecordBusiness();
                var targetRecord = trBusiness.FindByDepartment(target.Id, department.Id, (int)energyType);
                if (targetRecord == null)
                {
                    data.SchoolTake = 0;
                    data.SelfTake   = -data.RemainAmount;
                }
                else
                {
                    data.SchoolTake = Math.Round(-data.RemainAmount * targetRecord.SchoolTake);
                    data.SelfTake   = -data.RemainAmount - data.SchoolTake;
                }
            }

            return(data);
        }