package service

import (
	"XT_Admin_Api/models"
	"XT_Admin_Api/models/admin_models"
	"XT_Admin_Api/utils"
	"fmt"
	"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, is_super_admin int64) (list []*models.Org, err error, total int64) {

	fmt.Println(follow)
	offset := (page - 1) * limit
	fmt.Println(offset)
	db := readUserDb.Model(&models.Org{})

	if len(keyword) > 0 {
		likeKey := "%" + keyword + "%"
		db = db.Where("contact_name  LIKE ? Or org_name LIKE ? Or org_short_name Like ? ", likeKey, likeKey, 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 AND sgj_user_admin_login_log.operate_type = 1 ")
			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 AND sgj_user_admin_login_log.operate_type = 1 ")
			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 AND sgj_user_admin_login_log.operate_type = 1 ")
			break
		}

		switch depth_active_status {
		case 1:
			now := time.Now()
			zeroHourTimeOfToday := utils.ZeroHourTimeOfDay(now)
			db = db.Joins("JOIN sgj_xt.xt_dialysis_order ON sgj_xt.xt_dialysis_order.user_org_id = sgj_user_org.id AND sgj_xt.xt_dialysis_order.created_time >= ? AND sgj_xt.xt_dialysis_order.created_time <= ?", zeroHourTimeOfToday.Unix(), now.Unix())

			break
		case 2:
			db = db.Joins("JOIN sgj_xt.xt_dialysis_order ON sgj_xt.xt_dialysis_order.user_org_id = sgj_user_org.id  AND DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(sgj_xt.xt_dialysis_order.created_time)) AND sgj_xt.xt_dialysis_order.status = 1")

			break
		case 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_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(sgj_xt.xt_dialysis_order.created_time)) AND sgj_xt.xt_dialysis_order.status = 1")

			break
		case 4:
			db = db.Joins("JOIN sgj_xt.xt_dialysis_order on sgj_xt.xt_dialysis_order.user_org_id = sgj_user_org.id AND DATE_SUB(CURDATE(), INTERVAL 1 Year) <= date(from_unixtime(sgj_xt.xt_dialysis_order.created_time)) AND sgj_xt.xt_dialysis_order.status = 1")
			break
		}

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

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

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

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

		if follow != 0 {
			db = db.Joins("JOIN sgj_org_follow on sgj_org_follow.org_id = sgj_user_org.id AND sgj_org_follow.admin_user_id = ? AND sgj_org_follow.status = 1", follow)
		}

	}
	//if is_super_admin == 1 {
	db = db.Where("sgj_user_org.status = 1 AND sgj_user_org.import <> 1").Group("sgj_user_org.id")
	//
	//} else {
	//	db = db.Where("sgj_user_org.status = 1 AND sgj_user_org.import <> 1 AND DATE_SUB(CURDATE(), INTERVAL 15 DAY) <= date(from_unixtime(sgj_user_org.ctime))").Group("sgj_user_org.id")
	//}

	db = db.Count(&total)
	err = db.Preload("OrgFollow", func(db *gorm.DB) *gorm.DB {
		return db.Model(&models.OrgFollow{}).Where("status = 1").Preload("AdminAccount", "status = 1")
	}).Preload("AdminUserLoginLog", func(db *gorm.DB) *gorm.DB {
		return db.Model(&models.AdminUserLoginLog{}).Where("id = (SELECT max(id))")
	}).Preload("Citys").Offset(offset).Limit(limit).Order("sgj_user_org.ctime DESC").Find(&list).Error
	return
}

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

func GetDistrictsById(id int64) (district models.District, err error) {
	err = readUserDb.Model(&models.District{}).Where("id = ? ", id).First(&district).Error
	return
}

func GetAllOrgType() (types []*models.OrgType, err error) {
	err = readUserDb.Model(&models.OrgType{}).Find(&types).Error
	return
}

