using System;
using System.Collections.Generic;
using System.Web.Http;
using Model;
using BLL;
using System.Linq;

namespace WebAPI.Controllers.CQMS
{
    /// <summary>
    /// 施工绩效控制器
    /// </summary>
    public class PerformanceController : ApiController
    {
        #region 根据主键、人员id、项目id获取数据
        /// <summary>
        /// 根据主键、人员id、项目id、当前日期 获取数据
        /// </summary>
        /// <param name="Projectid"></param>
        /// <param name="Userid"></param>
        /// <param name="PerformanceGid"></param>
        /// <param name="Nowdate"></param>
        /// <returns></returns>
        [HttpGet]
        public Model.ResponeData getPerformanceData(string Projectid, string Userid, string PerformanceGid, string Nowdate)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                var responeData = new Model.ResponeData();
                try
                {
                    //根据当前日期 和主键id获取当前是第几周
                    var TodayDate = Convert.ToDateTime(Nowdate).Date;
                    var childModel3 = db.CQMS_Performance_Child3.FirstOrDefault(x => x.PerformanceGid == PerformanceGid
                           && x.CurrentDate == TodayDate);
                    if (childModel3 == null)
                    {
                        responeData.code = 0;
                        responeData.message = "未查询到数据,请去pc端表单";
                        return responeData;
                    }
                    var Performance_ChildGid2 = childModel3.Performance_ChildGid2;
                    var NowWeek = db.CQMS_Performance_Child2.FirstOrDefault(x => x.PerformanceGid == PerformanceGid
                      && x.Performance_ChildGid2 == Performance_ChildGid2).NowWeek;

                    //获取数量和本周任务安排
                    var child2List = Funs.DB.CQMS_Performance_Child2.Where(x => x.PerformanceGid == PerformanceGid && x.NowWeek
                    == NowWeek).OrderBy(x => x.SortIndex).ToList();


                    List<Model.CQMS_Performance_Child3> childList3 = new List<CQMS_Performance_Child3>();

                    
                    var Onindex = 0;
                    foreach (var item in child2List)
                    {
                        var FModel1Itemize = db.CQMS_Performance_Child1.FirstOrDefault(x => x.Performance_ChildGid1 == item.Performance_ChildGid1);
                        if (FModel1Itemize.Itemize.IndexOf("施工进度")>-1)
                        {
                            FModel1Itemize.Itemize = "施工进度";
                        }
                        else if (FModel1Itemize.Itemize.IndexOf("HSE管理") > -1)
                        {
                            FModel1Itemize.Itemize = "HSE管理";
                        }
                        else if (FModel1Itemize.Itemize.IndexOf("质量管理情况") > -1)
                        {
                            FModel1Itemize.Itemize = "质量管理情况";
                        }
                        else if (FModel1Itemize.Itemize.IndexOf("图纸、材料状态跟踪") > -1)
                        {
                            FModel1Itemize.Itemize = "图纸、材料状态跟踪";
                        }
                        else if (FModel1Itemize.Itemize.IndexOf("关键事项跟踪") > -1)
                        {
                            FModel1Itemize.Itemize = "关键事项跟踪";
                        }
                        else if (FModel1Itemize.Itemize.IndexOf("可交付成果") > -1)
                        {
                            FModel1Itemize.Itemize = "可交付成果";
                        }


                        item.TaskContent = FModel1Itemize.Itemize+"(本周任务安排:"+ item.TaskContent + ")";

                        var child3Model = db.CQMS_Performance_Child3.FirstOrDefault(x => x.PerformanceGid == PerformanceGid
                       && x.CurrentDate == TodayDate && x.Performance_ChildGid2 == item.Performance_ChildGid2);
                        childList3.Add(child3Model);
                        Onindex += 1;
                    }

                    responeData.data = new { child2List.Count, child2List, childList3 };


                    //responeData.data = APICheckSpecialService.getCheckSpecialById(CheckSpecialId, state);
                }
                catch (Exception ex)
                {
                    responeData.code = 0;
                    responeData.message = ex.Message;
                }

