using Masuit.Tools;
using Masuit.Tools.Models;
using PVDEMCS.Common;
using PVDEMCS.Common.Constant;
using PVDEMCS.Common.DI;
using PVDEMCS.Services.Models;
using PVDEMCS.Services.Repositories.Entities;

namespace PVDEMCS.Services.Repositories
{
    /// <summary>
    /// 设备信息 数据层 实现
    /// </summary>
    internal class EquipmentRepository : IEquipmentRepository
    {
        /// <summary>
        /// 获取设备分页列表
        /// </summary>
        /// <param name="equipmentName">设备名称</param>
        /// <param name="equipmentCode">设备编号</param>
        /// <param name="equipmentType">设备类型</param>
        /// <param name="activated">是否启用</param>
        /// <param name="page">当前页</param>
        /// <param name="size">页大小</param>
        /// <returns></returns>
        public Result<PagedList<EquipmentInfo>> GetEquipmentPageList(string equipmentName, string equipmentCode, string equipmentType, bool? activated, int page, int size)
        {
            using (var context = new EFContext())
            {
                var query = QueryEquipment(equipmentName, equipmentCode, equipmentType, activated, context);

                var result = query.OrderBy(f => f.EquipmentCode).ToPagedList(page, size);

                return new Result<PagedList<EquipmentInfo>>(result);

            }
        }

        /// <summary>
        /// 获取设备列表
        /// </summary>
        /// <param name="equipmentName">设备名称</param>
        /// <param name="equipmentCode">设备编号</param>
        /// <param name="equipmentType">设备类型</param>
        /// <param name="activated">是否启用</param>
        public Result<List<EquipmentInfo>> GetEquipmentList(string equipmentName, string equipmentCode, string equipmentType, bool? activated)
        {
            using (var context = new EFContext())
            {
                var query = QueryEquipment(equipmentName, equipmentCode, equipmentType, activated, context);

                var result = query.OrderBy(f => f.EquipmentCode).ToList();

                return new Result<List<EquipmentInfo>>(result);

            }
        }

        /// <summary>
        /// 获取设备明显
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Result<EquipmentInfoEntity> GetEquipmentDetail(string id)
        {
            using (var context = new EFContext())
            {
                var result = new Result<EquipmentInfoEntity>();

                var entity = context.EquipmentInfos.Where(f => !f.IsDelete && f.Id == id).FirstOrDefault();
                if (entity == null)
                {
                    result.Message = $"没有找到Id:【{id}】的数据";
                    return result;
                }

                result.Content = entity;

                return result;
            }
        }

        private IQueryable<EquipmentInfo> QueryEquipment(string equipmentName, string equipmentCode, string equipmentType, bool? activated, EFContext context)
        {
            var query = context.EquipmentInfos.Where(f => !f.IsDelete).Select(f => new EquipmentInfo
            {
                Id = f.Id,
                EquipmentCode = f.EquipmentCode,
                EquipmentName = f.EquipmentName,
                EquipmentType = f.EquipmentType,
                Activated = f.Activated,
                OrdrNo = f.OrdrNo,
                Model = f.Model,
                IsAlarm = f.IsAlarm,
                Remark = f.Remark

            });

            if (!equipmentCode.IsNullOrEmpty())
            {
                query = query.Where(f => f.EquipmentCode.StartsWith(equipmentCode));
            }
            if (!equipmentName.IsNullOrEmpty())
            {
                query = query.Where(f => f.EquipmentName.StartsWith(equipmentName));
            }
            if (!equipmentType.IsNullOrEmpty())
            {
                query = query.Where(f => f.EquipmentType == equipmentType);
            }
            if (activated.HasValue)
            {
                query = query.Where(f => f.Activated == activated.Value);
            }

            return query;
        }

        /// <summary>
        /// 添加设备信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Result AddEquipment(EquipmentInfoEntity entity)
        {
            using (var context = new EFContext())
            {
                var result = new Result();

                var query = context.EquipmentInfos.Where(f => !f.IsDelete);
                if (query.Where(f => f.EquipmentCode == entity.EquipmentCode).FirstOrDefault() != null)
                {
                    result.Message = $"EquipmentCode:【{entity.EquipmentCode}】已存在";
                    return result;
                }
                if (query.Where(f => f.EquipmentName == entity.EquipmentName).FirstOrDefault() != null)
                {
                    result.Message = $"EquipmentName:【{entity.EquipmentName}】已存在";
                    return result;
                }

                entity.Create();
                context.EquipmentInfos.Add(entity);
                context.SaveChanges();

                return result;
            }
        }

        /// <summary>
        /// 更新设备信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Result UpdateEquipment(EquipmentInfoEntity entity)
        {
            using (var context = new EFContext())
            {
                var result = new Result();

                var query = context.EquipmentInfos.Where(f => !f.IsDelete);
                var update = query.Where(f => f.Id == entity.Id).FirstOrDefault();
                if (update == null)
                {
                    result.Message = $"没有找到Id:【{entity.Id}】的数据";
                    return result;
                }
                if (query.Where(f => f.EquipmentCode == entity.EquipmentCode && f.Id != entity.Id).FirstOrDefault() != null)
                {
                    result.Message = $"EquipmentCode:【{entity.EquipmentCode}】已存在";
                    return result;
                }
                if (query.Where(f => f.EquipmentName == entity.EquipmentName && f.Id != entity.Id).FirstOrDefault() != null)
                {
                    result.Message = $"EquipmentName:【{entity.EquipmentName}】已存在";
                    return result;
                }

                update.EquipmentCode = entity.EquipmentCode;
                update.EquipmentName = entity.EquipmentName;
                update.Activated = entity.Activated;
                update.OrdrNo = entity.OrdrNo;
                update.Remark = entity.Remark;
                update.Model = entity.Model;
                update.IsAlarm = entity.IsAlarm;
                update.Update();
                context.SaveChanges();

                return result;
            }
        }

        /// <summary>
        /// 删除设备信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Result DeleteEquipment(string id)
        {
            using (var context = new EFContext())
            {
                var result = new Result();

                var query = context.EquipmentInfos.Where(f => !f.IsDelete);
                var entity = query.Where(f => f.Id == id).FirstOrDefault();
                if (entity == null)
                {
                    result.Message = $"没有找到Id:【{id}】的数据";
                    return result;
                }
                entity.Delete();
                context.SaveChanges();

                return result;
            }
        }
    }
}