//设置今天
func GetOrgDialysisCount(statices_type int64, org_id int64, from int64, to int64) (count int64, err error) {
	var counts int64

	switch statices_type {
	case 1: //病人管理
		rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_patients WHERE user_org_id = ? AND created_time >= ? AND created_time <= ? AND status = 1", org_id, from, to).Rows()
		defer rows.Close()
		if err != nil {
			return 0, err
		}
		if rows.Next() {
			rows.Scan(&counts)
		}
		break
	case 2: //透析处方
		rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_dialysis_prescription WHERE user_org_id = ? AND created_time >= ? AND created_time <= ? AND status = 1", org_id, from, to).Rows()
		defer rows.Close()

		if err != nil {
			return 0, err
		}
		if rows.Next() {
			rows.Scan(&counts)
		}
		break
	case 3: //接诊评估
		rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_receive_treatment_asses WHERE user_org_id = ? AND created_time >= ? AND created_time <= ? AND status = 1", org_id, from, to).Rows()
		defer rows.Close()

		if err != nil {
			return 0, err
		}
		if rows.Next() {
			rows.Scan(&counts)
		}
		break
	case 4: //透前评估
		rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_assessment_before_dislysis WHERE user_org_id = ? AND created_time >= ? AND created_time <= ? AND status = 1", org_id, from, to).Rows()
		defer rows.Close()

		if err != nil {
			return 0, err
		}
		if rows.Next() {
			rows.Scan(&counts)
		}
		break
	case 5: //临时医嘱
		rows, err := readDb.Raw("SELECT COUNT(DISTINCT patient_id) AS count FROM xt_doctor_advice WHERE user_org_id = ? AND created_time >= ? AND created_time <= ? AND status = 1", org_id, from, to).Rows()
		defer rows.Close()

		if err != nil {
			return 0, err
		}
		if rows.Next() {
			rows.Scan(&counts)
		}
		break
	case 6: //双人核对
		rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_double_check WHERE user_org_id = ? AND created_time >= ? AND created_time <= ? AND status = 1", org_id, from, to).Rows()
		defer rows.Close()

		if err != nil {
			return 0, err
		}
		if rows.Next() {
			rows.Scan(&counts)
		}
		break
	case 7: //透析上机
		rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_dialysis_order WHERE user_org_id = ? AND created_time >= ? AND created_time <= ?  AND status = 1 AND (stage = 2 OR stage = 1)", org_id, from, to).Rows()
		defer rows.Close()

		if err != nil {
			return 0, err
		}
		if rows.Next() {
			rows.Scan(&counts)
		}
		break

	case 8: //透析监测
		rows, err := readDb.Raw("SELECT COUNT(DISTINCT patient_id) AS count FROM xt_monitoring_record WHERE user_org_id = ? AND created_time >= ? AND created_time <= ? AND status = 1", org_id, from, to).Rows()
		defer rows.Close()

		if err != nil {
			return 0, err
		}
		if rows.Next() {
			rows.Scan(&counts)
		}
		break
	case 9: //透析下机
		rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_dialysis_order WHERE user_org_id = ? AND created_time >= ? AND created_time <= ? AND  stage = 2  AND status = 1", org_id, from, to).Rows()
		defer rows.Close()

		if err != nil {
			return 0, err
		}
		if rows.Next() {
			rows.Scan(&counts)
		}
		break
	case 10: //透后评估
		rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_assessment_after_dislysis WHERE user_org_id = ? AND created_time >= ? AND created_time <= ? AND status = 1", org_id, from, to).Rows()
		defer rows.Close()

		if err != nil {
			return 0, err
		}
		if rows.Next() {
			rows.Scan(&counts)
		}
		break
	case 11: //入库单
		rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_warehouse WHERE org_id = ? AND ctime >= ? AND mtime <= ? AND status = 1", org_id, from, to).Rows()
		defer rows.Close()

		if err != nil {
			return 0, err
		}
		if rows.Next() {
			rows.Scan(&counts)
		}
		break
	case 12: //出库单
		rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_warehouse_out WHERE org_id = ? AND ctime >= ? AND mtime <= ? AND status = 1", org_id, from, to).Rows()
		defer rows.Close()

		if err != nil {
			return 0, err
		}
		if rows.Next() {
			rows.Scan(&counts)
		}
		break
	}
	return counts, nil
}

