package cmd

import (
	"ctyun_lts_cli_tool/lts"
	"fmt"
	"strings"

	"github.com/spf13/cobra"
)

var LogUnitCmd = &cobra.Command{
	Use:   "logUnit",
	Short: "Ctyun Log LogUnit openApi",
}

var createLogUnitCmd = &cobra.Command{
	Use:     "createLogUnit",
	Short:   "Create a new log unit",
	Example: `ctlts logUnit createLogUnit --project_code=<string> --unit_name=<string> [--ttlDays=<int> --description=<string>] [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		config_name, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, config_name)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}
		project_code, _ := cmd.Flags().GetString("project_code")
		unit_name, _ := cmd.Flags().GetString("unit_name")
		description, _ := cmd.Flags().GetString("description")
		var description2 *string
		if cmd.Flags().Changed("description") { // 判断是否传入description
			description2 = &description
		} else {
			description2 = nil
		}
		ttlDays, _ := cmd.Flags().GetInt("ttlDays")
		lts.BuildLogCommand(cmd)
		response := lts.CreateLogUnit(ak, sk, endpoint, project_code, unit_name, ttlDays, description2, region_id)
		fmt.Println("response:", response)
	},
}

var updateLogUnitCmd = &cobra.Command{
	Use:     "updateLogUnit",
	Short:   "update a new log unit",
	Example: `ctlts logUnit updateLogUnit --project_code=<string> --unit_code=<string> [--ttlDays=<int> --description=<string>] [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		config_name, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, config_name)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}
		project_code, _ := cmd.Flags().GetString("project_code")
		unit_code, _ := cmd.Flags().GetString("unit_code")
		description, _ := cmd.Flags().GetString("description")
		var description2 *string
		if cmd.Flags().Changed("description") { // 判断是否传入description
			description2 = &description
		} else {
			description2 = nil
		}
		ttlDays, _ := cmd.Flags().GetInt("ttlDays")
		lts.BuildLogCommand(cmd)
		response := lts.UpdateLogUnit(ak, sk, endpoint, project_code, unit_code, ttlDays, description2, region_id)
		fmt.Println("response:", response)
	},
}

var deleteLogUnitCmd = &cobra.Command{
	Use:     "deleteLogUnit",
	Short:   "Delete a new log unit",
	Example: `ctlts logUnit deleteLogUnit --project_code=<string> --unit_code=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		config_name, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, config_name)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}
		project_code, _ := cmd.Flags().GetString("project_code")
		unit_code, _ := cmd.Flags().GetString("unit_code")

		lts.BuildLogCommand(cmd)
		response := lts.DeleteLogUnit(ak, sk, endpoint, project_code, unit_code, region_id)
		fmt.Println("response:", response)
	},
}

var listLogUnitCmd = &cobra.Command{
	Use:     "listLogUnit",
	Short:   "List log unit all or by unitName",
	Example: `ctlts logUnit listLogUnit --project_code=<string> [--unit_name=<string>] [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		config_name, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, config_name)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}
		project_code, _ := cmd.Flags().GetString("project_code")
		unit_name, _ := cmd.Flags().GetString("unit_name")
		var unit_name2 *string
		if cmd.Flags().Changed("unit_name") {
			unit_name2 = &unit_name
		} else {
			unit_name2 = nil
		}
		lts.BuildLogCommand(cmd)

		response := lts.ListLogUnit(ak, sk, endpoint, project_code, unit_name2, region_id)
		fmt.Println("response:", response)
	},
}

