package db

import (
	"applet/app/db/model"
	"applet/app/utils/logx"

	"xorm.io/xorm"
)

// UserRelateInsert is 插入一条数据到用户关系表
func UserRelateInsert(Db *xorm.Engine, userRelate *model.UserRelate) (int64, error) {
	affected, err := Db.Insert(userRelate)
	if err != nil {
		return 0, err
	}
	return affected, nil
}
func UserRelateInsertWithSess(sess *xorm.Session, userRelate *model.UserRelate) (int64, error) {
	affected, err := sess.Insert(userRelate)
	if err != nil {
		return 0, err
	}
	return affected, nil
}
func UserRelateUpdate(Db *xorm.Engine, userRelate *model.UserRelate) (int64, error) {
	affected, err := Db.Where("parent_uid=? and uid=?", userRelate.ParentUid, userRelate.Uid).Cols("level,invite_time").Update(userRelate)
	if err != nil {
		return 0, err
	}
	return affected, nil
}

//UserRelateByPuid is 获取用户关系列表 by puid
func UserRelatesByPuid(Db *xorm.Engine, puid interface{}, limit, start int) (*[]model.UserRelate, error) {
	var m []model.UserRelate
	if limit == 0 && start == 0 {
		if err := Db.Where("parent_uid = ?", puid).
			Cols(`id,parent_uid,uid,level,invite_time`).
			Find(&m); err != nil {
			return nil, logx.Warn(err)
		}
		return &m, nil
	}
	if err := Db.Where("parent_uid = ?", puid).
		Cols(`id,parent_uid,uid,level,invite_time`).Limit(limit, start).
		Find(&m); err != nil {
		return nil, logx.Warn(err)
	}

	return &m, nil
}

//UserRelatesByPuidByLv is 获取用户关系列表 by puid 和lv
func UserRelatesByPuidByLv(Db *xorm.Engine, puid, lv interface{}, limit, start int) (*[]model.UserRelate, error) {
	var m []model.UserRelate
	if limit == 0 && start == 0 {
		if err := Db.Where("parent_uid = ? AND level = ?", puid, lv).
			Cols(`id,parent_uid,uid,level,invite_time`).
			Find(&m); err != nil {
			return nil, logx.Warn(err)
		}
		return &m, nil
	}
	if err := Db.Where("parent_uid = ? AND level = ?", puid, lv).
		Cols(`id,parent_uid,uid,level,invite_time`).Limit(limit, start).
		Find(&m); err != nil {
		return nil, logx.Warn(err)
	}

	return &m, nil
}

//UserRelatesByPuidByLvByTime is 获取直属 level =1用户关系列表 by puid 和lv by time
func UserRelatesByPuidByLvByTime(Db *xorm.Engine, puid, lv, stime, etime interface{}, limit, start int) (*[]model.UserRelate, error) {
	var m []model.UserRelate
	if limit == 0 && start == 0 {
		if err := Db.Where("parent_uid = ? AND level = ? AND  invite_time > ? AND invite_time < ?", puid, lv, stime, etime).
			Find(&m); err != nil {
			return nil, logx.Warn(err)
		}
		return &m, nil
	}
	if err := Db.Where("parent_uid = ? AND level = ? AND  invite_time > ? AND invite_time < ?", puid, lv, stime, etime).
		Limit(limit, start).
		Find(&m); err != nil {
		return nil, logx.Warn(err)
	}

	return &m, nil
}

//UserRelatesByPuidByTime is 获取户关系列表 by puid 和lv by time
func UserRelatesByPuidByTime(Db *xorm.Engine, puid, stime, etime interface{}, limit, start int) (*[]model.UserRelate, error) {
	var m []model.UserRelate
	if limit == 0 && start == 0 {
		if err := Db.Where("parent_uid = ? AND  invite_time > ? AND invite_time < ?", puid, stime, etime).
			Cols(`id,parent_uid,uid,level,invite_time`).
			Find(&m); err != nil {
			return nil, logx.Warn(err)
		}
		return &m, nil
	}
	if err := Db.Where("parent_uid = ? AND  invite_time > ? AND invite_time < ?", puid, stime, etime).
		Cols(`id,parent_uid,uid,level,invite_time`).Limit(limit, start).
		Find(&m); err != nil {
		return nil, logx.Warn(err)
	}

	return &m, nil
}

//UserRelatesByPuidExceptLv is 获取用户关系列表 by puid 和非 lv
func UserRelatesByPuidExceptLv(Db *xorm.Engine, puid, lv interface{}, limit, start int) (*[]model.UserRelate, error) {
	var m []model.UserRelate
	if limit == 0 && start == 0 {
		if err := Db.Where("parent_uid = ? AND level != ?", puid, lv).
			Cols(`id,parent_uid,uid,level,invite_time`).
			Find(&m); err != nil {
			return nil, logx.Warn(err)
		}
		return &m, nil
	}
	if err := Db.Where("parent_uid = ? AND level != ?", puid, lv).
		Cols(`id,parent_uid,uid,level,invite_time`).Limit(limit, start).
		Find(&m); err != nil {
		return nil, logx.Warn(err)
	}

	return &m, nil
}

