// Code generated by hertz generator.

package admin

import (
	"context"
	admin2 "formulago/biz/domain/admin"
	logic "formulago/biz/logic/admin"
	"formulago/data"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
	"github.com/jinzhu/copier"

	"formulago/api/model/admin"
	base "formulago/api/model/base"
	"github.com/cloudwego/hertz/pkg/app"
)

// CreateDictionary .
// @router /api/admin/dict/create [POST]
func CreateDictionary(ctx context.Context, c *app.RequestContext) {
	var err error
	var req admin.DictionaryInfo
	resp := new(base.BaseResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	var DictInfo admin2.DictionaryInfo
	err = copier.Copy(&DictInfo, &req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}
	err = logic.NewDictionary(data.Default()).Create(ctx, &DictInfo)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// UpdateDictionary .
// @router /api/admin/dict/update [POST]
func UpdateDictionary(ctx context.Context, c *app.RequestContext) {
	var err error
	var req admin.DictionaryInfo
	resp := new(base.BaseResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	var DictInfo admin2.DictionaryInfo
	err = copier.Copy(&DictInfo, &req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}
	err = logic.NewDictionary(data.Default()).Update(ctx, &DictInfo)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// DeleteDictionary .
// @router /api/admin/dict [DELETE]
func DeleteDictionary(ctx context.Context, c *app.RequestContext) {
	var err error
	var req base.IDReq
	resp := new(base.BaseResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	err = logic.NewDictionary(data.Default()).Delete(ctx, req.ID)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// DictionaryList .
// @router /api/admin/dict/list [GET]
func DictionaryList(ctx context.Context, c *app.RequestContext) {
	var err error
	var req admin.DictionaryPageReq
	resp := new(admin.DictionaryListResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	var dictListReq admin2.DictListReq
	err = copier.Copy(&dictListReq, &req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}
	// get dict list
	dictList, total, err := logic.NewDictionary(data.Default()).List(ctx, &dictListReq)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}
	var dataList []*admin.DictionaryInfo
	for _, dict := range dictList {
		dataList = append(dataList, &admin.DictionaryInfo{
			ID:          dict.ID,
			Name:        dict.Name,
			Title:       dict.Title,
			Status:      dict.Status,
			Description: dict.Description,
			CreatedAt:   dict.CreatedAt,
			UpdatedAt:   dict.UpdatedAt,
		})
	}

	resp.Data = dataList
	resp.Total = uint64(total)
	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// CreateDictionaryDetail .
// @router /api/admin/dict/detail/create [POST]
func CreateDictionaryDetail(ctx context.Context, c *app.RequestContext) {
	var err error
	var req admin.DictionaryDetail
	resp := new(base.BaseResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	var DictDetail admin2.DictionaryDetail
	err = copier.Copy(&DictDetail, &req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	err = logic.NewDictionary(data.Default()).CreateDetail(ctx, &DictDetail)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// UpdateDictionaryDetail .
// @router /api/admin/dict/detail/update [POST]
func UpdateDictionaryDetail(ctx context.Context, c *app.RequestContext) {
	var err error
	var req admin.DictionaryDetail
	resp := new(base.BaseResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	var DictDetail admin2.DictionaryDetail
	err = copier.Copy(&DictDetail, &req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	err = logic.NewDictionary(data.Default()).UpdateDetail(ctx, &DictDetail)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// DeleteDictionaryDetail .
// @router /api/admin/dict/detail [DELETE]
func DeleteDictionaryDetail(ctx context.Context, c *app.RequestContext) {
	var err error
	var req base.IDReq
	resp := new(base.BaseResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	err = logic.NewDictionary(data.Default()).DeleteDetail(ctx, req.ID)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}

	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}

// DetailByDictionaryName .
// @router /api/admin/dict/detail/list [GET]
func DetailByDictionaryName(ctx context.Context, c *app.RequestContext) {
	var err error
	var req admin.DictionaryDetailReq
	resp := new(admin.DictionaryDetailListResp)
	err = c.BindAndValidate(&req)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusBadRequest, resp)
		return
	}

	dictDetailList, total, err := logic.NewDictionary(data.Default()).DetailListByDictName(ctx, req.Name)
	if err != nil {
		resp.ErrCode = base.ErrCode_Fail
		resp.ErrMsg = err.Error()
		c.JSON(consts.StatusInternalServerError, resp)
		return
	}
	var dataList []*admin.DictionaryDetail
	for _, dictDetail := range dictDetailList {
		dataList = append(dataList, &admin.DictionaryDetail{
			ID:        dictDetail.ID,
			ParentID:  dictDetail.ParentID,
			Title:     dictDetail.Title,
			Status:    dictDetail.Status,
			Key:       dictDetail.Key,
			Value:     dictDetail.Value,
			CreatedAt: dictDetail.CreatedAt,
			UpdatedAt: dictDetail.UpdatedAt,
		})
	}

	resp.Data = dataList
	resp.Total = total
	resp.ErrCode = base.ErrCode_Success
	resp.ErrMsg = "success"
	c.JSON(consts.StatusOK, resp)
}
