using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using MadRunFabric.Common;
using MadRunFabric.Model.PLCAutomationApi;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using PLCAutomationApi.IRepositories;

namespace PLCAutomationApi.Controllers
{
    /// <summary>
    /// plc数据
    /// </summary>
    [ApiVersion("6.0")]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class PLCDataController : BaseController
    {
        public IActionResult Index()
        {
            return View();
        }

        private readonly ILogger<PLCDataController> _logger;
        private readonly IPLC_InputDataRepository _plc_inputdataRepository;
        private readonly IPLC_MonthDataRepository _plc_monthdataRepository;

        public PLCDataController(ILogger<PLCDataController> logger, IPLC_InputDataRepository plc_inputdataRepository, IPLC_MonthDataRepository plc_monthdataRepository)
        {
            _logger = logger;
            _plc_inputdataRepository = plc_inputdataRepository;
            _plc_monthdataRepository = plc_monthdataRepository;
        }

        /// <summary>
        /// 用电量信息
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        [HttpGet("getelectricdatalisto")]
        public IActionResult GetElectricDataListO(string projectid, int datetype = 4)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("TimeValue");
            dt.Columns.Add("DataValue");

            if (datetype == 0)//天
            {
                int[] test = { 11, 24, 58, 32, 65, 74, 18, 15, 24, 23 };
                DateTime dt1 = DateTime.Now.AddDays(-10);

                for (int i = 1; i <= 10; i++)
                {
                    DateTime dt2 = dt1.AddDays(i);
                    DataRow dr = dt.NewRow();
                    dr["TimeValue"] = dt2.Day;
                    dr["DataValue"] = 200 - test[i - 1];

                    dt.Rows.Add(dr);
                }
                //int h = DateTime.Now.Hour;
                //for (int i = 0; i <= h; i++)
                //{
                //    //Random rd = new Random();
                //    //int j = rd.Next(1, 20);
                //    DataRow dr = dt.NewRow();
                //    dr["TimeValue"] = i;
                //    if (i % 2 == 0)
                //    {
                //        dr["DataValue"] = 200 - (i*2);
                //    }
                //    else
                //    {
                //        dr["DataValue"] = 150 + (i * 2);
                //    }
                //    dt.Rows.Add(dr);
                //}
            }
            else if (datetype == 1)//本周
            {

            }
            if (datetype == 2)//月
            {
                int[] test = { 11, 24, 58, 32, 65, 74, 18, 15, 24, 23 };
                DateTime dt1 = DateTime.Now.AddMonths(-10);

                for (int i = 1; i <= 10; i++)
                {
                    DateTime dt2 = dt1.AddMonths(i);
                    DataRow dr = dt.NewRow();
                    dr["TimeValue"] = dt2.Month;
                    dr["DataValue"] = 1000 + test[i - 1];

                    dt.Rows.Add(dr);
                }
            }
            if (datetype == 3)//年
            {
                for (int i = 2016; i <= 2018; i++)
                {
                    //Random rd = new Random();
                    //int j = rd.Next(1, 20);
                    DataRow dr = dt.NewRow();
                    dr["TimeValue"] = i;

                    dr["DataValue"] = 1500 + (i / 4);

                    dt.Rows.Add(dr);
                }


            }

            //string start = string.Empty;
            //string end = string.Empty;
            //GetStartEndTime(datetype, out start, out end);
            //var result = _runningchartRepository.GetRunningList(projectid, start, end, userinfo);

            return Success("获取成功", dt);
        }

        /// <summary>
        /// 用电量信息
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        [HttpGet("getelectricdatalist")]
        public async Task<IActionResult> GetElectricDataList(string projectname, int datetype = 4)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("TimeValue");
            dt.Columns.Add("DataValue");

            if (datetype == 0)//天
            {
                #region
                for (int i = -10; i < 0; i++)
                {
                    string starttime = DateTime.Now.AddDays(i).ToString("yyyy-MM-dd");
                    string endtime = DateTime.Now.AddDays(i + 1).ToString("yyyy-MM-dd");
                    decimal totalnum = 0;
                    var list = new List<FilterDefinition<PLC_InputData>>();
                    list.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 1 & s.inputtime == starttime & s.deleteflag == 0&s.projectname ==projectname ));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                    var filter = Builders<PLC_InputData>.Filter.And(list);
                    var dlist = await _plc_inputdataRepository.Get(filter);
                    if (dlist != null)
                    {
                        var lis = dlist.ToList();
                        foreach (var item in lis)
                        {
                            var dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 1 & s.inputtime == endtime & s.deleteflag == 0 & s.dataname == item.dataname & s.projectname == projectname, null);
                            if (dModel != null)
                            {
                                decimal x = decimal.Parse(dModel.startvalue) - decimal.Parse(item.startvalue);
                                if (x > 0)
                                { totalnum += x; }
                            }
                        }
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = DateTime.Now.AddDays(i + 1).Day;//starttime;
                        dr["DataValue"] = totalnum * 1500;

