using MadRunFabric.Common;
using MadRunFabric.Common.Options;
using MadRunFabric.Model;
using MadRunFabric.Model.MessageApi;
using MessageApi.IRepositories;
using MessageApi.Model;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace MessageApi.Repositories
{
    public class Wo_EquipmentRunningRepository : BaseRepository<Wo_EquipmentRunning, string>, IWo_EquipmentRunningRepository
    {
        protected readonly ILogger<BaseRepository<Wo_EquipmentRunning, string>> _logger;
        protected readonly IMongoCollection<Wo_Order_User_Map> _collection_wo_order_user_map;
        public Wo_EquipmentRunningRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Wo_EquipmentRunning, string>> logger) : base(settings, logger)
        {
            _collection_wo_order_user_map = _context.GetCollection<Wo_Order_User_Map>();
            _logger = logger;
        }

        /// <summary>
        /// Linq 关联查询 分页 设备运行工单 - 不用
        /// </summary>
        /// <returns></returns>
        public IEnumerable<WoWorkOrderListDto> GetList(string usercode)
        {
            var query =
                from repair in _collection.AsQueryable()
                join order_user in _collection_wo_order_user_map.AsQueryable() on repair.wocode equals order_user.wocode into order_userDefa
                where repair.isdelete == 0
                select new WoWorkOrderListDto
                {
                    id = repair.id,
                    //equipmentid = repair.equipmentid,
                    wocode = repair.wocode,
                    wostate = repair.wostate,
                    content = repair.checkcontent,
                    createby = repair.createby,
                    createtime = repair.createtime,
                    readby = order_userDefa != null && order_userDefa.Count() > 0 ? order_userDefa.First().readby : "",
                    isread = order_userDefa != null && order_userDefa.Count() > 0 ? order_userDefa.First().isread : 0
                };
            #region 查询条件
            if (!string.IsNullOrEmpty(usercode))
                query = query.Where(it => it.readby == usercode);
            query = query.Where(it => it.isread == 0);
            #endregion
            var list = query.OrderByDescending(it => it.createtime);
            return list;
        }
    }
}
