using ASHermed.SinoInterface.Api.ContractModel;
using ASHermed.SinoInterface.Api.ContractModel.App;
using ASHermed.SinoInterface.Api.IRepository.App;
using ASHermed.SinoInterface.Repository.MySql;
using PoJun.Util.Maps;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ASHermed.SinoInterface.Api.MySqlRepository.App
{
    public class SupplementaryMaterialsRepository : BaseRepositoryToASHermed_SinoInterface<Sys_Supplementary_Materials>, ISupplementaryMaterialsRepository
    {
        /// <summary>
        /// 新增补充材料
        /// </summary>
        /// <returns></returns>
        public async Task<int> SMR_InsertAsync(AddSupplementaryMaterialsParam param)
        {
            Sys_Supplementary_Materials _Materials = param.MapTo<Sys_Supplementary_Materials>();
            _Materials.Created(param.UserId);
            StringBuilder str = new StringBuilder();
            str.Append(" INSERT INTO `sys_supplementary_materials` ");
            str.Append(@"
(
`MedicalRecords`,
`IsMedicine`,
`Medicine`,
`MedicineUrl`, 
`Temperature`,
`Heartbeat`,
`RespiratoryRate`,
`OxygenSaturation`,
`BloodPressureMin`,
`BloodPressureMax`,
`BloodFatMin`,
`BloodFatMax`,
`Weight`,
`Pulse`,
`PatientId`,
`DoctorId`,
`AptId`,
`DoctorName`,
`DepartmentId`,
`Department`,
`CreateUser`,
`CreateTime`,
`UpdateTime`,
`UpdateUser`, 
`IsDeleted`,
`IsDrugAllergy`, 
`DrugAllergyName`  
) 
");
            str.Append(" VALUES  ");
            str.Append(@"
( 
@MedicalRecords,
@IsMedicine, 
@Medicine,
@MedicineUrl, 
@Temperature,
@Heartbeat,
@RespiratoryRate, 
@OxygenSaturation, 
@BloodPressureMin, 
@BloodPressureMax,
@BloodFatMin,
@BloodFatMax,
@Weight,
@Pulse,
@PatientId, 
@DoctorId, 
@AptId,
@DoctorName, 
@DepartmentId, 
@Department,
@CreateUser, 
@CreateTime,
@UpdateTime, 
@UpdateUser,
@IsDeleted,
@IsDrugAllergy, 
@DrugAllergyName  
);
");
            str.Append("select @@identity; ");
            var parms = new Dictionary<string, object>();
            parms.Add("@MedicalRecords", _Materials.MedicalRecords);
            parms.Add("@IsMedicine", _Materials.IsMedicine);
            parms.Add("@Medicine", _Materials.Medicine);
            parms.Add("@MedicineUrl", _Materials.MedicineUrl);
            parms.Add("@Temperature", _Materials.Temperature);
            parms.Add("@Heartbeat", _Materials.Heartbeat);
            parms.Add("@RespiratoryRate", _Materials.RespiratoryRate);
            parms.Add("@OxygenSaturation", _Materials.OxygenSaturation);
            parms.Add("@BloodPressureMax", _Materials.BloodPressureMax);
            parms.Add("@BloodPressureMin", _Materials.BloodPressureMin);
            parms.Add("@BloodFatMax", _Materials.BloodFatMax);
            parms.Add("@BloodFatMin", _Materials.BloodFatMin);
            parms.Add("@Weight", _Materials.Weight);
            parms.Add("@Pulse", _Materials.Pulse);
            parms.Add("@PatientId", _Materials.PatientId);
            parms.Add("@DoctorId", _Materials.DoctorId);
            parms.Add("@AptId", _Materials.AptId);
            parms.Add("@DoctorName", _Materials.DoctorName);
            parms.Add("@DepartmentId", _Materials.DepartmentId);
            parms.Add("@Department", _Materials.Department);
            parms.Add("@CreateUser", _Materials.CreateUser);
            parms.Add("@CreateTime", _Materials.CreateTime);
            parms.Add("@UpdateTime", _Materials.UpdateTime);
            parms.Add("@UpdateUser", _Materials.UpdateUser);
            parms.Add("@IsDeleted", _Materials.IsDeleted);
            parms.Add("@IsDrugAllergy", _Materials.IsDrugAllergy);
            parms.Add("@DrugAllergyName", _Materials.DrugAllergyName);
            var result = await ExecuteAsync(str.ToString(), parms);
            return result;
        }
        /// <summary>
        /// 修改补充材料
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<int> SMR_UpdateAsync(AddSupplementaryMaterialsParam param)
        {

            Sys_Supplementary_Materials _Materials = param.MapTo<Sys_Supplementary_Materials>();
            _Materials.Updated(param.UserId);
            var parms = new Dictionary<string, object>();
            parms.Add("@MedicalRecords", _Materials.MedicalRecords);
            parms.Add("@IsMedicine", _Materials.IsMedicine);
            parms.Add("@Medicine", _Materials.Medicine);
            parms.Add("@MedicineUrl", _Materials.MedicineUrl);
            parms.Add("@Temperature", _Materials.Temperature);
            parms.Add("@Heartbeat", _Materials.Heartbeat);
            parms.Add("@RespiratoryRate", _Materials.RespiratoryRate);
            parms.Add("@OxygenSaturation", _Materials.OxygenSaturation);
            parms.Add("@BloodPressureMax", _Materials.BloodPressureMax);
            parms.Add("@BloodPressureMin", _Materials.BloodPressureMin);
            parms.Add("@BloodFatMax", _Materials.BloodFatMax);
            parms.Add("@BloodFatMin", _Materials.BloodFatMin);
            parms.Add("@Weight", _Materials.Weight);
            parms.Add("@Pulse", _Materials.Pulse);
            parms.Add("@PatientId", _Materials.PatientId);
            parms.Add("@DoctorId", _Materials.DoctorId);
            parms.Add("@DoctorName", _Materials.DoctorName);
            parms.Add("@DepartmentId", _Materials.DepartmentId);
            parms.Add("@Department", _Materials.Department);
            parms.Add("@UpdateTime", _Materials.UpdateTime);
            parms.Add("@UpdateUser", _Materials.UpdateUser);
            parms.Add("@IsDrugAllergy", _Materials.IsDrugAllergy);
            parms.Add("@DrugAllergyName", _Materials.DrugAllergyName);
            parms.Add("@Id", _Materials.Id);

            StringBuilder str = new StringBuilder();
            str.Append(" UPDATE `sys_supplementary_materials`  ");
            str.Append(@"
SET `MedicalRecords` = @MedicalRecords,
`IsMedicine` = @IsMedicine,
`Medicine` = @Medicine,
`MedicineUrl` = @MedicineUrl, 
`Temperature` = @Temperature, 
`Heartbeat` = @Heartbeat, 
`RespiratoryRate` = @RespiratoryRate,
`OxygenSaturation` = @OxygenSaturation, 
`BloodPressureMax` = @BloodPressureMax, 
`BloodPressureMin` = @BloodPressureMin, 
`BloodFatMax` = @BloodFatMax, 
`BloodFatMin` = @BloodFatMin, 
`Weight` = @Weight, 
`Pulse` =@Pulse, 
`PatientId` = @PatientId, 
`DoctorId` = @DoctorId, 
`DoctorName` = @DoctorName,
`DepartmentId` = @DepartmentId, 
`Department` = @Department, 
`UpdateTime` = @UpdateTime, 
`UpdateUser` = @UpdateUser, 
`IsDrugAllergy` = @IsDrugAllergy, 
`DrugAllergyName` = @DrugAllergyName
WHERE `Id` = @Id;
");
            var result = await ExecuteAsync(str.ToString(), parms);
            return result;
        }
        /// <summary>
        /// 删除 --逻辑删除
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<int> SMR_DeletedAsync(DeletedSupplementaryMaterialsParam param)
        {
            Sys_Supplementary_Materials _Materials = param.MapTo<Sys_Supplementary_Materials>();
            _Materials.Deleted(param.UserId);
            var parms = new Dictionary<string, object>();
            parms.Add("@Id", _Materials.Id);

            StringBuilder str = new StringBuilder();
            str.Append(" UPDATE `sys_supplementary_materials`  ");
            str.Append(@"
SET `IsDeleted` = 1
WHERE `Id` = @Id;
");
            var result = await ExecuteAsync(str.ToString(), parms);
            return result;
        }

        public async Task<AddSupplementaryMaterialsParam> GetSupplementaryMaterials(GetSupplementaryMaterialsParam param)
        {
            AddSupplementaryMaterialsParam res = new AddSupplementaryMaterialsParam();
            var parms = new Dictionary<string, object>();
            parms.Add("@AptId", param.AptId);
            StringBuilder str = new StringBuilder();
            str.Append(" SELECT * FROM `sys_supplementary_materials` WHERE AptId=@AptId ");
            var result = await QueryAsync<Sys_Supplementary_Materials>(str.ToString(), parms);
            res = result.FirstOrDefault().MapTo<AddSupplementaryMaterialsParam>();
            return res;
        }

    }
}
