using Model.CQMS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL.API.CQMS
{
    public class ApiInspectionManagementService
    {
        public static bool AddInspectionManagement(InspectionManagement inspectionManagement)
        {
            using (var db = new Model.SGGLDB(Funs.ConnString))
            {
                Model.ProcessControl_InspectionManagement newInspectionManagement=null;
                if (!string.IsNullOrEmpty(inspectionManagement.InspectionId))                
                {
                    newInspectionManagement = db.ProcessControl_InspectionManagement.FirstOrDefault(x=>x.InspectionId==inspectionManagement.InspectionId);
                }
                if (newInspectionManagement == null)
                {
                    newInspectionManagement = new Model.ProcessControl_InspectionManagement();
                    newInspectionManagement.InspectionId = Guid.NewGuid().ToString();
                }
                newInspectionManagement.InspectionId = inspectionManagement.InspectionId;
                newInspectionManagement.ProjectId = inspectionManagement.ProjectId;
                newInspectionManagement.UnitId = inspectionManagement.UnitId;
                newInspectionManagement.CNProfessionalId = inspectionManagement.CNProfessionalId;
                newInspectionManagement.InspectionCode = inspectionManagement.InspectionCode;
                newInspectionManagement.UnitWorkId = inspectionManagement.UnitWorkId;
                newInspectionManagement.Branch = inspectionManagement.Branch;
                newInspectionManagement.ControlPointType = inspectionManagement.ControlPointType;
                newInspectionManagement.AcceptanceSite = inspectionManagement.AcceptanceSite;
                newInspectionManagement.IsOnceQualified = inspectionManagement.IsOnceQualified == "true" ? true : false;
                if (!string.IsNullOrEmpty(inspectionManagement.InspectionDate))
                    newInspectionManagement.InspectionDate = DateTime.Parse(inspectionManagement.InspectionDate);
                newInspectionManagement.AttachUrl = inspectionManagement.AttachUrl;
                newInspectionManagement.AttachUrl2 = inspectionManagement.AttachUrl2;
                newInspectionManagement.NoticeCode = inspectionManagement.NoticeCode;
                newInspectionManagement.ParentDivisionProjectId = inspectionManagement.ParentDivisionProjectId;
                if (!string.IsNullOrEmpty(inspectionManagement.CheckDate))
                    newInspectionManagement.CheckDate = DateTime.Parse(inspectionManagement.CheckDate);
                newInspectionManagement.CheckMan = inspectionManagement.CheckMan;
                newInspectionManagement.CompileMan = inspectionManagement.CompileMan;
                newInspectionManagement.CompileMan2 = inspectionManagement.CompileMan2;
                if (!string.IsNullOrEmpty(inspectionManagement.CompileDate))
                    newInspectionManagement.CompileDate = DateTime.Parse(inspectionManagement.CompileDate);
                newInspectionManagement.UnqualifiedReason = inspectionManagement.UnqualifiedReason;
                newInspectionManagement.AcceptanceCheckMan = inspectionManagement.AcceptanceCheckMan;
                newInspectionManagement.FileType = inspectionManagement.FileType;
                newInspectionManagement.CCUnitIds = inspectionManagement.CCUnitIds;
                newInspectionManagement.MainSendUnitId = inspectionManagement.MainSendUnitId;
                newInspectionManagement.AuditMan = inspectionManagement.AuditMan;
                newInspectionManagement.Status = inspectionManagement.Status;
                if (!string.IsNullOrEmpty(inspectionManagement.PlanComplateDate))
                    newInspectionManagement.PlanComplateDate = DateTime.Parse(inspectionManagement.PlanComplateDate);
                db.ProcessControl_InspectionManagement.InsertOnSubmit(newInspectionManagement);
                db.SubmitChanges();

                foreach (var InspectionDetail in inspectionManagement.inspectionManagementDetails)

                {
                    Model.ProcessControl_InspectionManagementDetail newInspectionDetail = new Model.ProcessControl_InspectionManagementDetail();
                    newInspectionDetail.InspectionDetailId = InspectionDetail.InspectionDetailId;
                    newInspectionDetail.InspectionId = newInspectionManagement.InspectionId;
                    newInspectionDetail.UnitWorkId = InspectionDetail.UnitWorkId;
                    newInspectionDetail.Branch = InspectionDetail.Branch;
                    newInspectionDetail.ControlPointType = InspectionDetail.ControlPointType;
                    if (!string.IsNullOrEmpty(InspectionDetail.CreateDate))
                        newInspectionDetail.CreateDate = DateTime.Parse(InspectionDetail.CreateDate);

                    db.ProcessControl_InspectionManagementDetail.InsertOnSubmit(newInspectionDetail);
                }
                db.SubmitChanges();
            }
            return true;
        }

        public static List<InspectionManagementDetail> getInspectionManagementDetail(string projectId, int index, int page, string state, string name)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                var dataList = from x in db.ProcessControl_InspectionManagementDetail
                               where x.ProcessControl_InspectionManagement.ProjectId == projectId  
                               select x;
                var listRes = dataList.Skip(index * page).Take(page);

                List<InspectionManagementDetail> list = new List<InspectionManagementDetail>();
                foreach(var item in listRes)
                {
                    InspectionManagementDetail detail = new InspectionManagementDetail() {
                    InspectionDetailId=item.InspectionDetailId,
                    UnitWorkId=item.UnitWorkId,
                    UnitWorkName=db.WBS_UnitWork.FirstOrDefault(x=>x.UnitWorkId== item.InspectionId).UnitWorkName,
                    ControlPointType= item.ControlPointType,
                    CreateDate =item.CreateDate.HasValue?item.CreateDate.Value.ToString("yyyy-MM-dd"):"",
                    Branch = item.Branch,
                    };
                    InspectionManagement inspectionManagement = new InspectionManagement();

                    inspectionManagement.InspectionId = item.ProcessControl_InspectionManagement.InspectionId;
                    inspectionManagement.ProjectId = item.ProcessControl_InspectionManagement.ProjectId;
                    inspectionManagement.ProjectName =  db.Base_Project.FirstOrDefault(x=>x.ProjectId== item.ProcessControl_InspectionManagement.ProjectId).ProjectName;
                    inspectionManagement.UnitId = item.ProcessControl_InspectionManagement.UnitId;
                    inspectionManagement.UnitName = db.Base_Unit.FirstOrDefault(x=>x.UnitId==item.ProcessControl_InspectionManagement.UnitId).UnitName;
                    inspectionManagement.CNProfessionalId = item.ProcessControl_InspectionManagement.CNProfessionalId;
                    if (!string.IsNullOrEmpty(inspectionManagement.CNProfessionalId))
                    {
                        inspectionManagement.CNProfessionalName = db.Base_CNProfessional.FirstOrDefault(x => x.CNProfessionalId == item.ProcessControl_InspectionManagement.CNProfessionalId).ProfessionalName;
                    }
                    inspectionManagement.InspectionCode = item.ProcessControl_InspectionManagement.InspectionCode;
                    inspectionManagement.UnitWorkId = item.ProcessControl_InspectionManagement.UnitWorkId;
                    if (!string.IsNullOrEmpty(inspectionManagement.UnitWorkId))
                    {
                        inspectionManagement.UnitWorkName = db.WBS_UnitWork.FirstOrDefault(x => x.UnitWorkId == item.ProcessControl_InspectionManagement.UnitWorkId).UnitWorkName;
                    }
                    inspectionManagement.Branch = item.ProcessControl_InspectionManagement.Branch;
                    inspectionManagement.ControlPointType = item.ProcessControl_InspectionManagement.ControlPointType;
                    inspectionManagement.AcceptanceSite = item.ProcessControl_InspectionManagement.AcceptanceSite;
                    inspectionManagement.IsOnceQualified = item.ProcessControl_InspectionManagement.IsOnceQualified.HasValue? item.ProcessControl_InspectionManagement.IsOnceQualified.Value.ToString():"";
                    inspectionManagement.InspectionDate = item.ProcessControl_InspectionManagement.InspectionDate.HasValue?"":"";
                    inspectionManagement.AttachUrl = item.ProcessControl_InspectionManagement.AttachUrl;
                    inspectionManagement.CheckDate = item.ProcessControl_InspectionManagement.CheckDate.HasValue ? item.ProcessControl_InspectionManagement.CheckDate.Value.ToString("yyyy-MM-dd"):"";
                    inspectionManagement.CheckMan = item.ProcessControl_InspectionManagement.CheckMan;
                    inspectionManagement.CheckMan = item.ProcessControl_InspectionManagement.CheckMan;
                    inspectionManagement.UnqualifiedReason = item.ProcessControl_InspectionManagement.UnqualifiedReason;
                    inspectionManagement.NoticeCode = item.ProcessControl_InspectionManagement.NoticeCode;
                    inspectionManagement.AcceptanceCheckMan = item.ProcessControl_InspectionManagement.AcceptanceCheckMan;
                    inspectionManagement.ParentDivisionProjectId = item.ProcessControl_InspectionManagement.ParentDivisionProjectId;
                    inspectionManagement.CompileMan = item.ProcessControl_InspectionManagement.CompileMan;
                    if (!string.IsNullOrEmpty(inspectionManagement.CompileMan))
                    {
                        inspectionManagement.CompileManName = db.Sys_User.FirstOrDefault(x => x.UserId == item.ProcessControl_InspectionManagement.CompileMan).UserName;  //item.ProcessControl_InspectionManagement.CompileManName;
                    }  inspectionManagement.CompileDate = item.ProcessControl_InspectionManagement.CompileDate.HasValue? item.ProcessControl_InspectionManagement.CompileDate.Value.ToString("yyyy-MM-dd"):"";
                    inspectionManagement.FileType = item.ProcessControl_InspectionManagement.FileType;
                    inspectionManagement.AttachUrl2 = item.ProcessControl_InspectionManagement.AttachUrl2;
                    inspectionManagement.MainSendUnitId = item.ProcessControl_InspectionManagement.MainSendUnitId;
                    if(!string.IsNullOrEmpty(inspectionManagement.MainSendUnitId))
                    inspectionManagement.MainSendUnitName = UnitService.getUnitNamesUnitIdsForApi(inspectionManagement.MainSendUnitId);//item.ProcessControl_InspectionManagement.MainSendUnitName;
                    inspectionManagement.CCUnitIds = item.ProcessControl_InspectionManagement.CCUnitIds;
                    if (!string.IsNullOrEmpty(inspectionManagement.CCUnitIds))
                    {
                        inspectionManagement.CCUnitName = UnitService.getUnitNamesUnitIdsForApi(inspectionManagement.CCUnitIds);//  item.ProcessControl_InspectionManagement.CCUnitName;
                    }
                    inspectionManagement.PlanComplateDate = item.ProcessControl_InspectionManagement.PlanComplateDate.HasValue? item.ProcessControl_InspectionManagement.PlanComplateDate.Value.ToString("yyyy-MM-dd"): "";

                    inspectionManagement.Status = item.ProcessControl_InspectionManagement.Status;
                    inspectionManagement.CompileMan2 = item.ProcessControl_InspectionManagement.CompileMan2;

                    inspectionManagement.AuditMan = item.ProcessControl_InspectionManagement.AuditMan;
                    if (!string.IsNullOrEmpty(inspectionManagement.AuditMan))
                    { inspectionManagement.AuditManName = db.Sys_User.FirstOrDefault(x => x.UserId == item.ProcessControl_InspectionManagement.AuditMan).UserName; }
                    detail.inspectionManagement = inspectionManagement;
                    list.Add(detail);
                }
                return list;
            }
        }
        public static List<InspectionManagement> getInspectionManagement(string projectId, int index, int page, string state, string name)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                var dataList = from x in db.ProcessControl_InspectionManagement
                               where x.ProjectId == projectId
                               select x;
                var listRes = dataList.Skip(index * page).Take(page);

                List<InspectionManagement> list = new List<InspectionManagement>();
                foreach (var item in listRes)
                {
                  
                    InspectionManagement inspectionManagement = new InspectionManagement();

                    inspectionManagement.InspectionId = item.InspectionId;
                    inspectionManagement.ProjectId = item.ProjectId;
                    inspectionManagement.ProjectName = db.Base_Project.FirstOrDefault(x => x.ProjectId == item.ProjectId).ProjectName;
                    inspectionManagement.UnitId = item.UnitId;
                    inspectionManagement.UnitName = db.Base_Unit.FirstOrDefault(x => x.UnitId == item.UnitId).UnitName;
                    inspectionManagement.CNProfessionalId = item.CNProfessionalId;
                    if (!string.IsNullOrEmpty(inspectionManagement.CNProfessionalId))
                    {
                        inspectionManagement.CNProfessionalName = db.Base_CNProfessional.FirstOrDefault(x => x.CNProfessionalId == item.CNProfessionalId).ProfessionalName;
                    }
                    inspectionManagement.InspectionCode = item.InspectionCode;
                    inspectionManagement.UnitWorkId = item.UnitWorkId;
                    if (!string.IsNullOrEmpty(inspectionManagement.UnitWorkId))
                    {
                        inspectionManagement.UnitWorkName = db.WBS_UnitWork.FirstOrDefault(x => x.UnitWorkId == item.UnitWorkId).UnitWorkName;
                    }
                    inspectionManagement.Branch = item.Branch;
                    inspectionManagement.ControlPointType = item.ControlPointType;
                    inspectionManagement.AcceptanceSite = item.AcceptanceSite;
                    inspectionManagement.IsOnceQualified = item.IsOnceQualified.HasValue ? item.IsOnceQualified.Value.ToString() : "";
                    inspectionManagement.InspectionDate = item.InspectionDate.HasValue ? "" : "";
                    inspectionManagement.AttachUrl = item.AttachUrl;
                    inspectionManagement.CheckDate = item.CheckDate.HasValue ? item.CheckDate.Value.ToString("yyyy-MM-dd") : "";
                    inspectionManagement.CheckMan = item.CheckMan;
                    inspectionManagement.CheckMan = item.CheckMan;
                    inspectionManagement.UnqualifiedReason = item.UnqualifiedReason;
                    inspectionManagement.NoticeCode = item.NoticeCode;
                    inspectionManagement.AcceptanceCheckMan = item.AcceptanceCheckMan;
                    inspectionManagement.ParentDivisionProjectId = item.ParentDivisionProjectId;
                    inspectionManagement.CompileMan = item.CompileMan;
                    if (!string.IsNullOrEmpty(inspectionManagement.CompileMan))
                    {
                        inspectionManagement.CompileManName = db.Sys_User.FirstOrDefault(x => x.UserId == item.CompileMan).UserName;  //item.CompileManName;
                    }
                    inspectionManagement.CompileDate = item.CompileDate.HasValue ? item.CompileDate.Value.ToString("yyyy-MM-dd") : "";
                    inspectionManagement.FileType = item.FileType;
                    inspectionManagement.AttachUrl2 = item.AttachUrl2;
                    inspectionManagement.MainSendUnitId = item.MainSendUnitId;
                    if (!string.IsNullOrEmpty(inspectionManagement.MainSendUnitId))
                        inspectionManagement.MainSendUnitName = UnitService.getUnitNamesUnitIdsForApi(inspectionManagement.MainSendUnitId);//item.MainSendUnitName;
                    inspectionManagement.CCUnitIds = item.CCUnitIds;
                    if (!string.IsNullOrEmpty(inspectionManagement.CCUnitIds))
                    {
                        inspectionManagement.CCUnitName = UnitService.getUnitNamesUnitIdsForApi(inspectionManagement.CCUnitIds);//  item.CCUnitName;
                    }
                    inspectionManagement.PlanComplateDate = item.PlanComplateDate.HasValue ? item.PlanComplateDate.Value.ToString("yyyy-MM-dd") : "";

                    inspectionManagement.Status = item.Status;
                    inspectionManagement.CompileMan2 = item.CompileMan2;

                    inspectionManagement.AuditMan = item.AuditMan;
                    if (!string.IsNullOrEmpty(inspectionManagement.AuditMan))
                    { inspectionManagement.AuditManName = db.Sys_User.FirstOrDefault(x => x.UserId == item.AuditMan).UserName; }
                   
                    list.Add(inspectionManagement);
                }
                return list;
            }
        }




        public static List<InspectionManagementDetail> getInspectionManagementDetailByinspectionId(string inspectionId)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                var dataList = from x in db.ProcessControl_InspectionManagementDetail
                               where x.InspectionId == inspectionId
                               select x;
                var listRes = dataList.ToList();

                List<InspectionManagementDetail> list = new List<InspectionManagementDetail>();
                foreach (var item in listRes)
                {
                    InspectionManagementDetail detail = new InspectionManagementDetail()
                    {
                        InspectionDetailId = item.InspectionDetailId,
                        UnitWorkId = item.UnitWorkId,
                        UnitWorkName = db.WBS_UnitWork.FirstOrDefault(x => x.UnitWorkId == item.InspectionId).UnitWorkName,
                        ControlPointType = item.ControlPointType,
                        CreateDate = item.CreateDate.HasValue ? item.CreateDate.Value.ToString("yyyy-MM-dd") : "",
                        Branch = item.Branch,
                    };
                    InspectionManagement inspectionManagement = new InspectionManagement();

                    inspectionManagement.InspectionId = item.ProcessControl_InspectionManagement.InspectionId;
                    inspectionManagement.ProjectId = item.ProcessControl_InspectionManagement.ProjectId;
                    inspectionManagement.ProjectName = db.Base_Project.FirstOrDefault(x => x.ProjectId == item.ProcessControl_InspectionManagement.ProjectId).ProjectName;
                    inspectionManagement.UnitId = item.ProcessControl_InspectionManagement.UnitId;
                    inspectionManagement.UnitName = db.Base_Unit.FirstOrDefault(x => x.UnitId == item.ProcessControl_InspectionManagement.UnitId).UnitName;
                    inspectionManagement.CNProfessionalId = item.ProcessControl_InspectionManagement.CNProfessionalId;
                    if (!string.IsNullOrEmpty(inspectionManagement.CNProfessionalId))
                    {
                        inspectionManagement.CNProfessionalName = db.Base_CNProfessional.FirstOrDefault(x => x.CNProfessionalId == item.ProcessControl_InspectionManagement.CNProfessionalId).ProfessionalName;
                    }
                    inspectionManagement.InspectionCode = item.ProcessControl_InspectionManagement.InspectionCode;
                    inspectionManagement.UnitWorkId = item.ProcessControl_InspectionManagement.UnitWorkId;
                    if (!string.IsNullOrEmpty(inspectionManagement.UnitWorkId))
                    {
                        inspectionManagement.UnitWorkName = db.WBS_UnitWork.FirstOrDefault(x => x.UnitWorkId == item.ProcessControl_InspectionManagement.UnitWorkId).UnitWorkName;
                    }
                    inspectionManagement.Branch = item.ProcessControl_InspectionManagement.Branch;
                    inspectionManagement.ControlPointType = item.ProcessControl_InspectionManagement.ControlPointType;
                    inspectionManagement.AcceptanceSite = item.ProcessControl_InspectionManagement.AcceptanceSite;
                    inspectionManagement.IsOnceQualified = item.ProcessControl_InspectionManagement.IsOnceQualified.HasValue ? item.ProcessControl_InspectionManagement.IsOnceQualified.Value.ToString() : "";
                    inspectionManagement.InspectionDate = item.ProcessControl_InspectionManagement.InspectionDate.HasValue ? "" : "";
                    inspectionManagement.AttachUrl = item.ProcessControl_InspectionManagement.AttachUrl;
                    inspectionManagement.CheckDate = item.ProcessControl_InspectionManagement.CheckDate.HasValue ? item.ProcessControl_InspectionManagement.CheckDate.Value.ToString("yyyy-MM-dd") : "";
                    inspectionManagement.CheckMan = item.ProcessControl_InspectionManagement.CheckMan;
                    inspectionManagement.CheckMan = item.ProcessControl_InspectionManagement.CheckMan;
                    inspectionManagement.UnqualifiedReason = item.ProcessControl_InspectionManagement.UnqualifiedReason;
                    inspectionManagement.NoticeCode = item.ProcessControl_InspectionManagement.NoticeCode;
                    inspectionManagement.AcceptanceCheckMan = item.ProcessControl_InspectionManagement.AcceptanceCheckMan;
                    inspectionManagement.ParentDivisionProjectId = item.ProcessControl_InspectionManagement.ParentDivisionProjectId;
                    inspectionManagement.CompileMan = item.ProcessControl_InspectionManagement.CompileMan;
                    if (!string.IsNullOrEmpty(inspectionManagement.CompileMan))
                    {
                        inspectionManagement.CompileManName = db.Sys_User.FirstOrDefault(x => x.UserId == item.ProcessControl_InspectionManagement.CompileMan).UserName;  //item.ProcessControl_InspectionManagement.CompileManName;
                    }
                    inspectionManagement.CompileDate = item.ProcessControl_InspectionManagement.CompileDate.HasValue ? item.ProcessControl_InspectionManagement.CompileDate.Value.ToString("yyyy-MM-dd") : "";
                    inspectionManagement.FileType = item.ProcessControl_InspectionManagement.FileType;
                    inspectionManagement.AttachUrl2 = item.ProcessControl_InspectionManagement.AttachUrl2;
                    inspectionManagement.MainSendUnitId = item.ProcessControl_InspectionManagement.MainSendUnitId;
                    if (!string.IsNullOrEmpty(inspectionManagement.MainSendUnitId))
                        inspectionManagement.MainSendUnitName = UnitService.getUnitNamesUnitIdsForApi(inspectionManagement.MainSendUnitId);//item.ProcessControl_InspectionManagement.MainSendUnitName;
                    inspectionManagement.CCUnitIds = item.ProcessControl_InspectionManagement.CCUnitIds;
                    if (!string.IsNullOrEmpty(inspectionManagement.CCUnitIds))
                    {
                        inspectionManagement.CCUnitName = UnitService.getUnitNamesUnitIdsForApi(inspectionManagement.CCUnitIds);//  item.ProcessControl_InspectionManagement.CCUnitName;
                    }
                    inspectionManagement.PlanComplateDate = item.ProcessControl_InspectionManagement.PlanComplateDate.HasValue ? item.ProcessControl_InspectionManagement.PlanComplateDate.Value.ToString("yyyy-MM-dd") : "";

                    inspectionManagement.Status = item.ProcessControl_InspectionManagement.Status;
                    inspectionManagement.CompileMan2 = item.ProcessControl_InspectionManagement.CompileMan2;

                    inspectionManagement.AuditMan = item.ProcessControl_InspectionManagement.AuditMan;
                    if (!string.IsNullOrEmpty(inspectionManagement.AuditMan))
                    { inspectionManagement.AuditManName = db.Sys_User.FirstOrDefault(x => x.UserId == item.ProcessControl_InspectionManagement.AuditMan).UserName; }
                    detail.inspectionManagement = inspectionManagement;
                    list.Add(detail);
                }
                return list;
            }
        }



    }
}