package data_manage

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

// EdbInfoNoPermissionAdmin 指标不可见用户配置表
type EdbInfoNoPermissionAdmin struct {
	Id         int `orm:"column(id);pk"`
	EdbInfoId  int `orm:"column(edb_info_id);"`
	AdminId    int `orm:"column(admin_id);"`
	CreateTime time.Time
}

func AddEdbInfoNoPermissionAdmin(item *EdbInfoNoPermissionAdmin) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("data")
	lastId, err = o.Insert(item)
	return
}

// GetAllListByEdbInfoId 根据指标id获取所有的配置
func (item EdbInfoNoPermissionAdmin) GetAllListByEdbInfoId(edbInfoId int) (items []*EdbInfoNoPermissionAdmin, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM edb_info_no_permission_admin WHERE edb_info_id = ? ORDER BY create_time desc`
	_, err = o.Raw(sql, edbInfoId).QueryRows(&items)
	return
}

// GetAllListByAdminId 根据用户id获取该用户的所有配置
func (item EdbInfoNoPermissionAdmin) GetAllListByAdminId(adminId int) (items []*EdbInfoNoPermissionAdmin, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM edb_info_no_permission_admin WHERE admin_id = ? ORDER BY create_time desc`
	_, err = o.Raw(sql, adminId).QueryRows(&items)
	return
}

// GetByEdbInfoIdAndAdminId 根据用户id和指标id获取配置
func (item EdbInfoNoPermissionAdmin) GetByEdbInfoIdAndAdminId(adminId, edbInfoId int) (info *EdbInfoNoPermissionAdmin, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM edb_info_no_permission_admin WHERE admin_id = ? AND edb_info_id = ? ORDER BY create_time desc`
	err = o.Raw(sql, adminId, edbInfoId).QueryRow(&info)
	return
}

// DeleteAllByEdbInfoId 根据指标id删除所有的配置
func (item EdbInfoNoPermissionAdmin) DeleteAllByEdbInfoId(edbInfoId int) (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `DELETE FROM edb_info_no_permission_admin WHERE edb_info_id = ? `
	_, err = o.Raw(sql, edbInfoId).Exec()
	return
}

// DeleteByEdbInfoIdAndAdminId 根据指标id和用户id删除配置
func (item EdbInfoNoPermissionAdmin) DeleteByEdbInfoIdAndAdminId(edbInfoId, adminId int) (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `DELETE FROM edb_info_no_permission_admin WHERE edb_info_id = ? AND admin_id = ? `
	_, err = o.Raw(sql, edbInfoId, adminId).Exec()
	return
}

// ModifyByEdbInfoId 根据指标id和用户ID列表 重新配置
func (item EdbInfoNoPermissionAdmin) ModifyByEdbInfoId(edbInfoId int, adminIdList []int) (err error) {
	to, err := orm.NewOrmUsingDB("data").Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()
	sql := `DELETE FROM edb_info_no_permission_admin WHERE edb_info_id = ? `
	_, err = to.Raw(sql, edbInfoId).Exec()
	if err != nil {
		return
	}

	insertArr := make([]*EdbInfoNoPermissionAdmin, 0)
	for _, adminId := range adminIdList {
		insertArr = append(insertArr, &EdbInfoNoPermissionAdmin{
			//Id:         0,
			EdbInfoId:  edbInfoId,
			AdminId:    adminId,
			CreateTime: time.Now(),
		})
	}
	if len(insertArr) > 0 {
		_, err = to.InsertMulti(len(insertArr), insertArr)
	}
	return
}

// AddByEdbInfoId 根据指标id和用户ID列表 添加配置
func (item EdbInfoNoPermissionAdmin) AddByEdbInfoId(edbInfoId int, adminIdList []int) (err error) {
	to, err := orm.NewOrmUsingDB("data").Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()

	var items []*EdbInfoNoPermissionAdmin
	sql := `SELECT * FROM edb_info_no_permission_admin WHERE edb_info_id = ? ORDER BY create_time desc`
	_, err = to.Raw(sql, edbInfoId).QueryRows(&items)
	if err != nil {
		return
	}

	hasMap := make(map[int]int)
	for _, v := range items {
		hasMap[v.AdminId] = v.EdbInfoId
	}

	insertArr := make([]*EdbInfoNoPermissionAdmin, 0)
	for _, adminId := range adminIdList {
		if _, ok := hasMap[adminId]; !ok {
			insertArr = append(insertArr, &EdbInfoNoPermissionAdmin{
				//Id:         0,
				EdbInfoId:  edbInfoId,
				AdminId:    adminId,
				CreateTime: time.Now(),
			})
		}
	}
	if len(insertArr) > 0 {
		_, err = to.InsertMulti(len(insertArr), insertArr)
	}
	return
}

type NoPermissionChart struct {
	AdminId     int
	ChartInfoId int
}

// GetAllChartListByAdminId 根据用户id获取该用户的所有配置
func (item EdbInfoNoPermissionAdmin) GetAllChartListByAdminId(adminId int) (items []*NoPermissionChart, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT a.admin_id,b.chart_info_id FROM edb_info_no_permission_admin AS a 
			JOIN chart_edb_mapping AS b ON a.edb_info_id=b. edb_info_id
			WHERE a.admin_id = ? ORDER BY a.create_time desc `
	_, err = o.Raw(sql, adminId).QueryRows(&items)
	return
}

// GetChartByEdbInfoIdAndAdminId 根据用户id和指标id获取配置
func (item EdbInfoNoPermissionAdmin) GetChartByEdbInfoIdAndAdminId(adminId, edbInfoId int) (info *NoPermissionChart, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT a.admin_id,b.chart_info_id FROM edb_info_no_permission_admin AS a 
			JOIN chart_edb_mapping AS b ON a.edb_info_id=b. edb_info_id
			WHERE a.admin_id = ? AND a.edb_info_id = ? ORDER BY a.create_time desc`
	err = o.Raw(sql, adminId, edbInfoId).QueryRow(&info)
	return
}
