using ASHermed.SinoInterface.Api.ContractModel;
using ASHermed.SinoInterface.Api.ContractModel.App.Entity;
using ASHermed.SinoInterface.Api.IRepository;
using ASHermed.SinoInterface.Api.IRepository.App;
using ASHermed.SinoInterface.Api.IService.App;
using ASHermed.SinoInterface.ContractModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASHermed.SinoInterface.Api.Service.App
{
    public class InsuranceService : BaseService, IInsuranceService
    {
        private readonly IPubInsuranceRepository _pubInsuranceRepository;
        private readonly IPubPatInfoRepository _pubPatInfoRepository;
        private readonly IFileService _fileService;
        public InsuranceService(IPubInsuranceRepository pub_InsuranceRepository, IPubPatInfoRepository pubPatInfoRepository, IFileService fileService)
        {
            _pubInsuranceRepository = pub_InsuranceRepository;
            _pubPatInfoRepository = pubPatInfoRepository;
            _fileService = fileService;
        }
        public BaseResponse<List<Pub_Insurance>> GetPubInsurancesList(int patId)
        {
            List<int> list = new List<int>();
            if (patId != 0)
            {
                list.Add(patId);
            }
            else
            {
                list.AddRange(_pubPatInfoRepository.GetPatIdsByUserId(GetBaseUserInfo().ID).Select(w => w.Pat_ID).ToList());
            }
            BaseResponse<List<Pub_Insurance>> response = new BaseResponse<List<Pub_Insurance>>();
            var res = _pubInsuranceRepository.GetPubInsurancesList(0, list, "", new List<int>());
            if (res != null && res.Count > 0)
            {
                response.Data = res;
            }
            response.Msg = "成功";
            response.Code = 1;
            return response;
        }

        public List<Pub_Insurance> GetPatUsedInsurancesList(int patId)
        {
            var user = GetBaseUserInfo();
            List<int> patIds = new List<int>();
            if (patId != 0)
            {
                patIds.Add(patId);
            }
            else
            {
                patIds = _pubPatInfoRepository.GetPatIdsByUserId(user.ID).Select(w => w.Pat_ID).ToList();
            }

            var res = _pubInsuranceRepository.GetPubInsurancesList(0, patIds, "", new List<int>() { 1, 2 });
            //TODO
            //res = _fileService.GetFileContentSelect<Pub_Insurance>(res);
            return res;
        }

        public BaseResponse<int> AddPubInsurances(Pub_Insurance request)
        {
            //TODO 新上传文件ID转换缩略图
            //request = _fileService.GetFileContentAdd<Pub_Insurance>(request);
            request.InsuranceStatus = 1; 
            request.IsReadInsuInfo = true;
            //计算到期时间
            //request.ValidityDate = DateTime.Now;
            request.CreateTime = DateTime.Now;
            //request.UpdateTime = DateTime.Now;
            request.CreateUser = GetBaseUserInfo().Nick_Name;
            //request.UpdateUser = GetBaseUserInfo().Nick_Name;
            BaseResponse<int> response = new BaseResponse<int>();
            var res = _pubInsuranceRepository.AddPubInsurances(request);
            if (res > 0)
            {
                response.Data = res;
                response.Code = 1;
                response.Msg = "成功";
            }
            return response;
        }
        public BaseResponse<bool> UpdatePubInsurances(Pub_Insurance request)
        {
            BaseResponse<bool> response = new BaseResponse<bool>();
            if (request.Id == 0)
            {
                response.Data = false;
                return response;
            }
            request.ValidityDate = DateTime.MinValue;
            request.UpdateTime = DateTime.Now;
            request.UpdateUser = GetBaseUserInfo().Nick_Name;
            var res = _pubInsuranceRepository.UpdatePubInsurances(request) > 0 ? true : false;
            response.Data = res;
            response.Code = 1;
            return response;
        }

        public BaseResponse<InsuranceModel> GetPubInsuranceInfo(int patId)
        {
            BaseResponse<InsuranceModel> res = new BaseResponse<InsuranceModel>();
            var insu = _pubInsuranceRepository.GetPubInsurance(patId);
            if (insu != null)
            {
                res.Data = insu;
                res.Code = 1;
                res.Msg = "成功";
            }
            return res;
        }

        public InsuranceModel GetPubInsuranceById(int id)
        {
            var insu = _pubInsuranceRepository.GetPubInsuranceById(id);
            return insu;
        }

        public bool DeletePubInsurances(int id)
        {
            return _pubInsuranceRepository.DeletePubInsurances(id);
        }




    }
}
