//--------------------------------------------------------------------
//     此代码由T4模板自动生成  byzhouhang 20210918
//	   生成时间 2022-02-15 15:57:21 
//     对此文件的更改可能会导致不正确的行为，并且如果重新生成代码，这些更改将会丢失。
//--------------------------------------------------------------------

using IRaCIS.Core.Application.Contracts;
using Microsoft.AspNetCore.Mvc;
namespace IRaCIS.Core.Application.Services
{
    /// <summary>
    /// SystemBasicDataService
    /// </summary>	
    [ApiExplorerSettings(GroupName = "Common")]
    public class SystemBasicDataService : BaseService, ISystemBasicDataService
    {
        private readonly IRepository<SystemBasicData> _systemBasicDataRepository;

        public SystemBasicDataService(IRepository<SystemBasicData> systemBasicDataRepository) {
            this._systemBasicDataRepository = systemBasicDataRepository;
        }


        /// <summary>
        /// 模板列表
        /// </summary>
        /// <param name="querySystemBasicData"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutput<SystemBasicDataView>> GetSystemBasicDataList(SystemBasicDataQuery querySystemBasicData)
        {
            var systemBasicDataQueryable = _systemBasicDataRepository.Where(t => t.ParentId == null)
            .ProjectTo<SystemBasicDataView>(_mapper.ConfigurationProvider);

            return await systemBasicDataQueryable.ToPagedListAsync(querySystemBasicData.PageIndex, querySystemBasicData.PageSize, String.IsNullOrEmpty(querySystemBasicData.SortField) ? "Code" : querySystemBasicData.SortField, querySystemBasicData.Asc);
        }


        [HttpGet("{code}")]
        public async Task<SystemBasicDataView> GetSystemBasicData(string code)
        {
            return (await _repository.Where<SystemBasicData>(t => t.Code == code).ProjectTo<SystemBasicDataView>(_mapper.ConfigurationProvider).FirstOrDefaultAsync()).IfNullThrowException();
        }


        /// <summary>
        /// 模板关联的场景  
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        [HttpGet("{parentId:guid}")]
        public async Task<List<SystemBasicDataView>> GetChildList(Guid parentId)
        {
            return await _systemBasicDataRepository.Where(t => t.ParentId == parentId&&t.IsEnable).OrderBy(t => t.Code).ProjectTo<SystemBasicDataView>(_mapper.ConfigurationProvider).ToListAsync();
        }


        public async Task<IResponseOutput> AddOrUpdateSystemBasicData(SystemBasicDataAddOrEdit addOrEditSystemBasicData)
        {

            if (addOrEditSystemBasicData.Id == null)
            {
                var entity = _mapper.Map<SystemBasicData>(addOrEditSystemBasicData);
                await _systemBasicDataRepository.AddAsync(entity, true);
                return ResponseOutput.Ok(entity.Id.ToString());
            }
            else
            {

                var entity =await _systemBasicDataRepository.Where(t => t.Id == addOrEditSystemBasicData.Id,true).FirstOrDefaultAsync();
                _mapper.Map(addOrEditSystemBasicData, entity);
                await _systemBasicDataRepository.SaveChangesAsync();

                //var entity = _mapper.Map<SystemBasicData>(addOrEditSystemBasicData);
                //await _systemBasicDataRepository.UpdateFromDTOAsync(entity);
                //await _systemBasicDataRepository.SaveChangesAsync();
            }
            return ResponseOutput.Ok(true);
        }




        [HttpDelete("{systemBasicDataId:guid}")]
        public async Task<IResponseOutput> DeleteSystemBasicData(Guid systemBasicDataId)
        {
            var success = await _systemBasicDataRepository.DeleteFromQueryAsync(x=>x.Id== systemBasicDataId);
            await _systemBasicDataRepository.SaveChangesAsync();
            return ResponseOutput.Result(true);
        }


        /// <summary>
        /// 传递父亲Code 数组  返回多个下拉框数据
        /// </summary>
        /// <param name="searchArray"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Dictionary<string, List<SystemBasicDataSelect>>> GetBasicDataSelect(string[] searchArray)
        {

            var searchList = await _systemBasicDataRepository.Where(t => searchArray.Contains(t.Parent.Code) && t.ParentId != null).ProjectTo<SystemBasicDataSelect>(_mapper.ConfigurationProvider,new { _userInfo.IsEn_Us}).ToListAsync();

            return searchList.GroupBy(t => t.ParentCode).ToDictionary(g => g.Key, g => g.ToList());

        }
    }
}