                return responeData;
            }
        }
        #endregion

        #region 添加当日计划
        /// <summary>
        /// 添加实体类
        /// </summary>
        public class PerformanceInserModelItem {
            /// <summary>
            /// 主键id
            /// </summary>
            public string PerformanceGid { get; set; }
            /// <summary>
            /// 本日工作计划
            /// </summary>
            public string WorkPlan { get; set; }
            /// <summary>
            /// 本日完成情况
            /// </summary>
            public string CompletStatus { get; set; }

            /// <summary>
            /// 当前日期
            /// </summary>
            public DateTime CurrentDate { get; set; }

            /// <summary>
            /// 本周工作完成情况
            /// </summary>
            public string TaskCompletContent { get; set; }

        }

        /// <summary>
        /// 添加实体
        /// </summary>
        public class AddLists {
            public List<PerformanceInserModelItem> childList  { get; set; }
            
        }

        /// <summary>
        /// 添加当日计划
        /// </summary>
        /// <param name="CreateList"></param>
        /// <returns></returns>
        [HttpPost]
        public Model.ResponeData InsertPerformance([FromBody] AddLists CreateList) {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                List<PerformanceInserModelItem> AddList = CreateList.childList;
                var responeData = new Model.ResponeData();
                try
                {
                    if (AddList==null)
                    {
                        responeData.code = 0;
                        responeData.message = "未查询到数据,请重试。";
                        return responeData;
                    }
                    var cdate = AddList[0].CurrentDate;
                    var PerformanceGid = AddList[0].PerformanceGid;

                    // 根据当前日期 和主键id获取当前是第几周
                    var TodayDate = Convert.ToDateTime(cdate).Date;
                    var childModel3 = db.CQMS_Performance_Child3.FirstOrDefault(x => x.PerformanceGid == PerformanceGid
                           && x.CurrentDate == TodayDate);
                    if (childModel3 == null)
                    {
                        responeData.code = 0;
                        responeData.message = "未查询到数据,请去pc端表单";
                        return responeData;
                    }

                    var Performance_ChildGid2 = childModel3.Performance_ChildGid2;
                    var NowWeek = db.CQMS_Performance_Child2.FirstOrDefault(x => x.PerformanceGid == PerformanceGid
                      && x.Performance_ChildGid2 == Performance_ChildGid2).NowWeek;
                    //加载数据。
                    //判断施工管理工作任务安排有几个(倒序)
                    var child2List = Funs.DB.CQMS_Performance_Child2.Where(x => x.PerformanceGid == PerformanceGid && x.NowWeek
                    == NowWeek).OrderByDescending(x => x.SortIndex).ToList();

                    var objModel = db.CQMS_Performance_Child3.Where(x => x.PerformanceGid == PerformanceGid
                            && x.CurrentDate == TodayDate);

                    if (child2List != null)
                    {
                        //可交付成果
                        var Child3Model = objModel.FirstOrDefault(x => x.Performance_ChildGid2 == child2List[0].Performance_ChildGid2);
                        Child3Model.WorkPlan = AddList[AddList.Count-1].WorkPlan;
                        db.SubmitChanges();

                        //周计划
                        Model.CQMS_Performance_Child2 Child4Model = child2List[0];
                        Child4Model.TaskCompletContent = AddList[AddList.Count-1].TaskCompletContent;
                        PerformanceService.UpdateChild2(Child4Model);

                        //关键事项跟踪
                        Child3Model = objModel.FirstOrDefault(x => x.Performance_ChildGid2 == child2List[1].Performance_ChildGid2);
                        Child3Model.WorkPlan = AddList[AddList.Count - 2].WorkPlan;
                        db.SubmitChanges();

                        Child4Model = child2List[1];
                        Child4Model.TaskCompletContent = AddList[AddList.Count - 2].TaskCompletContent;
                        PerformanceService.UpdateChild2(Child4Model);

                        //图纸、材料状态跟踪;统计台账
                        Child3Model = objModel.FirstOrDefault(x => x.Performance_ChildGid2 == child2List[2].Performance_ChildGid2);
                        Child3Model.WorkPlan = AddList[AddList.Count - 3].WorkPlan;
                        db.SubmitChanges();

                        Child4Model = child2List[2];
                        Child4Model.TaskCompletContent = AddList[AddList.Count - 3].TaskCompletContent;
                        PerformanceService.UpdateChild2(Child4Model);

                        //质量管理情况
                        Child3Model = objModel.FirstOrDefault(x => x.Performance_ChildGid2 == child2List[3].Performance_ChildGid2);
                        Child3Model.WorkPlan = AddList[AddList.Count - 4].WorkPlan;
                        db.SubmitChanges();

                        Child4Model = child2List[3];
                        Child4Model.TaskCompletContent = AddList[AddList.Count - 4].TaskCompletContent;
                        PerformanceService.UpdateChild2(Child4Model);

                        //HSE管理
                        Child3Model = objModel.FirstOrDefault(x => x.Performance_ChildGid2 == child2List[4].Performance_ChildGid2);
                        Child3Model.WorkPlan = AddList[AddList.Count - 5].WorkPlan;
                        db.SubmitChanges();

                        Child4Model = child2List[4];
                        Child4Model.TaskCompletContent = AddList[AddList.Count - 5].TaskCompletContent;
                        PerformanceService.UpdateChild2(Child4Model);

                        var Sgcount = Funs.DB.CQMS_Performance.FirstOrDefault(x => x.PerformanceGid == PerformanceGid).SgCount;
                        child2List = Funs.DB.CQMS_Performance_Child2.Where(x => x.PerformanceGid == PerformanceGid && x.NowWeek
                    == NowWeek).OrderBy(x => x.SortIndex).ToList();

                        for (int i = 0; i < Sgcount; i++)
                        {
                            Child3Model = objModel.FirstOrDefault(x => x.Performance_ChildGid2 == child2List[i].Performance_ChildGid2);
                            Child3Model.WorkPlan = AddList[i].WorkPlan;
                            Child3Model.CompletStatus = AddList[i].CompletStatus;
                            db.SubmitChanges();

                            Child4Model = child2List[i];
                            Child4Model.TaskCompletContent = AddList[i].TaskCompletContent;
                            PerformanceService.UpdateChild2(Child4Model);
                        }

                        //if (child2List.Count == 6)
                        //{
                        //    Child3Model = objModel.FirstOrDefault(x => x.Performance_ChildGid2 == child2List[5].Performance_ChildGid2);
                        //    Child3Model.WorkPlan = AddList[AddList.Count - 6].WorkPlan;
                        //    Child3Model.CompletStatus = AddList[AddList.Count - 6].CompletStatus;
                        //    db.SubmitChanges();

                        //    Child4Model = child2List[5];
                        //    Child4Model.TaskCompletContent = AddList[AddList.Count - 6].TaskCompletContent;
                        //    PerformanceService.UpdateChild2(Child4Model);
                        //}
                        //else if (child2List.Count == 7)
                        //{
                        //    Child3Model = objModel.FirstOrDefault(x => x.Performance_ChildGid2 == child2List[5].Performance_ChildGid2);
                        //    Child3Model.WorkPlan = AddList[AddList.Count - 6].WorkPlan;
                        //    Child3Model.CompletStatus = AddList[AddList.Count - 6].CompletStatus;
                        //    db.SubmitChanges();

                        //    Child4Model = child2List[5];
                        //    Child4Model.TaskCompletContent = AddList[AddList.Count -6].TaskCompletContent;
                        //    PerformanceService.UpdateChild2(Child4Model);

                        //    Child3Model = objModel.FirstOrDefault(x => x.Performance_ChildGid2 == child2List[6].Performance_ChildGid2);
                        //    Child3Model.WorkPlan = AddList[AddList.Count - 7].WorkPlan;
                        //    Child3Model.CompletStatus = AddList[AddList.Count - 7].CompletStatus;
                        //    db.SubmitChanges();

                        //    Child4Model = child2List[6];
                        //    Child4Model.TaskCompletContent = AddList[AddList.Count - 7].TaskCompletContent;
                        //    PerformanceService.UpdateChild2(Child4Model);
                        //}
                        //else if (child2List.Count == 8)
                        //{
                        //    Child3Model = objModel.FirstOrDefault(x => x.Performance_ChildGid2 == child2List[5].Performance_ChildGid2);
                        //    Child3Model.WorkPlan = AddList[AddList.Count - 6].WorkPlan;
                        //    Child3Model.CompletStatus = AddList[AddList.Count - 6].CompletStatus;
                        //    db.SubmitChanges();

                        //    Child4Model = child2List[5];
                        //    Child4Model.TaskCompletContent = AddList[AddList.Count - 6].TaskCompletContent;
                        //    PerformanceService.UpdateChild2(Child4Model);

                        //    Child3Model = objModel.FirstOrDefault(x => x.Performance_ChildGid2 == child2List[6].Performance_ChildGid2);
                        //    Child3Model.WorkPlan = AddList[AddList.Count - 7].WorkPlan;
                        //    Child3Model.CompletStatus = AddList[AddList.Count - 7].CompletStatus;
                        //    db.SubmitChanges();

                        //    Child4Model = child2List[6];
                        //    Child4Model.TaskCompletContent = AddList[AddList.Count - 7].CompletStatus;
                        //    PerformanceService.UpdateChild2(Child4Model);

                        //    Child3Model = objModel.FirstOrDefault(x => x.Performance_ChildGid2 == child2List[7].Performance_ChildGid2);
                        //    Child3Model.WorkPlan = AddList[AddList.Count - 8].WorkPlan;
                        //    Child3Model.CompletStatus = AddList[AddList.Count - 8].CompletStatus;
                        //    db.SubmitChanges();

                        //    Child4Model = child2List[7];
                        //    Child4Model.TaskCompletContent = AddList[AddList.Count - 8].CompletStatus;
                        //    PerformanceService.UpdateChild2(Child4Model);
                        //}
                    }
                    responeData.message = "保存成功";
                }
                catch (Exception ex)
                {
                    responeData.code = 0;
                    responeData.message = ex.Message;
                }

                return responeData;
            }
        }

        #endregion
    }
}