var GetIndexCmd = &cobra.Command{
	Use:     "getIndex",
	Short:   "Query index information for a specified log unit",
	Example: `ctlts logUnit getIndex --project_code=<string> --unit_code=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		configName, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		projectCode, _ := cmd.Flags().GetString("project_code")
		unitCode, _ := cmd.Flags().GetString("unit_code")

		lts.BuildLogCommand(cmd)

		response := lts.GetIndex(ak, sk, endpoint, projectCode, unitCode, region_id)
		fmt.Println("response:", response)
	},
}

var CreateIndexCmd = &cobra.Command{
	Use:     "createIndex",
	Short:   "Create index information for a specified log unit",
	Example: `ctlts logUnit createIndex --project_code=<string> --unit_code=<string> --fieldName=<string,string,...> --dataType=<string,string,...> [--fullTextIndex=<bool>] [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		configName, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		projectCode, _ := cmd.Flags().GetString("project_code")
		unitCode, _ := cmd.Flags().GetString("unit_code")
		fieldName, _ := cmd.Flags().GetString("fieldName")
		dataType, _ := cmd.Flags().GetString("dataType")
		fullTextIndex, _ := cmd.Flags().GetBool("fullTextIndex")
		fieldNameList := strings.Split(fieldName, ",")
		dataTypeList := strings.Split(dataType, ",")
		for _, v := range dataTypeList {
			if v != "double" && v != "text" {
				fmt.Println("dataType must be double or text")
				return
			}
		}
		if len(fieldNameList) != len(dataTypeList) {
			fmt.Println("list of fieldName and list of dataType must be same length")
			return
		}
		lts.BuildLogCommand(cmd)
		response := lts.CreateIndex(ak, sk, endpoint, projectCode, unitCode, fullTextIndex, fieldNameList, dataTypeList, region_id)
		fmt.Println("response:", response)
	},
}

var UpdateIndexCmd = &cobra.Command{
	Use:     "updateIndex",
	Short:   "Update index information for a specified log unit",
	Example: `ctlts logUnit updateIndex --project_code=<string> --unit_code=<string> --fieldName=<string,string,...> --dataType=<string,string,...> [--fullTextIndex=<bool>] [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		configName, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		projectCode, _ := cmd.Flags().GetString("project_code")
		unitCode, _ := cmd.Flags().GetString("unit_code")
		fieldName, _ := cmd.Flags().GetString("fieldName")
		dataType, _ := cmd.Flags().GetString("dataType")
		fullTextIndex, _ := cmd.Flags().GetBool("fullTextIndex")
		fieldNameList := strings.Split(fieldName, ",")
		dataTypeList := strings.Split(dataType, ",")
		if len(fieldNameList) != len(dataTypeList) {
			fmt.Println("list of fieldName and list of dataType must be same length")
			return
		}
		lts.BuildLogCommand(cmd)
		response := lts.UpdateIndex(ak, sk, endpoint, projectCode, unitCode, fullTextIndex, fieldNameList, dataTypeList, region_id)
		fmt.Println("response:", response)
	},
}

var DeleteIndexCmd = &cobra.Command{
	Use:     "deleteIndex",
	Short:   "Delete index for a specified log unit",
	Example: `ctlts logUnit deleteIndex --project_code=<string> --unit_code=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		configName, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		projectCode, _ := cmd.Flags().GetString("project_code")
		unitCode, _ := cmd.Flags().GetString("unit_code")

		lts.BuildLogCommand(cmd)

		response := lts.DeleteIndex(ak, sk, endpoint, projectCode, unitCode, region_id)
		fmt.Println("response:", response)
	},
}

var UpdateTtlCmd = &cobra.Command{
	Use:     "updateTtl",
	Short:   "Update TTL for a specified log unit",
	Example: `ctlts logUnit updateTtl --unit_code=<string> --ttl_days=<int> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		configName, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		unitCode, _ := cmd.Flags().GetString("unit_code")
		ttlDays, _ := cmd.Flags().GetInt("ttl_days")
		lts.BuildLogCommand(cmd)

		response := lts.UpdateTtl(ak, sk, endpoint, unitCode, ttlDays, region_id)
		fmt.Println("response:", response)
	},
}

var CountUnitCmd = &cobra.Command{
	Use:     "countUnit",
	Short:   "Get the count of log units",
	Example: `ctlts logUnit countUnit [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		configName, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		project_code, _ := cmd.Flags().GetString("project_code")
		lts.BuildLogCommand(cmd)

		response := lts.CountUnit(ak, sk, endpoint, project_code, region_id)
		fmt.Println("response:", response)
	},
}

