using System.Linq;
using System.Threading.Tasks;
using ASHermed.SinoInterface.Api.ContractModel.App.Entity;
using ASHermed.SinoInterface.Api.ContractModel.App.RequestForm;
using ASHermed.SinoInterface.Api.IRepository.App;
using ASHermed.SinoInterface.Api.IService.App;

namespace ASHermed.SinoInterface.Api.Service.App
{
    /// <summary>
    /// 患者录入数据统计服务
    /// </summary>
    public class PubMemberCountService : IPubMemberCountService
    {
        private readonly IPubMenberCountRepository _pubMemberCountRepository;

        /// <summary>
        /// ctor
        /// </summary>
        public PubMemberCountService(IPubMenberCountRepository pubMemberCountRepository)
        {
            _pubMemberCountRepository = pubMemberCountRepository;
        }

        /// <summary>
        /// 记录
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<bool> RecordAsync(RecordPubMenberCountParam param)
        {
            switch (param.Category)
            {
                case 1:
                case 2:
                    await RecordAddAndEditAsync(param.Category);
                    break;
                case 3:
                    await RecordPatMrn(param.PatMrn, param.Category);
                    break;
            }

            return true;
        }

        /// <summary>
        /// 记录新增和编辑
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        private async Task RecordAddAndEditAsync(int category)
        {
            var patMrnIsNullList = await _pubMemberCountRepository.QueryAsync(0, null, category);

            //如果存在，次数+1
            if (patMrnIsNullList != null && patMrnIsNullList.Any())
            {
                var patMrnIsNull = patMrnIsNullList.FirstOrDefault();
                if (patMrnIsNull != null)
                {
                    await _pubMemberCountRepository.UpdateQuantityAsync(patMrnIsNull.Id, patMrnIsNull.Quantity + 1);
                }
                return;
            }

            //如果不存在，新增一条记录
            var patMrnIsNullEntity = new Pub_Menber_Count
            {
                Category = category,
                Quantity = 1
            };
            await _pubMemberCountRepository.InsertAsync(patMrnIsNullEntity);
        }

        /// <summary>
        /// 记录patmrn
        /// </summary>
        /// <param name="patMrn"></param>
        /// <param name="category"></param>
        /// <returns></returns>
        private async Task RecordPatMrn(string patMrn, int category)
        {
            var pubMemberCountList = await _pubMemberCountRepository.QueryAsync(0, patMrn, category);

            //如果存在，次数+1
            if (pubMemberCountList != null && pubMemberCountList.Any())
            {
                var pubMemberCountEnitty = pubMemberCountList.FirstOrDefault();
                if (pubMemberCountEnitty != null)
                {
                    await _pubMemberCountRepository.UpdateQuantityAsync(pubMemberCountEnitty.Id, pubMemberCountEnitty.Quantity + 1);}
                return;
            }

            //如果不存在，新增一条记录
            var entity = new Pub_Menber_Count
            {
                PatMrn = patMrn,
                Category = category,
                Quantity = 1
            };

            await _pubMemberCountRepository.InsertAsync(entity);
        }
    }
}