package controller

import (
	"cold-delivery/app/admin/model"
	"cold-delivery/app/admin/service"
	"cold-delivery/app/admin/service/dto"
	"cold-delivery/common/actions"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"gogs.baozhida.cn/zoie/OAuth-core/api"
	"gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
	_ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
)

type IceLockerController struct {
	api.Api
}

// GetPage 获取冷冻柜列表
// @Summary 获取冷冻柜列表
// @Description 获取冷冻柜列表
// @Tags 冷冻柜
// @Param name query string false "冷冻柜名称"
// @Param pageSize query int false "页条数"
// @Param page query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]model.IceLocker}} "{"code": 200, "data": [...]}"
// @Router /api/ice-locker [get]
// @Security Bearer
func (e IceLockerController) GetPage(c *gin.Context) {
	s := service.IceLocker{}
	req := dto.IceLockerGetPageReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.Query).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

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

	list := make([]model.IceLocker, 0)
	var count int64

	err = s.GetPage(&req, &list, &count, p)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 通过id获取冷冻柜
// @Summary 通过id获取冷冻柜
// @Description 通过id获取冷冻柜
// @Tags 冷冻柜
// @Param id path string true "冷冻柜id"
// @Success 200 {object} response.Response{data=model.IceLocker} "{"code": 200, "data": [...]}"
// @Router /api/ice-locker/{id} [get]
// @Security Bearer
func (e IceLockerController) Get(c *gin.Context) {
	s := service.IceLocker{}
	req := dto.IceLockerGetReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, nil).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	var object model.IceLocker
	p := actions.GetPermissionFromContext(c)

	//数据权限检查
	err = s.Get(&req, &object, p)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}

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

// Insert 添加冷冻柜
// @Summary 添加冷冻柜
// @Description 添加冷冻柜
// @Tags 冷冻柜
// @Accept  application/json
// @Product application/json
// @Param data body dto.IceLockerInsertReq true "data"
// @Success 200 {string} string	"{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "添加失败"}"
// @Router /api/ice-locker [post]
// @Security Bearer
func (e IceLockerController) Insert(c *gin.Context) {
	s := service.IceLocker{}
	req := dto.IceLockerInsertReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	p := actions.GetPermissionFromContext(c)
	// 设置创建人
	req.SetCreateBy(user.GetUserId(c))
	req.SetDeptId(p.DeptId)
	err = s.Insert(&req, p)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.OK(req.GetId(), "添加成功")
}

// Update 修改冷冻柜
// @Summary 修改冷冻柜
// @Description 修改冷冻柜
// @Tags 冷冻柜
// @Accept  application/json
// @Product application/json
// @Param data body dto.IceLockerUpdateReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "添加失败"}"
// @Router /api/ice-locker [put]
// @Security Bearer
func (e IceLockerController) Update(c *gin.Context) {
	s := service.IceLocker{}
	req := dto.IceLockerUpdateReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	p := actions.GetPermissionFromContext(c)

	req.SetUpdateBy(user.GetUserId(c))
	err = s.Update(&req, p)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.OK(req.GetId(), "修改成功")
}

// Delete 删除冷冻柜
// @Summary 删除冷冻柜
// @Description 删除冷冻柜
// @Tags 冷冻柜
// @Accept  application/json
// @Product application/json
// @Param data body dto.IceLockerDeleteReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "删除失败"}"
// @Router /api/ice-locker [delete]
// @Security Bearer
func (e IceLockerController) Delete(c *gin.Context) {
	s := service.IceLocker{}
	req := dto.IceLockerDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON, nil).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	//数据权限检查
	p := actions.GetPermissionFromContext(c)
	err = s.Remove(&req, p)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.OK(req.GetId(), "删除成功")
}

// BatchInsert 批量添加冷冻柜
// @Summary 批量添加冷冻柜
// @Description 批量添加冷冻柜
// @Tags 冷冻柜
// @Accept  application/json
// @Product application/json
// @Param data body dto.IceLockerBatchInsertReq true "data"
// @Success 200 {string} string	"{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "添加失败"}"
// @Router /api/ice-locker/import [post]
// @Security Bearer
func (e IceLockerController) BatchInsert(c *gin.Context) {
	s := service.IceLocker{}
	req := dto.IceLockerBatchInsertReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	p := actions.GetPermissionFromContext(c)
	// 设置创建人
	req.SetCreateBy(user.GetUserId(c))
	req.SetDeptId(p.DeptId)
	err = s.BatchInsert(&req)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.OK(nil, "添加成功")
}

// GetIceLocker 获取指定冷冻柜下的所有冰排信息
// @Summary 获取指定冷冻柜下的所有冰排信息
// @Description 获取指定冷冻柜下的所有冰排信息
// @Tags 冷冻柜
// @Accept  application/json
// @Product application/json
// @Param data body dto.CoolerBoxBatchInsertReq true "data"
// @Success 200 {string} string	"{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "添加失败"}"
// @Router /api/cooler-box/geticelocker [get]
// @Security Bearer
func (e IceLockerController) GetIceLocker(c *gin.Context) {
	s := service.IceLocker{}
	req := dto.GetIceLockerReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.Form).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	list := make([]model.IceRaft, 0)
	var count int64
	//数据权限检查
	p := actions.GetPermissionFromContext(c)
	err = s.GetIceLocker(&req, &list, p, &count)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}
