package {{.ModuleName}}

import (
	"github.com/gin-gonic/gin"
	"go-admin/app/admin/service"
	common "go-admin/common/models"
	"net/http"

	"go-admin/app/admin/models"
	"go-admin/app/admin/service/dto"
	"go-admin/common/actions"
	"go-admin/common/apis"
	"go-admin/common/log"
	"go-admin/tools"
)

type {{.ClassName}} struct {
	apis.Api
}

func (e *{{.ClassName}}) Get{{.ClassName}}List(c *gin.Context) {
	msgID := tools.GenerateMsgIDFromContext(c)
	d := new(dto.{{.ClassName}}Search)
	db, err := tools.GetOrm(c)
	if err != nil {
		log.Error(err)
		return
	}

	req := d.Generate()

	//查询列表
	err = req.Bind(c)
	if err != nil {
		e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
		return
	}

	//数据权限检查
	p := actions.GetPermissionFromContext(c)

	list := make([]models.{{.ClassName}}, 0)
	var count int64
	serviceStudent := service.{{.ClassName}}{}
	serviceStudent.MsgID = msgID
	serviceStudent.Orm = db
	err = serviceStudent.Get{{.ClassName}}Page(req, p, &list, &count)
	if err != nil {
		e.Error(c, http.StatusUnprocessableEntity, err, "查询失败")
		return
	}

	e.PageOK(c, list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

func (e *{{.ClassName}}) Get{{.ClassName}}(c *gin.Context) {
	control := new(dto.{{.ClassName}}ById)
	db, err := tools.GetOrm(c)
	if err != nil {
		log.Error(err)
		return
	}

	msgID := tools.GenerateMsgIDFromContext(c)
	//查看详情
	req := control.Generate()
	err = req.Bind(c)
	if err != nil {
		e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
		return
	}
	var object models.{{.ClassName}}

	//数据权限检查
	p := actions.GetPermissionFromContext(c)

	service{{.ClassName}} := service.{{.ClassName}}{}
	service{{.ClassName}}.MsgID = msgID
	service{{.ClassName}}.Orm = db
	err = service{{.ClassName}}.Get{{.ClassName}}(req, p, &object)
	if err != nil {
		e.Error(c, http.StatusUnprocessableEntity, err, "查询失败")
		return
	}

	e.OK(c, object, "查看成功")
}

func (e *{{.ClassName}}) Insert{{.ClassName}}(c *gin.Context) {
	control := new(dto.{{.ClassName}}Control)
	db, err := tools.GetOrm(c)
	if err != nil {
		log.Error(err)
		return
	}

	msgID := tools.GenerateMsgIDFromContext(c)
	//新增操作
	req := control.Generate()
	err = req.Bind(c)
	if err != nil {
		e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
		return
	}
	var object common.ActiveRecord
	object, err = req.GenerateM()
	if err != nil {
		e.Error(c, http.StatusInternalServerError, err, "模型生成失败")
		return
	}
	// 设置创建人
	object.SetCreateBy(tools.GetUserIdUint(c))

	service{{.ClassName}} := service.{{.ClassName}}{}
	service{{.ClassName}}.Orm = db
	service{{.ClassName}}.MsgID = msgID
	err = service{{.ClassName}}.Insert{{.ClassName}}(object)
	if err != nil {
		log.Error(err)
		e.Error(c, http.StatusInternalServerError, err, "创建失败")
		return
	}

	e.OK(c, object.GetId(), "创建成功")
}

func (e *{{.ClassName}}) Update{{.ClassName}}(c *gin.Context) {
	control := new(dto.{{.ClassName}}Control)
	db, err := tools.GetOrm(c)
	if err != nil {
		log.Error(err)
		return
	}

	msgID := tools.GenerateMsgIDFromContext(c)
	req := control.Generate()
	//更新操作
	err = req.Bind(c)
	if err != nil {
		e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
		return
	}
	var object common.ActiveRecord
	object, err = req.GenerateM()
	if err != nil {
		e.Error(c, http.StatusInternalServerError, err, "模型生成失败")
		return
	}
	object.SetUpdateBy(tools.GetUserIdUint(c))

	//数据权限检查
	p := actions.GetPermissionFromContext(c)

	service{{.ClassName}} := service.{{.ClassName}}{}
	service{{.ClassName}}.Orm = db
	service{{.ClassName}}.MsgID = msgID
	err = service{{.ClassName}}.Update{{.ClassName}}(object, p)
	if err != nil {
		log.Error(err)
		return
	}
	e.OK(c, object.GetId(), "更新成功")
}

func (e *{{.ClassName}}) Delete{{.ClassName}}(c *gin.Context) {
	control := new(dto.{{.ClassName}}ById)
	db, err := tools.GetOrm(c)
	if err != nil {
		log.Error(err)
		return
	}

	msgID := tools.GenerateMsgIDFromContext(c)
	//删除操作
	req := control.Generate()
	err = req.Bind(c)
	if err != nil {
		log.Errorf("MsgID[%s] Bind error: %s", msgID, err)
		e.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
		return
	}
	var object common.ActiveRecord
	object, err = req.GenerateM()
	if err != nil {
		e.Error(c, http.StatusInternalServerError, err, "模型生成失败")
		return
	}

	// 设置编辑人
	object.SetUpdateBy(tools.GetUserIdUint(c))

	// 数据权限检查
	p := actions.GetPermissionFromContext(c)

	service{{.ClassName}} := service.{{.ClassName}}{}
	service{{.ClassName}}.Orm = db
	service{{.ClassName}}.MsgID = msgID
	err = service{{.ClassName}}.Remove{{.ClassName}}(req, object, p)
	if err != nil {
		log.Error(err)
		return
	}
	e.OK(c, object.GetId(), "删除成功")
}
