package services

import (
	"encoding/json"
	"errors"
	"fmt"
	"gin-vue-admin/common"
	"gin-vue-admin/global"
	"gin-vue-admin/models"
	"gin-vue-admin/repository"
	"io/ioutil"
	"net/http"
	"net/url"
	"time"
)

// SetDefaultHospital 设置员工 默认使用哪个医院
func IsDefaultHospital(phone string) (interface{}, error) {

	return repository.QueryIsDefaultHospital(phone)
}

// QueryHospitalList 查询医院列表
func QueryHospitalList(phone, sourceType string) (interface{}, error) {
	if sourceType != "2" {
		sourceType = "1"
	}
	return repository.QueryHospitalsByEmployeePhone(phone, sourceType)
}

// SetDefaultHospital 设置员工 默认使用哪个医院
func SetDefaultHospital(phone string, hospitalID, employeeLocalID int) error {

	return repository.SetEmployeeDefaultHospital(phone, hospitalID, employeeLocalID)
}

// QueryUserInfo 查询员工详细信息
func QueryUserInfo(phone string) (interface{}, error) {

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

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

	// 3. 使用db查询员工详细信息
	u, err := repository.QueryUserInfoByID(db, employeeLocalID)
	if err != nil {
		return nil, err
	}

	isBinding, err := repository.QueryUserIsBindingWx(phone)

	if err != nil {
		return nil, err
	}
	u.IsBindingWx = isBinding

	// 是否是设备投放医院
	if chainCode == "ePyEPzOOhX" {
		u.IsDevice = 1
	}

	timer := time.Duration(1800) * time.Second
	global.GVA_REDIS.Set("Medical:hospitalCode:"+phone, u.HospitalCode, timer)

	return u, nil
}

// QueryRolePermissions 查询职位列表及列表的权限
func QueryRolePermissions(phone string) (interface{}, error) {

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

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

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

	return repository.QueryRolePermissions(db, hospitalLocalId)
}

// QueryPermissionDetail 查询权限详情
func QueryPermissionDetail(phone string, roleID int) (interface{}, error) {

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

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

	var r models.PermissionDetail

	permissions, err := repository.QueryPermissionDetail(db, roleID)
	if err != nil {
		return nil, err
	}

	users, err := repository.QueryUserListByRoleID(db, roleID)
	if err != nil {
		return nil, err
	}

	r.Permissions = permissions
	r.Users = users

	return r, nil
}

func QueryPermissionDetailByEmployeeID(phone string, employeeID int) (interface{}, error) {

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

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

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

	permissions, err := repository.QueryPermissionDetailByEmployeeID(db, employeeID, hospitalID)
	if err != nil {
		return nil, err
	}

	return permissions, nil
}

// SetPermission 设置权限是否开启
func SetPermission(phone string, roleID, doctorModuleID, operateType int) error {

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

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

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

	// 3. 使用db查询员工详细信息
	return repository.SetPermission(db, roleID, doctorModuleID, operateType, hospitalID)
}

// QueryUserList 获取员工列表
func QueryUserList(phone string) (interface{}, error) {

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

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

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

	// 3. 使用db查询员工详细信息
	return repository.QueryUserList(db, hospitalLocalId)
}

// SetJobState 修改在职状态
func SetJobState(phone string, employeeID, jobState int) error {

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

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

	// 3. 使用db查询员工详细信息
	return repository.SetJobState(db, employeeID, jobState)
}

// SetJobState 修改在职状态
func SetEmployeeImage(phone string, employeeID int, imageURL string) error {

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

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

	// 3. 使用db查询员工详细信息
	return repository.SetEmployeeImage(db, employeeID, imageURL)
}

func UntyingWx(phone string) error {
	return repository.Untying(phone)
}

// 传给ai
func DiscernLisImage(urlStr string) (interface{}, error) {

	formValues := url.Values{}
	formValues.Set("url", urlStr)

	response, err := http.PostForm(global.GVA_CONFIG.LisOcr.Url+"/api/receipt_ocr", formValues)
	if err != nil {
		fmt.Println("err: ", err)
		return nil, err
	}

	defer response.Body.Close()
	responseBody, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return nil, err
	}

	temp := &models.LisOcrResponse{}
	err = json.Unmarshal(responseBody, &temp)
	if err != nil {
		return nil, err
	}

	if temp.Code != 200 {
		fmt.Println("err: ", temp.Message)
		return nil, errors.New(temp.Message)
	}

	temp.Data.ImageUrl = urlStr

	return temp.Data, nil
}

func SaveLisOcrResult(p *models.LisExamine, phone string, body string) error {

	chainCode, employeeId, err := repository.QueryChainCodeAndLocalIDByPhone(phone)
	if err != nil {
		return err
	}

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

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

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

	return repository.SaveLisOcrResult(p, chainCode, hospitalCode, body)
}
