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;
using static WebAPI.Controllers.HSSE.ImageRecognitionController;
using Microsoft.Office.Interop.Word;

namespace WebAPI.Controllers.HSSE
{
    public class SafetyInspectionController : ApiController
    {

        #region 保存专检主项
        [HttpPost]
        public Model.ResponeData SaveInspection(Model.Inspect_Inspection inspection)
        {
            var responeData = new Model.ResponeData();
            try
            {
                Model.Inspect_Inspection obj = new Model.Inspect_Inspection();
                if (inspection.InspectionId == null)
                {
                    obj = Inspect_InspectionService.SaveInspection(inspection);
                    
                }else
                {
                    Inspect_InspectionService.UpdateInspection(inspection);
                }
                responeData.data = obj.InspectionId;



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


        #region 获取专检主项详细
        public Model.ResponeData getInspectionById(string InspectionId)
        {
            var responeData = new Model.ResponeData();
            try
            {
                responeData.data = Inspect_InspectionService.GetInspectionById(InspectionId);
            }
            catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = ex.Message;
            }
            return responeData;
        }
        #endregion

        #region 获取专检主项列表
        public Model.ResponeData getInspectionList(string ProjectId, string States, int PageNumber, int PageSize, string type = "1")
        {
            var responeData = new Model.ResponeData();
            try
            {
                responeData.data = Inspect_InspectionService.GetInspectionList(ProjectId, type, States,  PageNumber, PageSize);
            }
            catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = ex.Message;
            }
            return responeData;
        }
        #endregion

        #region 保存专检子项
        [HttpPost]
        public Model.ResponeData SaveInspectionChild(Model.Inspect_InspectionItem child)
        {
            var responeData = new Model.ResponeData();
            try
            {
                if (string.IsNullOrEmpty(child.InspectionItemId))
                {
                    Inspect_InspectionService.SaveInspectionItem(child);
                }
                else
                {
                    Inspect_InspectionService.UpdateInspectionItem(child);
                }
            } catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = ex.Message;
            }
        

            return responeData;
        }
        #endregion

        #region 获取专检子项详细
        public Model.ResponeData getInspectItemsById(string InspectionItemId)
        {
            var responeData = new Model.ResponeData();
            try
            {
                responeData.data = Inspect_InspectionService.GetInspectItemsById(InspectionItemId);
            }
            catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = ex.Message;
            }
            return responeData;
        }
        #endregion

        #region 根据项目和检查日期获取专检  (公司级查询)
        public Model.ResponeData getInspectionByProjectIdAndCheckDate(string projectId, string checkDate, string type)
        {
            var responeData = new Model.ResponeData();
            try
            {
                DateTime mdate = Funs.GetNewDateTimeOrNow(checkDate);

                var getDataLists = (from x in Funs.DB.Inspect_Inspection
                                    where x.InspectType == type
                                    select new InspectionDto
                                    {
                                        InspectionId = x.InspectionId,
                                        InspectionCode = x.InspectionCode,
                                        ProjectId = x.ProjectId,
                                        ProjectName = Funs.DB.Base_Project.FirstOrDefault(p => p.ProjectId == x.ProjectId).ProjectName,
                                        PersonResponsible = x.PersonResponsible,
                                        PersonResponsibleName = Funs.DB.Sys_User.FirstOrDefault(u => u.UserId == x.PersonResponsible).UserName,
                                        Description = x.Description,
                                        States = x.States,
                                        CreateManName = Funs.DB.Sys_User.FirstOrDefault(u => u.UserId == x.CreateMan).UserName,
                                        CreateMan = x.CreateMan,
                                        CreateTime = x.CreateTime,
                                        InspectType = x.InspectType,
                                        ProblemTypeId = x.ProblemTypeId,
                                        ProblemTypeName = x.ProblemTypeName,
                                        Place = x.Place,
                                        ChildsCount = (from a in Funs.DB.Inspect_InspectionItem
                                                         where a.InspectionId == x.InspectionId
                                                         select a).Count().ToString(),
                                    });
                if (!string.IsNullOrEmpty(projectId)&&projectId!=null)
                {
                    getDataLists = getDataLists.Where(q => q.ProjectId == projectId);
                }

                 getDataLists  =  getDataLists.Where(q => q.CreateTime.Value.Year == mdate.Year
                                       && q.CreateTime.Value.Month == mdate.Month
                                       && q.CreateTime.Value.Day == mdate.Day);
                responeData.data = getDataLists.ToList();
            }
            catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = ex.Message;
            }
            return responeData;
        }
        #endregion








    }
}