using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace BLL
{
    /// 
    /// 机具报验
    /// 
    public static class InspectionMachineService
    {
        public static Model.SUBQHSEDB db = Funs.DB;
        /// 
        /// 记录数
        /// 
        private static int count
        {
            get;
            set;
        }
        /// 
        /// 获取分页列表
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static IEnumerable GetListData(string projectId, string unitId, string cnProfessionalId, string inspectionType, int startRowIndex, int maximumRows)
        {
            IQueryable q = from x in db.Comprehensive_InspectionMachine
                                                                  where x.ProjectId == projectId
                                                                  orderby x.InspectionMachineCode descending
                                                                  select x;
            if (unitId != "0")
            {
                q = q.Where(e => e.UnitId == unitId);
            }
            if (cnProfessionalId != "0")
            {
                q = q.Where(e => e.CNProfessionalId == cnProfessionalId);
            }
            if (inspectionType != "0")
            {
                q = q.Where(e => e.InspectionType == inspectionType);
            }
            count = q.Count();
            if (count == 0)
            {
                return new object[] { "" };
            }
            return from x in q.Skip(startRowIndex).Take(maximumRows)
                   select new
                   {
                       x.InspectionMachineId,
                       x.ProjectId,
                       UnitName = (from y in db.Base_Unit where y.UnitId == x.UnitId select y.UnitName).FirstOrDefault(),
                       x.InspectionMachineCode,
                       x.InspectionMachineName,
                       x.SpecificationModel,
                       x.NextTestDate,
                       x.TestCycle,
                       x.IsCheckOK,
                       IsVerification = x.IsVerification == true ? "是" : "否",
                       x.InspectionDate,
                       x.AttachUrl,
                       x.CNProfessionalId,
                       x.InspectionType,
                       x.UnitsCount,
                       x.LeaveDate,
                       ProfessionalName = (from y in db.Base_CNProfessional where y.CNProfessionalId == x.CNProfessionalId select y.ProfessionalName).FirstOrDefault(),
                   };
        }
        /// 
        /// 获取分页列表数
        /// 
        /// 
        /// 
        /// 
        public static int GetListCount(string projectId, string unitId, string cnProfessionalId, string inspectionType)
        {
            return count;
        }
        /// 
        /// 根据主键获取机具校验
        /// 
        /// 
        /// 
        public static Model.Comprehensive_InspectionMachine GetInspectionMachineById(string inspectionMachineId)
        {
            return Funs.DB.Comprehensive_InspectionMachine.FirstOrDefault(e => e.InspectionMachineId == inspectionMachineId);
        }
        /// 
        /// 添加机具校验
        /// 
        /// 
        public static void AddInspectionMachine(Model.Comprehensive_InspectionMachine inspectionMachine)
        {
            Model.SUBQHSEDB db = Funs.DB; ;
            Model.Comprehensive_InspectionMachine newInspectionMachine = new Model.Comprehensive_InspectionMachine();
            newInspectionMachine.InspectionMachineId = inspectionMachine.InspectionMachineId;
            newInspectionMachine.ProjectId = inspectionMachine.ProjectId;
            newInspectionMachine.UnitId = inspectionMachine.UnitId;
            newInspectionMachine.InspectionMachineCode = inspectionMachine.InspectionMachineCode;
            newInspectionMachine.InspectionMachineName = inspectionMachine.InspectionMachineName;
            newInspectionMachine.SpecificationModel = inspectionMachine.SpecificationModel;
            newInspectionMachine.IsCheckOK = inspectionMachine.IsCheckOK;
            newInspectionMachine.InspectionType = inspectionMachine.InspectionType;
            newInspectionMachine.NextTestDate = inspectionMachine.NextTestDate;
            newInspectionMachine.TestCycle = inspectionMachine.TestCycle;
            newInspectionMachine.IsVerification = inspectionMachine.IsVerification;
            newInspectionMachine.InspectionDate = inspectionMachine.InspectionDate;
            newInspectionMachine.AttachUrl = inspectionMachine.AttachUrl;
            newInspectionMachine.CNProfessionalId = inspectionMachine.CNProfessionalId;
            newInspectionMachine.CompileMan = inspectionMachine.CompileMan;
            newInspectionMachine.CompileDate = inspectionMachine.CompileDate;
            newInspectionMachine.CompileDate = inspectionMachine.CompileDate;
            newInspectionMachine.IsOnSite = inspectionMachine.IsOnSite;
            newInspectionMachine.UnitsCount = inspectionMachine.UnitsCount;
            newInspectionMachine.LeaveDate = inspectionMachine.LeaveDate;
            newInspectionMachine.Status = inspectionMachine.Status;
            db.Comprehensive_InspectionMachine.InsertOnSubmit(newInspectionMachine);
            db.SubmitChanges();
        }
        /// 
        /// 修改机具校验
        /// 
        /// 
        public static void UpdateInspectionMachine(Model.Comprehensive_InspectionMachine inspectionMachine)
        {
            Model.SUBQHSEDB db = Funs.DB;
            Model.Comprehensive_InspectionMachine newInspectionMachine = db.Comprehensive_InspectionMachine.FirstOrDefault(e => e.InspectionMachineId == inspectionMachine.InspectionMachineId);
            if (newInspectionMachine != null)
            {
                newInspectionMachine.ProjectId = inspectionMachine.ProjectId;
                newInspectionMachine.UnitId = inspectionMachine.UnitId;
                newInspectionMachine.InspectionMachineCode = inspectionMachine.InspectionMachineCode;
                newInspectionMachine.InspectionMachineName = inspectionMachine.InspectionMachineName;
                newInspectionMachine.SpecificationModel = inspectionMachine.SpecificationModel;
                newInspectionMachine.IsCheckOK = inspectionMachine.IsCheckOK;
                newInspectionMachine.InspectionType = inspectionMachine.InspectionType;
                newInspectionMachine.NextTestDate = inspectionMachine.NextTestDate;
                newInspectionMachine.TestCycle = inspectionMachine.TestCycle;
                newInspectionMachine.IsVerification = inspectionMachine.IsVerification;
                newInspectionMachine.InspectionDate = inspectionMachine.InspectionDate;
                newInspectionMachine.AttachUrl = inspectionMachine.AttachUrl;
                newInspectionMachine.CNProfessionalId = inspectionMachine.CNProfessionalId;
                newInspectionMachine.IsOnSite = inspectionMachine.IsOnSite;
                newInspectionMachine.UnitsCount = inspectionMachine.UnitsCount;
                newInspectionMachine.LeaveDate = inspectionMachine.LeaveDate;
                newInspectionMachine.Status = inspectionMachine.Status;
                db.SubmitChanges();
            }
        }
        /// 
        /// 根据主键删除机具校验
        /// 
        /// 
        public static void DeleteInspectionMachine(string inspectionMachineId)
        {
            Model.SUBQHSEDB db = Funs.DB;
            Model.Comprehensive_InspectionMachine inspectionMachine = db.Comprehensive_InspectionMachine.FirstOrDefault(e => e.InspectionMachineId == inspectionMachineId);
            if (inspectionMachine != null)
            {
                if (!string.IsNullOrEmpty(inspectionMachine.AttachUrl))
                {
                    BLL.UploadAttachmentService.DeleteFile(Funs.RootPath, inspectionMachine.AttachUrl);//删除附件
                }
                db.Comprehensive_InspectionMachine.DeleteOnSubmit(inspectionMachine);
                db.SubmitChanges();
            }
        }
    }
}