C#WPF实战出真汁13--【营业查询】

发布于:2025-08-19 ⋅ 阅读:(16) ⋅ 点赞:(0)

1、营业查询介绍

本模块是最后一个模块,该板块需要的功能有:营业数据列表,查询数据,导出数据,数据统计。

2、UI设计布局

TabControl 是 WPF 中用于创建多页标签式界面的控件,常用于组织多个子内容区域。每个子内容通过 TabItem 定义,用户可以通过点击标签头切换内容,通过修改样式和模板,可以完全自定义 TabControl 的外观。可以通过代码动态管理 TabControl 中的 TabItem,TabControl 支持数据绑定,可以通过 ItemsSource 属性绑定到一个集合,并使用 ItemTemplate 定义标签头的显示方式,TabControl 提供了多个有用的事件,例如 SelectionChanged,可以在用户切换标签页时执行自定义逻辑:通过设置 TabStripPlacement 属性,可以改变标签头的位置,使其显示在左侧、右侧或底部。

<UserControl
    x:Class="HQ.fResApp.UControls.BusinessInquiry"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:MetroChart="clr-namespace:De.TorstenMandelkow.MetroChart;assembly=De.TorstenMandelkow.MetroChart"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:pu="clr-namespace:Panuon.UI.Silver;assembly=Panuon.UI.Silver"
    xmlns:vm="clr-namespace:HQ.fResApp.ViewModel"
    d:DesignHeight="450"
    d:DesignWidth="1000"
    Background="#f6f9ff"
    mc:Ignorable="d">
    <UserControl.DataContext>
        <vm:BusinessInquiryListVModel />
    </UserControl.DataContext>
    <TabControl
        Margin="20"
        VerticalAlignment="Stretch"
        pu:TabControlHelper.HeaderPanelBackground="#fcfcfc"
        pu:TabControlHelper.ItemHeight="50"
        pu:TabControlHelper.ItemPadding="20,0"
        pu:TabControlHelper.ItemsAlignment="LeftOrTop"
        pu:TabControlHelper.SelectedBackground="#FF009BFF"
        pu:TabControlHelper.SelectedForeground="#ffffff"
        pu:TabControlHelper.TabControlStyle="Card">
        <TabItem
            Background="BlanchedAlmond"
            BorderBrush="AliceBlue"
            BorderThickness="1"
            Cursor="Hand"
            FontFamily="雅黑"
            FontSize="20"
            Header="营业订单数据">
            <Grid Margin="10">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="56*" />
                    <ColumnDefinition Width="411*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid Grid.ColumnSpan="2">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="80" />
                        <RowDefinition />
                        <RowDefinition Height="60" />
                    </Grid.RowDefinitions>
                    <Grid Background="#ffffff">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <StackPanel Orientation="Horizontal">
                            <TextBox
                                Width="280"
                                Height="40"
                                Margin="0,0,20,0"
                                pu:TextBoxHelper.Watermark="输入订单号搜索"
                                FontSize="15"
                                Foreground="#909399"
                                Text="{Binding BillHistoryKey}" />
                            <Button
                                x:Name="btnSearch"
                                Width="100"
                                Height="40"
                                Margin="0,0,20,0"
                                Padding="-10,0,0,0"
                                pu:ButtonHelper.ButtonStyle="Standard"
                                pu:ButtonHelper.ClickStyle="Sink"
                                pu:ButtonHelper.CornerRadius="20"
                                pu:ButtonHelper.HoverBrush="#009BFF"
                                pu:ButtonHelper.Icon="/HQ.fResApp;component/Resources/icon/search.png"
                                pu:IconHelper.Width="40"
                                Background="#FF009BFF"
                                BorderBrush="#FF009BFF"
                                Command="{Binding FindCommand}"
                                Content="查询"
                                Cursor="Hand"
                                FontSize="16"
                                Foreground="#ffffff"
                                IsDefault="true" />
                            <Button
                                Width="100"
                                Height="40"
                                Margin="0,0,20,0"
                                Padding="-10,0,0,0"
                                pu:ButtonHelper.ButtonStyle="Standard"
                                pu:ButtonHelper.ClickStyle="Sink"
                                pu:ButtonHelper.CornerRadius="20"
                                pu:ButtonHelper.HoverBrush="#009BFF"
                                pu:ButtonHelper.Icon="/HQ.fResApp;component/Resources/icon/Refresh.png"
                                pu:IconHelper.Width="40"
                                Background="LightSlateGray"
                                BorderBrush="#FF009BFF"
                                Command="{Binding ResetCommand}"
                                Content="重置"
                                Cursor="Hand"
                                FontSize="16"
                                Foreground="#ffffff" />
                            <Button
                                Width="140"
                                Height="40"
                                Margin="0,0,20,0"
                                Padding="-10,0,0,0"
                                pu:ButtonHelper.ButtonStyle="Standard"
                                pu:ButtonHelper.ClickStyle="Sink"
                                pu:ButtonHelper.CornerRadius="20"
                                pu:ButtonHelper.HoverBrush="#65d17f"
                                pu:ButtonHelper.Icon="/HQ.fResApp;component/Resources/icon/openorder.png"
                                pu:IconHelper.Width="35"
                                Background="#ab4321"
                                BorderBrush="#65d17f"
                                Command="{Binding BackUpAllCommand}"
                                Content="导出全部数据"
                                Cursor="Hand"
                                FontSize="16"
                                Foreground="#ffffff" />
                            <Button
                                Width="140"
                                Height="40"
                                Margin="0,0,20,0"
                                Padding="-10,0,0,0"
                                pu:ButtonHelper.ButtonStyle="Standard"
                                pu:ButtonHelper.ClickStyle="Sink"
                                pu:ButtonHelper.CornerRadius="20"
                                pu:ButtonHelper.HoverBrush="#65d17f"
                                pu:ButtonHelper.Icon="/HQ.fResApp;component/Resources/icon/openorder.png"
                                pu:IconHelper.Width="35"
                                Background="#65d17f"
                                BorderBrush="#65d17f"
                                Command="{Binding BackUpPageCommand}"
                                Content="导出本页数据"
                                Cursor="Hand"
                                FontSize="16"
                                Foreground="#ffffff" />
                        </StackPanel>
                    </Grid>
                    <DataGrid
                        x:Name="tableList"
                        Grid.Row="1"
                        pu:DataGridHelper.ColumnHorizontalContentAlignment="Center"
                        pu:DataGridHelper.HeaderBackground="#FF009BFF"
                        pu:DataGridHelper.HeaderForeground="#ffffff"
                        pu:DataGridHelper.HeaderMinHeight="50"
                        pu:DataGridHelper.HoverBackground="#FF009BFF"
                        pu:DataGridHelper.ResizeThumbThickness="0.5"
                        pu:DataGridHelper.SelectedBackground="Transparent"
                        pu:DataGridHelper.SelectedForeground="Red"
                        AlternatingRowBackground="#f7faff"
                        AutoGenerateColumns="False"
                        CanUserAddRows="False"
                        CanUserDeleteRows="False"
                        CanUserReorderColumns="False"
                        CanUserResizeRows="False"
                        Cursor="Pen"
                        FontSize="16"
                        ItemsSource="{Binding BillHistoryDataList}"
                        SelectionMode="Extended"
                        SelectionUnit="FullRow">
                        <DataGrid.RowStyle>
                            <Style TargetType="DataGridRow">
                                <!--  Event绑定MouseLeftButtonUp表示鼠标点击事件,Handler表示具体的事件处理程序  -->
                                <Setter Property="Height" Value="40" />
                                <!--  样式触发器,IsMouseOver表示鼠标滑过时事件  -->
                                <Style.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="Background">
                                            <Setter.Value>
                                                <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                                    <GradientStop Offset="0.98" Color="#FF73BCE8" />
                                                    <GradientStop Offset="0" Color="White" />
                                                </LinearGradientBrush>
                                            </Setter.Value>
                                        </Setter>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </DataGrid.RowStyle>
                        <DataGrid.Columns>
                            <DataGridTextColumn
                                Width="2*"
                                Binding="{Binding tName}"
                                FontSize="18"
                                Header="餐桌号"
                                IsReadOnly="True" />
                            <DataGridTextColumn
                                Width="3*"
                                Binding="{Binding obId}"
                                FontSize="18"
                                Header="订单号"
                                IsReadOnly="True" />
                            <DataGridTextColumn
                                Width="4*"
                                Binding="{Binding obDate, StringFormat='{}{0:yyyy年MM月dd日 dddd HH时mm分}', ConverterCulture=zh-CN}"
                                FontSize="18"
                                Header="开单时间"
                                IsReadOnly="True" />
                            <DataGridTextColumn
                                Width="2*"
                                Binding="{Binding obNumber}"
                                FontSize="18"
                                Header="消费人数"
                                IsReadOnly="True" />
                            <DataGridTextColumn
                                Width="2*"
                                Binding="{Binding opConsumeMoney}"
                                FontSize="18"
                                Header="消费金额"
                                IsReadOnly="True" />
                            <DataGridTextColumn
                                Width="2*"
                                Binding="{Binding opClientMoney}"
                                FontSize="18"
                                Header="支付金额"
                                IsReadOnly="True" />
                            <DataGridTextColumn
                                Width="4*"
                                Binding="{Binding obDate, StringFormat='{}{0:yyyy年MM月dd日 dddd HH时mm分}', ConverterCulture=zh-CN}"
                                FontSize="18"
                                Header="创建时间"
                                IsReadOnly="True" />
                            <DataGridTextColumn
                                Width="3*"
                                Binding="{Binding opRemark}"
                                FontSize="18"
                                Header="备注"
                                IsReadOnly="True" />
                        </DataGrid.Columns>
                    </DataGrid>
                    <Grid Grid.Row="2">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition Width="250" />
                        </Grid.ColumnDefinitions>
                        <pu:Pagination
                            x:Name="tabPagination"
                            Height="45"
                            Margin="0,0,20,0"
                            HorizontalAlignment="Left"
                            Background="#963F3F3F"
                            CurrentIndex="{Binding BillHistoryCurrentIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                            CurrentIndexChanged="tabPageActiveChanged"
                            Cursor="Hand"
                            HoverBrush="#FF009BFF"
                            Spacing="15"
                            TotalIndex="{Binding BillHistoryTotalIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                        <StackPanel
                            Grid.Column="1"
                            HorizontalAlignment="Right"
                            Orientation="Horizontal">
                            <Button
                                Width="20"
                                Height="45"
                                Padding="-35,0,0,0"
                                pu:ButtonHelper.ButtonStyle="Standard"
                                pu:ButtonHelper.HoverBrush="Transparent"
                                pu:IconHelper.Width="35"
                                Background="Transparent"
                                BorderBrush="Transparent"
                                Content="共"
                                FontSize="17"
                                FontWeight="ExtraBold"
                                Foreground="#2F4056" />
                            <Button
                                x:Name="txtTotalNum"
                                Height="45"
                                Padding="-35,0,0,0"
                                pu:ButtonHelper.ButtonStyle="Standard"
                                pu:ButtonHelper.HoverBrush="Transparent"
                                pu:IconHelper.Width="35"
                                Background="Transparent"
                                BorderBrush="Transparent"
                                Content="{Binding BillHistoryTotalNum, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                FontSize="17"
                                FontWeight="ExtraBold"
                                Foreground="#FF009BFF" />
                            <Button
                                Width="100"
                                Height="45"
                                Padding="-35,0,0,0"
                                pu:ButtonHelper.ButtonStyle="Standard"
                                pu:ButtonHelper.HoverBrush="Transparent"
                                pu:IconHelper.Width="35"
                                Background="Transparent"
                                BorderBrush="Transparent"
                                Content="条数据/每页"
                                FontSize="17"
                                FontWeight="ExtraBold"
                                Foreground="#2F4056" />
                            <TextBox
                                x:Name="txtPageSize"
                                Grid.Row="2"
                                Width="50"
                                Height="30"
                                HorizontalAlignment="Center"
                                pu:TextBoxHelper.CornerRadius="0"
                                Text="{Binding BillHistoryPageSize, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                            <Button
                                Width="20"
                                Height="45"
                                Padding="-35,0,0,0"
                                pu:ButtonHelper.ButtonStyle="Standard"
                                pu:ButtonHelper.HoverBrush="Transparent"
                                pu:IconHelper.Width="35"
                                Background="Transparent"
                                BorderBrush="Transparent"
                                Content="条"
                                FontSize="17"
                                FontWeight="ExtraBold"
                                Foreground="#2F4056" />
                        </StackPanel>
                    </Grid>
                </Grid>
            </Grid>
        </TabItem>
        <TabItem
            Background="BlanchedAlmond"
            BorderBrush="AliceBlue"
            BorderThickness="1"
            Cursor="Hand"
            FontFamily="雅黑"
            FontSize="20"
            FontWeight="Bold"
            Header="最近一周">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="9*" />
                </Grid.RowDefinitions>
                <StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
                    <Button
                        Width="100"
                        Height="40"
                        Margin="0,-10,20,0"
                        Padding="-10,0,0,0"
                        pu:ButtonHelper.ButtonStyle="Standard"
                        pu:ButtonHelper.ClickStyle="Sink"
                        pu:ButtonHelper.CornerRadius="20"
                        pu:ButtonHelper.HoverBrush="#009BFF"
                        pu:ButtonHelper.Icon="/HQ.fResApp;component/Resources/icon/Refresh.png"
                        pu:IconHelper.Width="40"
                        Background="#65d17f"
                        BorderBrush="#65d17f"
                        Content="刷新"
                        Cursor="Hand"
                        FontSize="16"
                        Foreground="#ffffff" />
                </StackPanel>
                <MetroChart:ClusteredColumnChart
                    Grid.Row="1"
                    Margin="0,-10,20,0"
                    ChartLegendVisibility="Collapsed"
                    ChartSubTitle="额度"
                    ChartTitle="最近一周营业情况统计"
                    ChartTitleVisibility="Visible"
                    IsRowColumnSwitched="True"
                    ToolTipFormat="{Binding Path=ToolTipFormat}">
                    <MetroChart:ClusteredColumnChart.Series>
                        <MetroChart:ChartSeries
                            DisplayMember="Name"
                            ItemsSource="{Binding Path=WeekDatas}"
                            SeriesTitle="日期"
                            ValueMember="Count" />
                    </MetroChart:ClusteredColumnChart.Series>
                </MetroChart:ClusteredColumnChart>
            </Grid>
        </TabItem>
        <TabItem
            Background="BlanchedAlmond"
            BorderBrush="AliceBlue"
            BorderThickness="1"
            Cursor="Hand"
            FontFamily="雅黑"
            FontSize="20"
            FontWeight="Bold"
            Header="最近一个月">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="9*" />
                </Grid.RowDefinitions>
                <StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
                    <Button
                        Width="100"
                        Height="40"
                        Margin="0,-10,20,0"
                        Padding="-10,0,0,0"
                        pu:ButtonHelper.ButtonStyle="Standard"
                        pu:ButtonHelper.ClickStyle="Sink"
                        pu:ButtonHelper.CornerRadius="20"
                        pu:ButtonHelper.HoverBrush="#009BFF"
                        pu:ButtonHelper.Icon="/HQ.fResApp;component/Resources/icon/Refresh.png"
                        pu:IconHelper.Width="40"
                        Background="#65d17f"
                        BorderBrush="#65d17f"
                        Content="刷新"
                        Cursor="Hand"
                        FontSize="16"
                        Foreground="#ffffff" />
                </StackPanel>
                <MetroChart:ClusteredColumnChart
                    Grid.Row="1"
                    Margin="0,-10,20,0"
                    ChartLegendVisibility="Collapsed"
                    ChartSubTitle="额度"
                    ChartTitle="最近一个月营业情况统计"
                    ChartTitleVisibility="Visible"
                    IsRowColumnSwitched="True"
                    ToolTipFormat="{Binding Path=ToolTipFormat}">
                    <MetroChart:ClusteredColumnChart.Series>
                        <MetroChart:ChartSeries
                            DisplayMember="Name"
                            ItemsSource="{Binding Path=MonthDatas}"
                            SeriesTitle="日期"
                            ValueMember="Count" />
                    </MetroChart:ClusteredColumnChart.Series>
                </MetroChart:ClusteredColumnChart>
            </Grid>
        </TabItem>
        <TabItem
            Background="BlanchedAlmond"
            BorderBrush="AliceBlue"
            BorderThickness="1"
            Cursor="Hand"
            FontFamily="雅黑"
            FontSize="20"
            FontWeight="Bold"
            Header="按年份统计">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="9*" />
                </Grid.RowDefinitions>
                <StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
                    <Button
                        Width="100"
                        Height="40"
                        Margin="0,-10,20,0"
                        Padding="-10,0,0,0"
                        pu:ButtonHelper.ButtonStyle="Standard"
                        pu:ButtonHelper.ClickStyle="Sink"
                        pu:ButtonHelper.CornerRadius="20"
                        pu:ButtonHelper.HoverBrush="#009BFF"
                        pu:ButtonHelper.Icon="/HQ.fResApp;component/Resources/icon/Refresh.png"
                        pu:IconHelper.Width="40"
                        Background="#65d17f"
                        BorderBrush="#65d17f"
                        Content="刷新"
                        Cursor="Hand"
                        FontSize="16"
                        Foreground="#ffffff" />
                </StackPanel>
                <MetroChart:ClusteredColumnChart
                    Grid.Row="1"
                    Margin="0,-10,20,0"
                    ChartLegendVisibility="Collapsed"
                    ChartSubTitle="额度"
                    ChartTitle="各年份营业情况统计"
                    ChartTitleVisibility="Visible"
                    IsRowColumnSwitched="True"
                    ToolTipFormat="{Binding Path=ToolTipFormat}">
                    <MetroChart:ClusteredColumnChart.Series>
                        <MetroChart:ChartSeries
                            DisplayMember="Name"
                            ItemsSource="{Binding Path=YearDatas}"
                            SeriesTitle="年份"
                            ValueMember="Count" />
                    </MetroChart:ClusteredColumnChart.Series>
                </MetroChart:ClusteredColumnChart>
            </Grid>
        </TabItem>
    </TabControl>
