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

namespace EquipmentApi.Repositories
{
    /// <summary>
    /// 评价
    /// </summary>
    public class Wo_Repair_EvaluateRepository : BaseRepository<Wo_Repair_Evaluate, string>, IWo_Repair_EvaluateRepository
    {
        protected readonly ILogger<BaseRepository<Wo_Repair_Evaluate, string>> _logger;
        protected readonly IMongoCollection<Sys_User_Account> _collection_sys_user_account;
        public Wo_Repair_EvaluateRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Wo_Repair_Evaluate, string>> logger) : base(settings, logger)
        {
            _collection_sys_user_account = _context.GetCollection<Sys_User_Account>();
            _logger = logger;
        }

        /// <summary>
        /// Linq 关联查询 分页
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="provincecode"></param>
        /// <param name="citycode"></param>
        /// <param name="projectid"></param>
        /// <param name="protype"></param>
        /// <param name="systemid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IEnumerable<object> GetListsByPage(string stime, string etime, int ismyd, int pageindex, int pagesize, out int recordCount)
        {
            //关联查询
            var query = from p in _collection.AsQueryable()
                        //join usr in _collection_sys_user_account.AsQueryable() on p.createuser equals usr.usercode into userinfo
                        where p.isdelete == 0
                        orderby p.createtime descending
                        select new
                        {
                            p.id,
                            p.evaluate,
                            p.label,
                            p.score,
                            p.wocode,
                            p.createuser,
                            //createusername = userinfo.Where(c => c.delete_flag == false).First().username ?? "",
                            p.createusername,
                            p.createtime,
                            p.isdelete
                        };
            #region 查询条件

            if (ismyd > 0)
            {
                if (ismyd == 1)
                {
                    query = query.Where(it => it.score >= 3);
                }
                else
                {
                    query = query.Where(it => it.score < 3);
                }
            }
            if (!string.IsNullOrEmpty(stime))
                query = query.Where(it => it.createtime >= DateTime.Parse(stime + " 00:00:00"));
            if (!string.IsNullOrEmpty(etime))
                query = query.Where(it => it.createtime <= DateTime.Parse(etime + " 23:59:59"));

            #endregion

            recordCount = query.Count();
            var list = query.Skip((pageindex - 1) * pagesize).Take(pagesize);
            return list;
        }

    }
}
