using Infrastructure;
using OpenAuth.App.BaseApp.Base;
using OpenAuth.App.Interface;
using OpenAuth.App.ServiceApp.DroneSsnydManage.Request;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using SqlSugar;
using Infrastructure;
using DocumentFormat.OpenXml.Spreadsheet;
using NPOI.SS.Formula.Functions;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using Newtonsoft.Json.Linq;

namespace OpenAuth.App.ServiceApp.DroneSsnydManage
{
    public class DroneSsnyApp : SqlSugarBaseApp<DroneSsnyd, SugarDbContext>
    {
        public DroneSsnyApp(ISugarUnitOfWork<SugarDbContext> unitWork, ISimpleClient<DroneSsnyd> repository, IAuth auth)
            : base(unitWork, repository, auth)
        {
        }

        /// <summary>
        /// 到期预警
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PageInfo<List<DroneSsnyd>> TimeoutWarning(string xiangmumc, string countyid, string streetid, int pageIndex, int pageSize)
        {
            int totalCount = 0;
            var endTime = DateTime.Now.AddMonths(2);

            var list = base.Repository.AsQueryable()
                    .WhereIF(!string.IsNullOrEmpty(xiangmumc), a => a.xiangmu_name.Contains(xiangmumc))
                    .WhereIF(!string.IsNullOrEmpty(countyid), a => a.countyid == countyid)
                    .WhereIF(!string.IsNullOrEmpty(streetid), a => a.streetid == streetid)
                     .Where(a => a.end_time <= endTime && a.end_time > DateTime.Now)
                     .OrderBy(a => a.end_time, OrderByType.Desc)
                     .ToPageList(pageIndex, pageSize, ref totalCount);

            return new PageInfo<List<DroneSsnyd>>
            {
                Items = list,
                Total = totalCount
            };
        }



        /// <summary>
        /// 超期报警
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PageInfo<List<DroneSsnyd>> TimeOutAlarmList(string xiangmumc, string countyid, string streetid, int pageIndex, int pageSize)
        {
            int totalCount = 0;
            var endTime = DateTime.Now;

            var list = base.Repository.AsQueryable()
                      .WhereIF(!string.IsNullOrEmpty(xiangmumc), a => a.xiangmu_name.Contains(xiangmumc))
                     .WhereIF(!string.IsNullOrEmpty(countyid), a => a.countyid == countyid)
                     .WhereIF(!string.IsNullOrEmpty(streetid), a => a.streetid == streetid)
                     .Where(a => a.end_time < endTime)
                     .OrderBy(a => a.end_time, OrderByType.Desc)
                     .ToPageList(pageIndex, pageSize, ref totalCount);

            return new PageInfo<List<DroneSsnyd>>
            {
                Items = list,
                Total = totalCount
            };
        }

        /// <summary>
        /// 历史项目
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public PageInfo<List<DroneSsnyd>> HistoryProject(DroneSnnyAppPageReq req)
        {
            var totalCount = 0;
            var list = Repository.AsQueryable()
                .Where(a => a.handle_status_id == 99)
                .WhereIF(!string.IsNullOrEmpty(req.xiangmu_no), a => a.xiangmu_no.Contains(req.xiangmu_no))
                .WhereIF(!string.IsNullOrEmpty(req.xiangmumc), a => a.xiangmumc.Contains(req.xiangmumc))
                .OrderBy(a => a.end_time, OrderByType.Desc)
                .ToPageList(req.page, req.limit, ref totalCount);
            return new PageInfo<List<DroneSsnyd>>
            {
                Items = list,
                Total = totalCount
            };
        }