</UserControl>

3、viewmodel模型

ViewModel 的主要职责是封装业务逻辑和状态,为视图提供数据绑定和命令绑定的接口。通过数据绑定,ViewModel 能够将数据从 Model 传递到 View,同时处理用户输入和界面逻辑,而无需直接操作 UI 元素,viewmodel就是一个类,这个类中包括了变量,属性,命令,其中属性和命令是最重要的,属性是动态通知属性,当属性的值发生更改时,自动更新界面元素标签,反过来也是这样的,命令来说就是响应事件,如单击,双击,右键等。

using HQ.BLL;
using HQ.COMM;
using HQ.COMM.DtoModel;
using HQ.fResApp.BaseModel;
using HQ.fResApp.Utils;
using HQ.fResApp.ViewModel.PageViewModel;
using HQ.MODEL.DBModel;
using Panuon.UI.Silver;
using Panuon.UI.Silver.Core;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace HQ.fResApp.ViewModel
{
    /// <summary>
    /// 营业查询视图模型
    /// </summary>
    public class BusinessInquiryListVModel : ViewModelBase
    {
        #region 变量
        BillHistoryBLL billhistoryBLL = new BillHistoryBLL();
        public string ToolTipFormat
        {
            get { return "当前额度是:{1},在总额中占比为:{3:P2}"; }
        }


        #endregion

        public BusinessInquiryListVModel()
        {
            BillHistoryDataList = getBillHistoryList();
            InitUIData();
        }


        #region 属性

        public ObservableCollection<Population> weekDatas = new ObservableCollection<Population>(); 
        public ObservableCollection<Population> WeekDatas
        {
            get { return weekDatas; }
            set
            {
                weekDatas = value;
                OnPropertyChanged();
            }
        }

        public ObservableCollection<Population> monthDatas = new ObservableCollection<Population>();
        public ObservableCollection<Population> MonthDatas
        {
            get { return monthDatas; }
            set
            {
                monthDatas = value;
                OnPropertyChanged();
            }
        }

        public ObservableCollection<Population> yearhDatas = new ObservableCollection<Population>(); 
        public ObservableCollection<Population> YearDatas
        {
            get { return yearhDatas; }
            set
            {
                yearhDatas = value;
                OnPropertyChanged();
            }
        }

        ObservableCollection<BillHistory> billHistoryDataList = new ObservableCollection<BillHistory>();
        /// <summary>
        ///历史订单列表
        /// </summary>
        public ObservableCollection<BillHistory> BillHistoryDataList
        {
            get { return billHistoryDataList; }
            set
            {
                billHistoryDataList = value;
                OnPropertyChanged();
            }
        }

        private int billHistoryCurrentIndex = 1;
        /// <summary>
        /// 当前页,默认第1页
        /// </summary>
        public int BillHistoryCurrentIndex
        {
            get { return billHistoryCurrentIndex; }
            set
            {
                billHistoryCurrentIndex = value;
                OnPropertyChanged();
            }
        }

        private int billHistoryTotalIndex;
        /// <summary>
        /// 总页数
        /// </summary>
        public int BillHistoryTotalIndex
        {
            get { return billHistoryTotalIndex; }
            set
            {
                billHistoryTotalIndex = value;
                OnPropertyChanged();
            }
        }
        private int billHistoryTotalNum;
        /// <summary>
        /// 总条数
        /// </summary>
        public int BillHistoryTotalNum
        {
            get { return billHistoryTotalNum; }
            set
            {
                billHistoryTotalNum = value;
                OnPropertyChanged();
            }
        }
        private int billHistoryPageSize = 15;
        /// <summary>
        /// 每页条数,默认10条
        /// </summary>
        public int BillHistoryPageSize
        {
            get { return billHistoryPageSize; }
            set
            {
                billHistoryPageSize = value;
                OnPropertyChanged();
                //当改变每页条数时,触发以下事件
                BillHistoryDataList = getBillHistoryList(); //订单历史数据集合
            }
        }

        private string billHistoryKey = "";
        /// <summary>
        /// 搜索时的关键字,默认为空
        /// </summary>
        public string BillHistoryKey
        {
            get { return billHistoryKey; }
            set
            {
                billHistoryKey = value;
                OnPropertyChanged();
            }
        }


        #endregion


        #region 方法

        private void InitUIData()
        {
            //最近一周
            var _resweek = billhistoryBLL.CountBillHistoryData(new CountModelDto { _where = 7 }).Result;
            if (_resweek.statusCode == (int)ApiEnum.Status)
            {
                foreach (var item in _resweek.data)
                {
                    weekDatas.Add(new Population() { Name = item.wDate, Count = item.countCost });
                }
            }
            //最近一月
            var _resmonth = billhistoryBLL.CountBillHistoryData(new CountModelDto { _where = 30 }).Result;
            if (_resmonth.statusCode == (int)ApiEnum.Status)
            {
                foreach (var item in _resmonth.data)
                {
                    monthDatas.Add(new Population() { Name = item.wDate, Count = item.countCost });
                }
            }
            //按年份
            var _resyear = billhistoryBLL.CountBillHistoryData(new CountModelDto { _where = 365 }).Result;
            if (_resyear.statusCode == (int)ApiEnum.Status)
            {
                foreach (var item in _resyear.data)
                {
                    yearhDatas.Add(new Population() { Name = item.wDate, Count = item.countCost });
                }
            }
        }



        /// <summary>
        /// 获取订单历史数据
        /// </summary>
        /// <returns></returns>
        public ObservableCollection<BillHistory> getBillHistoryList()
        {
            ObservableCollection<BillHistory> TableDataList = new ObservableCollection<BillHistory>();
            var parms = new PageParm { page = BillHistoryCurrentIndex, limit = BillHistoryPageSize, key = BillHistoryKey.Trim() };
            var pageRes = billhistoryBLL.GetBillHistoryPages(parms).Result;//分页查询结果
            if (pageRes.statusCode == (int)ApiEnum.Status)
            {
                var _pageResData = pageRes.data;
                var tabList = _pageResData.Items;
                //数据集需要重新处理,将每个数据项加上复选框,用于页面数据展示
                if (tabList != null && tabList.Count != 0)
                {
                    foreach (var item in tabList)
                    {
                        var curBill = new BillHistory
                        {
                            obGuid = item.obGuid,
                            obId = item.obId,
                            tName = item.tName,
                            obDate = item.obDate,
                            obNumber = item.obNumber,
                            opConsumeMoney = item.opConsumeMoney,
                            opClientMoney = item.opClientMoney,
                            opRemark = item.opRemark,
                            lastUpDate = item.lastUpDate 
                        };
                        TableDataList.Add(curBill);
                    }
                    BillHistoryTotalNum = (int)_pageResData.TotalItems;
                    BillHistoryCurrentIndex = (int)_pageResData.CurrentPage;
                    BillHistoryTotalIndex = (int)_pageResData.TotalPages;
                }
                else
                {
                    Notice.Show("没有获取到历史列表数据!", "提示", 3, MessageBoxIcon.Info);
                    Logger.Default.ProcessError((int)ApiEnum.Error, "没有获取到历史列表数据");
                }
            }
            else
            {
                Notice.Show("没有获取到历史列表数据!", "提示", 3, MessageBoxIcon.Info);
                Logger.Default.ProcessError(pageRes.statusCode, "获取到历史列表数据异常");
            }
            return TableDataList;
        }


        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="array">列表对象</param>
        /// <param name="filename">文件名称</param>
        /// <exception cref="NotImplementedException"></exception>
        private void Backup<T>(List<T> array, string name)
        {
            var t = typeof(T);
            var properties = t.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            var contents = new StringBuilder();

            //标题
            foreach (var item in properties)
            {
                //得到第一个自定义属性的参数的值,即属性描述
                var desc = item.CustomAttributes.ToList()[0].ConstructorArguments[0].Value.ToString();
                contents.Append(desc);
                contents.Append(",");
            }
            contents.Append("\r\n");//换行

            //内容
            foreach (var model in array)
            {
                var row = new StringBuilder();
                foreach (var property in properties)
                {
                    var val = property.GetValue(model);
                    row.Append(val);
                    row.Append(",");
                }
                contents.Append(row.ToString());
                contents.Append("\r\n");
            }

            //finename -> 表格+日期
            var date = string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now);
            var filename = $"{name}{date}.csv";
            var rootImagPath = Directory.GetCurrentDirectory().Replace(@"\bin\Debug", "") + @"\Resources\proData\" + filename;

            //保存
            File.WriteAllText(rootImagPath, contents.ToString(), Encoding.UTF8);//以utf-8的格式保存成csv格式
            MessageBoxX.Show("数据导出成功!", "提示", Application.Current.MainWindow, MessageBoxButton.OK, new MessageBoxXConfigurations()
            {
                MessageBoxIcon = MessageBoxIcon.Success,
                ButtonBrush = "#F1C825".ToColor().ToBrush(),
            });
        }


        #endregion


        /// <summary>
        /// 查询命令
        /// </summary>
        public ICommand FindCommand
        {
            get
            {
                return new RelayCommand(o =>
                {
                    BillHistoryDataList = getBillHistoryList();
                });
            }
        }

        /// <summary>
        /// 重置命令
        /// </summary>
        public ICommand ResetCommand
        {
            get
            {
                return new RelayCommand(o =>
                {
                    BillHistoryCurrentIndex = 1;//当前页为第1页
                    BillHistoryPageSize = 15;//每页10条
                    BillHistoryKey = "";//关键字清空 
                    BillHistoryDataList = getBillHistoryList();//订单数据集合 
                });
            }
        }

        /// <summary>
        /// 导出本页
        /// </summary>
        public ICommand BackUpPageCommand
        {
            get
            {
                return new RelayCommand(o =>
                {
                    Backup(BillHistoryDataList.ToList(), "billspage");
                });
            }
        }


        /// <summary>
        /// 导出全部
        /// </summary>
        public ICommand BackUpAllCommand
        {
            get
            {
                return new RelayCommand(o =>
                {
                    ParmString ps = new ParmString();
                    var objdata = billhistoryBLL.QueryBillHistoryList(ps).Result.data;
                    Backup(objdata, "billslist");
                });
            }
        }


    }
}

