package controllers

import (
	"LAPP_LF_MOM_BACKEND/utils"
	"LAPP_LF_MOM_BACKEND/web/middleware/jwts"
	"LAPP_LF_MOM_BACKEND/web/models"
	"LAPP_LF_MOM_BACKEND/web/supports"
	"github.com/kataras/iris/v12"
	"time"
)

//database列表
func Databaselist(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}

	search := models.SerchData{}
	search.Key = ctx.URLParam("searchKey")
	search.Val = ctx.URLParam("searchValue")

	var pageSize = 10
	var pageIndex = 1

	size := ctx.URLParam("pageSize")
	if size != "" {
		pageSize = utils.ValueToInt(size, 0)
	}

	index := ctx.URLParam("pageIndex")
	if index != "" {
		pageIndex = utils.ValueToInt(index, 0)
	}

	limit := int64(pageSize) * (int64(pageIndex) - 1)
	offet := int64(pageSize) * int64(pageIndex)

	data := new(models.Database)
	result, count := data.FindList(search, limit, offet)
	res := make(map[string]interface{})
	res["data"] = result
	res["count"] = count
	res["pageIndex"] = pageIndex
	res["pageSize"] = pageSize
	supports.Ok(ctx, supports.OptionSuccess, res)
}

func GetDatabase(ctx iris.Context) {
	var me models.Database
	me.DbName = ctx.URLParam("dbname")
	me.DriverName = ctx.URLParam("drivername")
	result, err := me.FindOne()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, result)
}

func DelDatabase(ctx iris.Context) {
	var me models.Database
	me.DbName = ctx.URLParam("dbname")
	me.DriverName = ctx.URLParam("drivername")
	err := me.DelData()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, "")
}

func InsertDatabase(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	log := new(models.LeitServerLog)
	log.File = "/controllers/Database_controller.go"
	log.Level = "info"
	log.Function = "InsertDatabase"
	log.Message = "添加Database表"
	log.Operator = user.Userid
	log.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	log.InsertRecord()
	data := new(models.Database)
	if err := ctx.ReadJSON(data); err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "json解析错误", nil)
		return
	}
	id := data.InsertRecord()
	if utils.ValueIsEmpty(id) {
		supports.Error(ctx, iris.StatusBadRequest, "数据已经存在！", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, data)
}

func UpdateDatabase(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	log := new(models.LeitServerLog)
	log.File = "/controllers/Etltab_controller.go"
	log.Level = "info"
	log.Function = "UpdateEtltab"
	log.Message = "更新Etltab表"
	log.Operator = user.Userid
	log.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	log.InsertRecord()
	data := new(models.Database)
	if err := ctx.ReadJSON(data); err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "json解析失败", nil)
		return
	}
	err := data.UpdateData()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "更新失败", nil)
		return
	}

	supports.Ok(ctx, "修改成功", "")
}
