package pasture

import (
	operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
	"gitee.com/xuyiping_admin/pkg/apierr"
	"gitee.com/xuyiping_admin/pkg/ginutil"
	"gitee.com/xuyiping_admin/pkg/valid"
	"kpt-tmr-group/http/middleware"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

func AddCattleCategory(c *gin.Context) {
	var req operationPb.AddCattleCategoryRequest
	if err := ginutil.BindProto(c, &req); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	if err := valid.ValidateStruct(&req,
		valid.Field(&req.Name, valid.Required),
		valid.Field(&req.ParentId, valid.Required, valid.Min(1), valid.Max(6)),
		valid.Field(&req.ParentName, valid.Required),
		valid.Field(&req.Number, valid.Required),
	); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	if err := middleware.BackendOperation(c).OpsService.AddCattleCategory(c, &req); err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}
	ginutil.JSONResp(c, &operationPb.CommonOK{
		Code: http.StatusOK,
		Msg:  "ok",
		Data: &operationPb.Success{Success: true},
	})
}

func EditCattleCategory(c *gin.Context) {
	var req operationPb.AddCattleCategoryRequest
	if err := ginutil.BindProto(c, &req); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	if err := valid.ValidateStruct(&req,
		valid.Field(&req.Id, valid.Required),
		valid.Field(&req.Name, valid.Required),
		valid.Field(&req.ParentId, valid.Required, valid.Min(1), valid.Max(6)),
		valid.Field(&req.ParentName, valid.Required),
		valid.Field(&req.Number, valid.Required),
	); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	if err := middleware.BackendOperation(c).OpsService.EditCattleCategory(c, &req); err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}
	ginutil.JSONResp(c, &operationPb.CommonOK{
		Code: http.StatusOK,
		Msg:  "ok",
		Data: &operationPb.Success{Success: true},
	})
}

func IsShowCattleCategory(c *gin.Context) {
	var req operationPb.IsShowCattleCategory
	if err := ginutil.BindProto(c, &req); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}
	if err := valid.ValidateStruct(&req,
		valid.Field(&req.CattleCategoryId, valid.Required, valid.Min(1)),
		valid.Field(&req.IsShow, valid.Required, valid.Min(1), valid.Max(2)),
	); err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}

	if err := middleware.BackendOperation(c).OpsService.IsShowCattleCategory(c, &req); err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}

	ginutil.JSONResp(c, &operationPb.CommonOK{
		Code: http.StatusOK,
		Msg:  "ok",
		Data: &operationPb.Success{Success: true},
	})
}

func DeleteCattleCategory(c *gin.Context) {
	cattleCategoryIdStr := c.Param("cattle_category_id")
	cattleCategoryId, _ := strconv.Atoi(cattleCategoryIdStr)

	if err := valid.Validate(cattleCategoryId, valid.Required, valid.Min(1)); err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}

	if err := middleware.BackendOperation(c).OpsService.DeleteCattleCategory(c, int64(cattleCategoryId)); err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}

	ginutil.JSONResp(c, &operationPb.CommonOK{
		Code: http.StatusOK,
		Msg:  "ok",
		Data: &operationPb.Success{Success: true},
	})
}

func SearchCattleCategory(c *gin.Context) {
	req := &operationPb.SearchCattleCategoryRequest{}
	if err := ginutil.BindProto(c, req); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	req.Pagination = &operationPb.PaginationModel{
		Page:       int32(c.GetInt(middleware.Page)),
		PageSize:   int32(c.GetInt(middleware.PageSize)),
		PageOffset: int32(c.GetInt(middleware.PageOffset)),
	}

	res, err := middleware.BackendOperation(c).OpsService.SearchCattleCategoryList(c, req)
	if err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}

	ginutil.JSONResp(c, res)
}

func AddForageCategory(c *gin.Context) {
	var req operationPb.AddForageCategoryRequest
	if err := ginutil.BindProto(c, &req); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	if err := valid.ValidateStruct(&req,
		valid.Field(&req.Name, valid.Required),
		valid.Field(&req.ParentId, valid.Required, valid.Min(1), valid.Max(6)),
		valid.Field(&req.ParentName, valid.Required),
		valid.Field(&req.Number, valid.Required),
	); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	if err := middleware.BackendOperation(c).OpsService.AddForageCategory(c, &req); err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}
	ginutil.JSONResp(c, &operationPb.CommonOK{
		Code: http.StatusOK,
		Msg:  "ok",
		Data: &operationPb.Success{Success: true},
	})
}

func EditForageCategory(c *gin.Context) {
	var req operationPb.AddForageCategoryRequest
	if err := ginutil.BindProto(c, &req); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	if err := valid.ValidateStruct(&req,
		valid.Field(&req.Id, valid.Required),
		valid.Field(&req.Name, valid.Required),
		valid.Field(&req.ParentId, valid.Required, valid.Min(1), valid.Max(6)),
		valid.Field(&req.ParentName, valid.Required),
		valid.Field(&req.Number, valid.Required),
	); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	if err := middleware.BackendOperation(c).OpsService.EditForageCategory(c, &req); err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}
	ginutil.JSONResp(c, &operationPb.CommonOK{
		Code: http.StatusOK,
		Msg:  "ok",
		Data: &operationPb.Success{Success: true},
	})
}

func IsShowForageCategory(c *gin.Context) {
	var req operationPb.IsShowForageCategory
	if err := ginutil.BindProto(c, &req); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}
	if err := valid.ValidateStruct(&req,
		valid.Field(&req.ForageCategoryId, valid.Required, valid.Min(1)),
		valid.Field(&req.IsShow, valid.Required, valid.Min(1), valid.Max(2)),
	); err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}

	if err := middleware.BackendOperation(c).OpsService.IsShowForageCategory(c, &req); err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}

	ginutil.JSONResp(c, &operationPb.CommonOK{
		Code: http.StatusOK,
		Msg:  "ok",
		Data: &operationPb.Success{Success: true},
	})
}

func DeleteForageCategory(c *gin.Context) {
	forageCategoryIdStr := c.Param("forage_category_id")
	forageCategoryId, _ := strconv.Atoi(forageCategoryIdStr)

	if err := valid.Validate(forageCategoryId, valid.Required, valid.Min(1)); err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}

	if err := middleware.BackendOperation(c).OpsService.DeleteForageCategory(c, int64(forageCategoryId)); err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}

	ginutil.JSONResp(c, &operationPb.CommonOK{
		Code: http.StatusOK,
		Msg:  "ok",
		Data: &operationPb.Success{Success: true},
	})
}

func SearchForageCategory(c *gin.Context) {
	req := &operationPb.SearchForageCategoryRequest{}
	if err := c.BindJSON(req); err != nil {
		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
		return
	}

	req.Pagination = &operationPb.PaginationModel{
		Page:       int32(c.GetInt(middleware.Page)),
		PageSize:   int32(c.GetInt(middleware.PageSize)),
		PageOffset: int32(c.GetInt(middleware.PageOffset)),
	}

	res, err := middleware.BackendOperation(c).OpsService.SearchForageCategoryList(c, req)
	if err != nil {
		apierr.ClassifiedAbort(c, err)
		return
	}

	ginutil.JSONResp(c, res)
}