package application

import (
	"fmt"
	account "github.com/fonchain_enterprise/micro-account/api/accountFiee"
	"github.com/fonchain_enterprise/micro-account/pkg/model"
)

var (
	authDb = "fontree-auth_v1"
)

func List(in *account.ListV2Request, limit int, offset int) ([]*model.User, int64) {

	var list []*model.User
	var count int64

	fmt.Println("进入查看测试测试车")
	//获取自己领导的部门

	//Select("a.*,a.id as tid").
	selectStr :=
		`
		ANY_VALUE(a.id) as id,
		ANY_VALUE(a.created_at) as created_at,
		ANY_VALUE(a.updated_at) as updated_at,
		ANY_VALUE(a.deleted_at) as deleted_at,
		ANY_VALUE(a.account) as account,
		ANY_VALUE(a.mnemonic_words ) as mnemonic_words,
		ANY_VALUE(a.tel_num) as tel_num,
		ANY_VALUE(a.password_digest) as  password_digest,
		ANY_VALUE(a.nickname) as nickname,
		ANY_VALUE(a.status) as status,
		ANY_VALUE(a.avatar) as avatar,
		ANY_VALUE(a.real_name_id) as real_name_id,
		ANY_VALUE(a.auth) as auth,
		ANY_VALUE(a.theme_id) as theme_id,
		ANY_VALUE(a.domain) as  domain,
		ANY_VALUE(a.public_key) as public_key,
		ANY_VALUE(a.is_need_change) as is_need_change,
		ANY_VALUE(a.enter_date) as enter_date,
		ANY_VALUE(a.extend) as extend,
		ANY_VALUE(a.title) as title,
		ANY_VALUE(a.job_num) as job_num,
		ANY_VALUE(a.birth_date) as birth_date,
		ANY_VALUE(a.sex) as sex,
		ANY_VALUE(a.last_login_date) as last_login_date,
		ANY_VALUE(a.ip) as ip,
		ANY_VALUE(a.invitation_code) as invitation_code,
		ANY_VALUE(a.left_date) as left_date,
		ANY_VALUE(a.remark) as remark,
		ANY_VALUE(a.recent_img) as recent_img,
		ANY_VALUE(a.english_name) as english_name,
		ANY_VALUE(a.mail_account) as mail_account,
		ANY_VALUE(a.ic_num) as ic_num,
		ANY_VALUE(a.train) as train,
		ANY_VALUE(a.certificate) as certificate,
		ANY_VALUE(a.operator) as operator
`

	searchObj := model.DB.
		Table("user as a").
		Select(selectStr).
		Joins("LEFT JOIN  `"+authDb+"`.hierarchy_dep_position_user b ON b.user_id = a.id and b.deleted_at = 0").
		Joins("LEFT JOIN  `"+authDb+"`.hierarchy_position c ON c.id = b.position_id and c.deleted_at = 0").
		Joins("LEFT JOIN  `"+authDb+"`.hierarchy_hierarchy d ON d.id = b.department_id and d.deleted_at = 0").
		Where("a.domain = ?", in.Domain).
		Where("a.deleted_at = 0").
		Order("a.id desc")

	if in.NickName != "" {
		searchObj = searchObj.Where("a.nickname like ?", "%"+in.NickName+"%")
	}

	if in.TelNum != "" {
		searchObj = searchObj.Where("a.tel_num = ?", in.TelNum)
	}

	if in.Status != "" {
		searchObj = searchObj.Where("a.status = ?", in.Status)
	}

	if in.PositionName != "" {
		searchObj = searchObj.Where("c.name like ?", "%"+in.PositionName+"%")
	}

	if in.PositionId != 0 {
		searchObj = searchObj.Where("b.position_id = ?", in.PositionId)
	}

	if in.DepartmentId != 0 {
		searchObj = searchObj.Where("b.department_id = ?", in.DepartmentId)
	}
	if len(in.DepartmentIds) != 0 {
		searchObj = searchObj.Where("b.department_id in (?)", in.DepartmentIds)
	}

	if in.DepartmentName != "" {
		searchObj = searchObj.Where("d.name like ?", "%"+in.DepartmentName+"%")
	}

	if len(in.DepartmentNames) > 0 && len(in.DepartmentNames) <= 10 {

		nameQueries := model.DB
		for _, k := range in.DepartmentNames {
			nameQueries = nameQueries.Or("d.name like ?", "%"+k+"%")
		}

		searchObj = searchObj.Where(nameQueries)
	}

	if in.JobNum != "" {
		searchObj = searchObj.Where("a.job_num = ?", in.JobNum)
		//searchObj = searchObj.Where(model.User{JobNum: in.JobNum})
	}

	if in.MailAccount != "" {
		searchObj = searchObj.Where("a.mail_account = ?", in.MailAccount)
		//searchObj = searchObj.Where(model.User{JobNum: in.JobNum})
	}

	if in.StartEnterDate != "" {
		searchObj = searchObj.Where("a.enter_date >= ?", in.StartEnterDate)
		//searchObj = searchObj.Where(model.User{JobNum: in.JobNum})
	}

	if in.EndEnterDate != "" {
		searchObj = searchObj.Where("a.enter_date <= ?", in.EndEnterDate)
	}

	searchObj = searchObj.Where("a.deleted_at = 0")
	searchObj.Select("COUNT(DISTINCT a.id)").Scan(&count)
	searchObj.Select(selectStr).Group("a.id").Limit(limit).Offset(offset).Find(&list)

	fmt.Println("进入查看测试测试车结束")

	return list, count
}

func SynNickName(in *account.UpdateRequest) {
	//if in.Domain == m.DefaultDomain {
	//	model.DB.Table(authDb+".hierarchy_department_user").Where("user_id = ?", in.ID).Update("user_name", in.NickName)
	//}
}
