using Infrastructure;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App.ServiceApp.BidCompanyInfoManager;
using OpenAuth.App.ServiceApp.BidCompanyInfoManager.Request;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers.ServiceController
{
    /// <summary>
    /// 企业信息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BidCompanyInfoController : ControllerBase
    {
        private readonly BidCompanyInfoApp _app;
        public BidCompanyInfoController(BidCompanyInfoApp app)
        {
            _app = app;
        }

        /// <summary>
        /// 查询企业信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<PpCompanyinfo>>>> LoadBidCompanyInfo([FromQuery]BidCompanyInfoReq req)
        {
            var result = new Response<PageInfo<List<PpCompanyinfo>>>();
            try
            {
                result = await _app.LoadBidCompanyInfo(req);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 根据id查询单个企业信息--编辑企业信息用
        /// </summary>
        /// <param name="id">企业信息id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PpCompanyinfo>> LoadBidCompanyInfoById(string id)
        {
            var result = new Response<PpCompanyinfo>();
            try
            {
                result = await _app.LoadBidCompanyInfoById(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }


        /// <summary>
        /// 添加企业信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> AddBidCompanyInfo(PpCompanyinfo info)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.AddBidCompanyInfo(info);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 编辑企业信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> EditBidCompanyInfo(PpCompanyinfo info)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.EditBidCompanyInfo(info);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 删除企业信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> DeleteBidCompanyInfo(string id)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.DeleteBidCompanyInfo(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }


        /// <summary>
        /// 审核企业信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> ReviewBidCompanyInfo(string id)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.ReviewBidCompanyInfo(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }


        /// <summary>
        /// 为企业分配账号信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> AddCompanyAccount(List<PpCompanyaccount> info)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.AddCompanyAccount(info);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 根据企业id查询分配的账号信息--编辑企业账号分配时使用
        /// </summary>
        /// <param name="id">企业信息id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<string>> LoadBidUserInfoById(string id)
        {
            var result = new Response<string>();
            try
            {
                result = await _app.LoadBidUserInfoById(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }


        /// <summary>
        /// 根据当前登录用户查询企业信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<PpCompanyinfo>>>> LoadCompanyInfoByUser()
        {
            var result = new Response<PageInfo<List<PpCompanyinfo>>>();
            try
            {
                result = await _app.LoadCompanyInfoByUser();
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
    }
}
