using BLL;
using System;
using System.Linq;
using System.Web.Http;
using BLL.OfficeCheck.Inspect;
using Model;
using static BLL.OfficeCheck.Inspect.Inspect_InspectionService;
using System.Collections.Generic;

namespace WebAPI.Controllers.HSSE
{
    public class SafetyInspectionController : ApiController
    {
        //新增
        /// <summary>
        /// 编辑 保存
        /// </summary>
        /// <param name="trainingPlan">安全专检</param>
        [HttpPost]
        public Model.ResponeData Save([FromBody] Model.Inspect_Inspection Inspection)
        {
            var responeData = new Model.ResponeData();
            try
            {
                if (string.IsNullOrEmpty(Inspection.InspectionId))
                {
                    Inspection.InspectionId = Guid.NewGuid().ToString();
                    Inspection.CreateTime = DateTime.Now;
                    Inspect_InspectionService.AddInspection(Inspection);
                }
                else
                {
                    Inspect_InspectionService.UpdateInspection(Inspection);
                }

            }
            catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = ex.Message;
            }

            return responeData;
        }
        
        //查询
        public Model.ResponeData getList(string ProjectId,string States,string PersonResponsible, int PageNumber, int PageSize)
        {
            var responeData = new Model.ResponeData();
            try
            {
                
                responeData.data = Inspect_InspectionService.GetInspectionList(ProjectId,States,PersonResponsible,PageNumber,PageSize);
            }
            catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = ex.Message;
            }

            return responeData;
        }
        
        //专检明细
        public Model.ResponeData getInspectionById(string InspectionId)
        {
            var responeData = new Model.ResponeData();
            try
            {
                var getDataLists = (from x in Funs.DB.Inspect_Inspection
                                                    join user in Funs.DB.Sys_User on x.InspectMan equals user.UserId into userJoin1
                                                    from subUser1 in userJoin1.DefaultIfEmpty()
                                                    join user in Funs.DB.Sys_User on x.CreateMan equals user.UserId into userJoin2
                                                    from subUser2 in userJoin2.DefaultIfEmpty()
                                                    join user in Funs.DB.Sys_User on x.PersonResponsible equals user.UserId into userJoin3
                                                    from subUser3 in userJoin3.DefaultIfEmpty()
                                                    join p in Funs.DB.Base_Project on x.ProjectId equals p.ProjectId into project
                                                    from pro in project.DefaultIfEmpty()
                                                    where x.InspectionId == InspectionId
                                    select new InspectionDto
                                                    {
                                                        InspectionId = x.InspectionId,
                                                        InspectionCode = x.InspectionCode,
                                                        ProjectId = x.ProjectId,
                                                        ProjectName = pro.ProjectName,
                                                        PersonResponsible = x.PersonResponsible,
                                                        PersonResponsibleName = subUser3.UserName,
                                                        InspectTime = x.InspectTime,
                                                        Description = x.Description,
                                                        States = x.States,
                                                        InspectMan = subUser1.UserName,
                                                        InspectManId = x.InspectMan,
                                                        InspectType = x.InspectType,
                                                        InspectItemSetId = x.InspectItemSetId,
                                                        CreateMan = subUser2.UserName,
                                                        CreateTime = x.CreateTime,
                                                    }).FirstOrDefault();

                responeData.data = getDataLists;
            }
            catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = ex.Message;
            }

            return responeData;
        }

        //项目负责人下拉
        public Model.ResponeData getPersonResponsibleList(string ProjectId)
        {
            var responeData = new Model.ResponeData();
            try
            {
                using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
                {
                    var users = (from x in db.Sys_User
                                 where x.IsPost == true && x.UserId != Const.hfnbdId && x.UserId != Const.sedinId
                                 orderby x.UserName
                                 select new UserDto
                                 {
                                     UserId = x.UserId,
                                     UserName = x.UserName,
                                 }).ToList();
                    if (!string.IsNullOrEmpty(ProjectId))
                    {
                        users = (from x in users
                                 join y in db.Project_ProjectUser on x.UserId equals y.UserId
                                 where y.ProjectId == ProjectId
                                 orderby x.UserName
                                 select new UserDto
                                 {
                                     UserId = x.UserId,
                                     UserName = x.UserName,
                                 }).ToList();
                    }

                    responeData.data = users;
                }
            }
            catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = ex.Message;
            }

            return responeData;
        }

        public class UserDto
        {
            public string UserId
            {
                get;
                set;
            }
            public string UserName
            {
                get;
                set;
            }
        }


            //专检明细
            public Model.ResponeData getInspectItemsByInspectionId(string InspectionId, int PageNumber, int PageSize)
        {
            var responeData = new Model.ResponeData();
            try
            {
                responeData.data = Inspect_InspectionService.GetInspectionItemList(InspectionId,PageNumber,PageSize);
            }
            catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = ex.Message;
            }

            return responeData;
        }


        //编辑 新增明细
        [HttpPost]
        public Model.ResponeData SaveInspectionItem([FromBody] List<Model.Inspect_InspectionItem> InspectionItems)
        {
            var responeData = new Model.ResponeData();
            try
            {
                foreach (var InspectionItem in InspectionItems)
                {
                    if (string.IsNullOrEmpty(InspectionItem.InspectionItemId))
                    {
                        InspectionItem.InspectionItemId = Guid.NewGuid().ToString();
                        InspectionItem.CompileTime = DateTime.Now;
                        Inspect_InspectionService.AddInspectionItem(InspectionItem);
                    }
                    else
                    {
                        Inspect_InspectionService.UpdateInspectionItem(InspectionItem);
                    }
                }

            }
            catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = ex.Message;
            }

            return responeData;
        }


        //专检子表明细

        public Model.ResponeData getInspectItemsById(string InspectionItemId)
        {
            var responeData = new Model.ResponeData();
            try
            {

                var getDataLists = (from x in Funs.DB.Inspect_InspectionItem
                                    join user in Funs.DB.Sys_User on x.CompileMan equals user.UserId into userJoin1
                                    from subUser1 in userJoin1.DefaultIfEmpty()
                                    join p in Funs.DB.Base_Project on x.ProjectId equals p.ProjectId into project
                                    from pro in project.DefaultIfEmpty()
                                    where x.InspectionItemId == InspectionItemId
                                    select new InspectionItemDto
                                    {
                                        InspectionItemId = x.InspectionItemId,
                                        InspectionId = x.InspectionId,
                                        InspectionItemCode = x.InspectionItemCode,
                                        ProjectId = x.ProjectId,
                                        ProjectName = pro.ProjectName,
                                        InspectionDescribe = x.InspectionDescribe,
                                        PhotoUrl = x.PhotoUrl,
                                        VideoUrl = x.VideoUrl,
                                        EvaluateResults = x.EvaluateResults,
                                        TimeLimited = x.TimeLimited,
                                        RectificationDescription = x.RectificationDescription,
                                        RectificationResults = x.RectificationResults,
                                        States = x.States,
                                        CompileMan = subUser1.UserName,
                                        CompileTime = x.CompileTime,
                                    }).FirstOrDefault();



                responeData.data = getDataLists;
            }
            catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = ex.Message;
            }

            return responeData;
        }


    }
}