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

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

        /// <summary>
        /// 分页查询招标信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<dynamic>>>> LoadBiddingInfo([FromQuery]BiddingInfoReq req)
        {
            var result = new Response<PageInfo<List<dynamic>>>();
            try
            {
                result = await _app.LoadBiddingInfo(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<PpBiddinginfo>> LoadBiddingInfoById(string id)
        {
            var result = new Response<PpBiddinginfo>();
            try
            {
                result = await _app.LoadBiddingInfoById(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>> AddBiddingInfo(PpBiddinginfo info)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.AddBiddingInfo(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>> EditBiddingInfo(PpBiddinginfo info)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.EditBiddingInfo(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>> DeleteBiddingInfo(string id)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.DeleteBiddingInfo(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }


        #region 招标代理
        /// <summary>
        /// 招标代理当前招标--只看自己代理的招标项目，nowstatus传待审核，正常，超期三种状态
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<dynamic>>>> LoadAgencyBiddingInfo([FromQuery] BiddingInfoReq req)
        {
            var result = new Response<PageInfo<List<dynamic>>>();
            try
            {
                result = await _app.LoadAgencyBiddingInfo(req);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        #endregion


        #region 供应商公司
        /// <summary>
        /// 查询招标信息--供应商公司的查询，只看自己报名的招标项目
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<dynamic>>>> LoadCompanyBiddingInfo([FromQuery] BiddingInfoReq req)
        {
            var result = new Response<PageInfo<List<dynamic>>>();
            try
            {
                result = await _app.LoadCompanyBiddingInfo(req);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        #endregion

        /// <summary>
        /// 分页查询招标信息 招商代理--当前招标
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<dynamic>>>> LoadAgencyBidding([FromQuery] BiddingInfoReq req)
        {
            var result = new Response<PageInfo<List<dynamic>>>();
            try
            {
                result = await _app.LoadAgencyBidding(req);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 根据招标信息查询报名企业
        /// </summary>
        /// <param name="bidinfoid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<PpCompanyinfo>>> LoadBidCompanyInfo(string bidinfoid)
        {
            var result = new Response<List<PpCompanyinfo>>();
            try
            {
                result = await _app.LoadBidCompanyInfo(bidinfoid);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }


        /// <summary>
        /// 审核招标信息
        /// </summary>
        /// <param name="biddinginfoid">招标信息id</param>
        /// <param name="isagree">是否通过</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> ReviewBiddingInfo(string biddinginfoid, bool isagree)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.ReviewBiddingInfo(biddinginfoid, isagree);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
    }
}