using HQ.BLL;
using HQ.COMM.DtoModel;
using HQ.fResApp.BaseModel;
using HQ.fResApp.Utils;
using HQ.fResApp.ViewModel.PageViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HQ.fResApp.ViewModel
{
    /// <summary>
    ///  营业查询视图模型
    /// </summary>
    public class MetroChartVModel : ViewModelBase
    {
        BillHistoryBLL openBillsService = new BillHistoryBLL();

        public string ToolTipFormat
        {
            get { return "当前额度是:{1},在总额中占比为:{3:P2}"; }
        }
        
        private readonly ObservableCollection<Population> _weekDatas = new ObservableCollection<Population>();
        private readonly ObservableCollection<Population> _monthDatas = new ObservableCollection<Population>();
        private readonly ObservableCollection<Population> _yearhDatas = new ObservableCollection<Population>();
        public ObservableCollection<Population> WeekDatas { get { return _weekDatas; } }
        public ObservableCollection<Population> MonthDatas { get { return _monthDatas; } }
        public ObservableCollection<Population> YearDatas { get { return _yearhDatas; } }

        public MetroChartVModel()
        {
            //最近一周
            var _resweek = openBillsService.CountBillHistoryData(new CountModelDto { _where = 7 }).Result;
            if (_resweek.statusCode == (int)ApiEnum.Status)
            {
                foreach (var item in _resweek.data) 
                { 
                    _weekDatas.Add(new Population(){ Name = item.wDate, Count = item.countCost }); 
                }
            }
            //最近一月
            var _resmonth = openBillsService.CountBillHistoryData(new CountModelDto { _where = 30 }).Result;
            if (_resmonth.statusCode == (int)ApiEnum.Status)
            {
                foreach (var item in _resmonth.data) 
                { 
                    _monthDatas.Add(new Population() { Name = item.wDate, Count = item.countCost }); 
                }
            }
            //按年份
            var _resyear = openBillsService.CountBillHistoryData(new CountModelDto { _where = 365 }).Result;
            if (_resyear.statusCode == (int)ApiEnum.Status)
            {
                foreach (var item in _resyear.data) 
                { 
                    _yearhDatas.Add(new Population() { Name = item.wDate, Count = item.countCost }); 
                }
            }
        }


    }
}

4、运行测试

原创不易,打字截图不易,走过路过,不要错过,欢迎点赞,收藏,转载,复制,抄袭,留言,灌水,喷火,吐槽,动动你的金手指,早日实现财务自由!


网站公告

今日签到

点亮在社区的每一天
去签到