using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Formula.Functions;
using OASystem.API.OAMethodLib.ALiYun;
using OASystem.API.OAMethodLib.ExcelOutput;
using OASystem.Domain.Common;
using OASystem.Domain.Dtos.Business;
using OASystem.Domain.Dtos.Groups;
using OASystem.Domain.Entities.Business;
using OASystem.Domain.Entities.Groups;
using OASystem.Domain.Entities.Resource;
using OASystem.Domain.ViewModels.Business;
using OASystem.Domain.ViewModels.Groups;
using OASystem.Infrastructure.Repositories.Business;
using OASystem.Infrastructure.Repositories.Groups;
using OASystem.Infrastructure.Repositories.System;
using Org.BouncyCastle.Asn1.Mozilla;

namespace OASystem.API.Controllers
{
    /// <summary>
    /// 通用业务操作
    /// </summary>
    [Route("api/[controller]/[action]")]
    public class BusinessController : ControllerBase
    {
        private readonly IMapper _mapper;
        private readonly CommonBusRepository _busRep;
        private readonly SetDataRepository _setDataRep;
        private readonly DelegationInfoRepository _groupRep;
        public BusinessController(IMapper mapper, CommonBusRepository busRep, SetDataRepository setDataRep, DelegationInfoRepository groupRep)
        {
            _mapper = mapper;
            _busRep = busRep;
            _setDataRep = setDataRep;
            _groupRep = groupRep;
        }

        #region 团组信息 团组详情
        /// <summary>
        ///  团组信息 团组详情
        /// </summary>
        /// <param name="dto">团组info请求dto</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> PostShareGroupInfo(ShareGroupInfoDto dto)
        {
            var groupData = await _groupRep.PostShareGroupInfo(dto);
            if (groupData.Code != 0)
            {
                return Ok(JsonView(false, groupData.Msg));
            }

            return Ok(JsonView(groupData.Data));
        }

        /// <summary>
        /// 团组信息 团组名称 List
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> GetGroupNameList(GroupNameDto dto)
        {
            var groupData = await _groupRep.GetGroupNameList(dto);
            if (groupData.Code != 0)
            {
                return Ok(JsonView(false, groupData.Msg));
            }

            return Ok(JsonView(groupData.Data, groupData.Data.Count));
        }

        #endregion

        #region 币种 List
        /// <summary>
        /// 币种 List
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet,HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> PostCurrencyList()
        {
            try
            {
                Result setData = await _setDataRep.GetSetDataBySTId(_setDataRep, 66); //币种类型
                if (setData.Code == 0)
                {
                    return Ok(JsonView(true, "查询成功", setData.Data));
                }
                else
                {
                    return Ok(JsonView(false, setData.Msg));
                }
            }
            catch (Exception)
            {
                return Ok(JsonView(false, "程序错误！"));
                throw;
            }
        }
        #endregion

        #region 会务物料单

        /// <summary>
        /// 获取会务活动列表
        /// </summary>
        /// <param name="paras"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> PostConferenceList()
        {
            List<Grp_DelegationInfo> listSource = _busRep.Query<Grp_DelegationInfo>(s => s.TeamDid == 691 && s.IsDel == 0).ToList();
            List<GroupNameView> viewList = new List<GroupNameView>();
            foreach (var group in listSource)
            {
                GroupNameView _view = new GroupNameView();
                _view.Id = group.Id;
                _view.GroupName = group.TeamName;
            }

            return Ok(JsonView(viewList));
        }

        /// <summary>
        /// 获取会务的采购物品计划清单
        /// </summary>
        /// <param name="ConfId">会务活动Id</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> PostConfItemList(int ConfId)
        {
            Bus_ConfItemListView view = new Bus_ConfItemListView();

            Bus_ConfItemListInfo _confListInfo = await _busRep.Query<Bus_ConfItemListInfo>(s => s.Diid == ConfId).FirstAsync();
            if (_confListInfo != null)
            {
                view.TotalCost = _confListInfo.TotalCost;

                string sqlItem = string.Format(@" Select c.Id,c.ItemId,d.ItemName,c.[Count],c.CurrCost,c.OpRemark
From Bus_ConfItem as c With(Nolock) 
Inner Join Res_ItemDetail as d with(Nolock) On c.ItemId=d.Id
Where c.ConfListId = {0}", ConfId);
                List<Bus_ConfItemView> confItemList = await _busRep._sqlSugar.SqlQueryable<Bus_ConfItemView>(sqlItem).ToListAsync();
                view.ItemList = new List<Bus_ConfItemView>(confItemList);
            }
            else
            {
                view.ItemList = new List<Bus_ConfItemView>();
                view.TotalCost = 0;
            }

            return Ok(JsonView(view));
        }

        /// <summary>
        /// 获取会务可采购的物料集合
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> PostOptionalItemList()
        {
            List<OptionalBusRangeView> result = await _busRep.GetViewList_OptionalItem();

            return Ok(JsonView(result));
        }

        /// <summary>
        /// 编辑物料采购清单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> PostEditOptionalItemList(JsonDtoBase jsonDto)
        {
            if (string.IsNullOrEmpty(jsonDto.Paras))
            {
                return Ok(JsonView(false, "参数为空"));
            }
            Edit_OptionalItemListDto _dto = JsonConvert.DeserializeObject<Edit_OptionalItemListDto>(jsonDto.Paras);
            //Edit_OptionalItemListDto _dto = System.Text.Json.JsonSerializer.Deserialize<Edit_OptionalItemListDto>(jsonDto.Paras);

            if (_dto.DiId < 1)
            {
                return Ok(JsonView(false, "团组Id为空"));
            }

            if (_dto.ConfItemListId < 1)
            {
                //新增
                int rstInsert = await _busRep.Insert_ConfItemList(_dto);
                return Ok(JsonView(rstInsert == 0));
            }
            else
            {
                //修改
                int rstUpdate = await _busRep.Edit_ConfItemList(_dto);
                return Ok(JsonView(rstUpdate == 0));
            }

            return Ok(JsonView(false));
        }

        /// <summary>
        /// 获取会务采购物料清单Excel
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> Excel_ConfItemList(JsonDtoBase jsonDto)
        {
            if (string.IsNullOrEmpty(jsonDto.Paras))
            {
                return Ok(JsonView(false, "参数为空"));
            }

            dynamic confList = JsonConvert.DeserializeObject<dynamic>(jsonDto.Paras);
            int confListId = confList.ConfListId;
            Bus_ConfItemListInfo _entityConfList = await _busRep.Query<Bus_ConfItemListInfo>(s => s.Id == confListId).FirstAsync();
            if (_entityConfList != null)
            {
                string result = new Excel_BusConfItemList().Excel(_entityConfList);


            }
            else
            {
                return Ok(JsonView(false, "请先保存数据"));
            }

            return Ok(JsonView(false));
        }

        #endregion

        #region 阿里云短信测试

        /// <summary>
        /// 编辑物料采购清单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
        public async Task<IActionResult> AliMessageTest(string mobile)
        {

            string add2dayZH = DateTime.Now.AddDays(2).ToString("yyyy年MM月dd日");
            string templateParam = JsonConvert.SerializeObject(new { teams = "测试团组", date = add2dayZH });
            AliMessagePost.PostMessage(mobile, "泛美国际团组", "SMS_461575447", templateParam);
            return Ok(JsonView(true));
        }

        #endregion
    }
}
