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

namespace ASHermed.SinoInterface.Api.MySqlRepository.App
{
    public class PubInsuranceRepository : BaseRepositoryToASHermed_SinoInterface<Pub_Insurance>, IPubInsuranceRepository
    {
        public List<Pub_Insurance> GetPubInsurancesList(int Id, List<int> patId, string insuranceCard, List<int> status)
        {
            StringBuilder selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@"SELECT   Id,
										PatId,
										PatName,
										InsuranceCard,
										InsuranceCompany,
                                        InsuranceName,
										InsuranceStatus,
										ValidityDate,
										IdentityFrontUrl,
										IdentityBackUrl,
										InsuranceCardUrl,
										IsReadInsuInfo,
										Remark,
										CreateTime,
										CreateUser,
										UpdateTime,
										UpdateUser
							FROM pub_insurance WHERE 1=1 ");
            if (Id != 0)
            {
                selectSql.Append(" AND Id=@Id ");
                parms.Add("@Id", Id);
            }
            if (patId.Count > 0)
            {
                selectSql.Append(" AND PatId in @PatId ");
                parms.Add("@PatId", patId);
            }
            if (!string.IsNullOrEmpty(insuranceCard))
            {
                selectSql.Append(" AND InsuranceCard=@InsuranceCard ");
                parms.Add("@InsuranceCard", insuranceCard);
            }
            if (status != null && status.Count > 0)
            {
                selectSql.Append(" AND InsuranceStatus in @InsuranceStatus ");
                parms.Add("@InsuranceStatus", status);
            }

            var res = Query<Pub_Insurance>(selectSql.ToString(), parms).ToList();
            return res;
        }

        public InsuranceModel GetPubInsurance(int patId)
        {
            StringBuilder selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@"SELECT   Id,
										PatId,
										PatName,
										InsuranceCard,
										InsuranceCompany,
                                        InsuranceName,
										InsuranceStatus,
										ValidityDate,
										IdentityFrontUrl,
										IdentityBackUrl,
										InsuranceCardUrl,
										IsReadInsuInfo,
										Remark
										
							FROM pub_insurance WHERE 1=1 and InsuranceStatus != 3");

            if (patId != 0)
            {
                selectSql.Append(" AND PatId=@PatId ");
                parms.Add("@PatId", patId);
            }
            var res = Query<InsuranceModel>(selectSql.ToString(), parms).ToList();
            return res?.FirstOrDefault();
        }

        public InsuranceModel GetPubInsuranceById(int id)
        {
            StringBuilder selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@"SELECT   Id,
										PatId,
										PatName,
										InsuranceCard,
										InsuranceCompany,
                                        InsuranceName,
										InsuranceStatus,
										ValidityDate,
										IdentityFrontUrl,
										IdentityBackUrl,
										InsuranceCardUrl,
										IsReadInsuInfo,
										Remark
							FROM pub_insurance WHERE id = @id");
            parms.Add("@id", id);
            var res = Query<InsuranceModel>(selectSql.ToString(), parms).ToList();
            return res?.FirstOrDefault();
        }

        public int AddPubInsurances(Pub_Insurance request)
        {
            StringBuilder selectSql = new StringBuilder();
            selectSql.Append(@"INSERT INTO `pub_insurance` (
								`PatId`,
								`PatName`,
								`InsuranceCard`,
								`InsuranceCompany`,
                                `InsuranceName`,
								`InsuranceStatus`,
								`ValidityDate`,
								`IdentityFrontUrl`,
								`IdentityBackUrl`,
								`InsuranceCardUrl`,
								`IsReadInsuInfo`,
								`Remark`,
								`CreateTime`,
								`CreateUser`,
								`UpdateTime`,
								`UpdateUser` 
								)
								VALUES
									(@PatId,@PatName,@InsuranceCard,@InsuranceCompany,@InsuranceName,@InsuranceStatus,@ValidityDate,@IdentityFrontUrl,@IdentityBackUrl,@InsuranceCardUrl,@IsReadInsuInfo,@Remark,@CreateTime,@CreateUser,@UpdateTime,@UpdateUser);");
            return Execute(selectSql.ToString(), request);
        }

        public bool DeletePubInsurances(int id)
        {
            StringBuilder selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@" delete from `pub_insurance`  where id = @id ");
            parms.Add("@id", id);
            return Execute(selectSql.ToString(), parms) > 0;
        }

        public int UpdatePubInsurances(Pub_Insurance request)
        {
            StringBuilder selectSql = new StringBuilder();
            selectSql.Append(@" UPDATE `pub_insurance`  SET ");
            if (request.PatId != 0)
            {
                selectSql.Append(" `PatId`=@PatId, ");
            }
            if (!string.IsNullOrEmpty(request.PatName))
            {
                selectSql.Append(" `PatName`= @PatName, ");
            }
            if (!string.IsNullOrEmpty(request.InsuranceCard))
            {
                selectSql.Append("	`InsuranceCard`= @InsuranceCard, ");
            }

            selectSql.Append(" `InsuranceCompany`= @InsuranceCompany, ");
            selectSql.Append(" `InsuranceName`= @InsuranceName, ");

            if (request.InsuranceStatus != 0)
            {
                selectSql.Append(" `InsuranceStatus`= @InsuranceStatus, ");
            }
            if (request.ValidityDate != DateTime.MinValue)
            {
                selectSql.Append(@" `ValidityDate`=@ValidityDate, ");
            }
            if (!string.IsNullOrEmpty(request.IdentityFrontUrl))
            {
                selectSql.Append(" `IdentityFrontUrl`=@IdentityFrontUrl, ");
            }
            if (!string.IsNullOrEmpty(request.IdentityBackUrl))
            {
                selectSql.Append(" `IdentityBackUrl`=@IdentityBackUrl, ");
            }
            if (!string.IsNullOrEmpty(request.InsuranceCardUrl))
            {
                selectSql.Append(" `InsuranceCardUrl`=@InsuranceCardUrl, ");
            }
            if (!string.IsNullOrEmpty(request.Remark))
            {
                selectSql.Append(" `Remark`=@Remark, ");
            }
            selectSql.Append(@"         `UpdateTime`=@UpdateTime,
										`UpdateUser`=@UpdateUser 
								WHERE Id=@Id");
            return Execute(selectSql.ToString(), request);
        }

        public List<Pub_Insurance> GetPubInsurancesForIds(List<int> patIds)
        {
            StringBuilder selectSql = new StringBuilder();
            selectSql.Append(@"SELECT   Id,
										PatId,
										PatName,
										InsuranceCard,
										InsuranceCompany,
                                        InsuranceName,
										InsuranceStatus,
										ValidityDate,
										IdentityFrontUrl,
										IdentityBackUrl,
										InsuranceCardUrl,
										IsReadInsuInfo,
										Remark,
										CreateTime,
										CreateUser,
										UpdateTime,
										UpdateUser
							FROM pub_insurance WHERE PatId IN @PatId ");
            return Query<Pub_Insurance>(selectSql.ToString(), new { PatId = patIds }).ToList();
        }

        public Pub_Map_User_Insurance GetPubMapUserInsurance(int id)
        {
            StringBuilder selectSql = new StringBuilder();
            selectSql.Append(@"SELECT * FROM pub_map_user_insurance WHERE Id = @Id ");
            return Query<Pub_Map_User_Insurance>(selectSql.ToString(), new { Id = id }).FirstOrDefault();
        }



    }
}
