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"
	"fmt"
	"github.com/kataras/iris/v12"
	"time"
)

func GetEtltabList(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 = "GetEtltabList"
	log.Message = "查询Etltab表"
	log.Operator = user.Userid
	log.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	log.InsertRecord()
	var data models.Etltab
	var err error
	var pageSize = 10
	var pageIndex = 1

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

	if index := ctx.URLParam("pageIndex"); index != "" {
		pageIndex = utils.ValueToInt(index, 0)
	}
	data.Finr = user.Pid
	data.Eid, _ = ctx.URLParamInt("eid")
	result, count, err := data.GetPage(pageSize, pageIndex)
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	res := make(map[string]interface{})
	res["data"] = result
	res["count"] = count
	res["pageIndex"] = pageIndex
	res["pageSize"] = pageSize
	supports.Ok(ctx, supports.OptionSuccess, res)
}

func GetEtltab(ctx iris.Context) {
	var me models.Etltab
	finr, _ := ctx.URLParamInt("finr")
	me.Finr = finr
	me.Eid, _ = ctx.URLParamInt("eid")
	result, err := me.SelectOne()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, result)
}

func InsertEtltab(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 = "InsertEtltab"
	log.Message = "添加Etltab表"
	log.Operator = user.Userid
	log.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	log.InsertRecord()
	data := new(models.Etltab)
	if err := ctx.ReadJSON(data); err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "json解析错误", nil)
		return
	}
	data.Finr = user.Pid
	data.Lastuser = user.Userid
	data.Credatuz = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	data.Lastmodif = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	err := data.Add()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "数据已经存在！", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, data)
}

func UpdateEtltab(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.Etltab)
	if err := ctx.ReadJSON(data); err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "json解析失败", nil)
		return
	}
	data.Lastuser = user.Userid
	data.Lastmodif = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	res := data.Update()
	if !res {
		supports.Error(ctx, iris.StatusBadRequest, "更新失败", nil)
		return
	}

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

func DeleteEtltab(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 = "DeleteEtltab"
	log.Message = "删除Etltab表"
	log.Operator = user.Userid
	log.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	log.InsertRecord()
	me := new(models.Etltab)
	me.Finr = user.Pid
	me.Eid, _ = ctx.URLParamInt("eid")
	res := me.Del()
	if !res {
		supports.Error(ctx, iris.StatusBadRequest, "删除失败", nil)
		return
	}
	supports.Ok(ctx, "删除成功", "")
}

//查询新增数据
func SelectArrEtltab(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	me := new(models.Etltab)
	me.Fromtable = ctx.URLParam("table")
	res, err := me.SelectArr()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "查询失败", nil)
		return
	}
	supports.Ok(ctx, "成功", res)
}

//测试sql语句
func TestEtltabSql(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	me := new(models.Etltab)
	sqlStr := ctx.URLParam("sqlshow")
	res, err := me.TestSql(sqlStr)
	if err != nil {
		errStr := fmt.Sprintf("%s", err)
		supports.Error(ctx, iris.StatusBadRequest, errStr, nil)
		return
	}
	supports.Ok(ctx, "成功", res)
}
