using AutoMapper.Internal.Mappers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Shentun.Peis.GuideTypes;
using Shentun.Peis.HelperDto;
using Shentun.Peis.Models;
using Shentun.Peis.Sexs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
namespace Shentun.Peis.MaritalStatuses
{
    /// 
    /// 婚姻状况
    /// 
    [ApiExplorerSettings(GroupName = "Work")]
    [Authorize]
    public class MaritalStatusesAppService : ApplicationService
    {
        private readonly IRepository _repository;
        public MaritalStatusesAppService(IRepository repository)
        {
            this._repository = repository;
        }
        /// 
        /// 获取婚姻状况表数据列表
        /// 
        /// 
        public async Task> GetListAsync(PagedAndSortedResultRequestDto input)
        {
            var totalCount = await _repository.CountAsync();
            var entlist = await _repository.GetPagedListAsync(input.SkipCount, input.MaxResultCount, input.Sorting);
            entlist = entlist.OrderBy(o => o.DisplayOrder).ToList();
            var entdto = ObjectMapper.Map, List>(entlist);
            return new PagedResultDto(totalCount, entdto);
        }
        /// 
        /// 修改
        /// 
        /// 主键ID
        /// 修改参数
        /// 
        public async Task UpdateAsync(char Id, UpdateMaritalStatusDto input)
        {
            var ent = await _repository.GetAsync(m => m.Id == Id);
            ent.SimpleCode = input.SimpleCode;
            ent.DisplayName = input.DisplayName;
            var newent = await _repository.UpdateAsync(ent);
            return ObjectMapper.Map(newent);
        }
        /// 
        /// 修改排序  相邻之间
        /// 
        /// 需要修改的ID
        /// 目标ID
        [HttpPut("api/app/maritalstatus/updatesort")]
        [RemoteService(false)]
        public async Task UpdateSortAsync(char id, char targetid)
        {
            var entity = await _repository.GetAsync(m => m.Id == id);
            var targetentity = await _repository.GetAsync(m => m.Id == targetid);
            int olddisplaynum = entity.DisplayOrder;
            entity.DisplayOrder = targetentity.DisplayOrder;
            targetentity.DisplayOrder = olddisplaynum;
            List entlist = new List();
            entlist.Add(entity);
            entlist.Add(targetentity);
            await _repository.UpdateManyAsync(entlist);
        }
        /// 
        /// 修改排序  置顶,置底
        /// 
        /// 需要修改的ID
        /// 修改方式:1 置顶   2 置底
        /// 
        [HttpPut("api/app/maritalstatus/updatemanysort")]
        public async Task UpdateManySortAsync(char id, int SortType)
        {
            var entity = await _repository.GetAsync(m => m.Id == id);
            List UptList = new List();
            if (SortType == 2)
            {
                UptList = (await _repository.GetListAsync(o => o.DisplayOrder > entity.DisplayOrder)).OrderBy(o => o.DisplayOrder).ToList();
                if (UptList.Count > 0)
                {
                    int indexnum = entity.DisplayOrder;  //原有值
                    entity.DisplayOrder = UptList[UptList.Count - 1].DisplayOrder;  //修改当前排序值为最大  
                    //置顶操作,往上一行开始,逐渐替换
                    foreach (var item in UptList)
                    {
                        int dqnum = item.DisplayOrder;
                        item.DisplayOrder = indexnum;
                        indexnum = dqnum;
                    }
                }
            }
            else
            {
                UptList = (await _repository.GetListAsync(o => o.DisplayOrder < entity.DisplayOrder)).OrderByDescending(o => o.DisplayOrder).ToList(); ;
                int indexnum = entity.DisplayOrder;  //原有值
                entity.DisplayOrder = UptList[UptList.Count - 1].DisplayOrder;  //修改当前排序值为最小  
                //置底操作,往下一行开始,逐渐替换
                foreach (var item in UptList)
                {
                    int dqnum = item.DisplayOrder;
                    item.DisplayOrder = indexnum;
                    indexnum = dqnum;
                }
            }
            UptList.Add(entity);
            await _repository.UpdateManyAsync(UptList);
        }
        /// 
        ///  修改排序  拖拽
        /// 
        /// 
        /// 
        [HttpPut("api/app/maritalstatus/updatesortmany")]
        public async Task UpdateSortManyAsync(UpdateSortManyNokeyDto input)
        {
            var entitylist = await _repository.GetListAsync(o => input.ItemList.Select(s => s.Id).Contains(o.Id.ToString()));
            foreach (var entity in entitylist)
            {
                foreach (var item in input.ItemList)
                {
                    if (item.Id == entity.Id.ToString())
                        entity.DisplayOrder = item.DisplayOrder;
                }
            }
            await _repository.UpdateManyAsync(entitylist);
        }
    }
}