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) (list []*models.Org, err error, total int64){
	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_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 AND sgj_user_admin_login_log.operate_type = 1 ",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 AND sgj_user_admin_login_log.operate_type = 1 ")

			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 ")
			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 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 ")
			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 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 ")
			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 AND sgj_user_admin_login_log.operate_type = 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)
		}

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


func GetOrgDetailInfo(org_id int64) (info models.Org, err error){
	err = readUserDb.Model(&models.Org{}).Where("id = ? AND status = 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()
		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()
		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()
		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()
		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()
		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()
		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()
		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()
		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()
		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()
		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()
		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()
		if err != nil {
			return 0, err
		}
		if rows.Next() {
			rows.Scan(&counts)
		}
		break
	}
	return counts, nil
}

//获取近七天的数据
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
}
