using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EmitMapper;
using NPOI.SS.Formula.Functions;
namespace BLL
{
    public static class APIHazardRegisterService
    {
        /// 
        /// 根据HazardRegisterId获取风险巡检信息
        /// 
        /// 
        /// 
        public static Model.HazardRegisterItem getHazardRegisterByHazardRegisterId(string hazardRegisterId)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                var getHazardRegister = db.View_Hazard_HazardRegister.FirstOrDefault(x => x.HazardRegisterId == hazardRegisterId);
                return ObjectMapperManager.DefaultInstance.GetMapper().Map(getHazardRegister);
            }
        }
        /// 
        /// 根据projectId、states获取风险信息(状态 1:待整改;2:已整改,待确认;3:已确认,即已闭环;4:已作废)
        /// 
        /// 
        /// 
        /// 
        public static List getHazardRegisterByProjectIdStates(string projectId, string states, int type ,int pageIndex)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                var hazardRegisters = (from x in db.View_Hazard_HazardRegister
                                       where x.ProjectId == projectId && x.Type == type && (x.States == states || states == null)
                                       orderby x.CheckTime descending
                                       select x).Skip(Funs.PageSize * (pageIndex - 1)).Take(Funs.PageSize).ToList();
                return ObjectMapperManager.DefaultInstance.GetMapper, List>().Map(hazardRegisters.ToList());
            }
        }
        /// 
        /// 保存HazardRegister
        /// 
        /// 
        /// 
        public static void SaveHazardRegister(Model.HazardRegisterItem hazardRegister)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                Model.HSSE_Hazard_HazardRegister newHazardRegister = new Model.HSSE_Hazard_HazardRegister
                {
                    HazardRegisterId = hazardRegister.HazardRegisterId,
                    HazardCode = hazardRegister.HazardCode,
                    RegisterDef = hazardRegister.RegisterDef,
                    Rectification = hazardRegister.Rectification,
                    Place = hazardRegister.Place,
                    ResponsibleUnit = hazardRegister.ResponsibleUnit,
                    ProjectId = hazardRegister.ProjectId,
                    States = hazardRegister.States,
                    IsEffective = "1",
                    ResponsibleMan = hazardRegister.ResponsibleMan,
                    CheckManId = hazardRegister.CheckManId,
                    CheckTime = hazardRegister.CheckTime,
                    RectificationPeriod = hazardRegister.RectificationPeriod,
                    ImageUrl = hazardRegister.ImageUrl,
                    RectificationImageUrl = hazardRegister.RectificationImageUrl,
                    RectificationTime = hazardRegister.RectificationTime,
                    ConfirmMan = hazardRegister.ConfirmMan,
                    ConfirmDate = hazardRegister.ConfirmDate,
                    HandleIdea = hazardRegister.HandleIdea,
                    CutPayment = hazardRegister.CutPayment,
                    ProblemTypes = hazardRegister.ProblemTypes,
                    RegisterTypesId = hazardRegister.RegisterTypesId,
                    CheckCycle = hazardRegister.CheckCycle,
                    SafeSupervisionIsOK = hazardRegister.SafeSupervisionIsOK,
                    IsWx = "Y",
                    CCManIds = hazardRegister.CCManIds,
                    Requirements = hazardRegister.Requirements,
                };
                var RegisterTypesDt = db.HSSE_Hazard_HazardRegisterTypes.FirstOrDefault(x => x.RegisterTypesId == newHazardRegister.RegisterTypesId);
                if (RegisterTypesDt != null && RegisterTypesDt.HazardRegisterType == "4")
                {
                    newHazardRegister.Type = 1; //常规巡检
                }
                else
                { 
                    newHazardRegister.Type = 0;  //日常巡检
                }
                var isUpdate = db.HSSE_Hazard_HazardRegister.FirstOrDefault(x => x.HazardRegisterId == newHazardRegister.HazardRegisterId);
                if (isUpdate == null)
                {
                    newHazardRegister.RegisterDate = DateTime.Now;
                    newHazardRegister.CheckTime = DateTime.Now;
                    if (string.IsNullOrEmpty(newHazardRegister.HazardRegisterId))
                    {
                        newHazardRegister.HazardRegisterId = SQLHelper.GetNewID();
                    }
                     db.HSSE_Hazard_HazardRegister.InsertOnSubmit(newHazardRegister);
                }
                else
                {
                    if (newHazardRegister.States == "2")
                    {
                        isUpdate.RectificationTime = DateTime.Now;
                        isUpdate.Rectification = newHazardRegister.Rectification;
                        isUpdate.RectificationImageUrl = newHazardRegister.RectificationImageUrl;
                    }
                    else
                    {
                        isUpdate.ConfirmDate = DateTime.Now;
                        isUpdate.ConfirmMan = newHazardRegister.ConfirmMan;
                        isUpdate.HandleIdea = newHazardRegister.HandleIdea;
                        isUpdate.SafeSupervisionIsOK = newHazardRegister.SafeSupervisionIsOK;
                    }
                    isUpdate.States = newHazardRegister.States;
                }
                db.SubmitChanges();
                if (hazardRegister.States == Const.State_1)
                {
                    APICommonService.SendSubscribeMessage(hazardRegister.ResponsibleMan, "安全巡检问题待整改", hazardRegister.CheckManName, string.Format("{0:yyyy-MM-dd HH:mm:ss}", hazardRegister.CheckTime));
                }
                else if (hazardRegister.States == Const.State_2)
                {
                    APICommonService.SendSubscribeMessage(hazardRegister.CheckManId, "安全巡检待复查验收", hazardRegister.ResponsibilityManName, string.Format("{0:yyyy-MM-dd HH:mm:ss}", hazardRegister.RectificationTime));
                }
            }
        }
        /// 
        /// 安全巡检
        /// 
        /// 
        /// 
        public static List getHazardRegisterByProjectIdStates(int pageIndex)
        {
            string unitName = null, unitId = null;
            var getUnit = CommonService.GetIsThisUnit();
            if (getUnit != null)
            {
                unitId = getUnit.UnitId;
                unitName = getUnit.UnitName;
            }
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                var hazardRegisters = (from x in db.HSSE_Hazard_HazardRegister
                                       join y in db.Base_Project on x.ProjectId equals y.ProjectId
                                       join u in db.Base_Unit on x.ResponsibleUnit equals u.UnitId
                                       join user in db.Sys_User on x.ResponsibleMan equals user.UserId
                                       where x.ProjectId != null && x.States != null
                                       select new Model.HazardRegisterItem
                                       {
                                           HazardRegisterId = x.HazardRegisterId,
                                           ProjectId = x.ProjectId,
                                           ProjectName = y.ProjectName,
                                           ProjectAddress = y.ProjectAddress,
                                           UnitId = unitId,
                                           UnitName = unitName,
                                           PUnitId = y.UnitId,
                                           PUnitName = db.Base_Unit.First(U => y.UnitId == U.UnitId).UnitName,
                                           States = x.States,
                                           WorkAreaName = db.WBS_UnitWork.First(w => x.Place == w.UnitWorkId).UnitWorkName,
                                           CheckTime = x.CheckTime,
                                           RegisterDef = x.RegisterDef,
                                           RegisterTypesName = db.HSSE_Hazard_HazardRegisterTypes.First(T => x.RegisterTypesId == T.RegisterTypesId).RegisterTypesName,
                                           Rectification = x.Rectification,
                                           ResponsibilityUnitName = db.Base_Unit.First(U => x.ResponsibleUnit == U.UnitId).UnitName,
                                           ResponsibilityManName = db.Sys_User.First(User => x.ResponsibleMan == User.UserId).UserName,
                                           RectificationTime = x.RectificationTime,
                                           Risk_Level = x.Risk_Level,
                                       }).OrderBy(x => x.CheckTime).Skip(Funs.PageSize * (pageIndex - 1)).Take(Funs.PageSize).ToList();
                return hazardRegisters;
            }
        }
    }
}