//获取昨天的数据
func GetYesterDayOrgDialysisCount(statices_type int64, org_id int64) (weekStatistics []*admin_models.Statistics, err error) {
	fmt.Println(statices_type)

	switch statices_type {
	case 1: //病人管理
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_patients  where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(created_time)) AND status = 1  AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 2: //透析处方
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_dialysis_prescription  where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 3: //接诊评估
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_receive_treatment_asses  where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 4: //透前评估
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_assessment_before_dislysis  where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
		break

	case 5: //临时医嘱
		err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_doctor_advice  where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 6: //双人核对
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_double_check  where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 7: //透析上机
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_dialysis_order  where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(created_time))  AND status = 1  AND user_org_id = ? AND (stage = 2 OR stage = 1)  GROUP BY times", org_id).Scan(&weekStatistics).Error
		break

	case 8: //透析监测
		err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_monitoring_record  where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ?  GROUP BY times", org_id).Scan(&weekStatistics).Error
		break
	case 9: //透析下机
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_dialysis_order  where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(created_time)) AND stage = 2 AND status = 1  AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 10: //透后评估
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_assessment_after_dislysis  where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(created_time)) AND status = 1  AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
		break
	case 11: //入库单
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times  from xt_warehouse  where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(ctime)) AND status = 1 AND org_id = ?  GROUP BY times", org_id).Scan(&weekStatistics).Error
		break
	case 12: //出库单
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times  from xt_warehouse_out  where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
		break
	}

	return
}

//获取近七天的数据
func GetWeekOrgDialysisCount(statices_type int64, org_id int64) (weekStatistics []*admin_models.Statistics, err error) {
	fmt.Println(statices_type)

	switch statices_type {
	case 1: //病人管理
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_patients  where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(created_time)) AND status = 1  AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 2: //透析处方
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_dialysis_prescription  where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 3: //接诊评估
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_receive_treatment_asses  where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 4: //透前评估
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_assessment_before_dislysis  where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
		break

	case 5: //临时医嘱
		err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_doctor_advice  where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 6: //双人核对
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_double_check  where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 7: //透析上机
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_dialysis_order  where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(created_time))  AND status = 1  AND user_org_id = ? AND (stage = 2 OR stage = 1)  GROUP BY times", org_id).Scan(&weekStatistics).Error
		break

	case 8: //透析监测
		err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_monitoring_record  where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ?  GROUP BY times", org_id).Scan(&weekStatistics).Error
		break
	case 9: //透析下机
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_dialysis_order  where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(created_time)) AND stage = 2 AND status = 1  AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 10: //透后评估
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_assessment_after_dislysis  where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(created_time)) AND status = 1  AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
		break
	case 11: //入库单
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times  from xt_warehouse  where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(ctime)) AND status = 1 AND org_id = ?  GROUP BY times", org_id).Scan(&weekStatistics).Error
		break
	case 12: //出库单
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times  from xt_warehouse_out  where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
		break
	}

	return
}

func GetMonthOrgDialysisCount(statices_type int64, org_id int64) (weekStatistics []*admin_models.Statistics, err error) {
	switch statices_type {
	case 1: //病人管理
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_patients  where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(created_time)) AND status = 1  AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 2: //透析处方
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_dialysis_prescription  where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 3: //接诊评估
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_receive_treatment_asses  where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 4: //透前评估
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_assessment_before_dislysis  where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
		break

	case 5: //临时医嘱
		err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_doctor_advice  where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 6: //双人核对
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_double_check  where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 7: //透析下机
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_dialysis_order  where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(created_time)) AND status = 1  AND user_org_id = ?  AND (stage = 2 OR stage = 1) GROUP BY times", org_id).Scan(&weekStatistics).Error
		break

	case 8: //透析监测
		err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_monitoring_record  where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ?  GROUP BY times", org_id).Scan(&weekStatistics).Error
		break
	case 9: //透析下机
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_dialysis_order  where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(created_time)) AND stage = 2 AND status = 1  AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 10: //透后评估
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times  from xt_assessment_after_dislysis  where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(created_time)) AND status = 1  AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
		break
	case 11: //入库单
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times  from xt_warehouse  where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(ctime)) AND status = 1 AND org_id = ?  GROUP BY times", org_id).Scan(&weekStatistics).Error
		break
	case 12: //出库单
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times  from xt_warehouse_out  where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
		break
	}
	return
}

