
using New_College.IServices;
using New_College.Model.Models;
using New_College.Services.BASE;
using New_College.IRepository.Base;
using Microsoft.AspNetCore.Mvc.RazorPages;
using New_College.Model.ViewModels;
using New_College.Model;
using System.Threading.Tasks;
using New_College.IRepository;
using LinqKit;
using System.Collections.Generic;
using System.Linq;

namespace New_College.Services
{
    public class V_ExaminationPolicyServices : BaseServices<V_ExaminationPolicy>, IV_ExaminationPolicyServices
    {
        private readonly IBaseRepository<V_ExaminationPolicy> _dal;
        private readonly ISysRegionRepository sysRegionRepository;
        public V_ExaminationPolicyServices(IBaseRepository<V_ExaminationPolicy> dal
            , ISysRegionRepository ISysRegionRepository)
        {
            this._dal = dal;
            sysRegionRepository = ISysRegionRepository;
            base.BaseDal = dal;
        }

        /// <summary>
        /// 获取分页
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<MessageModel<PageModel<ExaminationPolicyResult>>> GetExaminationPolicyByPage(ExaminationPolicySearchQuery query)
        {
            var wheres = PredicateBuilder.New<V_ExaminationPolicy>();
            wheres = wheres.And(x => x.IsDelete == false);
            if (query.AreaId > 0)
                wheres = wheres.And(x => x.AreaId == query.AreaId);
            var info = await _dal.QueryPage(wheres, query.PageIndex, query.PageSize);
            if (info.data.Count <= 0)
                return new MessageModel<PageModel<ExaminationPolicyResult>>()
                {
                    success = false,
                    msg = "获取失败"
                };
            var regioninfo = await sysRegionRepository.Query(x => x.ParentCode == "100000");
            PageModel<ExaminationPolicyResult> pageModel = new PageModel<ExaminationPolicyResult>() { };
            List<ExaminationPolicyResult> list = new List<ExaminationPolicyResult>() { };
            foreach (var item in info.data)
            {
                list.Add(new ExaminationPolicyResult()
                {
                    Id = item.Id,
                    Title = item.Title,
                    AreaName = regioninfo.Where(x => x.KeyId == item.AreaId).Select(x => x.RegionName)?.FirstOrDefault(),
                    ModifyTime = item.ModifyTime
                });
            }
            pageModel.data = list;
            pageModel.dataCount = info.dataCount;
            pageModel.page = info.page;
            pageModel.pageCount = info.pageCount;
            pageModel.PageSize = info.PageSize;
            return new MessageModel<PageModel<ExaminationPolicyResult>>()
            {
                success = true,
                msg = "获取成功",
                response = pageModel
            };
        }

        /// <summary>
        /// 获取单个
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<MessageModel<ExaminationPolicyOneResult>> GetExaminationPolicyOne(IdQuery query)
        {
            var info = await _dal.QueryById(query.Id);
            if (info == null)
                return new MessageModel<ExaminationPolicyOneResult>() { success = false, msg = "获取失败" };

            return new MessageModel<ExaminationPolicyOneResult>()
            {
                success = true,
                msg = "获取成功",
                response = new ExaminationPolicyOneResult()
                {
                    Id = info.Id,
                    Content = info.Content,
                    Img = info.Img,
                    AreaId = info.AreaId,
                    Title = info.Title
                }
            };

        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<MessageModel<bool>> AddExaminationPolicy(ExaminationPolicyQuery query)
        {
            var result = await _dal.Add(new V_ExaminationPolicy()
            {
                AreaId = query.AreaId,
                Content = query.Content,
                Img = query.Img,
                Title = query.Title,
                AreaName = query.AreaName
            });
            if (result > 0)
            {
                return new MessageModel<bool>() { success = true, msg = "添加成功", response = true };
            }
            else
            {
                return new MessageModel<bool>() { success = false, msg = "添加失败", response = false };
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<MessageModel<bool>> UpdateExaminationPolicy(ExaminationPolicyQuery query)
        {
            var info = await _dal.QueryById(query.Id);
            if (info == null)
                return new MessageModel<bool>() { success = false, msg = "修改内容不存在", response = false };
            info.AreaId = query.AreaId;
            info.Content = query.Content;
            info.Img = query.Img;
            info.Title = query.Title;
            info.AreaName = query.AreaName;
            var result = await _dal.Update(info);
            if (result)
            {
                return new MessageModel<bool>() { success = true, msg = "修改成功", response = true };
            }
            else
            {
                return new MessageModel<bool>() { success = false, msg = "修改失败", response = false };
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<MessageModel<bool>> DeleteExaminationPolicy(IdQuery query)
        {
            if (query.Ids == null || query.Ids.Count <= 0)
                return new MessageModel<bool>() { success = false, msg = "传入为空", response = false };
            var info = await _dal.Query(x => x.IsDelete == false && query.Ids.Contains(x.Id));
            if (info.Count <= 0)
                return new MessageModel<bool>() { success = false, msg = "没有可被删除项", response = false };
            foreach (var item in info)
            {
                item.IsDelete = true;
            }
            var result = await _dal.Update(info);
            if (result)
            {
                return new MessageModel<bool>() { success = true, msg = "删除成功", response = true };
            }
            else
            {
                return new MessageModel<bool>() { success = false, msg = "删除失败", response = false };
            }
        }

        /// <summary>
        /// 获取考试时间
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<MessageModel<ExaminationPolicyOneDetail>> GetExaminationPolicyDetail(ExaminationPolicyAreaQuery query)
        {
            var info = (await _dal.Query(x => x.IsDelete == false && x.AreaId == query.AreaId)).FirstOrDefault();
            if (info == null)
                return new MessageModel<ExaminationPolicyOneDetail>() { success = false, msg = "获取失败" };
            return new MessageModel<ExaminationPolicyOneDetail>()
            {
                success = true,
                msg = "获取成功",
                response = new ExaminationPolicyOneDetail()
                {
                    Id = info.Id,
                    Content = info.Content,
                    Img = info.Img,
                    Title = info.Title
                }
            };
        }
    }
}