        /// <summary>
        /// 项目变更
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public PageInfo<List<DroneSsnyd>> ProjectChange(DroneSnnyAppPageReq req)
        {
            var totalCount = 0;
            var list = Repository.AsQueryable()
                .Where(a => a.handle_status_id != 99)
                .WhereIF(!string.IsNullOrEmpty(req.xiangmu_no), a => a.xiangmu_no.Contains(req.xiangmu_no))
                .WhereIF(!string.IsNullOrEmpty(req.xiangmumc), a => a.xiangmumc.Contains(req.xiangmumc))
                .OrderBy(a => a.end_time, OrderByType.Desc)
                .ToPageList(req.page, req.limit, ref totalCount);
            return new PageInfo<List<DroneSsnyd>>
            {
                Items = list,
                Total = totalCount
            };
        }


        #region 导出
        //超期预警
        public List<DroneSsnyd> TimeoutWarningExport(string xiangmumc, string countyid, string streetid)
        {
            var endTime = DateTime.Now.AddMonths(2);

            var list = base.Repository.AsQueryable()
                    .WhereIF(!string.IsNullOrEmpty(xiangmumc), a => a.xiangmu_name.Contains(xiangmumc))
                    .WhereIF(!string.IsNullOrEmpty(countyid), a => a.countyid == countyid)
                    .WhereIF(!string.IsNullOrEmpty(streetid), a => a.streetid == streetid)
                     .Where(a => a.end_time <= endTime && a.end_time > DateTime.Now)
                     .OrderBy(a => a.end_time, OrderByType.Desc)
                     .ToList();

            return list;
        }

