using Microsoft.AspNetCore.Mvc;
using OASystem.API.OAMethodLib;
using OASystem.Domain.Dtos.CRM;
using OASystem.Domain.Entities.Customer;
using OASystem.Infrastructure.Repositories.CRM;
using static OASystem.Domain.Dtos.CRM.NewClientDataQueryDto;

namespace OASystem.API.Controllers
{
    /// <summary>
    /// 市场客户资料
    /// </summary>
    [Route("api/[controller]/[action]")]
    public class MarketCustomerResourcesController : ControllerBase
    {
        private readonly NewClientDataRepository _clientDataRepository;
        /// <summary>
        /// 初始化
        /// </summary>
        public MarketCustomerResourcesController(NewClientDataRepository clientDataRepository)
        {
            this._clientDataRepository = clientDataRepository;
        }

        /// <summary>
        /// 客户资料数据
        /// 基础数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> MarketCustomerInit(PortDtoBase dto)
        {
            JsonView jw = new JsonView();
            try
            {
                Result resultData = await _clientDataRepository._Init(dto.PortType);
                if (resultData.Code == 0)
                {
                    jw = JsonView(true, "查询成功!", resultData.Data);
                }
                else
                {
                    jw = JsonView(false, resultData.Msg);
                }
            }
            catch (Exception)
            {
                jw = JsonView(false, "程序错误！");
            }

            return Ok(jw);
        }

        /// <summary>
        /// 查询客户资料数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> QueryNewClientData(NewClientDataQueryDto dto)
        {
            #region 参数验证
            if (dto.OperationUserId < 0)
                return Ok(JsonView(false, "请传入有效的OperationUserId参数！"));

            if (dto.PortType < 0)
                return Ok(JsonView(false, "请传入有效的PortType参数！"));
            #endregion

            JsonView jw = new JsonView();
            try
            {
                Result resultData = await _clientDataRepository.QueryNewClientData(dto);
                if (resultData.Code == 0)
                {
                    #region 客户资料表操作记录
                    await GeneralMethod.NewClientOperationRecord(dto.PortType, OperationEnum.NoOperation, dto.OperationUserId, 0, "");
                    #endregion

                    jw = JsonView(true, "查询成功!", resultData.Data);
                }
                else
                {
                    jw = JsonView(false, resultData.Msg);
                }
            }
            catch (Exception)
            {
                jw = JsonView(false, "程序错误！");
            }

            return Ok(jw);
        }

        /// <summary>
        /// 客户资料数据
        /// Details
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> PostNewClientDataDetails(NewClientDataDetailsDto dto)
        {
            #region 参数验证
            if (dto.Id < 0)
                return Ok(JsonView(false, "请传入有效的Id参数！"));

            if (dto.UserId < 0)
                return Ok(JsonView(false, "请传入有效的UserId参数！"));

            if (dto.PortType < 0)
                return Ok(JsonView(false, "请传入有效的PortType参数！"));
            #endregion

            JsonView jw = new JsonView();
            try
            {
                Result resultData = await _clientDataRepository._Details(dto.PortType, dto.Id);
                if (resultData.Code == 0)
                {
                    #region 客户资料表操作记录
                    await GeneralMethod.NewClientOperationRecord(dto.PortType, OperationEnum.Details, dto.UserId, dto.Id, "");
                    #endregion
                    jw = JsonView(true, "查询成功!", resultData.Data);
                }
                else
                {
                    jw = JsonView(false, resultData.Msg);
                }
            }
            catch (Exception)
            {
                jw = JsonView(false, "程序错误！");
            }

            return Ok(jw);
        }

