using ASHermed.SinoInterface.Api.ContractModel;
using ASHermed.SinoInterface.Api.IRepository;
using ASHermed.SinoInterface.Api.IRepository.App;
using ASHermed.SinoInterface.Api.IService;
using ASHermed.SinoInterface.Api.IService.App;
using ASHermed.SinoInterface.Api.IService.OnlineAptInfo;
using ASHermed.SinoInterface.ContractModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ASHermed.SinoInterface.Api.Service.App
{
    /// <summary>
    /// 材料补充 
    /// </summary>
    public class SupplementaryMaterialsService : BaseService, ISupplementaryMaterialsService
    {
        #region 初始化
        private readonly ISupplementaryMaterialsRepository _supplementaryMaterialsRepository;
        private readonly IOnlineAptInfoRepository _onlineAptInfoRepository;
        private readonly IOrderService _orderService;
        private readonly ITencentService _tencentService;
        private readonly IPubPatInfoRepository _pubPatInfoRepository;
        private readonly ISysRepository _sysRepository;
        private readonly IPubUserInfoRepository _pubUserInfoRepository;
        /// <summary>
        /// 初始化
        /// </summary>
        public SupplementaryMaterialsService(ISupplementaryMaterialsRepository supplementaryMaterialsRepository, IOnlineAptInfoRepository onlineAptInfoRepository, IOrderService orderService, ITencentService tencentService, IPubPatInfoRepository pubPatInfoRepository, ISysRepository sysRepository, IPubUserInfoRepository pubUserInfoRepository)
        {
            _supplementaryMaterialsRepository = supplementaryMaterialsRepository;
            _onlineAptInfoRepository = onlineAptInfoRepository;
            _orderService = orderService;
            _tencentService = tencentService;
            _pubPatInfoRepository = pubPatInfoRepository;
            _sysRepository = sysRepository;
            _pubUserInfoRepository = pubUserInfoRepository;
        }
        #endregion

        /// <summary>
        /// 添加修改 补充材料
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async System.Threading.Tasks.Task<SinoInterface.ContractModel.BaseResponse<dynamic>> AddSupplementaryMaterials(AddSupplementaryMaterialsParam param)
        {
            var userInfo = GetBaseUserInfo();
            param.UserId = userInfo != null ? userInfo.ID.ToString() : "0";
            var resu = new SinoInterface.ContractModel.BaseResponse<dynamic>();
            if (userInfo == null)
            {
                // throw new NotImplementedException("未获取到当前登录人信息!");
            }
            if (param.Id > 0)
            {
                resu.Data = await _supplementaryMaterialsRepository.SMR_UpdateAsync(param);
                if (resu.Data > 0)
                {
                    SendCard(param.AptId);
                    resu.Code = 1;
                    resu.Msg = "成功";
                }
                else
                {
                    resu.Code = 0;
                    resu.Msg = "修改失败";
                }

            }
            else
            {
                resu.Data = await _supplementaryMaterialsRepository.SMR_InsertAsync(param);
                if (resu.Data > 0)
                {
                    SendCard(param.AptId);
                    resu.Code = 1;
                    resu.Msg = "成功";
                }
                else
                {
                    resu.Code = 0;
                    resu.Msg = "插入失败";
                }

            }
            return resu;
        }

        public void SendCard(int aptId)
        {
            var aptInfo = _onlineAptInfoRepository.GetAptInfo(aptId);
            var helper = _sysRepository.GetSysHelpers("医助")?.FirstOrDefault();
            var user = _pubUserInfoRepository.SelectById(aptInfo.ApplyUserId);
            if (aptInfo.AptType == 1)
            {
                //var user = _pubPatInfoRepository.GetUserByPatId(aptInfo.PatId);
                var group = _pubPatInfoRepository.IsExistGroup(user.ID, aptInfo.PatId, aptInfo.DocId);
                SendGroupMessage(group.GroupId, helper.ImId);
            }
            if (aptInfo.AptType == 2)
            {
                //var user = _pubPatInfoRepository.GetUserByPatId(aptInfo.PatId);
                //var user = _pubUserInfoRepository.SelectById(aptInfo.ApplyUserId);
                SendFriendMessage(helper.ImId, user.IM_ID);
            }
        }

        private void SendGroupMessage(string groupId, string fromWho)
        {
            var msg = new { messageType = "overCard", content = "健康档案已填写", title = "信息已提交" };
            _tencentService.SendCusMsgToGroup(groupId, fromWho, JsonConvert.SerializeObject(msg));
        }
        private void SendFriendMessage(string fromWho, string toWho)
        {
            var fmsg = new { messageType = "overCard", content = "健康档案已填写", title = "信息已提交" };
            _tencentService.SendFriendMsg(fromWho, toWho, JsonConvert.SerializeObject(fmsg));
        }
        /// <summary>
        /// 删除补充材料
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async System.Threading.Tasks.Task<SinoInterface.ContractModel.BaseResponse<dynamic>> DelSupplementaryMaterials(DeletedSupplementaryMaterialsParam param)
        {
            var userInfo = GetBaseUserInfo();
            param.UserId = userInfo != null ? userInfo.ID.ToString() : "0";
            var resu = new SinoInterface.ContractModel.BaseResponse<dynamic>();
            if (userInfo == null)
            {
                // throw new NotImplementedException("未获取到当前登录人信息!");
            }
            if (param.Id > 0)
            {
                resu.Data = await _supplementaryMaterialsRepository.SMR_DeletedAsync(param);
                if (resu.Data > 0)
                {
                    resu.Code = 1;
                    resu.Msg = "成功";
                }
                else
                {
                    resu.Code = 0;
                    resu.Msg = "删除失败";
                }
                return resu;
            }
            throw new NotImplementedException();
        }

        public async Task<BaseResponse<dynamic>> GetSupplementaryMaterials(GetSupplementaryMaterialsParam param)
        {
            var userInfo = GetBaseUserInfo();
            param.UserId = userInfo != null ? userInfo.ID.ToString() : "0";
            var resu = new SinoInterface.ContractModel.BaseResponse<dynamic>();
            resu.Data = await _supplementaryMaterialsRepository.GetSupplementaryMaterials(param);
            resu.Code = 1;
            resu.Msg = "成功";
            return resu;
        }
    }
}
