using AutoMapper;
using OASystem.Domain.Dtos.Groups;
using OASystem.Domain.Entities.Groups;
using OASystem.Domain.ViewModels.Groups;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OASystem.Infrastructure.Repositories.Groups
{
    /// 
    /// 签证提成录入
    /// 仓储
    /// 
    public class VisaCommissionRepository : BaseRepository
    {
        private readonly IMapper _mapper;
        private readonly DelegationInfoRepository _groupRep;
        public VisaCommissionRepository(SqlSugarClient sqlSugar, IMapper mapper, DelegationInfoRepository groupRep)
            : base(sqlSugar)
        {
            _mapper = mapper;
            _groupRep = groupRep;
        }
        /// 
        /// Init
        /// 
        /// 
        public async Task Init()
        {
            var groupInfo = await _groupRep.GetGroupNameList(new Domain.Dtos.Groups.GroupNameDto() { PortType = 1 });
            return new JsonView() { Code = StatusCodes.Status200OK, Msg = "操作成功!", Data = new { GroupData = groupInfo.Data } };
        }
        /// 
        /// Item
        /// 
        /// 
        public async Task Item(VisaCommissionItemDto _dto)
        {
            var data = await _sqlSugar.Queryable()
                                      .Where(x => x.IsDel == 0 && x.DiId == _dto.DiId && x.UId == _dto.CurrUserId)
                                      .Select(x => new
                                      {
                                          x.Id,
                                          x.Country,
                                          x.Quantity,
                                          x.Remark
                                      })
                                      .ToListAsync();
            var groupInfo = await _sqlSugar.Queryable().Where(x => x.IsDel == 0 && x.Id == _dto.DiId).FirstAsync();
            if (groupInfo != null)
            {
                string[] countryArray = _groupRep.GroupSplitCountry(groupInfo.VisitCountry).ToArray();
                foreach (string country in countryArray)
                {
                    if (data.Find(x => country.Equals(x.Country)) == null)
                    {
                        data.Add(new
                        {
                            Id = 0,
                            Country = country,
                            Quantity = 0,
                            Remark = ""
                        });
                    }
                }
            }
            return new JsonView() { Code = StatusCodes.Status200OK, Msg = "操作成功!", Data = data };
        }
        /// 
        /// Create
        /// 
        /// 
        public async Task Create(VisaCommissionCreateDto _dto)
        {
            var _view = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "操作失败!" };
            //添加查重
            var info = await _sqlSugar.Queryable()
                                      .Where(x => x.IsDel == 0 && x.DiId == _dto.DiId && x.Country.Equals(_dto.Country))
                                      .FirstAsync();
            //if (info != null)
            //{
            //    _view.Code = StatusCodes.Status400BadRequest;
            //    _view.Msg = "该数据已存在请勿重新添加!";
            //    return _view;
            //}
            var createInfo =  _mapper.Map(_dto);
            createInfo.CreateUserId = _dto.CurrUserId;
            createInfo.UId = _dto.CurrUserId;
            var createStatus = await _sqlSugar.Insertable(createInfo)
                                              .ExecuteCommandAsync();
            if (createStatus > 0)
            {
                _view.Code = StatusCodes.Status200OK;
                _view.Msg = "操作成功!";
                return _view;
            }
            return _view;
        }
        /// 
        /// Edit
        /// 
        /// 
        /// 
        /// 
        public async Task Edit(int id,VisaCommissionCreateDto _dto)
        {
            var _view = new JsonView() { Code = StatusCodes.Status204NoContent, Msg = "操作失败!" };
            var editInfo = _mapper.Map(_dto);
            editInfo.CreateUserId = _dto.CurrUserId;
            editInfo.UId = _dto.CurrUserId;
            var editStatus = await _sqlSugar.Updateable(editInfo)
                                            .UpdateColumns(x => new
                                            {
                                                x.Country,
                                                x.Quantity,
                                                x.Remark
                                            })
                                            .Where(x => x.Id == id)
                                            .ExecuteCommandAsync();
            if (editStatus > 0)
            {
                _view.Code = StatusCodes.Status200OK;
                _view.Msg = "操作成功!";
                return _view;
            }
            return _view;
        }
        /// 
        /// Del
        /// 
        /// 
        /// 
        /// 
        public async Task Del(int id,int currUserId)
        {
            var _view = new JsonView() { Code = StatusCodes.Status204NoContent, Msg = "操作失败!" };
            var editStatus = await _sqlSugar.Updateable()
                                            .SetColumns(x => new Grp_VisaCommission
                                            {
                                                DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                                DeleteUserId = currUserId,
                                            })
                                            .Where(x => x.Id == id)
                                            .ExecuteCommandAsync();
            if (editStatus > 0)
            {
                _view.Code = StatusCodes.Status200OK;
                _view.Msg = "操作成功!";
                return _view;
            }
            return _view;
        }
    }
}