var GetUnitByCodeCmd = &cobra.Command{
	Use:     "getUnitByCode",
	Short:   "Get log unit information by unit code",
	Example: `ctlts logUnit getUnitByCode --unit_code=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		configName, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		unitCode, _ := cmd.Flags().GetString("unit_code")

		lts.BuildLogCommand(cmd)

		response := lts.GetUnitByCode(ak, sk, endpoint, unitCode, region_id)
		fmt.Println("response:", response)
	},
}

var GetIndexCountCmd = &cobra.Command{
	Use:     "getIndexCount",
	Short:   "Get index count for a specified log unit by unit code",
	Example: `ctlts logUnit getIndexCount --unit_code=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		configName, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		unitCode, _ := cmd.Flags().GetString("unit_code")

		lts.BuildLogCommand(cmd)

		response := lts.GetIndexCount(ak, sk, endpoint, unitCode, region_id)
		fmt.Println("response:", response)
	},
}

var UpdateLogUnitNoteCmd = &cobra.Command{
	Use:     "updateLogUnitNote",
	Short:   "Update the description of a specified log unit",
	Example: `ctlts logUnit updateLogUnitNote --unit_code=<string> --description=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		configName, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		unitCode, _ := cmd.Flags().GetString("unit_code")
		description, _ := cmd.Flags().GetString("description")

		lts.BuildLogCommand(cmd)

		response := lts.UpdateLogUnitDescription(ak, sk, endpoint, unitCode, description, region_id)
		fmt.Println("response:", response)
	},
}

var GetLogUnitTagsCmd = &cobra.Command{
	Use:     "getLogUnitTags",
	Short:   "Get tags for a specified log unit by unit code",
	Example: `ctlts logUnit getLogUnitTags --unit_code=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		configName, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		unitCode, _ := cmd.Flags().GetString("unit_code")

		lts.BuildLogCommand(cmd)

		response := lts.GetLogUnitTags(ak, sk, endpoint, unitCode, region_id)
		fmt.Println("response:", response)
	},
}

var GetLogUnitNoteCmd = &cobra.Command{
	Use:     "getLogUnitNote",
	Short:   "Get the note (description) of a specified log unit by unit code",
	Example: `ctlts logUnit getLogUnitNote --unit_code=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		configName, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		unitCode, _ := cmd.Flags().GetString("unit_code")

		lts.BuildLogCommand(cmd)

		response := lts.GetLogUnitNote(ak, sk, endpoint, unitCode, region_id)
		fmt.Println("response:", response)
	},
}

var GetLogUnitOriginNameCmd = &cobra.Command{
	Use:     "getLogUnitOriginName",
	Short:   "Get the original name of a specified log unit by unit code",
	Example: `ctlts logUnit getLogUnitOriginName --unit_code=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		configName, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		unitCode, _ := cmd.Flags().GetString("unit_code")

		lts.BuildLogCommand(cmd)

		response := lts.GetLogUnitOriginName(ak, sk, endpoint, unitCode, region_id)
		fmt.Println("response:", response)
	},
}

var GetLogUnitAliasNameCmd = &cobra.Command{
	Use:     "getLogUnitAliasName",
	Short:   "Get the alias name of a specified log unit by unit code",
	Example: `ctlts logUnit getLogUnitAliasName --unit_code=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		configName, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		unitCode, _ := cmd.Flags().GetString("unit_code")

		lts.BuildLogCommand(cmd)

		response := lts.GetLogUnitAliasName(ak, sk, endpoint, unitCode, region_id)
		fmt.Println("response:", response)
	},
}

var RenameLogUnitCmd = &cobra.Command{
	Use:     "renameLogUnit",
	Short:   "Rename the alias of a specified log unit",
	Example: `ctlts logUnit renameLogUnit --unit_code=<string> --alias_name=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		configName, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		unitCode, _ := cmd.Flags().GetString("unit_code")
		aliasName, _ := cmd.Flags().GetString("alias_name")

		lts.BuildLogCommand(cmd)

		response := lts.RenameLogUnit(ak, sk, endpoint, unitCode, aliasName, region_id)
		fmt.Println("response:", response)
	},
}

