using AutoMapper;
using OASystem.Domain;
using OASystem.Domain.Dtos.Groups;
using OASystem.Domain.Entities.Groups;
using OASystem.Domain.Entities.Resource;
using OASystem.Domain.ViewModels.Groups;
using OASystem.Infrastructure.Repositories.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OASystem.Infrastructure.Repositories.Groups
{
    public class DelegationEnDataRepository : BaseRepository<Grp_DelegationEnData, Grp_DelegationEnData>
    {


        private readonly IMapper _mapper;
        public DelegationEnDataRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
        {
            _mapper = mapper;
        }

        /// <summary>
        /// 团组英文资料操作(Status:1.新增,2.修改)
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> OpDelegationEnData(OpDelegationEnDataDto dto)
        {
            Result result = new Result() { Code = -2, Msg = "未知错误" };
            try
            {
                if (dto.Status == 1)//添加
                {
                    string selectSql = string.Format(@"select * from Grp_DelegationEnData where Area='{0}' and Job='{1}' and IsDel='{2}'", dto.Area, dto.Job, 0);
                    var grp_DelegationEnData = await _sqlSugar.SqlQueryable<Grp_DelegationEnData>(selectSql).FirstAsync();//查询是否存在
                    if (grp_DelegationEnData != null)
                    {
                        return result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };

                    }
                    else//不存在,可添加
                    {

                        Grp_DelegationEnData _DelegationEnData = _mapper.Map<Grp_DelegationEnData>(dto);
                        int id = await _sqlSugar.Insertable(_DelegationEnData).ExecuteReturnIdentityAsync();
                        if (id == 0)
                        {
                            return result = new Result() { Code = -1, Msg = "添加失败!" };

                        }
                        return result = new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } };
                    }
                }
                else if (dto.Status == 2)//修改
                {
                    bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Grp_DelegationEnData
                    {
                        Area = dto.Area,
                        Job = dto.Job,
                        JobEn = dto.JobEn,
                        DelegationSetting = dto.DelegationSetting,
                        DelegationSettingEn = dto.DelegationSettingEn,
                        CreateUserId = dto.CreateUserId,
                        Remark = dto.Remark,
                    });
                    if (!res)
                    {
                        return result = new Result() { Code = -1, Msg = "修改失败!" };
                    }
                    return result = new Result() { Code = 0, Msg = "修改成功!", Data = new { Id = dto.Id } };
                }
                else
                {
                    return result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
                }
            }
            catch (Exception ex)
            {
                return result = new Result() { Code = -2, Msg = "程序错误!" };
            }
        }

        public async Task<Result> QueryDelegationEnData(QueryDelegationEnDataDto dto)
        {
            Result result = new Result() { Code = -2, Msg = "未知错误" };
            try
            {
                string sqlWhere = string.Empty;
                if (!string.IsNullOrWhiteSpace(dto.Area))
                {
                    sqlWhere += string.Format(@"And Area like '%{0}%'", dto.Area);
                }
                if (!string.IsNullOrWhiteSpace(dto.Job))
                {
                    sqlWhere += string.Format(@"And Job like '%{0}%'", dto.Job);
                }
                sqlWhere += string.Format(@"And IsDel={0}", 0);

                if (!string.IsNullOrEmpty(sqlWhere.Trim()))
                {
                    Regex r = new Regex("And");
                    sqlWhere = r.Replace(sqlWhere, "Where", 1);
                }
                string sql = string.Format(@"select * from Grp_DelegationEnData {0} order by CreateTime desc", sqlWhere);
                List<Grp_DelegationEnData> askDatd = await _sqlSugar.SqlQueryable<Grp_DelegationEnData>(sql).ToListAsync();
                if (askDatd.Count != 0 && askDatd.Count!=0)
                {
                    result = new Result() { Code = 0, Msg = "查询成功!", Data =askDatd };
                }
                else
                {
                    result = new Result() { Code = 0, Msg = "暂无数据!", Data =askDatd };
                }
            }
            catch (Exception ex)
            {
                result = new Result() { Code = -2, Msg = "未知错误" };
            }
            return result;
        }
    }
}