// 统计后台的 service

package service

import (
	"Data_Upload_Api/models"
	"Data_Upload_Api/models/sz"

	"github.com/jinzhu/gorm"
	"strings"
)

func FindAllDataUploadConfigOrgInfo(province_id int64, city_id int64, config_type int64) (data []*sz.DataUploadConfig, err error) {

	err = readDb.Model(&sz.DataUploadConfig{}).Where("status = 1 AND province_id = ? AND city_id = ? AND config_type = ?", province_id, city_id, config_type).Find(&data).Error
	return
}

func FindDataUploadOrgInfo(org_id int64, module_type int64) (data sz.DataUpload, err error) {
	err = readDb.Model(&sz.DataUpload{}).Where(" org_id = ? AND module_type = ?", org_id, module_type).Last(&data).Error
	return
}

func FindOrgData(org_id int64) (org models.UserOrg, err error) {
	err = readUserDb.Model(&models.UserOrg{}).Where("status = 1 AND id = ?", org_id).Preload("DeviceNumber", func(db *gorm.DB) *gorm.DB {
		return readDb.Where("status = 1")
	}).Find(&org).Error
	return
}
func FindOrgRolesData(org_id int64, lastUploadTime int64, nowTime int64) (role []*models.UserAdminRole, err error) {
	db := readUserDb.Model(&models.UserAdminRole{}).Where("status = 1 AND org_id = ?  ", org_id)
	if lastUploadTime != 0 && nowTime != 0 {
		db.Where("ctime >= ? AND ctime <= ?", lastUploadTime, nowTime)
	}
	err = db.Find(&role).Error
	return
}
func FindOrgPatientData(org_id int64, lastUploadTime int64, nowTime int64) (patient []*models.Patients, err error) {
	db := readDb.Model(&models.Patients{}).Where("status = 1 AND user_org_id = ?", org_id)
	if lastUploadTime != 0 && nowTime != 0 {
		db = db.Where("created_time >= ? AND created_time <= ?", lastUploadTime, nowTime)
	}
	err = db.Find(&patient).Error

	return
}

func FindOrgPatientOutData(org_id int64, lastUploadTime int64, nowTime int64) (patient []*models.Patients, err error) {
	db := readDb.Model(&models.Patients{}).Where("status = 1 AND user_org_id = ? AND lapseto = 2 ", org_id).Preload("PatientLapseto", "status = 1")
	if lastUploadTime != 0 && nowTime != 0 {
		db = db.Where("created_time >= ? AND created_time <= ?", lastUploadTime, nowTime)
	}
	err = db.Find(&patient).Error
	return
}

func FindOrgScheduleData(org_id int64, lastUploadTime int64, nowTime int64) (sch []*models.Schedule, err error) {
	db := readDb.Model(&models.Schedule{}).Where("status = 1 AND user_org_id = ?", org_id).Preload("DeviceNumber", "status = 1")

	if lastUploadTime != 0 && nowTime != 0 {
		db = db.Where("created_time >= ? AND created_time <= ?", lastUploadTime, nowTime)
	}
	err = db.Find(&sch).Error
	return
}

func FindOrgDialysisPrescriptionData(org_id int64, lastUploadTime int64, nowTime int64) (ps []*models.DialysisPrescription, err error) {
	db := readDb.Model(&models.DialysisPrescription{}).Where("status = 1 AND user_org_id = ?", org_id).Preload("AssessmentBeforeDislysis", "status = 1").Preload("DialysisOrder", "status = 1")
	if lastUploadTime != 0 && nowTime != 0 {
		db = db.Where("created_time >= ? AND created_time <= ?", lastUploadTime, nowTime)
	}
	err = db.Find(&ps).Error
	return
}

func FindOrgMonitorRecordData(org_id int64, lastUploadTime int64, nowTime int64) (monitor []*models.MonitoringRecord, err error) {
	db := readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND user_org_id = ?", org_id)

	if lastUploadTime != 0 && nowTime != 0 {
		db = db.Where("created_time >= ? AND created_time <= ?", lastUploadTime, nowTime)
	}
	err = db.Find(&monitor).Error

	return
}

func CreateOrgRecord(hospital *sz.TempHdHospital) (err error) {
	err = writeDb.Model(&sz.TempHdHospital{}).Create(&hospital).Error
	return
}

func CreateUploadRecord(upload *sz.DataUpload) (err error) {
	err = writeDb.Model(&sz.DataUpload{}).Create(&upload).Error
	return
}

func BatchCreateStaffRecord(staffs []*sz.TempHdStaff) (err error) {
	if len(staffs) > 0 {
		utx := writeDb.Begin()
		if len(staffs) > 0 {
			thisSQL := "INSERT INTO temp_hd_staff (hospital_id, staff_id, staff_name, position, permanent_type,create_time,update_time,sjscsj,mj) VALUES "
			insertParams := make([]string, 0)
			insertData := make([]interface{}, 0)
			for _, info := range staffs {
				insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?)")
				insertData = append(insertData, info.HospitalId)
				insertData = append(insertData, info.StaffId)
				insertData = append(insertData, info.StaffName)
				insertData = append(insertData, info.Position)
				insertData = append(insertData, info.PermanentType)
				insertData = append(insertData, info.CreateTime)
				insertData = append(insertData, info.UpdateTime)
				insertData = append(insertData, info.Sjscsj)
				insertData = append(insertData, info.Xgbz)
			}
			thisSQL += strings.Join(insertParams, ", ")
			err = utx.Exec(thisSQL, insertData...).Error
			if err != nil {
				utx.Rollback()
				return
			}
		}
		utx.Commit()
	}
	return
}
func FindOrgDialysisData(org_id int64, lastUploadTime int64, nowTime int64) (ps []*models.VMSchedule, err error) {
	db := readDb.Model(&models.VMSchedule{}).Where("status = 1 AND user_org_id = ? ", org_id).
		Preload("DeviceNumber", "status = 1").
		Preload("VMDialysisPrescription", "status = 1").
		Preload("AssessmentBeforeDislysis", "status = 1").
		Preload("AssessmentAfterDislysis", "status = 1").
		Preload("DialysisOrder", "status = 1").
		Preload("MonitoringRecord", "status = 1")

	if lastUploadTime != 0 && nowTime != 0 {
		db = db.Where("created_time >= ? AND created_time <= ?", lastUploadTime, nowTime)
	}
	err = db.Find(&ps).Error
	return
}
func FindOrgDeviceZoneRecordData(org_id int64, lastUploadTime int64, nowTime int64) (zone []*models.DeviceZone, err error) {
	db := readDb.Model(&models.DeviceZone{}).Where("status = 1 AND  org_id = ?", org_id)
	if lastUploadTime != 0 && nowTime != 0 {
		db = db.Where("ctime >= ? AND ctime <= ?", lastUploadTime, nowTime)
	}
	err = db.Find(&zone).Error

	return
}
