package services

import (
	"errors"
	"fmt"
	"gin-vue-admin/common"
	"gin-vue-admin/repository"
	"time"
)

func Employees(phone, t string) (interface{}, error) {

	// 1. 查询当前手机号是否有默认医院
	chainCode, employeeID, err := repository.QueryChainCodeAndLocalIDByPhone(phone)
	if err != nil {
		return nil, err
	}

	if chainCode == "" {
		return nil, errors.New("请设置默认医院")
	}

	// 2. 拿到db
	safeMap := common.NewSafeMap()
	db, err := safeMap.GetOrStore(chainCode)
	if err != nil {
		return nil, err
	}

	// TODO. 拿到员工id 获取员工是否是管理员
	fmt.Println("拿到员工id 获取员工是否是管理员", employeeID)
	HospitalLocalId, err := repository.QueryHospitalLocalIDByEmployeeLocalID(db, employeeID)
	if err != nil {
		return nil, err
	}

	return repository.QueryEmployeeList(db, t, HospitalLocalId)
}

func EmployeeDetailList(phone string) (interface{}, error) {

	// 1. 查询当前手机号是否有默认医院
	chainCode, employeeID, err := repository.QueryChainCodeAndLocalIDByPhone(phone)
	if err != nil {
		return nil, err
	}

	if chainCode == "" {
		return nil, errors.New("请设置默认医院")
	}

	// 2. 拿到db
	safeMap := common.NewSafeMap()
	db, err := safeMap.GetOrStore(chainCode)
	if err != nil {
		return nil, err
	}

	HospitalLocalId, err := repository.QueryHospitalLocalIDByEmployeeLocalID(db, employeeID)
	if err != nil {
		return nil, err
	}

	return repository.QueryEmployeeDetailList(db, HospitalLocalId)
}

//
func EmployeesWorkTrend(phone, t string, employeeID int) (interface{}, error) {

	// 1. 查询当前手机号是否有默认医院
	chainCode, _, err := repository.QueryChainCodeAndLocalIDByPhone(phone)
	if err != nil {
		return nil, err
	}

	if chainCode == "" {
		return nil, errors.New("请设置默认医院")
	}

	// 2. 拿到db
	safeMap := common.NewSafeMap()
	db, err := safeMap.GetOrStore(chainCode)
	if err != nil {
		return nil, err
	}

	return repository.QueryEmployeeByID(db, t, employeeID)
}

func WorkTrendLowerService(phone, t string, employeeID, _type int) (interface{}, error) {

	// 1. 查询当前手机号是否有默认医院
	chainCode, _, err := repository.QueryChainCodeAndLocalIDByPhone(phone)
	if err != nil {
		return nil, err
	}

	if chainCode == "" {
		return nil, errors.New("请设置默认医院")
	}

	// 2. 拿到db
	safeMap := common.NewSafeMap()
	db, err := safeMap.GetOrStore(chainCode)
	if err != nil {
		return nil, err
	}

	switch _type {
	case 1:
		return repository.QueryEmployeeAchievement(db, t, employeeID)
	case 2:
		return repository.QueryEmployeeSaleRoyalty(db, t, employeeID)
	case 3:
		return repository.QueryEmployeeServiceRoyalty(db, t, employeeID)
	default:
		return nil, errors.New("类型错误")
	}
}

func EmployeeBusinessBillService(phone, t string, employeeID int, loginID string) (interface{}, error) {

	// 1. 查询当前手机号是否有默认医院
	chainCode, _, err := repository.QueryChainCodeAndLocalIDByPhone(phone)
	if err != nil {
		return nil, err
	}

	if chainCode == "" {
		return nil, errors.New("请设置默认医院")
	}

	// 2. 拿到db
	safeMap := common.NewSafeMap()
	db, err := safeMap.GetOrStore(chainCode)
	if err != nil {
		return nil, err
	}

	if t == "" {
		t = time.Now().Format("2006-01-02")
	}

	var formatSQL, timeFormat string
	if len(t) == 10 {
		formatSQL = "%Y-%m-%d"
		timeFormat = "2006-01-02"
	} else if len(t) == 7 {
		formatSQL = "%Y-%m"
		timeFormat = "2006-01"
	} else if len(t) == 4 {
		formatSQL = "%Y"
		timeFormat = "2006"
	} else {
		return nil, errors.New("time error")
	}

	return repository.EmployeeBusinessBillRes(db, t, formatSQL, timeFormat, employeeID, loginID)
}

func TodayDataService(phone string) (interface{}, error) {

	// 1. 查询当前手机号是否有默认医院
	chainCode, employeeID, err := repository.QueryChainCodeAndLocalIDByPhone(phone)
	if err != nil {
		return nil, err
	}

	if chainCode == "" {
		return nil, errors.New("请设置默认医院")
	}

	// 2. 拿到db
	safeMap := common.NewSafeMap()
	db, err := safeMap.GetOrStore(chainCode)
	if err != nil {
		return nil, err
	}

	return repository.TodayDataRes(db, employeeID, chainCode)
}

func EmployeeBusinessBillDetailService(phone string, employeeID, _type int, t string) (interface{}, error) {

	// 1. 查询当前手机号是否有默认医院
	chainCode, _, err := repository.QueryChainCodeAndLocalIDByPhone(phone)
	if err != nil {
		return nil, err
	}

	if chainCode == "" {
		return nil, errors.New("请设置默认医院")
	}

	// 2. 拿到db
	safeMap := common.NewSafeMap()
	db, err := safeMap.GetOrStore(chainCode)
	if err != nil {
		return nil, err
	}

	var formatSQL, timeFormat string
	if len(t) == 10 {
		formatSQL = "%Y-%m-%d"
		timeFormat = "2006-01-02"
	} else if len(t) == 7 {
		formatSQL = "%Y-%m"
		timeFormat = "2006-01"
	} else if len(t) == 4 {
		formatSQL = "%Y"
		timeFormat = "2006"
	} else {
		return nil, errors.New("time error")
	}

	if t == "" {
		t = time.Now().Format(timeFormat)
	}

	hospitalLocalId, err := repository.QueryHospitalLocalIDByEmployeeLocalID(db, employeeID)
	if err != nil {
		return nil, err
	}

	//1 预约、 2 已接诊、 3 疫苗驱虫、4 美容洗澡、5 住院寄养

	switch _type {
	case 1:
		return repository.Appointment(db, formatSQL, t, employeeID, hospitalLocalId)
	case 2:
		return repository.Clinic(db, formatSQL, t, employeeID, hospitalLocalId)
	case 3:
		return repository.ProtectionAndInsect(db, formatSQL, t, employeeID, hospitalLocalId)
	case 4:
		return repository.Beauty(db, formatSQL, t, employeeID, hospitalLocalId)
	case 5:
		return repository.HospitalAndFoster(db, formatSQL, t, timeFormat, employeeID, hospitalLocalId)
	default:
		return nil, errors.New("type error")
	}
}