        /// <summary>
        /// 超期报警
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<DroneSsnyd> TimeOutAlarmExport(string xiangmumc, string countyid, string streetid)
        {
            var endTime = DateTime.Now;

            var list = base.Repository.AsQueryable()
                      .WhereIF(!string.IsNullOrEmpty(xiangmumc), a => a.xiangmu_name.Contains(xiangmumc))
                     .WhereIF(!string.IsNullOrEmpty(countyid), a => a.countyid == countyid)
                     .WhereIF(!string.IsNullOrEmpty(streetid), a => a.streetid == streetid)
                     .Where(a => a.end_time < endTime)
                     .OrderBy(a => a.end_time, OrderByType.Desc)
                     .ToList();

            return list;
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="list"></param>
        /// <param name="headers"></param>
        /// <returns></returns>
        public  Response<MemoryStream> ListToExcel(List<DroneSsnyd> list, List<string> headers)
        {
            Response<MemoryStream> response = new Response<MemoryStream>();
            try
            {
                HSSFWorkbook workbook = new HSSFWorkbook();
                ISheet sheet = workbook.CreateSheet();

                #region 内容样式

                IFont font1 = workbook.CreateFont(); //创建一个字体样式对象
                font1.FontName = "Microsoft YaHei"; //和excel里面的字体对应
                //font1.Boldweight = short.MaxValue;//字体加粗
                font1.FontHeightInPoints = 12; //字体大小
                ICellStyle style = workbook.CreateCellStyle(); //创建样式对象
                style.BorderBottom = BorderStyle.Thin;
                style.BorderLeft = BorderStyle.Thin;
                style.BorderRight = BorderStyle.Thin;
                style.BorderTop = BorderStyle.Thin;
                style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
                style.VerticalAlignment = VerticalAlignment.Center;
                style.SetFont(font1); //将字体样式赋给样式对象 
                style.WrapText = true;

                #endregion

                #region 标题样式

                IFont font = workbook.CreateFont(); //创建一个字体样式对象
                font.FontName = "Microsoft YaHei"; //和excel里面的字体对应
                font.Boldweight = (short)FontBoldWeight.Bold; //字体加粗
                font.FontHeightInPoints = 12; //字体大小
                ICellStyle style1 = workbook.CreateCellStyle(); //创建样式对象
                style1.BorderBottom = BorderStyle.Thin;
                style1.BorderLeft = BorderStyle.Thin;
                style1.BorderRight = BorderStyle.Thin;
                style1.BorderTop = BorderStyle.Thin;
                style1.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
                style1.VerticalAlignment = VerticalAlignment.Center;
                style1.SetFont(font); //将字体样式赋给样式对象 

                #endregion

                #region 创建表头

                IRow rowHeader = sheet.CreateRow(0);
                rowHeader.Height = 20 * 30;
                for (int i = 0; i < headers.Count; i++)
                {
                    var header = headers[i];
                    rowHeader.CreateCell(i);
                    rowHeader.Cells[i].CellStyle = style1;
                    rowHeader.Cells[i].SetCellValue(header);
                    if (i == 0)
                    {
                        sheet.SetColumnWidth(0, 20 * 200);
                    }
                    else
                    {
                        sheet.SetColumnWidth(i, 20 * 350);
                    }

                }

                #endregion

                #region 填充数据

                for (int i = 0; i < list.Count; i++) //循环数据
                {
                    var item = list[i];
                    IRow dataRow = sheet.CreateRow(i + 1);
                    //dataRow.Height = 20 * 20;
                    var type = item.GetType();
                    var props = type.GetProperties();
                    for (int j = 0; j < headers.Count; j++)
                    {
                        dataRow.CreateCell(j);
                        dataRow.Cells[j].CellStyle = style;
                    }
                    dataRow.Cells[0].SetCellValue(item.xiangmu_no);
                    dataRow.Cells[1].SetCellValue(item.xiangmu_name);
                    dataRow.Cells[2].SetCellValue(item.streetname);
                    dataRow.Cells[3].SetCellValue(item.communityname);
                    dataRow.Cells[4].SetCellValue(item.quanliren);
                    dataRow.Cells[5].SetCellValue(item.xingzhengquhua);
                    dataRow.Cells[6].SetCellValue(item.beian_no);
                    dataRow.Cells[7].SetCellValue(item.start_time.ToString());
                    dataRow.Cells[8].SetCellValue(item.end_time.ToString());
                    dataRow.Cells[9].SetCellValue(item.xiangmu_yt);
                    if (item.shenqing_area != null)
                    {
                        dataRow.Cells[10].SetCellValue(item.shenqing_area.ToString());
                    }
                    else
                    {
                        dataRow.Cells[10].SetCellValue("0.00");
                    }
                    if (item.shengchan_area != null)
                    {
                        dataRow.Cells[11].SetCellValue(item.shenqing_area.ToString());
                    }
                    else
                    {
                        dataRow.Cells[11].SetCellValue("0.00");
                    }
                    if (item.fuzhu_area != null)
                    {
                        dataRow.Cells[12].SetCellValue(item.fuzhu_area.ToString());
                    }
                    else
                    {
                        dataRow.Cells[12].SetCellValue("0.00");
                    }

                    dataRow.Cells[13].SetCellValue(item.xiafatime.ToString());

                    if (item.handle_status_id != null)
                    {
                        StatusType status = (StatusType)item.handle_status_id;
                        dataRow.Cells[14].SetCellValue(status.ToString());
                    }
                    else
                    {
                        dataRow.Cells[14].SetCellValue("--");
                    }
                }

                #endregion

                response.Result = new MemoryStream();

                workbook.Write(response.Result);
                workbook = null;
                response.Result.Close();
                response.Result.Dispose();
                response.Code = 200;
                response.Message = "获取成功";
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.Message;
            }

            return response;
        }
        #endregion

        #region 项目列表
        /// <summary>
        /// 到期预警
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PageInfo<List<DroneSsnyd>> GetDronssnydList(string xiangmumc, string countyid, string streetid, int pageIndex, int pageSize)
        {
            int totalCount = 0;
            var list = base.Repository.AsQueryable()
                    .WhereIF(!string.IsNullOrEmpty(xiangmumc), a => a.xiangmu_name.Contains(xiangmumc))
                    .WhereIF(!string.IsNullOrEmpty(countyid), a => a.countyid == countyid)
                    .WhereIF(!string.IsNullOrEmpty(streetid), a => a.streetid == streetid)
                     .OrderBy(a => a.end_time, OrderByType.Desc)
                     .ToPageList(pageIndex, pageSize, ref totalCount);

            return new PageInfo<List<DroneSsnyd>>
            {
                Items = list,
                Total = totalCount
            };
        }
        #endregion
    }
}