using Microsoft.EntityFrameworkCore;
using Quartz;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using wispro.sp.entity;

namespace wispro.sp.api.Job
{
    public class InvalidDataMessageJob : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            spDbContext spDb = new spDbContext();

            
            var lstItem = spDb.PerformanceItems.Where<PerformanceItem>(p =>
                (p.AgentFeedbackMemo != "已算绩效"  && p.CalMonth.Status == 0 && !p.CaseNo.StartsWith("J") 
                && (p.BasePoint ==null || (string.IsNullOrEmpty(p.CaseCoefficient) && p.Type =="新申请")
                || (p.Type =="OA" && string.IsNullOrEmpty(p.CaseCoefficient) && p.DoItemCoefficient =="实质")
                || (string.IsNullOrEmpty(p.DoItemCoefficient))
                )))
                .Include(p => p.Reviewer)
                .Include(p => p.CalMonth)
                .Include(p => p.Customer)
                .Include(p => p.WorkflowUser)
                .ToList<PerformanceItem>();

            if (lstItem.Count > 0)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("我方文号");
                dt.Columns.Add("业务人员");
                dt.Columns.Add("申请国家");
                dt.Columns.Add("客户名称");
                dt.Columns.Add("专利类型");
                dt.Columns.Add("处理事项");
                dt.Columns.Add("案件阶段");
                dt.Columns.Add("案件系数");
                dt.Columns.Add("处理事项系数");
                dt.Columns.Add("案件状态");
                dt.Columns.Add("翻译字数");
                dt.Columns.Add("基础点数");
                dt.Columns.Add("绩效类型");
                dt.Columns.Add("备注");

                foreach (var p in lstItem)
                {
                    DataRow row = dt.NewRow();
                    row["我方文号"] = p.CaseNo;
                    row["专利类型"] = p.ApplicationType;
                    row["处理事项"] = p.DoItem;
                    row["案件阶段"] = p.CaseStage;
                    row["案件系数"] = p.CaseCoefficient;
                    row["处理事项系数"] = p.DoItemCoefficient;
                    row["案件状态"] = p.CaseState;
                    row["翻译字数"] = p.WordCount;
                    row["基础点数"] = p.BasePoint;
                    row["绩效类型"] = p.Type;
                    row["备注"] = p.CaseMemo;
                    row["客户名称"] = p.ApplicationName;
                    row["申请国家"] = p.Country;

                    if (p.WorkflowUser != null)
                    {
                        row["业务人员"] = p.WorkflowUser.Name;
                    }

                    dt.Rows.Add(row);
                }

                string strPath = $"c:\\temp\\{DateTime.Now.ToString("yyyyMMddhhmmss")}-JXList.xlsx";
                utility.NPOIExcel.DataTableToExcel(dt, strPath);
                string strBody = $"<div style=\"position:absolute;width:800;height:300;margin-top:50px;margin-left:200px;margin-right:200px;box-shadow:0px 0px 3px 3px #ccc \"><div style= \"margin-top: 20px; margin-left:20px;;font-size:14px; \">各位好！</div><div style= \"margin-top: 25px; margin-left:20px;font-size:14px; \"><div>附件为本期绩效记录中疑似有问题数据，请检查并补充缺失数据！</div><div style= \"margin-top: 100px;margin-right:15px; padding-bottom: 20px; font-size:14px;color:#888888;float:right; \">小美集团绩效管理系统</div></div>";
                _ = QuartzUtil.AddMailJob("疑似有问题绩效数据清单", strBody, "罗才洋", "luocaiyang@china-wispro.com", strPath);
                _ = QuartzUtil.AddMailJob("疑似有问题绩效数据清单", strBody, "何青瓦", "heqingwa@china-wispro.com", strPath);
                _ = QuartzUtil.AddMailJob("疑似有问题绩效数据清单", strBody, "钟子敏", "zhongzimin@china-wispro.com", strPath);
                _ = QuartzUtil.AddMailJob("疑似有问题绩效数据清单", strBody, "夏敏", "xiamin@china-wispro.com", strPath);
                _ = QuartzUtil.AddMailJob("疑似有问题绩效数据清单", strBody, "吴芳", "wufang@china-wispro.com", strPath);
                _ = QuartzUtil.AddMailJob("疑似有问题绩效数据清单", strBody, "邢丽霞", "xinglixia@china-wispro.com", strPath);
                _ = QuartzUtil.AddMailJob("疑似有问题绩效数据清单", strBody, "孙心洁", "sunxinjie@china-wispro.com", strPath);
                _ = QuartzUtil.AddMailJob("疑似有问题绩效数据清单", strBody, "田婵玉", "tianchanyu@china-wispro.com", strPath);

            }

            return Task.CompletedTask;
        }
    }
}