var QueryLogUnitAliasNamesCmd = &cobra.Command{
	Use:     "queryLogUnitAliasNames",
	Short:   "Query alias names of log units in a specified project",
	Example: `ctlts logUnit queryLogUnitAliasNames --project_code=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		configName, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		projectCode, _ := cmd.Flags().GetString("project_code")

		lts.BuildLogCommand(cmd)

		response := lts.QueryLogUnitAliasNames(ak, sk, endpoint, projectCode, region_id)
		fmt.Println("response:", response)
	},
}

var QueryLogUnitOriginNamesCmd = &cobra.Command{
	Use:     "queryLogUnitOriginNames",
	Short:   "Query origin names of log units in a specified project",
	Example: `ctlts logUnit queryLogUnitOriginNames --project_code=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		configName, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		projectCode, _ := cmd.Flags().GetString("project_code")

		lts.BuildLogCommand(cmd)

		response := lts.QueryLogUnitOriginNames(ak, sk, endpoint, projectCode, region_id)
		fmt.Println("response:", response)
	},
}

var GetLogUnitTtlDaysCmd = &cobra.Command{
	Use:     "getLogUnitTtlDays",
	Short:   "Get the TTL days of a specified log unit",
	Example: `ctlts logUnit getLogUnitTtlDays --unit_code=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		configName, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		unitCode, _ := cmd.Flags().GetString("unit_code")

		lts.BuildLogCommand(cmd)

		response := lts.GetLogUnitTtlDays(ak, sk, endpoint, unitCode, region_id)
		fmt.Println("response:", response)
	},
}

var CheckLogUnitExistCmd = &cobra.Command{
	Use:     "checkLogUnitExist",
	Short:   "Check if a specified log unit exists",
	Example: `ctlts logUnit checkLogUnitExist --unit_name=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		configName, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		unitName, _ := cmd.Flags().GetString("unit_name")

		lts.BuildLogCommand(cmd)

		response := lts.CheckLogUnitExist(ak, sk, endpoint, unitName, region_id)
		fmt.Println("response:", response)
	},
}

