using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OASystem.Domain.Dtos.CRM;
using OASystem.Domain.Entities.Customer;
using OASystem.Infrastructure.Repositories.CRM;
using static OpenAI.GPT3.ObjectModels.SharedModels.IOpenAiModels;
namespace OASystem.API.Controllers
{
    /// 
    /// 签证客户资料相关
    /// 
    [Route("api/[controller]/[action]")]
    //[ApiController]
    public class CRMController : ControllerBase
    {
        private readonly VisaDeleClientCompanyRepository _clientCompanyRepository;
        private readonly VisaDeleClientRepository _clientRepository;
        private readonly CustomerCertRepository _customerCertRep;
        private readonly CustomerFamilyRepository _customerFamilyRep;
        private readonly CustomerSchoolRepository _customerSchoolRep;
        private readonly CustomerCompanyRepository _customerCompanyRep;
        /// 
        /// 初始化
        /// 
        /// 
        /// 
        public CRMController(VisaDeleClientCompanyRepository clientCompanyRepository, VisaDeleClientRepository clientRepository,
            CustomerCertRepository customerCertRep, CustomerFamilyRepository customerFamilyRep,CustomerSchoolRepository customerSchoolRep, CustomerCompanyRepository customerCompanyRep)
        {
            this._clientCompanyRepository = clientCompanyRepository;
            this._clientRepository = clientRepository;
            this._customerCertRep = customerCertRep;
            this._customerFamilyRep = customerFamilyRep;
            this._customerSchoolRep = customerSchoolRep;
            _customerCompanyRep = customerCompanyRep;
        }
        /// 
        /// 获取签证客户公司列表
        /// 
        /// 
        /// 
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task GetClientCompanyList(DtoBase dto)
        { 
            var clientCompanyData = await  _clientCompanyRepository.GetCrm_ClientCompanyList(dto);
            if (clientCompanyData.Code != 0)
            {
                return Ok(JsonView(false, clientCompanyData.Msg == null ? "操作失败" : clientCompanyData.Msg));
            }
            return Ok(JsonView(clientCompanyData.Data, clientCompanyData.Data.Count));
        }
        /// 
        /// 签证客户公司列表操作(Status:1.新增,2.修改)
        /// 
        /// 
        /// 
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task OperationClientCompany(OperationClientCompanyDto dto)
        {
            try
            {
                if (dto.CompanyName == "")
                {
                    return Ok(JsonView(false, "请检查客户单位名称是否填写!"));
                }
                if (dto.Address == "")
                {
                    return Ok(JsonView(false, "请检查客户单位地址是否填写!"));
                }
                if (dto.PostCodes == "")
                {
                    return Ok(JsonView(false, "请检查客户单位邮编是否填写!"));
                }
                Result clientCompanyData = await _clientCompanyRepository.OperationClientCompany(dto);
              
                if (clientCompanyData.Code != 0)
                {
                    return Ok(JsonView(false, clientCompanyData.Msg));
                }
                return Ok(JsonView(true, clientCompanyData.Msg));
            }
            catch (Exception)
            {
                return Ok(JsonView(false, "程序错误!"));
                throw;
            }
            
        }
        /// 
        /// 签证客户公司列表操作(删除)
        /// 
        /// 
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task DelClientCompany(DelBaseDto dto)
        {
            try
            {
                var res = await _clientCompanyRepository.SoftDeleteByIdAsync(dto.Id.ToString(), dto.DeleteUserId);
                if (!res)
                {
                    return Ok(JsonView(false, "删除失败"));
                }
                return Ok(JsonView(true, "删除成功!"));
            }
            catch (Exception ex)
            {
                return Ok(JsonView(false, "程序错误!"));
                throw;
            }
        }
        /// 
        /// 获取签证客户列表
        /// 
        /// 
        /// 
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task GetClientList(DtoBase dto)
        {
            var clientData = await _clientRepository.GetCrmList(dto);
            if (clientData.Code != 0)
            {
                return Ok(JsonView(false, clientData.Msg == null ? "操作失败" : clientData.Msg));
            }
            return Ok(JsonView(clientData.Data, clientData.Data.Count));
        }
        /// 
        /// 根据Id获取签证客户信息
        /// 
        /// 
        /// 
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task CustomerInfo(CustomerDto dto)
        {
            try
            {
                Crm_DeleClient crm_Dele = await _clientRepository.GetAsync(x => x.Id == dto.Id);
                if (crm_Dele == null)
                {
                    return Ok(JsonView(false, "查询失败!"));
                }
                List _VisaCustomerCompany = _clientRepository.Query(x => x.DcId == crm_Dele.Id && x.IsDel == 0).ToList();//客户工作经历表
                List _VisaCustomerSchool = _clientRepository.Query(x => x.DcId == crm_Dele.Id && x.IsDel == 0).ToList();//客户学历表
                List _VisaCustomerFamily = _clientRepository.Query(x => x.DcId == crm_Dele.Id && x.IsDel == 0).ToList();//客户学历表
                List _CustomerCerts= _clientRepository.Query(x => x.DcId == crm_Dele.Id && x.IsDel == 0).ToList();//客户证件表
                Crm_CustomerCompany _CustomerCompany =await _clientCompanyRepository.GetAsync(x => x.Id == crm_Dele.CrmCompanyId);//客户公司信息
                var data = new
                {
                    DeleClient = crm_Dele,
                    WorkExperience = _VisaCustomerCompany,
                    CustomerSchool = _VisaCustomerSchool,
                    CustomerFamily = _VisaCustomerFamily,
                    CustomerCompany= _CustomerCompany,
                    CustomerCerts= _CustomerCerts
                };
                return Ok(JsonView(true, "查询成功!", data));
            }
            catch (Exception)
            {
                return Ok(JsonView(false, "程序错误!"));
                throw;
            }
        }
        /// 
        /// 客户资料操作(Status:1.新增,2.修改)
        /// 
        /// 
        /// 
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task EditCustomer(DeleClientOpDto dto)
        {
            try
            {
                Domain.Result result =await _clientRepository.OpCustomer(dto);
                if (result.Code != 0)
                {
                    return Ok(JsonView(false, result.Msg));
                }
                return Ok(JsonView(true, result.Msg));
            }
            catch (Exception)
            {
                return Ok(JsonView(false, "程序错误!"));
                throw;
            }
        }
        /// 
        /// 客户资料操作删除
        /// 
        /// 
        /// 
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async TaskDelCustomer(DeleClientDelDto dto)
        {
            Result result = await _clientRepository.DelCustomer(dto);
            if (result.Code != 0)
            {
                return Ok(JsonView(false, result.Msg));
            }
            return Ok(JsonView(true, result.Msg));
        }
        /// 
        /// 证件表数据删除
        /// 
        /// 
        /// 
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task DelCustomerCerts(DelBaseDto dto)
        {
            var result = await _customerCertRep.SoftDeleteByIdAsync(dto.Id.ToString(), dto.DeleteUserId);
            if (!result)
            {
                return Ok(JsonView(false, "删除失败"));
            }
            return Ok(JsonView(true, "删除成功!"));
        }
        /// 
        /// 家庭成员表数据删除
        /// 
        /// 
        /// 
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task DelCustomerFamily(DelBaseDto dto)
        {
            var result = await _customerFamilyRep.SoftDeleteByIdAsync(dto.Id.ToString(), dto.DeleteUserId);
            if (!result)
            {
                return Ok(JsonView(false, "删除失败"));
            }
            return Ok(JsonView(true, "删除成功!"));
        }
        /// 
        /// 客户学历表数据删除
        /// 
        /// 
        /// 
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task DelCustomerSchool(DelBaseDto dto)
        {
            
            var result = await _customerSchoolRep.SoftDeleteByIdAsync(dto.Id.ToString(), dto.DeleteUserId);
            if (!result)
            {
                return Ok(JsonView(false, "删除失败"));
            }
            return Ok(JsonView(true, "删除成功!"));
            
        }
        /// 
        /// 客户工作经历表数据删除
        /// 
        /// 
        /// 
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task DelVisaCustomerCompany(DelBaseDto dto)
        {
            var result = await _customerCompanyRep.SoftDeleteByIdAsync(dto.Id.ToString(), dto.DeleteUserId);
            if (!result)
            {
                return Ok(JsonView(false, "删除失败"));
            }
            return Ok(JsonView(true, "删除成功!"));
        }
        /// 
        /// 客户资料导入
        /// 
        /// 
        /// 
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task PostClient(DelBaseDto dto)
        {
            var result = await _customerCompanyRep.SoftDeleteByIdAsync(dto.Id.ToString(), dto.DeleteUserId);
            if (!result)
            {
                return Ok(JsonView(false, "删除失败"));
            }
            return Ok(JsonView(true, "删除成功!"));
        }
    }
}