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
{
    /// <summary>
    /// 签证客户资料相关
    /// </summary>
    [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;
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="clientCompanyRepository"></param>
        /// <param name="clientRepository"></param>
        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;
        }

        /// <summary>
        /// 获取签证客户公司列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> 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));
        }

        /// <summary>
        /// 签证客户公司列表操作（Status:1.新增,2.修改）
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> 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;
            }
            
        }
        /// <summary>
        /// 签证客户公司列表操作（删除）
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> DelClientCompany(DelBaseDto dto)
        {
            try
            {
                var res = await _clientCompanyRepository.SoftDeleteByIdAsync<Crm_CustomerCompany>(dto.Id.ToString(), dto.DeleteUserId);
                if (!res)
                {
                    return Ok(JsonView(false, "删除失败"));
                }
                return Ok(JsonView(true, "删除成功！"));
            }
            catch (Exception ex)
            {
                return Ok(JsonView(false, "程序错误！"));
                throw;
            }
        }
        /// <summary>
        /// 获取签证客户列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> 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));
        }
        /// <summary>
        /// 根据Id获取签证客户信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> CustomerInfo(CustomerDto dto)
        {
            try
            {
                Crm_DeleClient crm_Dele = await _clientRepository.GetAsync<Crm_DeleClient>(x => x.Id == dto.Id);
                if (crm_Dele == null)
                {
                    return Ok(JsonView(false, "查询失败！"));
                }
                List<Crm_VisaCustomerCompany> _VisaCustomerCompany = _clientRepository.Query<Crm_VisaCustomerCompany>(x => x.DcId == crm_Dele.Id && x.IsDel == 0).ToList();//客户工作经历表
                List<Crm_VisaCustomerSchool> _VisaCustomerSchool = _clientRepository.Query<Crm_VisaCustomerSchool>(x => x.DcId == crm_Dele.Id && x.IsDel == 0).ToList();//客户学历表
                List<Crm_VisaCustomerFamily> _VisaCustomerFamily = _clientRepository.Query<Crm_VisaCustomerFamily>(x => x.DcId == crm_Dele.Id && x.IsDel == 0).ToList();//客户学历表
                List<Crm_CustomerCert> _CustomerCerts= _clientRepository.Query<Crm_CustomerCert>(x => x.DcId == crm_Dele.Id && x.IsDel == 0).ToList();//客户证件表
                Crm_CustomerCompany _CustomerCompany =await _clientCompanyRepository.GetAsync<Crm_CustomerCompany>(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;
            }
        }

        /// <summary>
        /// 客户资料操作（Status:1.新增,2.修改）
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> 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;
            }
        }

        /// <summary>
        /// 客户资料操作删除
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult>DelCustomer(DeleClientDelDto dto)
        {
            try
            {
                Result result = await _clientRepository.DelCustomer(dto);
                if (result.Code != 0)
                {
                    return Ok(JsonView(false, result.Msg));
                }
                return Ok(JsonView(true, result.Msg));
            }
            catch (Exception)
            {
                return Ok(JsonView(false, "程序错误！"));
                throw;
            }
            return Ok(JsonView(false, "程序错误！"));
        }
        /// <summary>
        /// 证件表数据删除
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> DelCustomerCerts(DelBaseDto dto)
        {
            try
            {
                var result = await _customerCertRep.SoftDeleteByIdAsync<Crm_CustomerCert>(dto.Id.ToString(),dto.DeleteUserId);
                if (!result)
                {
                    return Ok(JsonView(false, "删除失败"));
                }
                return Ok(JsonView(true, "删除成功！"));
            }
            catch (Exception ex)
            {
                return Ok(JsonView(false, "程序错误！"));
                throw;
            }
        }

        /// <summary>
        /// 家庭成员表数据删除
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> DelCustomerFamily(DelBaseDto dto)
        {
            try
            {
                var result = await _customerFamilyRep.SoftDeleteByIdAsync<Crm_VisaCustomerFamily>(dto.Id.ToString(), dto.DeleteUserId);
                if (!result)
                {
                    return Ok(JsonView(false, "删除失败"));
                }
                return Ok(JsonView(true, "删除成功！"));
            }
            catch (Exception ex)
            {
                return Ok(JsonView(false, "程序错误！"));
                throw;
            }
        }

        /// <summary>
        /// 客户学历表数据删除
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> DelCustomerSchool(DelBaseDto dto)
        {
            try
            {
                var result = await _customerSchoolRep.SoftDeleteByIdAsync<Crm_VisaCustomerSchool>(dto.Id.ToString(), dto.DeleteUserId);
                if (!result)
                {
                    return Ok(JsonView(false, "删除失败"));
                }
                return Ok(JsonView(true, "删除成功！"));
            }
            catch (Exception ex)
            {
                return Ok(JsonView(false, "程序错误！"));
                throw;
            }
        }
        /// <summary>
        /// 客户工作经历表数据删除
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> DelVisaCustomerCompany(DelBaseDto dto)
        {
            try
            {
                var result = await _customerCompanyRep.SoftDeleteByIdAsync<Crm_VisaCustomerCompany>(dto.Id.ToString(), dto.DeleteUserId);
                if (!result)
                {
                    return Ok(JsonView(false, "删除失败"));
                }
                return Ok(JsonView(true, "删除成功！"));
            }
            catch (Exception ex)
            {
                return Ok(JsonView(false, "程序错误！"));
                throw;
            }
        }
    }
}