var pageLogUnitCmd = &cobra.Command{
	Use:     "pageLogUnit",
	Short:   "Search log unit by page",
	Example: `ctlts logUnit pageLogUnit --project_code=<string> [--unit_name=<string> --page_num=<int> --page_size=<int>] [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		config_name, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, config_name)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		project_code, _ := cmd.Flags().GetString("project_code")
		unit_name, _ := cmd.Flags().GetString("unit_name")
		var unit_name2 *string
		if cmd.Flags().Changed("unit_name") { // 判断是否传入
			unit_name2 = &unit_name
		} else {
			unit_name2 = nil
		}
		page_num, _ := cmd.Flags().GetInt("page_num")
		page_size, _ := cmd.Flags().GetInt("page_size")
		lts.BuildLogCommand(cmd)
		response := lts.PageLogUnit(ak, sk, endpoint, project_code, unit_name2, page_num, page_size, region_id)
		fmt.Println("response:", response)
	},
}

func init() {
	// 添加子命令

	LogUnitCmd.AddCommand(createLogUnitCmd)
	LogUnitCmd.AddCommand(updateLogUnitCmd)
	LogUnitCmd.AddCommand(deleteLogUnitCmd)
	LogUnitCmd.AddCommand(listLogUnitCmd)
	LogUnitCmd.AddCommand(pageLogUnitCmd)
	LogUnitCmd.AddCommand(UpdateIndexCmd)
	LogUnitCmd.AddCommand(CreateIndexCmd)
	LogUnitCmd.AddCommand(CheckLogUnitExistCmd)
	LogUnitCmd.AddCommand(CountUnitCmd)
	LogUnitCmd.AddCommand(GetLogUnitNoteCmd)
	LogUnitCmd.AddCommand(GetLogUnitOriginNameCmd)
	LogUnitCmd.AddCommand(GetUnitByCodeCmd)
	LogUnitCmd.AddCommand(GetLogUnitAliasNameCmd)
	LogUnitCmd.AddCommand(RenameLogUnitCmd)
	LogUnitCmd.AddCommand(GetLogUnitTtlDaysCmd)
	LogUnitCmd.AddCommand(GetIndexCountCmd)
	LogUnitCmd.AddCommand(QueryLogUnitOriginNamesCmd)
	LogUnitCmd.AddCommand(QueryLogUnitAliasNamesCmd)
	LogUnitCmd.AddCommand(GetIndexCmd)
	LogUnitCmd.AddCommand(DeleteIndexCmd)
	LogUnitCmd.AddCommand(GetLogUnitTagsCmd)
	LogUnitCmd.AddCommand(UpdateTtlCmd)
	LogUnitCmd.AddCommand(UpdateLogUnitNoteCmd)

	// 添加项目创建命令的选项
	createLogUnitCmd.Flags().StringP("project_code", "p", "", "project_code")
	createLogUnitCmd.Flags().StringP("unit_name", "u", "", "unit_name")
	createLogUnitCmd.Flags().IntP("ttlDays", "t", -1, "ttlDays")
	createLogUnitCmd.Flags().StringP("description", "d", "", "description")
	createLogUnitCmd.MarkFlagRequired("project_code")
	createLogUnitCmd.MarkFlagRequired("unit_name")

	updateLogUnitCmd.Flags().StringP("project_code", "p", "", "project_code")
	updateLogUnitCmd.Flags().StringP("unit_code", "u", "", "unit_code")
	updateLogUnitCmd.Flags().IntP("ttlDays", "t", -1, "ttlDays")
	updateLogUnitCmd.Flags().StringP("description", "d", "", "description")
	updateLogUnitCmd.MarkFlagRequired("project_code")
	updateLogUnitCmd.MarkFlagRequired("unit_code")

	deleteLogUnitCmd.Flags().StringP("project_code", "p", "", "project_code")
	deleteLogUnitCmd.Flags().StringP("unit_code", "u", "", "unit_code")
	deleteLogUnitCmd.MarkFlagRequired("project_code")
	deleteLogUnitCmd.MarkFlagRequired("unit_code")

	listLogUnitCmd.Flags().StringP("project_code", "p", "", "project_code")
	listLogUnitCmd.Flags().StringP("unit_name", "u", "", "unit_name")
	listLogUnitCmd.MarkFlagRequired("project_code")

	pageLogUnitCmd.Flags().StringP("project_code", "p", "", "project_code")
	pageLogUnitCmd.Flags().StringP("unit_name", "u", "", "unit_name")
	pageLogUnitCmd.Flags().IntP("page_num", "n", -1, "page_num")
	pageLogUnitCmd.Flags().IntP("page_size", "z", -1, "page_size")
	pageLogUnitCmd.MarkFlagRequired("project_code")

	CreateIndexCmd.Flags().StringP("project_code", "p", "", "project_code")
	CreateIndexCmd.Flags().StringP("unit_code", "u", "", "unit_code")
	CreateIndexCmd.Flags().BoolP("fullTextIndex", "t", true, "fullTextIndex,是否开启全文索引")
	CreateIndexCmd.Flags().StringP("fieldName", "f", "", "索引名称,可填多个值用‘,’隔开")
	CreateIndexCmd.Flags().StringP("dataType", "d", "", "索引类型,可填多个值用‘,’隔开,只支持(double 和 text),应与索引名称一一对应")
	CreateIndexCmd.MarkFlagRequired("project_code")
	CreateIndexCmd.MarkFlagRequired("unit_code")
	CreateIndexCmd.MarkFlagRequired("fieldName")
	CreateIndexCmd.MarkFlagRequired("dataType")

	UpdateIndexCmd.Flags().StringP("project_code", "p", "", "project_code")
	UpdateIndexCmd.Flags().StringP("unit_code", "u", "", "unit_code")
	UpdateIndexCmd.Flags().BoolP("fullTextIndex", "t", true, "fullTextIndex,是否开启全文索引")
	UpdateIndexCmd.Flags().StringP("fieldName", "f", "", "索引名称,可填多个值用‘,’隔开")
	UpdateIndexCmd.Flags().StringP("dataType", "d", "", "索引类型,可填多个值用‘,’隔开,只支持(double 和 text),应与索引名称一一对应")
	UpdateIndexCmd.MarkFlagRequired("project_code")
	UpdateIndexCmd.MarkFlagRequired("unit_code")
	UpdateIndexCmd.MarkFlagRequired("fieldName")
	UpdateIndexCmd.MarkFlagRequired("dataType")

	GetIndexCmd.Flags().StringP("project_code", "p", "", "project_code")
	GetIndexCmd.Flags().StringP("unit_code", "u", "", "unit_code")
	GetIndexCmd.MarkFlagRequired("project_code")
	GetIndexCmd.MarkFlagRequired("unit_code")

	GetIndexCountCmd.Flags().StringP("unit_code", "u", "", "unit_code")
	GetIndexCountCmd.MarkFlagRequired("unit_code")

	DeleteIndexCmd.Flags().StringP("project_code", "p", "", "project_code")
	DeleteIndexCmd.Flags().StringP("unit_code", "u", "", "unit_code")
	DeleteIndexCmd.MarkFlagRequired("project_code")
	DeleteIndexCmd.MarkFlagRequired("unit_code")

	UpdateTtlCmd.Flags().StringP("unit_code", "u", "", "unit_code")
	UpdateTtlCmd.Flags().IntP("ttl_days", "t", 7, "ttl_days")
	UpdateTtlCmd.MarkFlagRequired("unit_code")
	UpdateTtlCmd.MarkFlagRequired("ttl_days")

	CountUnitCmd.Flags().StringP("project_code", "p", "", "project_code")
	CountUnitCmd.MarkFlagRequired("project_code")

	GetUnitByCodeCmd.Flags().StringP("unit_code", "u", "", "unit_code")
	GetUnitByCodeCmd.MarkFlagRequired("unit_code")

	UpdateLogUnitNoteCmd.Flags().StringP("unit_code", "u", "", "unit_code")
	UpdateLogUnitNoteCmd.Flags().StringP("description", "d", "", "description")
	UpdateLogUnitNoteCmd.MarkFlagRequired("unit_code")
	UpdateLogUnitNoteCmd.MarkFlagRequired("description")

	GetLogUnitTagsCmd.Flags().StringP("unit_code", "u", "", "unit_code")
	GetLogUnitTagsCmd.MarkFlagRequired("unit_code")

	GetLogUnitNoteCmd.Flags().StringP("unit_code", "u", "", "unit_code")
	GetLogUnitNoteCmd.MarkFlagRequired("unit_code")

	GetLogUnitOriginNameCmd.Flags().StringP("unit_code", "u", "", "unit_code")
	GetLogUnitOriginNameCmd.MarkFlagRequired("unit_code")

	GetLogUnitAliasNameCmd.Flags().StringP("unit_code", "u", "", "unit_code")
	GetLogUnitAliasNameCmd.MarkFlagRequired("unit_code")

	RenameLogUnitCmd.Flags().StringP("unit_code", "u", "", "unit_code")
	RenameLogUnitCmd.Flags().StringP("alias_name", "n", "", "alias_name")
	RenameLogUnitCmd.MarkFlagRequired("unit_code")
	RenameLogUnitCmd.MarkFlagRequired("alias_name")

	QueryLogUnitAliasNamesCmd.Flags().StringP("project_code", "p", "", "project_code")
	QueryLogUnitAliasNamesCmd.MarkFlagRequired("project_code")

	QueryLogUnitOriginNamesCmd.Flags().StringP("project_code", "p", "", "project_code")
	QueryLogUnitOriginNamesCmd.MarkFlagRequired("project_code")

	GetLogUnitTtlDaysCmd.Flags().StringP("unit_code", "u", "", "unit_code")
	GetLogUnitTtlDaysCmd.MarkFlagRequired("unit_code")

	CheckLogUnitExistCmd.Flags().StringP("unit_name", "u", "", "unit_name")
	CheckLogUnitExistCmd.MarkFlagRequired("unit_name")

	// 添加全局选项
	LogUnitCmd.PersistentFlags().StringP("ak", "a", "", "access_id")
	LogUnitCmd.PersistentFlags().StringP("sk", "s", "", "access_key")
	LogUnitCmd.PersistentFlags().StringP("endpoint", "e", "", "endpoint")
	LogUnitCmd.PersistentFlags().StringP("region_id", "i", "", "region_id")
	LogUnitCmd.PersistentFlags().StringP("config", "c", "", "use config.yaml ,such as: --config=huadong1")
	rootCmd.AddCommand(LogUnitCmd)
}
