using Furion.DynamicApiController;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Nirvana.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Waste.Domain;

namespace Waste.Application
{
    /// <summary>
    /// 商户管理
    /// </summary>
    public class BusinessAppService : IDynamicApiController
    {
        private readonly IBusinessService _businessService;
        private static readonly string LoginUserKey = Configs.GetString("LoginProviderKey");
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IBusinessApiService _businessApiService;

        public BusinessAppService(IBusinessService businessService, IHttpContextAccessor httpContextAccessor,IBusinessApiService businessApiService)
        {
            _businessService = businessService;
            _httpContextAccessor = httpContextAccessor;
            _businessApiService = businessApiService;
        }
        /// <summary>
        /// 商户列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageParms<BusinessList>> GetListAsync(QueryParams param)
        {
            return await _businessService.GetListAsync(param);
        }
        /// <summary>
        /// 商户列表,不包含管理员
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageParms<BusinessList>> GetListNoAdminAsync(QueryParams param)
        {
            return await _businessService.GetListAsync(param,true);
        }
        /// <summary>
        /// 信息提交
        /// </summary>
        /// <param name="buss"></param>
        /// <returns></returns>
        public async Task<ResultInfo> SubmitFormAsync(BusinessInfo buss)
        {
            return await _businessService.SubmitFormAsync(buss);
        }
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        [HttpGet]
        [QueryParameters]
        public async Task<ResultInfo> ResetPwdAsync(Guid id,string pwd)
        {
            return await _businessService.ResetPwdAsync(id,pwd);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="busienssPwd"></param>
        /// <returns></returns>
        public async Task<ResultInfo> ChangePwdAsync(BusienssPwd busienssPwd)
        {
            return await _businessService.ChangePwdAsync(busienssPwd);
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultInfo OutLogin()
        {
            _httpContextAccessor.HttpContext.Session.Remove(LoginUserKey);
            OperatorProvider.Provider.RemoveCurrent();
            return new ResultInfo(ResultState.SUCCESS, "success");
        }

        /// <summary>
        /// 授权列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageParms<BusinessApiInfo>> GetApiListAsync(QueryParams param)
        {
            return await _businessApiService.GetListAsync(param);
        }

        /// <summary>
        /// 授权信息提交
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultInfo> SubmitApiFormAsync(W_BusinessAppApi param)
        {
            return await _businessApiService.SubmitFormAsync(param);
        }
        /// <summary>
        /// 删除授权
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpGet]
        [QueryParameters]
        public async Task<ResultInfo> DeleteApiFormAsync(Guid keyValue)
        {
            return await _businessApiService.DeleteFormAsync(keyValue);
        }
    }
}
