package controllers


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

func GetEtcdtabList(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user,*user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	logs := new(models.LeitServerLog)
	logs.File = "/controllers/Etcdtab_controller.go"
	logs.Level = "info"
	logs.Function = "GetEtcdtabList"
	logs.Message = "Etcd查看"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(),"yyyyMMddHHmmss")
	logs.InsertRecord()
	var data models.Etcdtab
	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)
	}
	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 GetEtcdtab(ctx iris.Context) {
	var me models.Etcdtab
	id, _ := ctx.URLParamInt("id")
	me.Id = id
	result, err := me.SelectOne()
	if err !=nil{
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	result.Clipped()
	supports.Ok(ctx, supports.OptionSuccess, result)
}

func InsertEtcdtab(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user,*user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	logs := new(models.LeitServerLog)
	logs.File = "/controllers/Etcdtab_controller.go"
	logs.Level = "info"
	logs.Function = "InsertEtcdtab"
	logs.Message = "添加Etcd"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(),"yyyyMMddHHmmss")
	logs.InsertRecord()
	data := new(models.Etcdtab)
	data.Lastuser = user.Userid
	data.Credatuz = utils.TimeFormat(time.Now(),"yyyyMMddHHmmss")
	data.Lastmodif = utils.TimeFormat(time.Now(),"yyyyMMddHHmmss")
	if err := ctx.ReadJSON(data); err != nil {
		logs := new(models.LeitServerLog)
		logs.File = "/controllers/Etcdtab_controller.go"
		logs.Level = "debug"
		logs.Function = "InsertEtcdtab"
		logs.Message = error.Error(err)
		logs.Operator = user.Userid
		logs.TimeStamp = utils.TimeFormat(time.Now(),"yyyyMMddHHmmss")
		logs.InsertRecord()
		supports.Error(ctx, iris.StatusBadRequest, "json解析错误", nil)
		return
	}
	err := data.Add()
	if err != nil{
		supports.Error(ctx, iris.StatusBadRequest,"添加失败！", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, data)
}

func UpdateEtcdtab(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user,*user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	logs := new(models.LeitServerLog)
	logs.File = "/controllers/Etcdtab_controller.go"
	logs.Level = "info"
	logs.Function = "UpdateEtcdtab"
	logs.Message = "更新Etcd"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(),"yyyyMMddHHmmss")
	logs.InsertRecord()
	data := new(models.Etcdtab)
	if err := ctx.ReadJSON(data); err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	data.Lastuser = user.Userid

	health := strings.Trim(data.Health," ")
	switch health {
	case "install":
		//安装
		path := data.Path + " install"
		glog.InfoExt("cmd启动日志","path is :",path)
		err := data.CheckExe2(path)
		if err !=nil{
			supports.Error(ctx, iris.StatusBadRequest, "cmd启动失败", nil)
			return
		}
	case "remove":
		//卸载
		path := data.Path + " remove"
		glog.InfoExt("cmd启动日志","path is :",path)
		err := data.CheckExe2(path)
		if err !=nil{
			supports.Error(ctx, iris.StatusBadRequest, "cmd启动失败", nil)
			return
		}
	case "start":
		//启动
		path := data.Path + " start"
		glog.InfoExt("cmd启动日志","path is :",path)
		err := data.CheckExe2(path)
		if err !=nil{
			supports.Error(ctx, iris.StatusBadRequest, "cmd启动失败", nil)
			return
		}
	case "stop":
		//暂停
		path := data.Path + " stop"
		glog.InfoExt("cmd启动日志","path is :",path)
		err := data.CheckExe2(path)
		if err !=nil{
			supports.Error(ctx, iris.StatusBadRequest, "cmd启动失败", nil)
			return
		}

	}

	res:= data.Update()
	if !res{
		supports.Error(ctx, iris.StatusBadRequest, "更新失败", nil)
		return
	}

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

func DeleteEtcdtab(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user,*user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	logs := new(models.LeitServerLog)
	logs.File = "/controllers/Etcdtab_controller.go"
	logs.Level = "info"
	logs.Function = "DeleteEtcdtab"
	logs.Message = "删除Etcd"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(),"yyyyMMddHHmmss")
	logs.InsertRecord()
	me:=new(models.Etcdtab)
	id,_ := ctx.URLParamInt("id")
	me.Id = id
	res := me.Del()
	if !res {
		supports.Error(ctx, iris.StatusBadRequest, "删除失败", nil)
		return
	}
	supports.Ok(ctx, "删除成功", "")
}

