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

namespace EquipmentApi.Repositories
{
    public class Sys_User_AccountRepository : BaseRepository<Sys_User_Account, string>, ISys_User_AccountRepository
    {
        protected readonly ILogger<BaseRepository<Sys_User_Account, string>> _logger;
        protected readonly IMongoCollection<Sys_Role_Info> _collection_sysroleinfo;
        protected readonly IConfiguration _config;

        public Sys_User_AccountRepository(
            IOptions<MongodbOptions> settings,
            ILogger<BaseRepository<Sys_User_Account, string>> logger, IConfiguration config) : base(settings, logger)
        {
            _logger = logger;
            _collection_sysroleinfo = _context.GetCollection<Sys_Role_Info>();
            _config = config;
        }

        /// <summary>
        /// Linq 关联查询详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object GetUserInfo(string usercode)
        {
            //关联查询 postlist { get; set; } remark { get; set; }
        var querys =
                from usa in _collection.AsQueryable()
                join role in _collection_sysroleinfo.AsQueryable() on usa.role_id equals role.id into roleinfo
                where usa.delete_flag == false && usa.usercode == usercode
                orderby usa.create_time descending
                select new
                {
                    usa.id,
                    usa.username,
                    usa.usercode,
                    usa.mobile,
                    usa.type,
                    usa.role_id,
                    role_name = roleinfo.Count() > 0 ? roleinfo.First().role_name : null,
                    role_code = roleinfo.Count() > 0 ? roleinfo.First().role_code : null,
                    usa.seat_level,
                    usa.postlist,
                    usa.projectlist,
                    usa.remark
                };
            var userinfo = querys.FirstOrDefault();
            if (userinfo != null)
            {
                string rd = _config["roledic:" + userinfo.role_code];
                int level = 0;
                int.TryParse(rd, out level);
                //switch (userinfo.role_code)
                //{
                //    case "YWYG": level = 1; break;
                //    case "BFR": level = 1; break;
                //    case "YWZG": level = 2; break;
                //    case "YWFJL": level = 3; break;
                //    case "YWJL": level = 4; break;
                //    case "YWZJ": level = 5; break;
                //    case "CJGL": level = 6; break;
                //    case "XTWH": level = 7; break;
                //}
                int isallproject = 0;
                string allprojectrole = _config["allprojectrole"];
                if (allprojectrole.Split("|").Contains(userinfo.role_code) || userinfo.type == 2)
                {
                    isallproject = 1;
                }
                var model = new
                {
                    id = userinfo.id,
                    username = userinfo.username,
                    usercode = userinfo.usercode,
                    mobile = userinfo.mobile,
                    level = level,
                    type = userinfo.type,
                    role_id = userinfo.role_id,
                    role_name = userinfo.role_name,
                    role_code = userinfo.role_code,
                    isallproject = isallproject,
                    projectlist = userinfo.projectlist,
                    postlist = userinfo.postlist,
                    remark=userinfo.remark
                };
                return model;
            }
            return null;
        }
    }
}