func GetThreeMonthOrgDialysisCount(statices_type int64, org_id int64) (weekStatistics []*admin_models.Statistics, err error) {
	switch statices_type {
	case 1: //病人管理
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_patients  where DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(from_unixtime(created_time)) AND status = 1  AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 2: //透析处方
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_dialysis_prescription  where DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 3: //接诊评估
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_receive_treatment_asses  where DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 4: //透前评估
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_assessment_before_dislysis  where DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
		break

	case 5: //临时医嘱
		err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_doctor_advice  where DATE_SUB(CURDATE(),INTERVAL 3 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 6: //双人核对
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_double_check  where DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 7: //透析上机
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_dialysis_order  where DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(from_unixtime(created_time))  AND status = 1  AND user_org_id = ? AND (stage = 2 OR stage = 1) GROUP BY times", org_id).Scan(&weekStatistics).Error
		break

	case 8: //透析监测
		err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_monitoring_record  where DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times ", org_id).Scan(&weekStatistics).Error
		break
	case 9: //透析下机
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_dialysis_order  where DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(from_unixtime(created_time)) AND stage = 2 AND status = 1  AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 10: //透后评估
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_assessment_after_dislysis  where DATE_SUB(CURDATE(),INTERVAL 3 MONTH) <= date(from_unixtime(created_time)) AND status = 1  AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
		break
	case 11: //入库单
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m') as times  from xt_warehouse  where DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times ", org_id).Scan(&weekStatistics).Error
		break
	case 12: //出库单
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m') as times  from xt_warehouse_out  where DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
		break
	}
	return
}

func GetSixMonthOrgDialysisCount(statices_type int64, org_id int64) (weekStatistics []*admin_models.Statistics, err error) {
	switch statices_type {
	case 1: //病人管理
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_patients  where DATE_SUB(CURDATE(), INTERVAL 6 MONTH) <= date(from_unixtime(created_time)) AND status = 1  AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 2: //透析处方
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_dialysis_prescription  where DATE_SUB(CURDATE(), INTERVAL 6 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 3: //接诊评估
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_receive_treatment_asses  where DATE_SUB(CURDATE(), INTERVAL 6 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 4: //透前评估
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_assessment_before_dislysis  where DATE_SUB(CURDATE(), INTERVAL 6 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
		break

	case 5: //临时医嘱
		err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_doctor_advice  where DATE_SUB(CURDATE(),INTERVAL 6 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 6: //双人核对
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_double_check  where DATE_SUB(CURDATE(), INTERVAL 6 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 7: //透析上机
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_dialysis_order  where DATE_SUB(CURDATE(), INTERVAL 6 MONTH) <= date(from_unixtime(created_time))  AND status = 1  AND user_org_id = ? AND (stage = 2 OR stage = 1) GROUP BY times", org_id).Scan(&weekStatistics).Error
		break

	case 8: //透析监测
		err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_monitoring_record  where DATE_SUB(CURDATE(), INTERVAL 6 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times ", org_id).Scan(&weekStatistics).Error
		break
	case 9: //透析下机
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_dialysis_order  where DATE_SUB(CURDATE(), INTERVAL 6 MONTH) <= date(from_unixtime(created_time)) AND stage = 2 AND status = 1  AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 10: //透后评估
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_assessment_after_dislysis  where DATE_SUB(CURDATE(),INTERVAL 6 MONTH) <= date(from_unixtime(created_time)) AND status = 1  AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
		break
	case 11: //入库单
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m') as times  from xt_warehouse  where DATE_SUB(CURDATE(), INTERVAL 6 MONTH) <= date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times ", org_id).Scan(&weekStatistics).Error
		break
	case 12: //出库单
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m') as times  from xt_warehouse_out  where DATE_SUB(CURDATE(), INTERVAL 6 MONTH) <= date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
		break
	}
	return
}

func GetYearOrgDialysisCount(statices_type int64, org_id int64) (weekStatistics []*admin_models.Statistics, err error) {
	switch statices_type {
	case 1: //病人管理
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_patients  where DATE_SUB(CURDATE(), INTERVAL 12 MONTH) <= date(from_unixtime(created_time)) AND status = 1  AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 2: //透析处方
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_dialysis_prescription  where DATE_SUB(CURDATE(), INTERVAL 12 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 3: //接诊评估
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_receive_treatment_asses  where DATE_SUB(CURDATE(), INTERVAL 12 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 4: //透前评估
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_assessment_before_dislysis  where DATE_SUB(CURDATE(), INTERVAL 12 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
		break

	case 5: //临时医嘱
		err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_doctor_advice  where DATE_SUB(CURDATE(),INTERVAL 12 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 6: //双人核对
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_double_check  where DATE_SUB(CURDATE(), INTERVAL 12 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 7: //透析上机
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_dialysis_order  where DATE_SUB(CURDATE(), INTERVAL 12 MONTH) <= date(from_unixtime(created_time))  AND status = 1  AND user_org_id = ? AND (stage = 2 OR stage = 1) GROUP BY times", org_id).Scan(&weekStatistics).Error
		break

	case 8: //透析监测
		err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_monitoring_record  where DATE_SUB(CURDATE(), INTERVAL 12 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times ", org_id).Scan(&weekStatistics).Error
		break
	case 9: //透析下机
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_dialysis_order  where DATE_SUB(CURDATE(), INTERVAL 12 MONTH) <= date(from_unixtime(created_time)) AND stage = 2 AND status = 1  AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error

		break
	case 10: //透后评估
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times  from xt_assessment_after_dislysis  where DATE_SUB(CURDATE(),INTERVAL 12 MONTH) <= date(from_unixtime(created_time)) AND status = 1  AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
		break
	case 11: //入库单
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m') as times  from xt_warehouse  where DATE_SUB(CURDATE(), INTERVAL 12 MONTH) <= date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times ", org_id).Scan(&weekStatistics).Error
		break
	case 12: //出库单
		err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m') as times  from xt_warehouse_out  where DATE_SUB(CURDATE(), INTERVAL 12 MONTH) <= date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
		break
	}
	return
}

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

func FindAllFollower() (follower []*admin_models.AdminAccount, err error) {
	err = readUserDb.Model(&admin_models.AdminAccount{}).Where("status = 1 AND is_super_admin <> 1").Find(&follower).Error
	return

}

func FindPatientById(id int64, page int64, limit int64) (patients []*models.Patients, err error, total int64) {
	offset := (page - 1) * limit
	db := readDb.Model(&models.Patients{}).Where("user_org_id=? AND status = 1", id)
	db = db.Count(&total)
	err = db.Offset(offset).Limit(limit).Find(&patients).Error
	return
}

func GetAppTypeByOrgId(org_id int64) (appType models.OrgApp, err error) {
	err = readUserDb.Model(&models.OrgApp{}).Where("app_type = 3 AND org_id = ?", org_id).First(&appType).Error
	return
}

func GetRoles(orgID int64, appID int64, page int, count int) ([]*models.Role, int, error) {
	var roles []*models.Role
	var totalCount int
	err := readUserDb.Model(&models.Role{}).
		Where("org_id = ? AND app_id = ?", orgID, appID).
		Count(&totalCount).
		Order("ctime asc").Limit(count).Offset((page - 1) * count).
		Find(&roles).
		Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return make([]*models.Role, 0), 0, nil
		} else {
			return nil, 0, err
		}
	}
	return roles, totalCount, nil
}

type AdminUserManageViewModel struct {
	AdminUserId int    `gorm:"admin_user_id" json:"user_id"`
	UserName    string `gorm:"user_name" json:"user_name"`
	RoleName    string `gorm:"role_name" json:"role_name"`
	UserTitle   int    `gorm:"user_title" json:"user_title"`
	Ip          string `gorm:"ip" json:"ip"`
	LastTime    int64  `gorm:"ctime" json:"last_login_time"`
	Status      int    `gorm:"status" json:"status"`
	Mobile      string `gorm:"mobile" json:"mobile"`

	// LastLoginTimeStr string `gorm:"-" json:"last_login_time_formatted"`
	TitleName string `gorm:"-" json:"title_name"`
}

func GetAdminUsersAndLoginInfo(orgID int64, appID int64, page int64, count int64) ([]*AdminUserManageViewModel, int, error) {
	if count <= 0 {
		return []*AdminUserManageViewModel{}, 0, nil
	}
	if page < 1 {
		page = 1
	}
	var viewModels []*AdminUserManageViewModel = make([]*AdminUserManageViewModel, 0)
	rows, err := readUserDb.Raw("SELECT u_a_r.admin_user_id, u_a_r.user_name, u_r.role_name, u_a_r.user_title, u_l.ip, u_l.last_time, u_a_r.status, admin.mobile FROM sgj_user_admin_role AS u_a_r JOIN sgj_user_admin AS admin ON admin.id = u_a_r.admin_user_id  INNER JOIN sgj_user_role AS u_r ON u_a_r.org_id = u_r.org_id AND u_a_r.app_id = u_r.app_id AND u_r.id = u_a_r.role_id LEFT JOIN (SELECT * FROM (SELECT admin_user_id, org_id, app_id, ip, MAX(ctime) as last_time FROM sgj_user_admin_login_log WHERE org_id = ? AND app_id = ? GROUP BY admin_user_id ORDER BY ctime DESC) AS t GROUP BY admin_user_id) AS u_l ON u_a_r.org_id = u_l.org_id AND u_a_r.app_id = u_l.app_id AND u_a_r.admin_user_id = u_l.admin_user_id WHERE u_a_r.org_id = ? AND u_a_r.app_id = ? GROUP BY u_a_r.admin_user_id LIMIT ? OFFSET ?;", orgID, appID, orgID, appID, count, (page-1)*count).Rows()
	defer rows.Close()
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return viewModels, 0, nil
		} else {
			return nil, 0, err
		}
	}
	for rows.Next() {
		var viewModel AdminUserManageViewModel
		readUserDb.ScanRows(rows, &viewModel)
		title, _ := models.UserTitle[viewModel.UserTitle]
		viewModel.TitleName = title
		viewModels = append(viewModels, &viewModel)
	}
	total := 0
	readUserDb.Table("sgj_user_admin_role as u_a_r").Joins("join sgj_user_role as u_r on u_r.org_id = u_a_r.org_id AND u_r.app_id = u_a_r.app_id AND u_r.id = u_a_r.role_id").Where("u_a_r.org_id = ? AND u_a_r.app_id = ?", orgID, appID).Count(&total)
	return viewModels, total, nil
}

func GetAllValidDeviceZones(orgID int64) ([]*models.DeviceZone, error) {
	var zones []*models.DeviceZone
	err := readDb.Model(&models.DeviceZone{}).Where("org_id = ? AND status = 1", orgID).Find(&zones).Error
	if err != nil {
		return nil, err
	}
	return zones, nil
}

func GetAllValidDeviceGroups(orgID int64) ([]*models.DeviceGroup, error) {
	var groups []*models.DeviceGroup
	err := readDb.Model(&models.DeviceGroup{}).Where("org_id = ? AND status = 1", orgID).Find(&groups).Error
	if err != nil {
		return nil, err
	}
	return groups, nil
}

type DeviceNumberViewModel struct {
	models.DeviceNumber
	ZoneName  string `gorm:"column:zone_name" json:"zone_name"`
	GroupName string `gorm:"column:group_name" json:"group_name"`
}

func GetAllValidDeviceNumbers(orgID int64) ([]*DeviceNumberViewModel, error) {
	var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
	rows, err := readDb.Raw("SELECT n.*, z.name as zone_name, g.name as group_name FROM xt_device_number as n join xt_device_zone as z on z.id = n.zone_id join xt_device_group as g on g.id = n.group_id WHERE (n.org_id = ? AND n.status = 1)", orgID).Rows()
	defer rows.Close()
	if err != nil {
		return nil, err
	}
	for rows.Next() {
		var vm DeviceNumberViewModel
		readDb.ScanRows(rows, &vm)
		vms = append(vms, &vm)
	}
	return vms, nil
}

func GetFollowOrgList(follow int64) (list []*models.Org, err error) {

	err = readUserDb.Model(&models.Org{}).Where("NOT EXISTS (Select * FROM sgj_org_follow Where sgj_org_follow.org_id = sgj_user_org.id AND sgj_org_follow.status = 1) AND import <> 1 AND DATE_SUB(CURDATE(), INTERVAL 15 DAY) <= date(from_unixtime(sgj_user_org.ctime)) AND status = 1").Preload("OrgFollow", func(db *gorm.DB) *gorm.DB {
		return db.Model(&models.OrgFollow{}).Where("status = 1").Preload("AdminAccount", "status = 1")
	}).Preload("AdminUserLoginLog", func(db *gorm.DB) *gorm.DB {
		return db.Model(&models.AdminUserLoginLog{}).Where("id = (SELECT max(id))")
	}).Preload("Citys").Order("ctime desc").Scan(&list).Error

	return

}

type ActiveOrg struct {
	OrgId   int64  `gorm:"org_id" json:"org_id"`
	OrgName string `gorm:"org_name" json:"org_name"`
}

func GetActiveOrgList(from int64, to int64) (list []*ActiveOrg, err error) {
	err = readUserDb.Raw("Select a.org_id as org_id, org.org_name as org_name from (Select org_id  from sgj_user_admin_login_log log  JOIN  (Select d.user_org_id from (Select new_role.user_org_id from  (Select count(*) as count,org_id as user_org_id from sgj_users.sgj_user_admin_role role  where  role.`user_type` = 2 OR role.`user_type` = 3 Group by role.org_id) new_role where new_role.count > 2) d  JOIN (Select user_org_id from (Select count(*) as count,user_org_id from sgj_xt.xt_patients  where  status = 1  Group by user_org_id) new_patient where new_patient.count > 3) c On d.user_org_id = c.user_org_id) new_user_org ON new_user_org.user_org_id = log.org_id where ctime >= ? AND ctime <= ?  Group by log.org_id) a JOIN sgj_users.sgj_user_org org On a.org_id = org.id", from, to).Scan(&list).Error
	return
}

func GetNewCreateOrgList(from int64, to int64) (list []*ActiveOrg, err error) {
	err = readUserDb.Raw("select org.id as org_id, org.org_name as org_name  from sgj_user_org org  where   org.status = 1 AND org.import <> 1 AND org.ctime >= ? AND org.ctime <= ?", from, to).Scan(&list).Error
	return
}

type DoctorNurse struct {
	Date   string `gorm:"date" json:"date"`
	Mobile string `gorm:"mobile" json:"mobile"`
}

func GetNewDoctorNurseList(from int64, to int64) (list []*DoctorNurse, err error) {
	err = readUserDb.Raw("select from_unixtime(log.`ctime`, '%Y-%m-%d %H:%m:%s') as date, admin.mobile as mobile from sgj_user_admin_login_log  log Join  `sgj_user_admin` admin on log.`admin_user_id` = admin.`id` Where log.`operate_type` = 1 and log.`app_type` = 3 and log.ctime >= ? and log.ctime <= ?", from, to).Scan(&list).Error
	return
}

type Patient struct {
	CreatedTime string `gorm:"created_time" json:"created_time"`
	Name        string `gorm:"name" json:"name"`
}

func GetNewPatientList(from int64, to int64) (list []*Patient, err error) {
	err = readDb.Raw("select name, from_unixtime(created_time, '%Y-%m-%d %H:%m:%s') as created_time from xt_patients where created_time >= ? AND created_time <= ? AND status = 1", from, to).Scan(&list).Error
	return
}
