using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Ropin.Inspection.Api.Common;
using Ropin.Inspection.Model;
using Ropin.Inspection.Model.ViewModel;
using Ropin.Inspection.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Ropin.Inspection.Api.Controllers
{
    public class TpntStoreController : BaseController
    {
        private readonly ILogger<TpntStoreController> _logger;
        private readonly ITpntStoreService _TpntStoreService;
        private readonly ITpntStoreOrgService _TpntStoreOrgService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="TpntStoreService"></param>
        /// <param name="TpntStoreOrgService"></param>
        /// <param name="logger"></param>
        public TpntStoreController(ITpntStoreService TpntStoreService, ITpntStoreOrgService TpntStoreOrgService, ILogger<TpntStoreController> logger)
        {
            _TpntStoreService = TpntStoreService;
            _TpntStoreOrgService = TpntStoreOrgService;
            _logger = logger;
        }
        /// <summary>
        /// 通过ID获取网点信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetStoreAsync/{id}")]
        public async Task<ApiResult> GetStoreAsync(Guid id)
        {
            if (Guid.Empty == id)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var content = await _TpntStoreService.GetByIdAsync(id);
                return new ApiResult<TpntStoreViewModel>(content);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 获取所有网点
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetStoresAsync")]
        public async Task<ApiResult> GetStoresAsync()
        {
            try
            {
                var contentList = await _TpntStoreService.GetAllAsync();
                return new ApiResult<IEnumerable<TpntStoreViewModel>>(contentList);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 通过网点名称条件查询
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        [HttpPost("GetStoresByAsync")]
        public async Task<ApiResult> GetStoresByAsync(TpntStoreSearchModel searchModel)
        {
            if (searchModel == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            searchModel.IsPagination = false;
            try
            {
                var contentList = await _TpntStoreService.GetConditionAsync(searchModel);
                return new ApiResult<IEnumerable<TpntStoreViewModel>>(contentList);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 创建网点
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        [HttpPost("CreateStoreAsync")]
        public async Task<ApiResult> CreateStoreAsync(TpntStoreViewModel content)
        {
            if (content == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _TpntStoreService.CreateOneAsync(content);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }
        /// <summary>
        /// 创建组织架构下网点
        /// </summary>
        /// <param name="orgCode"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        [HttpPost("CreateStoreByOrgAsync/{orgCode}")]
        public async Task<ApiResult> CreateStoreByOrgAsync(Guid orgCode, TpntStoreCreateModel content)
        {
            if (content == null ||Guid.Empty == orgCode)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _TpntStoreService.CreateStoreByOrgAsync(orgCode,content);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }
        /// <summary>
        /// 分配组织架构下网点
        /// </summary>
        /// <param name="orgCode"></param>
        /// <param name="storeCode"></param>
        /// <returns></returns>
        [HttpPost("AllotStoreByOrgAsync/{orgCode}/{storeCode}")]
        public async Task<ApiResult> AllotStoreByOrgAsync(Guid orgCode, Guid storeCode)
        {
            if (Guid.Empty == storeCode || Guid.Empty == orgCode)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _TpntStoreService.AllotStoreByOrgAsync(orgCode, storeCode);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

        /// <summary>
        /// 删除网点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("DeleteStoreAsync/{id}")]
        public async Task<ApiResult> DeleteStoreAsync(Guid id)
        {
            if (Guid.Empty == id)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TpntStoreService.DeleteAsync(id);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }
        /// <summary>
        /// 更新网点
        /// </summary>
        /// <param name="id"></param>
        /// <param name="updateModel"></param>
        /// <returns></returns>
        [HttpPut("UpdateStoreAsync/{id}")]
        public async Task<ApiResult> UpdateStoreAsync(Guid id, TpntStoreUpdateModel updateModel)
        {
            if (Guid.Empty == id)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TpntStoreService.UpdateAsync(id, updateModel);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

        /// <summary>
        /// 为组织架构批量分配网点
        /// </summary>
        /// <param name="orgCode"></param>
        /// <param name="storeCodes"></param>
        /// <returns></returns>
        [HttpPost("CreateOrgStoresAsync/{orgCode}")]
        public async Task<ApiResult> CreateOrgStoresAsync(Guid orgCode, IEnumerable<string> storeCodes)
        {
            if (Guid.Empty.Equals(orgCode) || storeCodes == null || !storeCodes.Any())
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _TpntStoreOrgService.CreateOrgStoresAsync(orgCode, storeCodes);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }
        /// <summary>
        /// 创建组织架构下网点
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        [HttpPost("CreateStoreOrgAsync")]
        public async Task<ApiResult> CreateStoreOrgAsync(TpntStoreOrgViewModel mode)
        {
            if (mode == null || Guid.Empty.Equals(mode.C_OrgCode) || string.Empty.Equals(mode.C_StoreCode))
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                TpntStoreSearchModel searchModel = new TpntStoreSearchModel();
                searchModel.orgCode = mode.C_OrgCode;
                IEnumerable<TpntStoreViewModel> stores =  await _TpntStoreOrgService.GetStoresByOrgCodeAsync(searchModel);
                List<TpntStoreViewModel> listStores = stores.ToList();
                if (listStores[0] != null && listStores.FirstOrDefault(i => i.C_Code == mode.C_StoreCode) != null)
                    return new ApiResult(ReturnCode.GeneralError, "此组织架构下已有此网点");
                await _TpntStoreOrgService.CreateOneAsync(mode);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }
        /// <summary>
        /// 删除组织架构下网点
        /// </summary>
        /// <param name="orgCode"></param>
        /// <param name="storeCode"></param>
        /// <returns></returns>
        [HttpDelete("DeleteByOrgStoreCodeAsync/{orgCode}/{storeCode}")]
        public async Task<ApiResult> DeleteByOrgStoreCodeAsync(Guid orgCode,string storeCode)
        {
            if (Guid.Empty.Equals(orgCode)|| string.IsNullOrEmpty(storeCode))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TpntStoreOrgService.DeleteByOrgStoreCodeAsync(orgCode, storeCode);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }
        /// <summary>
        /// 通过网点Code获取关联的组织架构
        /// </summary>
        /// <param name="storeCode"></param>
        /// <returns></returns>
        [HttpGet("GetOrgsByStoreCodeAsync/{storeCode}")]
        public async Task<ApiResult> GetOrgsByStoreCodeAsync(string storeCode)
        {
            if (string.IsNullOrEmpty(storeCode))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var contentList = await _TpntStoreOrgService.GetOrgsByStoreCodeAsync(storeCode);
                return new ApiResult<IEnumerable<TsysOrganizeViewModel>>(contentList);
               
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 通过点击组织机构树查看下面的网点
        /// </summary>
        /// <param name="orgCode"></param>
        /// <returns></returns>
        [HttpGet("GetStoresByOrgCodeAsync/{orgCode}")]
        public async Task<ApiResult> GetStoresByOrgCodeAsync(Guid orgCode)
        {
            if (Guid.Empty == orgCode)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                TpntStoreSearchModel searchModel = new TpntStoreSearchModel();
                searchModel.orgCode = orgCode;
                var contentList = await _TpntStoreOrgService.GetStoresByOrgCodeAsync(searchModel);
                return new ApiResult<IEnumerable<TpntStoreViewModel>>(contentList);

            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 通过点击组织机构树查看下面的网点
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetStoresByOrgCodeNameAsync")]
        public async Task<ApiResult> GetStoresByOrgCodeNameAsync(TpntStoreSearchModel searchModel)
        {
            if (searchModel==null||Guid.Empty == searchModel.orgCode)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var contentList = await _TpntStoreOrgService.GetStoresByOrgCodeAsync(searchModel);
                return new ApiResult<IEnumerable<TpntStoreViewModel>>(contentList);

            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 通过组织架构Code获取关联的网点,管理公司可以查看所有
        /// </summary>
        /// <param name="orgCode"></param>
        /// <returns></returns>
        [HttpGet("GetStoresOnlyByOrgCodeAsync/{orgCode}")]
        public async Task<ApiResult> GetStoresOnlyByOrgCodeAsync(Guid orgCode)
        {
            if (Guid.Empty == orgCode)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var contentList = await _TpntStoreOrgService.GetStoresOnlyByOrgCodeAsync(orgCode);
                return new ApiResult<IEnumerable<TpntStoreViewModel>>(contentList);

            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        
        /// <summary>
        /// 获取所有的PNT类型
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetAllPntType")]
        public async Task<ApiResult> GetAllPntType()
        {
            try
            {
                var contentList = await _TpntStoreService.GetAllPntType();
                return new ApiResult<IList<TpntTypeViewModel>>(contentList);

            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 通过网点code获取网点信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet("GetStoreByCodeAsync/{code}")]
        public async Task<ApiResult> GetStoreByCodeAsync(string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var content = await _TpntStoreService.GetStoreByCodeAsync(code);
                return new ApiResult<TpntStoreViewModel>(content);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
    }
}