using OASystem.Domain;
using OASystem.Domain.Dtos;
using OASystem.Domain.Dtos.CRM;
using OASystem.Domain.Dtos.UserDto;
using OASystem.Domain.Entities.Customer;
using OASystem.Domain.Entities.Groups;
using OASystem.Domain.ViewModels.CRM;
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.CRM
{
    /// <summary>
    /// 签证客户 仓库
    /// </summary>
    public class VisaDeleClientRepository : BaseRepository<Crm_DeleClient, VisaDeleClientView>
    {
        public VisaDeleClientRepository(SqlSugarClient sqlSugar) :
            base(sqlSugar)
        { }

        /// <summary>
        /// 签证客户list
        /// </summary>
        /// <param name="loginDto"></param>
        /// <returns></returns>
        public async Task<Result> GetCrmList(DtoBase dto)
        {
            Result result = new Result() { Code = -2 };
            if (dto.PortType == 1 || dto.PortType == 2) 
            {
                string sql = string.Format(@"Select cdc.Id,LastName+FirstName ClientName,ccc.CompanyName,Sex,Marriage,
                                             	    Phone LandlinePhone,Tel,crmCard1.CertNo IDNo,crmCard2.CertNo PassportNo
                                             From Crm_DeleClient cdc
                                             Left Join Crm_CustomerCompany ccc On cdc.crmCompanyId = ccc.Id
                                             Left Join Crm_CustomerCert crmCard1 On crmCard1.SdId = 773 And  cdc.Id = crmCard1.DcId 
                                             Left Join Crm_CustomerCert crmCard2 On crmCard2.SdId = 774 And  cdc.Id = crmCard2.DcId ");

                var clientList = await _sqlSugar.SqlQueryable<VisaDeleClientListView>(sql).ToListAsync();

                if (clientList.Count > 0)
                {
                    result.Code = 0;
                    result.Msg = "成功!";
                    result.Data = clientList;
                }
                else
                {
                    result.Msg = "暂无数据!";
                }
            }

            return result;
        }


        /// <summary>
        /// 签证客户操作
        /// </summary>
        /// <param name="loginDto"></param>
        /// <returns></returns>
        public async Task<Result> CrmClinetoperation(LoginDto loginDto)
        {
            Result result = new Result() { Code = -2 };

            return result;
        }

        /// <summary>
        /// 签证客户 新增
        /// </summary>
        /// <param name="loginDto"></param>
        /// <returns></returns>
        public async Task<int> CrmClinetAdd(Crm_DeleClient client)
        {
            int addId = -1;

            


            return addId;
        }


        /// <summary>
        /// 根据身份证识别修改ocr添加
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public async Task<bool> SetCrmUpdPassIdCardOCR(SetCrmUpdPassIdCardOCRDto client)
        {

            string clientSql = string.Format(@"Select * From Crm_DeleClient Where LastName+FirstName='{0}' And Sex = {1}",
                                             client.ClientName, client.Sex);
            var clientInfo = await _sqlSugar.SqlQueryable<VisaDeleClientListView>(clientSql).FirstAsync();
            if(clientInfo == null) return false;

            string cardSql = string.Format(@"Select * From Crm_CustomerCert Where SdId=773 And DcId={0}", clientInfo.Id);
            var cardInfo = await _sqlSugar.SqlQueryable<CustomerCertView>(cardSql).FirstAsync();

            if (cardInfo == null)  //添加
            {
                int cerdAdd = await _sqlSugar.Insertable<Crm_CustomerCert>(new Crm_CustomerCert
                {
                    DcId = clientInfo.Id,
                    SdId = "773",
                    Country = "中国",
                    CertNo = client.CerdNo,
                    TargetCountry = "",
                    IssueDt = new DateTime(1990, 1, 1),
                    ExpiryDt = "1990-01-01 00:00:00.000",
                    CreateUserId = client.UserId,
                    CreateTime = DateTime.Now,
                    DeleteUserId = null,
                    DeleteTime = "1990-01-01 00:00:00.000",
                    Remark = "",
                    IsDel = 0
                }).ExecuteReturnIdentityAsync();
                if (cerdAdd > 0) return true;

            }
            else  //修改
            {
                var cerdStatus = await _sqlSugar.Updateable<Crm_CustomerCert>()
                    .Where(c => c.Id == cardInfo.Id)
                    .SetColumns(c => new Crm_CustomerCert
                    {
                        CertNo = cardInfo.CertNo,
                        IDCardAddress = cardInfo.IDCardAddress
                    }).ExecuteCommandAsync();

                if (cerdStatus > 0) return true;
            }

            
            return false;
        }
    }
}