package service

import (
	"XT_Admin_Api/models"
	"XT_Admin_Api/utils"
	"github.com/jinzhu/gorm"
	"time"
)

func GetAllOrgList(active_status int64,depth_active_status int64,start_time int64,end_time int64,province int64,city int64,district int64,follow int64,keyword string,page int64, limit int64) (list []*models.Org, err error, total int64){
	offset := (page - 1) * limit

	db := readUserDb.Model(&models.Org{})
	if len(keyword) > 0{
		likeKey := "%" + keyword + "%"
		db = db.Where("contact_name  LIKE ?",likeKey)
	}else{

		switch active_status {
		case 1:
			now := time.Now()
			zeroHourTimeOfToday := utils.ZeroHourTimeOfDay(now)
			db = db.Joins("JOIN sgj_user_admin_login_log on sgj_user_admin_login_log.org_id = sgj_user_org.id AND sgj_user_admin_login_log.ctime >= ? AND sgj_user_admin_login_log.ctime <= ? AND sgj_user_admin_login_log.app_type = 3",zeroHourTimeOfToday.Unix(),now.Unix())
			break
		case 2:
			db = db.Joins("JOIN sgj_user_admin_login_log on sgj_user_admin_login_log.org_id = sgj_user_org.id AND DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(sgj_user_admin_login_log.ctime)) AND sgj_user_admin_login_log.app_type = 3")
			break
		case 3:
			db = db.Joins("JOIN sgj_user_admin_login_log on sgj_user_admin_login_log.org_id = sgj_user_org.id AND DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= date(from_unixtime(sgj_user_admin_login_log.ctime)) AND sgj_user_admin_login_log.app_type = 3")
			break
		case 4:
			db = db.Joins("JOIN sgj_user_admin_login_log on sgj_user_admin_login_log.org_id = sgj_user_org.id AND DATE_SUB(CURDATE(), INTERVAL 1 Year) <= date(from_unixtime(sgj_user_admin_login_log.ctime)) AND sgj_user_admin_login_log.app_type = 3")
			break
		}

		switch depth_active_status {
		case 1:
			now := time.Now()
			zeroHourTimeOfToday := utils.ZeroHourTimeOfDay(now)
			db = db.Joins("JOIN sgj_user_admin_login_log on sgj_user_admin_login_log.org_id = sgj_user_org.id AND sgj_user_admin_login_log.ctime >= ? AND sgj_user_admin_login_log.ctime <= ? AND sgj_user_admin_login_log.app_type = 3",zeroHourTimeOfToday.Unix(),now.Unix())
			db = db.Joins("JOIN sgj_xt.xt_dialysis_order on sgj_xt.xt_dialysis_order.user_org_id = sgj_user_org.id AND DATE_FORMAT(date(from_unixtime(sgj_user_admin_login_log.ctime)) ,'%Y-%m-%d') = DATE_FORMAT(date(from_unixtime(sgj_xt.xt_dialysis_order.dialysis_date)) ,'%Y-%m-%d') AND sgj_user_admin_login_log.app_type = 3",zeroHourTimeOfToday.Unix(),now.Unix())

			break
		case 2:
			db = db.Joins("JOIN sgj_user_admin_login_log on sgj_user_admin_login_log.org_id = sgj_user_org.id AND DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(sgj_user_admin_login_log.ctime)) AND sgj_user_admin_login_log.app_type = 3")
			db = db.Joins("JOIN sgj_xt.xt_dialysis_order on sgj_xt.xt_dialysis_order.user_org_id = sgj_user_org.id AND DATE_FORMAT(date(from_unixtime(sgj_user_admin_login_log.ctime)) ,'%Y-%m-%d') = DATE_FORMAT(date(from_unixtime(sgj_xt.xt_dialysis_order.dialysis_date)) ,'%Y-%m-%d') AND sgj_user_admin_login_log.app_type = 3")

			break
		case 3:
			db = db.Joins("JOIN sgj_user_admin_login_log on sgj_user_admin_login_log.org_id = sgj_user_org.id AND DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= date(from_unixtime(sgj_user_admin_login_log.ctime)) AND sgj_user_admin_login_log.app_type = 3")
			db = db.Joins("JOIN sgj_xt.xt_dialysis_order on sgj_xt.xt_dialysis_order.user_org_id = sgj_user_org.id AND DATE_FORMAT(date(from_unixtime(sgj_user_admin_login_log.ctime)) ,'%Y-%m-%d') = DATE_FORMAT(date(from_unixtime(sgj_xt.xt_dialysis_order.dialysis_date)) ,'%Y-%m-%d') AND sgj_user_admin_login_log.app_type = 3")

			break
		case 4:
			db = db.Joins("JOIN sgj_user_admin_login_log on sgj_user_admin_login_log.org_id = sgj_user_org.id AND DATE_SUB(CURDATE(), INTERVAL 1 Year) <= date(from_unixtime(sgj_user_admin_login_log.ctime)) AND sgj_user_admin_login_log.app_type = 3")
			db = db.Joins("JOIN sgj_xt.xt_dialysis_order on sgj_xt.xt_dialysis_order.user_org_id = sgj_user_org.id AND DATE_FORMAT(date(from_unixtime(sgj_user_admin_login_log.ctime)) ,'%Y-%m-%d') = DATE_FORMAT(date(from_unixtime(sgj_xt.xt_dialysis_order.dialysis_date)) ,'%Y-%m-%d') AND sgj_user_admin_login_log.app_type = 3")

			break
		}
		if province > 0{
			db = db.Where("sgj_user_org.province = ?",province)
		}

		if city > 0{
			db = db.Where("sgj_user_org.province = ?",city)
		}

		if district > 0{
			db = db.Where("sgj_user_org.province = ?",district)
		}

		if start_time != 0 {
			db = db.Where("ctime>=?", start_time)
		}
		if end_time != 0 {
			db = db.Where("ctime<=?", end_time)
		}

	}
	err = db.Where("status = 1").Count(&total).Offset(offset).Limit(limit).Find(&list).Error
	return
}


func GetOrgDetailInfo(org_id int64) (info models.Org, err error){
	err = readUserDb.Model(&models.Org{}).Where("org_id = ? AND status = 1",org_id).Preload("AdminUserLoginLog", func(db *gorm.DB) *gorm.DB {
		return db.Model(&models.AdminUserLoginLog{}).Where("status = 1 AND id = (SELECT max(id))")
	}).Find(&info).Error
	return
}



func GetOrgNewPatientInfo(org_id int64, from int64,to int64) (count int64, err error){

	var counts int64
	rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_patients WHERE org_id = ? AND created_time >= ? AND created_time <= ?",org_id ,from, to).Rows()
	if err != nil {
		return 0, err
	}
	if rows.Next() {
		rows.Scan(&counts)
	}
	return counts, nil
}



func GetDistrictsByUpid(id int64) (dis []*models.District, err error) {
	err = readUserDb.Model(&models.District{}).Where("upid=?", id).Find(&dis).Error
	return
}