//UserRelateByUID is 获取用户关系表 by uid
func UserRelateByUID(Db *xorm.Engine, uid interface{}) (*model.UserRelate, bool, error) {
	r := new(model.UserRelate)
	has, err := Db.Where("uid=?", uid).Get(r)
	if err != nil {
		return nil, false, err
	}
	return r, has, nil
}

//UserRelateByUIDByLv is 获取用户关系表 by uid
func UserRelateByUIDByLv(Db *xorm.Engine, uid, lv interface{}) (*model.UserRelate, bool, error) {
	r := new(model.UserRelate)
	has, err := Db.Where("uid=? AND level=?", uid, lv).Get(r)
	if err != nil {
		return nil, false, err
	}
	return r, has, nil
}

//UserRelateByUIDAndPUID 根据 Puid 和uid 查找 ，用于确认关联
func UserRelateByUIDAndPUID(Db *xorm.Engine, uid, puid interface{}) (*model.UserRelate, bool, error) {
	r := new(model.UserRelate)
	has, err := Db.Where("uid=? AND parent_uid=?", uid, puid).Get(r)
	if err != nil {
		return nil, false, err
	}
	return r, has, nil
}

//UserRelatesByPuIDAndLv is 查询用户关系表 获取指定等级和puid的关系
func UserRelatesByPuIDAndLv(Db *xorm.Engine, puid, lv interface{}) (*[]model.UserRelate, error) {
	var m []model.UserRelate
	if err := Db.Where("parent_uid = ? AND level = ?", puid, lv).
		Cols(`id,parent_uid,uid,level,invite_time`).
		Find(&m); err != nil {
		return nil, logx.Warn(err)
	}
	return &m, nil
}

// UserRelateInByUID is In查询
func UserRelateInByUID(Db *xorm.Engine, ids []int) (*[]model.UserRelate, error) {
	var m []model.UserRelate
	if err := Db.In("uid", ids).
		Find(&m); err != nil {
		return nil, logx.Warn(err)
	}
	return &m, nil
}

// UserRelatesByUIDDescLv is Where 查询 根据level 降序
func UserRelatesByUIDDescLv(Db *xorm.Engine, id interface{}) (*[]model.UserRelate, error) {
	var m []model.UserRelate
	if err := Db.Where("uid = ?", id).Desc("level").
		Find(&m); err != nil {
		return nil, logx.Warn(err)
	}
	return &m, nil
}
func UserRelatesByInvite(Db *xorm.Engine, times interface{}) (*[]model.UserRelate, error) {
	var m []model.UserRelate
	if err := Db.Where("invite_time >= ?", times).
		Find(&m); err != nil {
		return nil, logx.Warn(err)
	}
	return &m, nil
}

//UserRelateCountByPUID is 根据puid 计数
func UserRelateCountByPUID(Db *xorm.Engine, pid interface{}) (int64, error) {

	count, err := Db.Where("parent_uid = ?", pid).Count(model.UserRelate{})
	if err != nil {
		return 0, nil
	}
	return count, nil
}

//UserRelateDelete is  删除关联他上级的关系记录,以及删除他下级的关联记录
func UserRelateDelete(Db *xorm.Engine, uid interface{}) (int64, error) {
	// 删除与之上级的记录
	_, err := Db.Where("uid = ?", uid).Delete(model.UserRelate{})
	if err != nil {
		return 0, err
	}
	// 删除与之下级的记录
	_, err = Db.Where("parent_uid = ?", uid).Delete(model.UserRelate{})
	if err != nil {
		return 0, err
	}

	return 1, nil
}
func UserRelateDeleteWithSession(sess *xorm.Session, uid interface{}) (int64, error) {
	// 删除与之上级的记录
	_, err := sess.Where("uid = ?", uid).Delete(model.UserRelate{})
	if err != nil {
		return 0, err
	}
	// 删除与之下级的记录
	_, err = sess.Where("parent_uid = ?", uid).Delete(model.UserRelate{})
	if err != nil {
		return 0, err
	}

	return 1, nil
}

//UserRelateDelete is  删除关联他上级的关系记录
func UserRelateExtendDelete(Db *xorm.Engine, uid interface{}) (int64, error) {
	// 删除与之上级的记录
	_, err := Db.Where("uid = ?", uid).Delete(model.UserRelate{})
	if err != nil {
		return 0, err
	}
	return 1, nil
}
