using Siger.Middlelayer.Common;
using Siger.Middlelayer.Repository.Entities;
using Siger.Middlelayer.Repository.Repositories.Interface;
using Siger.Middlelayer.Repository.Response;
using System.Linq;

namespace Siger.Middlelayer.Repository.Repositories
{
    internal class SigerProjectSectionRepository : ApiConfigRepositoryBase<siger_project_section>, ISigerProjectSectionRepository
    {
        private readonly ApiConfigDbContext _context;
        public SigerProjectSectionRepository(ApiConfigDbContext context) : base(context)
        {
            _context = context;
        }

        public bool VerifyDepartment(int userMid, int section)
        {

            ////var user = _context.siger_user.FirstOrDefault(f => f.id == userMid);
            ////if (user == null)
            ////    return false;
            ////if (user.type == (int)UserType.User)
            ////{
            ////    var query = from f in _context.siger_project_user
            ////                join s in _context.siger_project_section on f.sectionid equals s.id
            ////                where f.mid == userMid && f.sectionid == section
            ////                select new ResponseIdName
            ////                {
            ////                    id = s.id,
            ////                    name = s.title
            ////                };
            ////    return query.Count() > 0;
            ////}
            //管理员 允许
            return true;
        }

        public bool VerifyDepartment(int userMid, string name)
        {
            var user = _context.siger_user.FirstOrDefault(f => f.id == userMid);
            if (user == null)
                return false;
            if (user.type == (int)UserType.User)
            {
                var query = from f in _context.siger_project_user
                            join s in _context.siger_project_section on f.sectionid equals s.id
                            where f.mid == userMid && s.title == name
                            select new ResponseIdName
                            {
                                id = s.id,
                                name = s.title
                            };
                return query.Count() > 0;
            }
            //管理员 允许
            return true;
        }
    }
}