                        dt.Rows.Add(dr);
                    }

                }
                #endregion

            }
            else if (datetype == 1)//本周
            {

            }
            if (datetype == 2)//月
            {

                #region 新修改
                for (int i = -9; i <= 0; i++)
                {
                    DateTime dtime = DateTime.Now.AddMonths(i);
                    string starttime = dtime.ToString("yyyy-MM-dd");
                    //var aModel = await _plc_monthdataRepository.GetSingle(s => s.datayear == dtime.Year .ToString () & (int)s.datamonth == dtime.Month & s.datakind == 1, null);
                    decimal totalnum = 0;
                    var list = new List<FilterDefinition<PLC_MonthData>>();
                    list.Add(Builders<PLC_MonthData>.Filter.Where(s => s.datayear == dtime.Year.ToString() & (int)s.datamonth == dtime.Month & s.datakind == 1 & s.projectname == projectname));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                    var filter = Builders<PLC_MonthData>.Filter.And(list);
                    var dlist = await _plc_monthdataRepository.Get(filter);
                    if (dlist == null)
                    {
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = dtime.Month;
                        dr["DataValue"] = 0;

                        dt.Rows.Add(dr);
                    }
                    else
                    {
                        var lis = dlist.ToList();
                        foreach (var item in lis)
                        {

                            decimal x = decimal.Parse(item.endvalue) - decimal.Parse(item.startvalue);
                            if (x > 0)
                            { totalnum += x; }

                        }
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = dtime.Month;
                        dr["DataValue"] = totalnum * 1500;

                        dt.Rows.Add(dr);
                    }

                }
                #endregion

                #region
                /*for (int i = -9; i <= 0; i++)
                {
                    DateTime dtime = DateTime.Now.AddMonths(i);
                    string starttime = dtime.AddDays(1 - dtime.Day).ToString("yyyy-MM-dd");
                    string endtime = dtime.AddDays(1 - dtime.Day).AddMonths(1).ToString("yyyy-MM-dd");
                    decimal totalnum = 0;
                    var list = new List<FilterDefinition<PLC_InputData>>();
                    list.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 1 & s.inputtime == starttime & s.deleteflag == 0));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                    var filter = Builders<PLC_InputData>.Filter.And(list);
                    var dlist = await _plc_inputdataRepository.Get(filter);
                    if (dlist == null)
                    {
                        for (int j = 1; j < 30; j++)
                        {
                            var list1 = new List<FilterDefinition<PLC_InputData>>();
                            list1.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 1 & s.inputtime == dtime.AddDays(1 - dtime.Day + j).ToString("yyyy-MM-dd") & s.deleteflag == 0));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                            var filter1 = Builders<PLC_InputData>.Filter.And(list1);
                            dlist = await _plc_inputdataRepository.Get(filter1);
                            if (dlist != null)
                                break;
                        }
                    }
                    if (dlist != null)
                    {
                        var lis = dlist.ToList();
                        foreach (var item in lis)
                        {
                            var dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 1 & s.inputtime == endtime & s.deleteflag == 0 & s.dataname == item.dataname, null);
                            if (dModel == null)
                            {
                                for (int j = 1; j < 30; j++)
                                {
                                    dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 1 & s.inputtime == dtime.AddDays(1 - dtime.Day + j).AddMonths(1).ToString("yyyy-MM-dd") & s.deleteflag == 0 & s.dataname == item.dataname, null);
                                    if (dModel != null)
                                        break;
                                }
                            }
                            if (dModel == null)
                            {
                                for (int j = 1; j < 30; j++)
                                {
                                    dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 1 & s.inputtime == dtime.AddDays(-j).ToString("yyyy-MM-dd") & s.deleteflag == 0 & s.dataname == item.dataname, null);
                                    if (dModel != null)
                                        break;
                                }
                            }
                            if (dModel != null)
                            {
                                decimal x = decimal.Parse(dModel.startvalue) - decimal.Parse(item.startvalue);
                                if (x > 0)
                                { totalnum += x; }
                            }
                        }
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = dtime.Month;
                        dr["DataValue"] = totalnum * 1500;

                        dt.Rows.Add(dr);
                    }

                }*/
                #endregion
            }
            if (datetype == 3)//年
            {
                #region 新修改
                for (int i = -2; i <= 0; i++)
                {
                    DateTime dtime = DateTime.Now.AddYears(i);
                    string starttime = dtime.ToString("yyyy-MM-dd");
                    //var aModel = await _plc_monthdataRepository.GetSingle(s => s.datayear == dtime.Year .ToString () & (int)s.datamonth == dtime.Month & s.datakind == 1, null);
                    decimal totalnum = 0;
                    var list = new List<FilterDefinition<PLC_MonthData>>();
                    list.Add(Builders<PLC_MonthData>.Filter.Where(s => s.datayear == dtime.Year.ToString() & s.datakind == 1 & s.projectname == projectname));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                    var filter = Builders<PLC_MonthData>.Filter.And(list);
                    var dlist = await _plc_monthdataRepository.Get(filter);
                    if (dlist == null)
                    {
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = DateTime.Now.AddYears(i).Year;
                        dr["DataValue"] = 0;

                        dt.Rows.Add(dr);
                    }
                    else
                    {
                        var lis = dlist.ToList();
                        foreach (var item in lis)
                        {

                            decimal x = decimal.Parse(item.endvalue) - decimal.Parse(item.startvalue);
                            if (x > 0)
                            { totalnum += x; }

                        }
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = DateTime.Now.AddYears(i).Year;
                        dr["DataValue"] = totalnum * 1500;

                        dt.Rows.Add(dr);
                    }

                }
                #endregion
                #region
                /*for (int i = 2 ; i >= 0; i--)
                {
                    DateTime dt1= DateTime.Parse(DateTime.Now.AddYears(-i).Year + "-01-01 00:00:00");
                    DateTime dt2 = DateTime.Parse(DateTime.Now.AddYears(-i + 1).Year + "-01-01 00:00:00");
                    string starttime = DateTime.Now.AddYears(-i).Year + "-01-01"; //DateTime.Parse (DateTime.Now.AddYears(-3).Year+"01-01 00:00:00" );
                    string  endtime = DateTime.Now.AddYears(-i+1).Year + "-01-01";
                    decimal totalnum = 0;
                    var list = new List<FilterDefinition<PLC_InputData>>();
                    list.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 1 & s.inputtime == starttime & s.deleteflag == 0));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                    var filter = Builders<PLC_InputData>.Filter.And(list);
                    var dlist = await _plc_inputdataRepository.Get(filter);
                    if (dlist == null)
                    {
                        for (int j = 1; j < 365; j++)
                        {
                            var list1 = new List<FilterDefinition<PLC_InputData>>();
                            list1.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 1 & s.inputtime == dt1.AddDays(j).ToString("yyyy-MM-dd") & s.deleteflag == 0));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                            var filter1 = Builders<PLC_InputData>.Filter.And(list1);
                            dlist = await _plc_inputdataRepository.Get(filter1);
                            if (dlist != null)
                                break;
                        }
                    }
                    if (dlist != null)
                    {
                        var lis = dlist.ToList();
                        foreach (var item in lis)
                        {
                            var dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 1 & s.inputtime == endtime & s.deleteflag == 0 & s.dataname == item.dataname, null);
                            if (dModel == null)
                            {
                                for (int j = 1; j < 365; j++)
                                {
                                    dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 1 & s.inputtime == dt2.AddDays(j).AddMonths(1).ToString("yyyy-MM-dd") & s.deleteflag == 0 & s.dataname == item.dataname, null);
                                    if (dModel != null)
                                        break;
                                }
                            }
                            if (dModel != null)
                            {
                                decimal x = decimal.Parse(dModel.startvalue) - decimal.Parse(item.startvalue);
                                if (x > 0)
                                { totalnum += x; }
                            }
                        }
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = DateTime.Now.AddYears(-i).Year;
                        dr["DataValue"] = totalnum * 1500;

                        dt.Rows.Add(dr);
                    }

                    //Random rd = new Random();
                    //int j = rd.Next(1, 20);
                    
                }*/
                #endregion

            }

            //string start = string.Empty;
            //string end = string.Empty;
            //GetStartEndTime(datetype, out start, out end);
            //var result = _runningchartRepository.GetRunningList(projectid, start, end, userinfo);

            return Success("获取成功", dt);
        }

        /// <summary>
        /// 用水量信息
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        [HttpGet("getwaterdatalist")]
        public async Task<IActionResult> GetWaterDataList(string projectname, int datetype = 4)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("TimeValue");
            dt.Columns.Add("DataValue");

            if (datetype == 0)//天
            {
                #region
                for (int i = -10; i < 0; i++)
                {
                    string starttime = DateTime.Now.AddDays(i).ToString("yyyy-MM-dd");
                    string endtime = DateTime.Now.AddDays(i + 1).ToString("yyyy-MM-dd");
                    decimal totalnum = 0;
                    var list = new List<FilterDefinition<PLC_InputData>>();
                    list.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 0 & s.inputtime == starttime & s.deleteflag == 0 & s.projectname == projectname));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                    var filter = Builders<PLC_InputData>.Filter.And(list);
                    var dlist = await _plc_inputdataRepository.Get(filter);
                    if (dlist != null)
                    {
                        var lis = dlist.ToList();
                        foreach (var item in lis)
                        {
                            var dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 0 & s.inputtime == endtime & s.deleteflag == 0 & s.dataname == item.dataname & s.projectname == projectname, null);
                            if (dModel != null)
                            {
                                decimal x = decimal.Parse(dModel.startvalue) - decimal.Parse(item.startvalue);
                                if (x > 0)
                                { totalnum += x; }
                            }
                        }
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = DateTime.Now.AddDays(i + 1).Day; //starttime;
                        dr["DataValue"] = totalnum;

                        dt.Rows.Add(dr);
                    }

                }
                #endregion

            }
            else if (datetype == 1)//本周
            {

            }
            if (datetype == 2)//月
            {
                #region 新修改
                for (int i = -9; i <= 0; i++)
                {
                    DateTime dtime = DateTime.Now.AddMonths(i);
                    string starttime = dtime.ToString("yyyy-MM-dd");
                    //var aModel = await _plc_monthdataRepository.GetSingle(s => s.datayear == dtime.Year .ToString () & (int)s.datamonth == dtime.Month & s.datakind == 1, null);
                    decimal totalnum = 0;
                    var list = new List<FilterDefinition<PLC_MonthData>>();
                    list.Add(Builders<PLC_MonthData>.Filter.Where(s => s.datayear == dtime.Year.ToString() & (int)s.datamonth == dtime.Month & s.datakind == 0 & s.projectname == projectname));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                    var filter = Builders<PLC_MonthData>.Filter.And(list);
                    var dlist = await _plc_monthdataRepository.Get(filter);
                    if (dlist == null)
                    {
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = dtime.Month;
                        dr["DataValue"] = 0;

                        dt.Rows.Add(dr);
                    }
                    else
                    {
                        var lis = dlist.ToList();
                        foreach (var item in lis)
                        {

                            decimal x = decimal.Parse(item.endvalue) - decimal.Parse(item.startvalue);
                            if (x > 0)
                            { totalnum += x; }

                        }
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = dtime.Month;
                        dr["DataValue"] = totalnum;

                        dt.Rows.Add(dr);
                    }

                }
                #endregion
                #region
                /*for (int i = -9; i <= 0; i++)
                {
                    DateTime dtime = DateTime.Now.AddMonths(i);
                    string starttime = dtime.AddDays(1 - dtime.Day).ToString("yyyy-MM-dd");
                    string endtime = dtime.AddDays(1 - dtime.Day).AddMonths(1).ToString("yyyy-MM-dd");
                    decimal totalnum = 0;
                    var list = new List<FilterDefinition<PLC_InputData>>();
                    list.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 0 & s.inputtime == starttime & s.deleteflag == 0));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                    var filter = Builders<PLC_InputData>.Filter.And(list);
                    var dlist = await _plc_inputdataRepository.Get(filter);
                    if (dlist == null)
                    {
                        for (int j = 1; j < 30; j++)
                        {
                            var list1 = new List<FilterDefinition<PLC_InputData>>();
                            list1.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 0 & s.inputtime == dtime.AddDays(1 - dtime.Day + j).ToString("yyyy-MM-dd") & s.deleteflag == 0));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                            var filter1 = Builders<PLC_InputData>.Filter.And(list1);
                            dlist = await _plc_inputdataRepository.Get(filter1);
                            if (dlist != null)
                                break;
                        }
                    }
                    if (dlist != null)
                    {
                        var lis = dlist.ToList();
                        foreach (var item in lis)
                        {
                            var dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 0 & s.inputtime == endtime & s.deleteflag == 0 & s.dataname == item.dataname, null);
                            if (dModel == null)
                            {
                                for (int j = 1; j < 30; j++)
                                {
                                    dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 0 & s.inputtime == dtime.AddDays(1 - dtime.Day + j).AddMonths(1).ToString("yyyy-MM-dd") & s.deleteflag == 0 & s.dataname == item.dataname, null);
                                    if (dModel != null)
                                        break;
                                }
                            }
                            if (dModel != null)
                            {
                                decimal x = decimal.Parse(dModel.startvalue) - decimal.Parse(item.startvalue);
                                if (x > 0)
                                { totalnum += x; }
                            }
                        }
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = dtime.Month;
                        dr["DataValue"] = totalnum;

                        dt.Rows.Add(dr);
                    }

                }*/
                #endregion
            }
            if (datetype == 3)//年
            {
                #region 新修改
                for (int i = -2; i <= 0; i++)
                {
                    DateTime dtime = DateTime.Now.AddYears(i);
                    string starttime = dtime.ToString("yyyy-MM-dd");
                    //var aModel = await _plc_monthdataRepository.GetSingle(s => s.datayear == dtime.Year .ToString () & (int)s.datamonth == dtime.Month & s.datakind == 1, null);
                    decimal totalnum = 0;
                    var list = new List<FilterDefinition<PLC_MonthData>>();
                    list.Add(Builders<PLC_MonthData>.Filter.Where(s => s.datayear == dtime.Year.ToString() & s.datakind == 0 & s.projectname == projectname));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                    var filter = Builders<PLC_MonthData>.Filter.And(list);
                    var dlist = await _plc_monthdataRepository.Get(filter);
                    if (dlist == null)
                    {
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = DateTime.Now.AddYears(i).Year;
                        dr["DataValue"] = 0;

                        dt.Rows.Add(dr);
                    }
                    else
                    {
                        var lis = dlist.ToList();
                        foreach (var item in lis)
                        {

                            decimal x = decimal.Parse(item.endvalue) - decimal.Parse(item.startvalue);
                            if (x > 0)
                            { totalnum += x; }

                        }
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = DateTime.Now.AddYears(i).Year;
                        dr["DataValue"] = totalnum;

                        dt.Rows.Add(dr);
                    }

                }
                #endregion
                #region
                /*for (int i = 2; i >= 0; i--)
                {
                    DateTime dt1 = DateTime.Parse(DateTime.Now.AddYears(-i).Year + "-01-01 00:00:00");
                    DateTime dt2 = DateTime.Parse(DateTime.Now.AddYears(-i + 1).Year + "-01-01 00:00:00");
                    string starttime = DateTime.Now.AddYears(-i).Year + "-01-01"; //DateTime.Parse (DateTime.Now.AddYears(-3).Year+"01-01 00:00:00" );
                    string endtime = DateTime.Now.AddYears(-i + 1).Year + "-01-01";
                    decimal totalnum = 0;
                    var list = new List<FilterDefinition<PLC_InputData>>();
                    list.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 0 & s.inputtime == starttime & s.deleteflag == 0));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                    var filter = Builders<PLC_InputData>.Filter.And(list);
                    var dlist = await _plc_inputdataRepository.Get(filter);
                    if (dlist == null)
                    {
                        for (int j = 1; j < 365; j++)
                        {
                            var list1 = new List<FilterDefinition<PLC_InputData>>();
                            list1.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 0 & s.inputtime == dt1.AddDays(j).ToString("yyyy-MM-dd") & s.deleteflag == 0));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                            var filter1 = Builders<PLC_InputData>.Filter.And(list1);
                            dlist = await _plc_inputdataRepository.Get(filter1);
                            if (dlist != null)
                                break;
                        }
                    }
                    if (dlist != null)
                    {
                        var lis = dlist.ToList();
                        foreach (var item in lis)
                        {
                            var dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 1 & s.inputtime == endtime & s.deleteflag == 0 & s.dataname == item.dataname, null);
                            if (dModel == null)
                            {
                                for (int j = 1; j < 365; j++)
                                {
                                    dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 1 & s.inputtime == dt2.AddDays(j).AddMonths(1).ToString("yyyy-MM-dd") & s.deleteflag == 0 & s.dataname == item.dataname, null);
                                    if (dModel != null)
                                        break;
                                }
                            }
                            if (dModel != null)
                            {
                                decimal x = decimal.Parse(dModel.startvalue) - decimal.Parse(item.startvalue);
                                if (x > 0)
                                { totalnum += x; }
                            }
                        }
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = DateTime.Now.AddYears(-i).Year;
                        dr["DataValue"] = totalnum;

                        dt.Rows.Add(dr);
                    }

                    
                }*/
                #endregion

            }


            return Success("获取成功", dt);
        }

        /// <summary>
        /// 用气量信息
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        [HttpGet("getgasdatalist")]
        public async Task<IActionResult> GetGasDataList(string projectname, int datetype = 4)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("TimeValue");
            dt.Columns.Add("DataValue");

            if (datetype == 0)//天
            {
                #region
                for (int i = -10; i < 0; i++)
                {
                    string starttime = DateTime.Now.AddDays(i).ToString("yyyy-MM-dd");
                    string endtime = DateTime.Now.AddDays(i + 1).ToString("yyyy-MM-dd");
                    decimal totalnum = 0;
                    var list = new List<FilterDefinition<PLC_InputData>>();
                    list.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 2 & s.inputtime == starttime & s.deleteflag == 0 & s.projectname == projectname));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                    var filter = Builders<PLC_InputData>.Filter.And(list);
                    var dlist = await _plc_inputdataRepository.Get(filter);
                    if (dlist != null)
                    {
                        var lis = dlist.ToList();
                        foreach (var item in lis)
                        {
                            var dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 2 & s.inputtime == endtime & s.deleteflag == 0 & s.dataname == item.dataname & s.projectname == projectname, null);
                            if (dModel != null)
                            {
                                decimal x = decimal.Parse(dModel.startvalue) - decimal.Parse(item.startvalue);
                                if (x > 0)
                                { totalnum += x; }
                            }
                        }
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = DateTime.Now.AddDays(i + 1).Day; //starttime;
                        dr["DataValue"] = totalnum;

                        dt.Rows.Add(dr);
                    }

                }
                #endregion

            }
            else if (datetype == 1)//本周
            {

            }
            if (datetype == 2)//月
            {
                #region 新修改
                for (int i = -9; i <= 0; i++)
                {
                    DateTime dtime = DateTime.Now.AddMonths(i);
                    string starttime = dtime.ToString("yyyy-MM-dd");
                    //var aModel = await _plc_monthdataRepository.GetSingle(s => s.datayear == dtime.Year .ToString () & (int)s.datamonth == dtime.Month & s.datakind == 1, null);
                    decimal totalnum = 0;
                    var list = new List<FilterDefinition<PLC_MonthData>>();
                    list.Add(Builders<PLC_MonthData>.Filter.Where(s => s.datayear == dtime.Year.ToString() & (int)s.datamonth == dtime.Month & s.datakind == 2 & s.projectname == projectname));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                    var filter = Builders<PLC_MonthData>.Filter.And(list);
                    var dlist = await _plc_monthdataRepository.Get(filter);
                    if (dlist == null)
                    {
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = dtime.Month;
                        dr["DataValue"] = 0;

                        dt.Rows.Add(dr);
                    }
                    else
                    {
                        var lis = dlist.ToList();
                        foreach (var item in lis)
                        {

                            decimal x = decimal.Parse(item.endvalue) - decimal.Parse(item.startvalue);
                            if (x > 0)
                            { totalnum += x; }

                        }
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = dtime.Month;
                        dr["DataValue"] = totalnum;

                        dt.Rows.Add(dr);
                    }

                }
                #endregion
                #region
                /*for (int i = -9; i <= 0; i++)
                {
                    DateTime dtime = DateTime.Now.AddMonths(i);
                    string starttime = dtime.AddDays(1 - dtime.Day).ToString("yyyy-MM-dd");
                    string endtime = dtime.AddDays(1 - dtime.Day).AddMonths(1).ToString("yyyy-MM-dd");
                    decimal totalnum = 0;
                    var list = new List<FilterDefinition<PLC_InputData>>();
                    list.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 2 & s.inputtime == starttime & s.deleteflag == 0));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                    var filter = Builders<PLC_InputData>.Filter.And(list);
                    var dlist = await _plc_inputdataRepository.Get(filter);
                    if (dlist == null)
                    {
                        for (int j = 1; j < 10; j++)
                        {
                            var list1 = new List<FilterDefinition<PLC_InputData>>();
                            list1.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 2 & s.inputtime == dtime.AddDays(1 - dtime.Day + j).ToString("yyyy-MM-dd") & s.deleteflag == 0));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                            var filter1 = Builders<PLC_InputData>.Filter.And(list1);
                            dlist = await _plc_inputdataRepository.Get(filter1);
                            if (dlist != null)
                                break;
                        }
                    }
                    if (dlist != null)
                    {
                        var lis = dlist.ToList();
                        foreach (var item in lis)
                        {
                            var dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 2 & s.inputtime == endtime & s.deleteflag == 0 & s.dataname == item.dataname, null);
                            if (dModel == null)
                            {
                                for (int j = 1; j < 10; j++)
                                {
                                    dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 2 & s.inputtime == dtime.AddDays(1 - dtime.Day + j).AddMonths(1).ToString("yyyy-MM-dd") & s.deleteflag == 0 & s.dataname == item.dataname, null);
                                    if (dModel != null)
                                        break;
                                }
                            }
                            if (dModel != null)
                            {
                                decimal x = decimal.Parse(dModel.startvalue) - decimal.Parse(item.startvalue);
                                if (x > 0)
                                { totalnum += x; }
                            }
                        }
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = dtime.Month;
                        dr["DataValue"] = totalnum;

                        dt.Rows.Add(dr);
                    }

                }*/
                #endregion
            }
            if (datetype == 3)//年
            {
                #region 新修改
                for (int i = -2; i <= 0; i++)
                {
                    DateTime dtime = DateTime.Now.AddYears(i);
                    string starttime = dtime.ToString("yyyy-MM-dd");
                    //var aModel = await _plc_monthdataRepository.GetSingle(s => s.datayear == dtime.Year .ToString () & (int)s.datamonth == dtime.Month & s.datakind == 1, null);
                    decimal totalnum = 0;
                    var list = new List<FilterDefinition<PLC_MonthData>>();
                    list.Add(Builders<PLC_MonthData>.Filter.Where(s => s.datayear == dtime.Year.ToString() & s.datakind == 2 & s.projectname == projectname));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                    var filter = Builders<PLC_MonthData>.Filter.And(list);
                    var dlist = await _plc_monthdataRepository.Get(filter);
                    if (dlist == null)
                    {
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = DateTime.Now.AddYears(i).Year;
                        dr["DataValue"] = 0;

                        dt.Rows.Add(dr);
                    }
                    else
                    {
                        var lis = dlist.ToList();
                        foreach (var item in lis)
                        {

                            decimal x = decimal.Parse(item.endvalue) - decimal.Parse(item.startvalue);
                            if (x > 0)
                            { totalnum += x; }

                        }
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = DateTime.Now.AddYears(i).Year;
                        dr["DataValue"] = totalnum;

                        dt.Rows.Add(dr);
                    }

                }
                #endregion
                #region
                /*for (int i = 2; i >= 0; i--)
                {
                    DateTime dt1 = DateTime.Parse(DateTime.Now.AddYears(-i).Year + "-01-01 00:00:00");
                    DateTime dt2 = DateTime.Parse(DateTime.Now.AddYears(-i + 1).Year + "-01-01 00:00:00");
                    string starttime = DateTime.Now.AddYears(-i).Year + "-01-01"; //DateTime.Parse (DateTime.Now.AddYears(-3).Year+"01-01 00:00:00" );
                    string endtime = DateTime.Now.AddYears(-i + 1).Year + "-01-01";
                    decimal totalnum = 0;
                    var list = new List<FilterDefinition<PLC_InputData>>();
                    list.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 2 & s.inputtime == starttime & s.deleteflag == 0));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                    var filter = Builders<PLC_InputData>.Filter.And(list);
                    var dlist = await _plc_inputdataRepository.Get(filter);
                    if (dlist == null)
                    {
                        for (int j = 1; j < 365; j++)
                        {
                            var list1 = new List<FilterDefinition<PLC_InputData>>();
                            list1.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 2 & s.inputtime == dt1.AddDays(j).ToString("yyyy-MM-dd") & s.deleteflag == 0));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                            var filter1 = Builders<PLC_InputData>.Filter.And(list1);
                            dlist = await _plc_inputdataRepository.Get(filter1);
                            if (dlist != null)
                                break;
                        }
                    }
                    if (dlist != null)
                    {
                        var lis = dlist.ToList();
                        foreach (var item in lis)
                        {
                            var dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 2 & s.inputtime == endtime & s.deleteflag == 0 & s.dataname == item.dataname, null);
                            if (dModel == null)
                            {
                                for (int j = 1; j < 365; j++)
                                {
                                    dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 2 & s.inputtime == dt2.AddDays(j).AddMonths(1).ToString("yyyy-MM-dd") & s.deleteflag == 0 & s.dataname == item.dataname, null);
                                    if (dModel != null)
                                        break;
                                }
                            }
                            if (dModel != null)
                            {
                                decimal x = decimal.Parse(dModel.startvalue) - decimal.Parse(item.startvalue);
                                if (x > 0)
                                { totalnum += x; }
                            }
                        }
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = DateTime.Now.AddYears(-i).Year;
                        dr["DataValue"] = totalnum;

                        dt.Rows.Add(dr);
                    }


                }*/
                #endregion

            }


            return Success("获取成功", dt);
        }

        /// <summary>
        /// 用氧量信息
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        [HttpGet("getoxygendatalist")]
        public async Task<IActionResult> GetOxygenDataList(string projectname, int datetype = 4)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("TimeValue");
            dt.Columns.Add("DataValue");

            if (datetype == 0)//天
            {
                #region
                for (int i = -10; i < 0; i++)
                {
                    string starttime = DateTime.Now.AddDays(i).ToString("yyyy-MM-dd");
                    string endtime = DateTime.Now.AddDays(i + 1).ToString("yyyy-MM-dd");
                    decimal totalnum = 0;
                    var list = new List<FilterDefinition<PLC_InputData>>();
                    list.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 3 & s.inputtime == starttime & s.deleteflag == 0 & s.projectname == projectname));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                    var filter = Builders<PLC_InputData>.Filter.And(list);
                    var dlist = await _plc_inputdataRepository.Get(filter);
                    if (dlist != null)
                    {
                        var lis = dlist.ToList();
                        foreach (var item in lis)
                        {
                            var dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 3 & s.inputtime == endtime & s.deleteflag == 0 & s.dataname == item.dataname & s.projectname == projectname, null);
                            if (dModel != null)
                            {
                                decimal x = decimal.Parse(dModel.startvalue) - decimal.Parse(item.startvalue);
                                if (x > 0)
                                { totalnum += x; }
                            }
                        }
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = DateTime.Now.AddDays(i + 1).Day;// starttime;
                        dr["DataValue"] = totalnum;

                        dt.Rows.Add(dr);
                    }

                }
                #endregion

            }
            else if (datetype == 1)//本周
            {

            }
            if (datetype == 2)//月
            {
                #region 新修改
                for (int i = -9; i <= 0; i++)
                {
                    DateTime dtime = DateTime.Now.AddMonths(i);
                    string starttime = dtime.ToString("yyyy-MM-dd");
                    //var aModel = await _plc_monthdataRepository.GetSingle(s => s.datayear == dtime.Year .ToString () & (int)s.datamonth == dtime.Month & s.datakind == 1, null);
                    decimal totalnum = 0;
                    var list = new List<FilterDefinition<PLC_MonthData>>();
                    list.Add(Builders<PLC_MonthData>.Filter.Where(s => s.datayear == dtime.Year.ToString() & (int)s.datamonth == dtime.Month & s.datakind == 3 & s.projectname == projectname));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                    var filter = Builders<PLC_MonthData>.Filter.And(list);
                    var dlist = await _plc_monthdataRepository.Get(filter);
                    if (dlist == null)
                    {
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = dtime.Month;
                        dr["DataValue"] = 0;

                        dt.Rows.Add(dr);
                    }
                    else
                    {
                        var lis = dlist.ToList();
                        foreach (var item in lis)
                        {

                            decimal x = decimal.Parse(item.endvalue) - decimal.Parse(item.startvalue);
                            if (x > 0)
                            { totalnum += x; }

                        }
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = dtime.Month;
                        dr["DataValue"] = totalnum;

                        dt.Rows.Add(dr);
                    }

                }
                #endregion
                #region
                /*for (int i = -9; i <= 0; i++)
                {
                    DateTime dtime = DateTime.Now.AddMonths(i);
                    string starttime = dtime.AddDays(1 - dtime.Day).ToString("yyyy-MM-dd");
                    string endtime = dtime.AddDays(1 - dtime.Day).AddMonths(1).ToString("yyyy-MM-dd");
                    decimal totalnum = 0;
                    var list = new List<FilterDefinition<PLC_InputData>>();
                    list.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 3 & s.inputtime == starttime & s.deleteflag == 0));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                    var filter = Builders<PLC_InputData>.Filter.And(list);
                    var dlist = await _plc_inputdataRepository.Get(filter);
                    if (dlist == null)
                    {
                        for (int j = 1; j < 10; j++)
                        {
                            var list1 = new List<FilterDefinition<PLC_InputData>>();
                            list1.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 3 & s.inputtime == dtime.AddDays(1 - dtime.Day + j).ToString("yyyy-MM-dd") & s.deleteflag == 0));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                            var filter1 = Builders<PLC_InputData>.Filter.And(list1);
                            dlist = await _plc_inputdataRepository.Get(filter1);
                            if (dlist != null)
                                break;
                        }
                    }
                    if (dlist != null)
                    {
                        var lis = dlist.ToList();
                        foreach (var item in lis)
                        {
                            var dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 3 & s.inputtime == endtime & s.deleteflag == 0 & s.dataname == item.dataname, null);
                            if (dModel == null)
                            {
                                for (int j = 1; j < 10; j++)
                                {
                                    dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 3 & s.inputtime == dtime.AddDays(1 - dtime.Day + j).AddMonths(1).ToString("yyyy-MM-dd") & s.deleteflag == 0 & s.dataname == item.dataname, null);
                                    if (dModel != null)
                                        break;
                                }
                            }
                            if (dModel != null)
                            {
                                decimal x = decimal.Parse(dModel.startvalue) - decimal.Parse(item.startvalue);
                                if (x > 0)
                                { totalnum += x; }
                            }
                        }
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = dtime.Month;
                        dr["DataValue"] = totalnum;

                        dt.Rows.Add(dr);
                    }

                }*/
                #endregion
            }
            if (datetype == 3)//年
            {
                #region 新修改
                for (int i = -2; i <= 0; i++)
                {
                    DateTime dtime = DateTime.Now.AddYears(i);
                    string starttime = dtime.ToString("yyyy-MM-dd");
                    //var aModel = await _plc_monthdataRepository.GetSingle(s => s.datayear == dtime.Year .ToString () & (int)s.datamonth == dtime.Month & s.datakind == 1, null);
                    decimal totalnum = 0;
                    var list = new List<FilterDefinition<PLC_MonthData>>();
                    list.Add(Builders<PLC_MonthData>.Filter.Where(s => s.datayear == dtime.Year.ToString() & s.datakind == 3 & s.projectname == projectname));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                    var filter = Builders<PLC_MonthData>.Filter.And(list);
                    var dlist = await _plc_monthdataRepository.Get(filter);
                    if (dlist == null)
                    {
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = DateTime.Now.AddYears(i).Year;
                        dr["DataValue"] = 0;

                        dt.Rows.Add(dr);
                    }
                    else
                    {
                        var lis = dlist.ToList();
                        foreach (var item in lis)
                        {

                            decimal x = decimal.Parse(item.endvalue) - decimal.Parse(item.startvalue);
                            if (x > 0)
                            { totalnum += x; }

                        }
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = DateTime.Now.AddYears(i).Year;
                        dr["DataValue"] = totalnum;

                        dt.Rows.Add(dr);
                    }

                }
                #endregion
                #region
                /*for (int i = 2; i >= 0; i--)
                {
                    DateTime dt1 = DateTime.Parse(DateTime.Now.AddYears(-i).Year + "-01-01 00:00:00");
                    DateTime dt2 = DateTime.Parse(DateTime.Now.AddYears(-i + 1).Year + "-01-01 00:00:00");
                    string starttime = DateTime.Now.AddYears(-i).Year + "-01-01"; //DateTime.Parse (DateTime.Now.AddYears(-3).Year+"01-01 00:00:00" );
                    string endtime = DateTime.Now.AddYears(-i + 1).Year + "-01-01";
                    decimal totalnum = 0;
                    var list = new List<FilterDefinition<PLC_InputData>>();
                    list.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 3 & s.inputtime == starttime & s.deleteflag == 0));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                    var filter = Builders<PLC_InputData>.Filter.And(list);
                    var dlist = await _plc_inputdataRepository.Get(filter);
                    if (dlist == null)
                    {
                        for (int j = 1; j < 365; j++)
                        {
                            var list1 = new List<FilterDefinition<PLC_InputData>>();
                            list1.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 3 & s.inputtime == dt1.AddDays(j).ToString("yyyy-MM-dd") & s.deleteflag == 0));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                            var filter1 = Builders<PLC_InputData>.Filter.And(list1);
                            dlist = await _plc_inputdataRepository.Get(filter1);
                            if (dlist != null)
                                break;
                        }
                    }
                    if (dlist != null)
                    {
                        var lis = dlist.ToList();
                        foreach (var item in lis)
                        {
                            var dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 3 & s.inputtime == endtime & s.deleteflag == 0 & s.dataname == item.dataname, null);
                            if (dModel == null)
                            {
                                for (int j = 1; j < 365; j++)
                                {
                                    dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 3 & s.inputtime == dt2.AddDays(j).AddMonths(1).ToString("yyyy-MM-dd") & s.deleteflag == 0 & s.dataname == item.dataname, null);
                                    if (dModel != null)
                                        break;
                                }
                            }
                            if (dModel != null)
                            {
                                decimal x = decimal.Parse(dModel.startvalue) - decimal.Parse(item.startvalue);
                                if (x > 0)
                                { totalnum += x; }
                            }
                        }
                        DataRow dr = dt.NewRow();
                        dr["TimeValue"] = DateTime.Now.AddYears(-i).Year;
                        dr["DataValue"] = totalnum;

                        dt.Rows.Add(dr);
                    }


                }*/
                #endregion


            }


            return Success("获取成功", dt);
        }

        /// <summary>
        /// 用水量信息
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        [HttpGet("getwaterdatalisto")]
        public IActionResult GetWaterDataListo(string projectid, int datetype = 4)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("TimeValue");
            dt.Columns.Add("DataValue");

            if (datetype == 0)//天
            {
                int[] test = { 11, 24, 58, 32, 65, 74, 18, 15, 24, 23 };
                DateTime dt1 = DateTime.Now.AddDays(-10);

                for (int i = 1; i <= 10; i++)
                {
                    DateTime dt2 = dt1.AddDays(i);
                    DataRow dr = dt.NewRow();
                    dr["TimeValue"] = dt2.Day;
                    dr["DataValue"] = 100 + test[i - 1];

                    dt.Rows.Add(dr);
                }
                //int h = DateTime.Now.Hour;
                //for (int i = 0; i <= h; i++)
                //{
                //    //Random rd = new Random();
                //    //int j = rd.Next(1, 20);
                //    DataRow dr = dt.NewRow();
                //    dr["TimeValue"] = i;
                //    if (i % 2 == 0)
                //    {
                //        dr["DataValue"] = 200 - (i*2);
                //    }
                //    else
                //    {
                //        dr["DataValue"] = 150 + (i * 2);
                //    }
                //    dt.Rows.Add(dr);
                //}
            }
            else if (datetype == 1)//本周
            {

            }
            if (datetype == 2)//月
            {
                int[] test = { 11, 24, 58, 32, 65, 74, 18, 15, 24, 23 };
                DateTime dt1 = DateTime.Now.AddMonths(-10);

                for (int i = 1; i <= 10; i++)
                {
                    DateTime dt2 = dt1.AddMonths(i);
                    DataRow dr = dt.NewRow();
                    dr["TimeValue"] = dt2.Month;
                    dr["DataValue"] = 2000 - test[i - 1];

                    dt.Rows.Add(dr);
                }
            }
            if (datetype == 3)//年
            {
                for (int i = 2016; i <= 2018; i++)
                {
                    //Random rd = new Random();
                    //int j = rd.Next(1, 20);
                    DataRow dr = dt.NewRow();
                    dr["TimeValue"] = i;

                    dr["DataValue"] = 1000 + (i / 4);

                    dt.Rows.Add(dr);
                }


            }

            //string start = string.Empty;
            //string end = string.Empty;
            //GetStartEndTime(datetype, out start, out end);
            //var result = _runningchartRepository.GetRunningList(projectid, start, end, userinfo);

            return Success("获取成功", dt);
        }

        /// <summary>
        /// 用气量信息
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        [HttpGet("getgasdatalisto")]
        public IActionResult GetGasDataListo(string projectid, int datetype = 4)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("TimeValue");
            dt.Columns.Add("DataValue");

            if (datetype == 0)//天
            {
                int[] test = { 11, 24, 58, 32, 65, 74, 18, 15, 24, 23 };
                DateTime dt1 = DateTime.Now.AddDays(-10);

                for (int i = 1; i <= 10; i++)
                {
                    DateTime dt2 = dt1.AddDays(i);
                    DataRow dr = dt.NewRow();
                    dr["TimeValue"] = dt2.Day;
                    dr["DataValue"] = 150 - test[i - 1];

                    dt.Rows.Add(dr);
                }
                //int h = DateTime.Now.Hour;
                //for (int i = 0; i <= h; i++)
                //{
                //    //Random rd = new Random();
                //    //int j = rd.Next(1, 20);
                //    DataRow dr = dt.NewRow();
                //    dr["TimeValue"] = i;
                //    if (i % 2 == 0)
                //    {
                //        dr["DataValue"] = 200 - (i*2);
                //    }
                //    else
                //    {
                //        dr["DataValue"] = 150 + (i * 2);
                //    }
                //    dt.Rows.Add(dr);
                //}
            }
            else if (datetype == 1)//本周
            {

            }
            if (datetype == 2)//月
            {
                int[] test = { 11, 24, 58, 32, 65, 74, 18, 15, 24, 23 };
                DateTime dt1 = DateTime.Now.AddMonths(-10);

                for (int i = 1; i <= 10; i++)
                {
                    DateTime dt2 = dt1.AddMonths(i);
                    DataRow dr = dt.NewRow();
                    dr["TimeValue"] = dt2.Month;
                    dr["DataValue"] = 1200 + test[i - 1];

                    dt.Rows.Add(dr);
                }
            }
            if (datetype == 3)//年
            {
                for (int i = 2016; i <= 2018; i++)
                {
                    //Random rd = new Random();
                    //int j = rd.Next(1, 20);
                    DataRow dr = dt.NewRow();
                    dr["TimeValue"] = i;

                    dr["DataValue"] = 1100 + (i / 4);

                    dt.Rows.Add(dr);
                }


            }

            //string start = string.Empty;
            //string end = string.Empty;
            //GetStartEndTime(datetype, out start, out end);
            //var result = _runningchartRepository.GetRunningList(projectid, start, end, userinfo);

            return Success("获取成功", dt);
        }

        /// <summary>
        /// 用氧量信息
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        [HttpGet("getoxygendatalisto")]
        public IActionResult GetOxygenDataListo(string projectid, int datetype = 3)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("TimeValue");
            dt.Columns.Add("DataValue");

            if (datetype == 0)//天
            {
                int[] test = { 11, 24, 58, 32, 65, 74, 18, 15, 24, 23 };
                DateTime dt1 = DateTime.Now.AddDays(-10);

                for (int i = 1; i <= 10; i++)
                {
                    DateTime dt2 = dt1.AddDays(i);
                    DataRow dr = dt.NewRow();
                    dr["TimeValue"] = dt2.Day;
                    dr["DataValue"] = 170 - test[i - 1];

                    dt.Rows.Add(dr);
                }
                //int h = DateTime.Now.Hour;
                //for (int i = 0; i <= h; i++)
                //{
                //    //Random rd = new Random();
                //    //int j = rd.Next(1, 20);
                //    DataRow dr = dt.NewRow();
                //    dr["TimeValue"] = i;
                //    if (i % 2 == 0)
                //    {
                //        dr["DataValue"] = 200 - (i*2);
                //    }
                //    else
                //    {
                //        dr["DataValue"] = 150 + (i * 2);
                //    }
                //    dt.Rows.Add(dr);
                //}
            }
            else if (datetype == 1)//本周
            {

            }
            if (datetype == 2)//月
            {
                int[] test = { 11, 24, 58, 32, 65, 74, 18, 15, 24, 23 };
                DateTime dt1 = DateTime.Now.AddMonths(-10);

                for (int i = 1; i <= 10; i++)
                {
                    DateTime dt2 = dt1.AddMonths(i);
                    DataRow dr = dt.NewRow();
                    dr["TimeValue"] = dt2.Month;
                    dr["DataValue"] = 1700 + test[i - 1];

                    dt.Rows.Add(dr);
                }
            }
            if (datetype == 3)//年
            {
                for (int i = 2016; i <= 2018; i++)
                {
                    //Random rd = new Random();
                    //int j = rd.Next(1, 20);
                    DataRow dr = dt.NewRow();
                    dr["TimeValue"] = i;

                    dr["DataValue"] = 1000 + (i / 2);

                    dt.Rows.Add(dr);
                }


            }

            //string start = string.Empty;
            //string end = string.Empty;
            //GetStartEndTime(datetype, out start, out end);
            //var result = _runningchartRepository.GetRunningList(projectid, start, end, userinfo);

            return Success("获取成功", dt);
        }

        /// <summary>
        /// 获取当天、本周、本月、本年、累计时间
        /// </summary>
        /// <param name="datetype"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        public void GetStartEndTime(int datetype, out string start, out string end)
        {
            start = string.Empty;
            end = string.Empty;

            var datenow = DateTime.Now;
            var datestart = datenow;
            var dateend = datenow;
            switch (datetype)
            {
                case 0://当天
                    break;
                case 1://本周
                    int n = (int)datenow.DayOfWeek == 0 ? 7 : (int)datenow.DayOfWeek;
                    datestart = datenow.AddDays(1 - n);//本周周一
                    dateend = datestart.AddDays(6);//本周周日
                    break;
                case 2://本月

                    datestart = datenow.AddDays(1 - datenow.Day);  //本月月初  
                    dateend = datestart.AddMonths(1).AddDays(-1);  //本月月末  
                    break;
                case 3://本年
                    datestart = new DateTime(datenow.Year, 1, 1);  //本年年初  
                    dateend = new DateTime(datenow.Year, 12, 31);  //本年年末  
                    break;
                case 4://全部
                    break;
                default:
                    break;
            }
            if (datetype != 4)
            {
                start = datestart.ToString("yyyy-MM-dd") + " 00:00:00";
                end = dateend.ToString("yyyy-MM-dd") + " 23:59:59";
            }
        }

        #region 报表
        //获取数据
        [HttpGet("getdatalist")]
        public async Task<IActionResult> GetDataList(string projectname,string checkyear, string checkmonth)
        {


            DataTable dtNew = new DataTable();
            dtNew = GetData(projectname,checkyear, checkmonth).Result;
            if (dtNew != null)
            {
                return Success("获取能耗报表数据成功", dtNew);
            }
            else
            {
                return Error("获取能耗报表数据失败");
            }
        }

        //获取数据源
        [HttpGet("getdata")]
        private async Task<DataTable> GetData(string projectname,string checkyear, string checkmonth)
        {
            DataTable dtNew = new DataTable();
            #region 编辑表头
            DataColumn dc1 = new DataColumn("日期", Type.GetType("System.String"));
            DataColumn dc2 = new DataColumn("水", Type.GetType("System.String"));
            DataColumn dc3 = new DataColumn("电", Type.GetType("System.String"));
            DataColumn dc4 = new DataColumn("气", Type.GetType("System.String"));
            DataColumn dc5 = new DataColumn("氧", Type.GetType("System.String"));
            dtNew.Columns.Add(dc1);
            dtNew.Columns.Add(dc2);
            dtNew.Columns.Add(dc3);
            dtNew.Columns.Add(dc4);
            dtNew.Columns.Add(dc5);
            decimal totals = 0;//水合计
            decimal totald = 0;//电合计
            decimal totalq = 0;//气合计
            decimal totaly = 0;//氧合计
            #endregion
            if (!String.IsNullOrEmpty(checkyear))
            {
                if (String.IsNullOrEmpty(checkmonth))//查询年报表（按年的每个月统计）
                {
                    for (int i = 1; i <= 12; i++)
                    {
                        DataRow dr = dtNew.NewRow();
                        //查询水
                        #region
                        decimal totalnum = 0;
                        var list = new List<FilterDefinition<PLC_MonthData>>();
                        list.Add(Builders<PLC_MonthData>.Filter.Where(s => s.datayear == checkyear & (int)s.datamonth == i & s.datakind == 0 & s.projectname == projectname));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                        var filter = Builders<PLC_MonthData>.Filter.And(list);
                        var dlist = await _plc_monthdataRepository.Get(filter);
                        if (dlist == null)
                        {
                            dr["水"] = 0;
                        }
                        else
                        {
                            var lis = dlist.ToList();
                            foreach (var item in lis)
                            {
                                if (!String.IsNullOrEmpty(item.endvalue))
                                {
                                    decimal x = decimal.Parse(item.endvalue) - decimal.Parse(item.startvalue);
                                    if (x > 0)
                                    { totalnum += x; }
                                }

                            }                          
                            dr["水"] = totalnum;
                            totals += totalnum;
                        }
                        #endregion
                        //查询电
                        #region
                        decimal totalnum1 = 0;
                        var list1 = new List<FilterDefinition<PLC_MonthData>>();
                        list1.Add(Builders<PLC_MonthData>.Filter.Where(s => s.datayear == checkyear & (int)s.datamonth == i & s.datakind == 1&s.projectname== projectname));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                        var filter1 = Builders<PLC_MonthData>.Filter.And(list1);
                        var dlist1 = await _plc_monthdataRepository.Get(filter1);
                        if (dlist1 == null)
                        {
                            dr["电"] = 0;
                        }
                        else
                        {
                            var lis = dlist1.ToList();
                            foreach (var item in lis)
                            {
                                if (!String.IsNullOrEmpty(item.endvalue))
                                {
                                    decimal x = decimal.Parse(item.endvalue) - decimal.Parse(item.startvalue);
                                    if (x > 0)
                                    { totalnum1 += x; }
                                }

                            }
                            dr["电"] = totalnum1*1500;
                            totald += totalnum1 * 1500;
                        }
                        #endregion
                        //查询气
                        #region
                        decimal totalnum2 = 0;
                        var list2 = new List<FilterDefinition<PLC_MonthData>>();
                        list2.Add(Builders<PLC_MonthData>.Filter.Where(s => s.datayear == checkyear & (int)s.datamonth == i & s.datakind == 2 & s.projectname == projectname));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                        var filter2 = Builders<PLC_MonthData>.Filter.And(list2);
                        var dlist2 = await _plc_monthdataRepository.Get(filter2);
                        if (dlist2 == null)
                        {
                            dr["气"] = 0;
                        }
                        else
                        {
                            var lis = dlist2.ToList();
                            foreach (var item in lis)
                            {
                                if (!String.IsNullOrEmpty(item.endvalue))
                                {
                                    decimal x = decimal.Parse(item.endvalue) - decimal.Parse(item.startvalue);
                                    if (x > 0)
                                    { totalnum2 += x; }
                                }

                            }
                            dr["气"] = totalnum2 ;
                            totalq += totalnum2;
                        }
                        #endregion
                        //查询氧
                        #region
                        decimal totalnum3 = 0;
                        var list3 = new List<FilterDefinition<PLC_MonthData>>();
                        list3.Add(Builders<PLC_MonthData>.Filter.Where(s => s.datayear == checkyear & (int)s.datamonth == i & s.datakind == 3 & s.projectname == projectname));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                        var filter3 = Builders<PLC_MonthData>.Filter.And(list3);
                        var dlist3 = await _plc_monthdataRepository.Get(filter3);
                        if (dlist3 == null)
                        {
                            dr["氧"] = 0;
                        }
                        else
                        {
                            var lis = dlist3.ToList();
                            foreach (var item in lis)
                            {
                                if (!String.IsNullOrEmpty(item.endvalue))
                                {
                                    decimal x = decimal.Parse(item.endvalue) - decimal.Parse(item.startvalue);
                                    if (x > 0)
                                    { totalnum3 += x; }
                                }

                            }
                            dr["氧"] = totalnum3;
                            totaly += totalnum3;
                        }
                        #endregion

                        dr["日期"] = checkyear + "年" + i.ToString() + "月";
                        dtNew.Rows.Add(dr);
                    }
                    #region 合计行
                    DataRow dr1 = dtNew.NewRow();
                    dr1["日期"] = "合计";
                    dr1["水"] = totals;
                    dr1["电"] = totald;
                    dr1["气"] = totalq;
                    dr1["氧"] = totaly;
                    dtNew.Rows.Add(dr1);
                    #endregion
                }
                else//查询月报表
                {
                    DateTime datecheck = DateTime.Parse (checkyear + "-" + checkmonth + "-01 12:00:00");
                    DateTime datestart = datecheck.AddDays(1 - datecheck.Day);  //月初  
                    DateTime dateend = datestart.AddMonths(1).AddDays(-1);  //月末
                    for (int i = 0; i < dateend.Day; i++)
                    {
                        DataRow dr = dtNew.NewRow();
                        string starttime = datestart.AddDays(i).ToString("yyyy-MM-dd");
                        string endtime = datestart.AddDays(i+1).ToString("yyyy-MM-dd");
                        #region 水
                        decimal totalnum = 0;
                        var list = new List<FilterDefinition<PLC_InputData>>();
                        list.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 0 & s.inputtime == starttime & s.deleteflag == 0 & s.projectname == projectname));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                        var filter = Builders<PLC_InputData>.Filter.And(list);
                        var dlist = await _plc_inputdataRepository.Get(filter);
                        if (dlist != null)
                        {
                            var lis = dlist.ToList();
                            foreach (var item in lis)
                            {
                                var dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 0 & s.inputtime == endtime & s.deleteflag == 0 & s.dataname == item.dataname & s.projectname == projectname, null);
                                if (dModel != null)
                                {
                                    decimal x = decimal.Parse(dModel.startvalue) - decimal.Parse(item.startvalue);
                                    if (x > 0)
                                    { totalnum += x; }
                                }
                            }
                            dr["水"] = totalnum;
                            totals += totalnum;
                        }
                        #endregion

                        #region 电
                        decimal totalnum1 = 0;
                        var list1 = new List<FilterDefinition<PLC_InputData>>();
                        list1.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 1 & s.inputtime == starttime & s.deleteflag == 0 & s.projectname == projectname));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                        var filter1 = Builders<PLC_InputData>.Filter.And(list1);
                        var dlist1 = await _plc_inputdataRepository.Get(filter1);
                        if (dlist1 != null)
                        {
                            var lis = dlist1.ToList();
                            foreach (var item in lis)
                            {
                                var dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 1 & s.inputtime == endtime & s.deleteflag == 0 & s.dataname == item.dataname & s.projectname == projectname, null);
                                if (dModel != null)
                                {
                                    decimal x = decimal.Parse(dModel.startvalue) - decimal.Parse(item.startvalue);
                                    if (x > 0)
                                    { totalnum1 += x; }
                                }
                            }
                            dr["电"] = totalnum1*1500;
                            totald += totalnum1 * 1500;
                        }
                        #endregion

                        #region 气
                        decimal totalnum2 = 0;
                        var list2 = new List<FilterDefinition<PLC_InputData>>();
                        list2.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 2 & s.inputtime == starttime & s.deleteflag == 0 & s.projectname == projectname));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                        var filter2 = Builders<PLC_InputData>.Filter.And(list2);
                        var dlist2 = await _plc_inputdataRepository.Get(filter2);
                        if (dlist2 != null)
                        {
                            var lis = dlist2.ToList();
                            foreach (var item in lis)
                            {
                                var dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 2 & s.inputtime == endtime & s.deleteflag == 0 & s.dataname == item.dataname & s.projectname == projectname, null);
                                if (dModel != null)
                                {
                                    decimal x = decimal.Parse(dModel.startvalue) - decimal.Parse(item.startvalue);
                                    if (x > 0)
                                    { totalnum2 += x; }
                                }
                            }
                            dr["气"] = totalnum2;
                            totalq += totalnum2;
                        }
                        #endregion

                        #region 氧
                        decimal totalnum3 = 0;
                        var list3 = new List<FilterDefinition<PLC_InputData>>();
                        list3.Add(Builders<PLC_InputData>.Filter.Where(s => s.datakind == 3 & s.inputtime == starttime & s.deleteflag == 0 & s.projectname == projectname));// || s.dealtype.Equals(2) || s.dealtype.Equals(3) || s.dealtype.Equals(4) || s.dealtype.Equals(5) || s.dealtype.Equals(6)
                        var filter3 = Builders<PLC_InputData>.Filter.And(list3);
                        var dlist3 = await _plc_inputdataRepository.Get(filter3);
                        if (dlist3 != null)
                        {
                            var lis = dlist3.ToList();
                            foreach (var item in lis)
                            {
                                var dModel = await _plc_inputdataRepository.GetSingle(s => s.datakind == 3 & s.inputtime == endtime & s.deleteflag == 0 & s.dataname == item.dataname & s.projectname == projectname, null);
                                if (dModel != null)
                                {
                                    decimal x = decimal.Parse(dModel.startvalue) - decimal.Parse(item.startvalue);
                                    if (x > 0)
                                    { totalnum3 += x; }
                                }
                            }
                            dr["氧"] = totalnum3;
                            totaly += totalnum3;
                        }
                        #endregion

                        dr["日期"] = datestart.AddDays(i).ToString("yyyy年MM月dd日");
                        dtNew.Rows.Add(dr);
                    }

                    #region 合计行
                    DataRow dr1 = dtNew.NewRow();
                    dr1["日期"] = "合计";
                    dr1["水"] = totals ;
                    dr1["电"] = totald ;
                    dr1["气"] = totalq;
                    dr1["氧"] = totaly;
                    dtNew.Rows.Add(dr1);
                    #endregion
                }
            }
            return dtNew;
        }

        //导出
        
        [HttpGet("exportexcel")]
        public async Task<IActionResult> ExportExcelAsync(string projectname, string checkyear, string checkmonth)
        {
            try
            {

                DataTable dtNew = new DataTable();
                dtNew = GetData(projectname, checkyear, checkmonth).Result;
                if (dtNew != null)
                {
                    //导出dtnew 
                    NPOIHelper npoi = new NPOIHelper();
                    string[] cols = { "日期", "水", "电", "气", "氧" };

                    byte[] sm = npoi.ExportToExcel1(dtNew, cols);
                    if (sm != null)
                    {
                        if (!String.IsNullOrEmpty(checkyear))
                        {
                            if (String.IsNullOrEmpty(checkmonth))//查询年报表（按年的每个月统计）
                            {
                                return File(sm, "application/vnd.ms-excel", projectname +"能耗年报.xlsx");
                            }
                            else
                            {
                                return File(sm, "application/vnd.ms-excel", projectname + "能耗月报.xlsx");
                            }
                        }
                    }
                    else
                    {
                        return Error("导出失败");
                    }
                }
                return Error("导出失败");

                //return Success("根据条件获取数据成功", tasklist);

            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "导出异常");
                return Error("导出失败");
            }
            //return Success("导出成功");
        }
        #endregion
    }
}