        /// <summary>
        /// 客户资料操作（Status:1.新增,2.修改）
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> NewClientOp(NewClientOpDto dto)
        {
            #region 参数验证

            if (dto.CreateUserId < 0)
            {
                return Ok(JsonView(false, "请传入有效的CreateUserId参数！"));
            }

            if (dto.PortType < 0)
            {
                return Ok(JsonView(false, "请传入有效的PortType参数！"));
            }
            #endregion

            try
            {
                Domain.Result result = await _clientDataRepository.NewClientOp(dto);
                if (result.Code != 0)
                {
                    return Ok(JsonView(false, result.Msg));
                }
                #region 客户资料操作记录
                OperationEnum operationEnum = OperationEnum.NoOperation;
                if (dto.Status == 1)
                {
                    operationEnum = OperationEnum.Add;
                    dto.Id = Convert.ToInt32(result.Data);
                }
                else if (dto.Status == 2) operationEnum = OperationEnum.Edit;

                await GeneralMethod.NewClientOperationRecord(dto.PortType, operationEnum, dto.CreateUserId, dto.Id, "");
                #endregion

                return Ok(JsonView(true, result.Msg + "Id:" + dto.Id));
            }
            catch (Exception ex)
            {
                return Ok(JsonView(false, "程序错误！Msg:" + ex.Message));
            }
        }

        /// <summary>
        /// 新客户资料操作（删除）
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> NewClientDel(DelBaseDto dto)
        {
            #region 参数验证
            if (dto.Id < 0)
            {
                return Ok(JsonView(false, "请传入有效的Id参数！"));
            }

            if (dto.DeleteUserId < 0)
            {
                return Ok(JsonView(false, "请传入有效的DeleteUserId参数！"));
            }

            if (dto.PortType < 0 )
            {
                return Ok(JsonView(false, "请传入有效的PortType参数！"));
            }
            #endregion
            var res = await _clientDataRepository.DelNewClientData(dto);
            if (res.Code != 0)
            {
                return Ok(JsonView(false, "删除失败"));
            }

            #region 客户资料表操作记录
            await GeneralMethod.NewClientOperationRecord(dto.PortType, OperationEnum.Del, dto.DeleteUserId, dto.Id, "");
            #endregion

            return Ok(JsonView(true, "删除成功！"));
        }

        /// <summary>
        /// 获取下拉列表数据和单条数据信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> QuerySelectAndSingleData(QuerySingleDto dto)
        {
            JsonView jw = new JsonView();
            var result = await _clientDataRepository.QuerySelectAndSingleData(dto);
            if (result.Code == 0)
            {
                #region 客户资料表操作记录
                await GeneralMethod.NewClientOperationRecord(dto.PortType, OperationEnum.Details, dto.UserId, dto.Id, "");
                #endregion

                jw = JsonView(true, result.Msg, result.Data);
            }
            else
            {
                jw = JsonView(false, result.Msg);
            }
            return Ok(jw);
        }

        /// <summary>
        /// 获取现有负责人
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> QueryUserSelect()
        {
            try
            {
                Result resTable = _clientDataRepository.QueryUserSelect();
                return Ok(JsonView(true, resTable.Msg, resTable.Data));
            }
            catch (Exception)
            {
                return Ok(JsonView(false, "程序错误！"));
            }
        }

        /// <summary>
        /// 获取出团数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> QueryNumberGroups()
        {
            var result = await _clientDataRepository.QueryNumberGroups();
            if (result.Code != 0)
            {
                return Ok(JsonView(false, result.Msg));
            }
            return Ok(JsonView(true, result.Msg, result.Data));
        }

        /// <summary>
        /// 新客户资料操作
        /// 批量分配
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> PostBatchAssignment(BatchAssignmentDto dto)
        {
            #region 参数验证
            if (dto.UserId < 0)
            {
                return Ok(JsonView(false, "请传入有效的UserId参数！"));
            }

            if (dto.PortType < 0)
            {
                return Ok(JsonView(false, "请传入有效的PortType参数！"));
            }
            #endregion

            var res = await _clientDataRepository._BatchAssignment(dto);
            if (res.Code != 0)
            {
                return Ok(JsonView(false, "操作失败!"));
            }
            return Ok(JsonView(true, "操作成功！"));
        }
    }
}
