using AutoMapper;
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.Resource;
using OASystem.Domain.ViewModels.CRM;
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 VisaDeleClientCompanyRepository : BaseRepository<Crm_CustomerCompany, CustomerCompanyView>
    {
        private readonly IMapper _mapper;
        public VisaDeleClientCompanyRepository(SqlSugarClient sqlSugar, IMapper mapper) :
            base(sqlSugar)
        {
            _mapper= mapper;
        }

        /// <summary>
        /// 签证客户公司 List
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Result> GetCrm_ClientCompanyList(DtoBase dto)
        {
            Result result = new Result() { Code = -2 };

            if (dto.PortType == 1 || dto.PortType == 2)
            {
                string sql = string.Format(@"Select ccc.Id,ccc.CompanyName,ccc.Address,ccc.PostCodes,su.CnName UserName,ccc.CreateTime,ccc.Remark
                                             From Crm_CustomerCompany ccc Inner Join Sys_Users su On ccc.CreateUserId = su.Id where ccc.IsDel=0 order by CreateTime Desc");

                var _clientCompanyList = await _sqlSugar.SqlQueryable<CustomerCompanyListView>(sql).ToListAsync();
                if (_clientCompanyList.Count > 0)
                {
                    result.Code = 0;
                    result.Msg = "成功!";
                    result.Data = _clientCompanyList;
                }
                else
                {
                    result.Msg = "查询失败!";
                }
            }

            return result;
        }

        public async  Task<Result> OperationClientCompany(OperationClientCompanyDto dto)
        {
            Result result = new Result() { Code = -2, Msg = "未知错误" };
            try
            {
                if (dto.Status == 1)//添加
                {
                    string selectSql = string.Format(@"select * from Crm_CustomerCompany WHERE CompanyName='{0}' and IsDel='{1}'"
                                                       , dto.CompanyName, 0);
                    var CustomerCompany = await _sqlSugar.SqlQueryable<Crm_CustomerCompany>(selectSql).FirstAsync();//查询是否存在
                    if (CustomerCompany != null)
                    {
                        return result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };

                    }
                    else//不存在,可添加
                    {
                        Crm_CustomerCompany _CustomerCompany = _mapper.Map<Crm_CustomerCompany>(dto);
                        int id = await AddAsyncReturnId(_CustomerCompany);
                        if (id == 0)
                        {
                            return result = new Result() { Code = -1, Msg = "添加失败!" };

                        }
                        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 Crm_CustomerCompany
                    {
                        CompanyName=dto.CompanyName,
                        Address = dto.Address,
                        PostCodes = dto.PostCodes,
                        LastedOpUserId = dto.LastedOpUserId,
                        LastedOpDt = dto.LastedOpDt,
                        Remark = dto.Remark,
                     
                    });
                    if (!res)
                    {
                        return result = new Result() { Code = -1, Msg = "修改失败!" };
                    }
                    result = new Result() { Code = 0, Msg = "修改成功!" };
                }
                else
                {
                    return result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
                }
            }
            catch (Exception ex)
            {
                return result = new Result() { Code = -2, Msg = "程序错误!" };
            }
            return result;
        }
    }
}