package controller

import (
	"cold-delivery/app/admin/model"
	"cold-delivery/app/admin/service"
	"cold-delivery/app/admin/service/dto"
	"cold-delivery/common/actions"
	"encoding/json"
	"fmt"
	"github.com/beego/beego/v2/core/logs"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/xuri/excelize/v2"
	"gogs.baozhida.cn/zoie/OAuth-core/api"
	"gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
	_ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
	"math"
	"net/url"
	"os"
	"time"
)

type IceRaftController 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.IceRaft}} "{"code": 200, "data": [...]}"
// @Router /api/ice-raft [get]
// @Security Bearer
func (e IceRaftController) GetPage(c *gin.Context) {
	s := service.IceRaft{}
	req := dto.IceRaftGetPageReq{}
	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.IceRaft, 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(), "查询成功")
}
func (e IceRaftController) GetPageByCoolerBoxId(c *gin.Context) {
	s := service.IceRaft{}
	req := dto.GetPageByCoolerBoxIdReq{}
	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([]string, 0)

	err = s.GetPageByCoolerBoxId(req.CoolerBoxId, &list, p)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.OK(list, "查询成功")
}

// GetNewestRecordPage 获取冰排记录列表
// @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.IceRaft}} "{"code": 200, "data": [...]}"
// @Router /api/ice-raft [get]
// @Security Bearer
func (e IceRaftController) GetNewestRecordPage(c *gin.Context) {
	s := service.IceRaft{}
	req := dto.IceRaftGetNewestRecordPageReq{}
	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.IceRaft, 0)
	var count int64

	err = s.GetRecordPage(&req, &list, &count, p)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}
func (e IceRaftController) GetNewestRecordPageSSE(c *gin.Context) {
	s := service.IceRaft{}
	req := dto.IceRaftGetNewestRecordPageReq{}
	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.IceRaft, 0)
	var count int64

	// Set the response header to indicate SSE content type
	c.Header("Content-Type", "text/event-stream")
	c.Header("Cache-Control", "no-cache")
	c.Header("Connection", "keep-alive")
	// Create a channel to send events to the client
	println("Client connected")
	// Listen for client close and remove the client from the list
	notify := c.Writer.CloseNotify()
	go func() {
		<-notify
		fmt.Println("Client disconnected")
	}()
	type Page struct {
		Count    int         `json:"count"`    //总数
		Page     int         `json:"page"`     //页码
		PageSize int         `json:"pageSize"` //页条数
		List     interface{} `json:"list"`
	}
	type Response struct {
		RequestId string `protobuf:"bytes,1,opt,name=requestId,proto3" json:"requestId,omitempty"`
		Code      int32  `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"`
		Msg       string `protobuf:"bytes,3,opt,name=msg,proto3" json:"msg,omitempty"`
		Status    string `protobuf:"bytes,4,opt,name=status,proto3" json:"status,omitempty"`
		Data      Page   `json:"data"`
	}
	// Continuously send data to the client
	for {
		err = s.GetRecordPage(&req, &list, &count, p)
		if err != nil {
			e.Error(500, err, err.Error())
			return
		}

		var response Response
		var page Page
		response.Code = 200
		response.Msg = "查询成功"
		response.Msg = "查询成功"
		page.Count = int(count)
		page.Page = req.GetPageIndex()
		page.PageSize = req.GetPageSize()
		page.List = list
		response.Data = page
		//data := <-eventChan
		res, _ := json.Marshal(&response)
		fmt.Fprintf(c.Writer, "data: %s\n\n", string(res))
		c.Writer.Flush()
		time.Sleep(10 * time.Second)
	}
}

// Get 通过id获取冰排
// @Summary 通过id获取冰排
// @Description 通过id获取冰排
// @Tags 冰排
// @Param id path string true "冰排id"
// @Success 200 {object} response.Response{data=model.IceRaft} "{"code": 200, "data": [...]}"
// @Router /api/ice-raft/{id} [get]
// @Security Bearer
func (e IceRaftController) Get(c *gin.Context) {
	s := service.IceRaft{}
	req := dto.IceRaftGetReq{}
	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.IceRaft
	p := actions.GetPermissionFromContext(c)

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

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

// GetByCode 通过code获取冰排
// @Summary 通过code获取冰排
// @Description 通过code获取冰排
// @Tags 冰排
// @Param id path string true "冰排id"
// @Success 200 {object} response.Response{data=model.IceRaft} "{"code": 200, "data": [...]}"
// @Router /api/ice-raft/code/{code} [get]
// @Security Bearer
func (e IceRaftController) GetByCode(c *gin.Context) {
	s := service.IceRaft{}
	req := dto.IceRaftGetByCodeReq{}
	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.IceRaft
	p := actions.GetPermissionFromContext(c)

	//数据权限检查
	err = s.GetByCode(&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.IceRaftInsertReq true "data"
// @Success 200 {string} string	"{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "添加失败"}"
// @Router /api/ice-raft [post]
// @Security Bearer
func (e IceRaftController) Insert(c *gin.Context) {
	s := service.IceRaft{}
	req := dto.IceRaftInsertReq{}
	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)
	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.IceRaftUpdateReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "添加失败"}"
// @Router /api/ice-raft [put]
// @Security Bearer
func (e IceRaftController) Update(c *gin.Context) {
	s := service.IceRaft{}
	req := dto.IceRaftUpdateReq{}
	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.IceRaftDeleteReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "删除失败"}"
// @Router /api/ice-raft [delete]
// @Security Bearer
func (e IceRaftController) Delete(c *gin.Context) {
	s := service.IceRaft{}
	req := dto.IceRaftDeleteReq{}
	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(), "删除成功")
}

// InStorage 冰排入库
// @Summary 冰排入库
// @Description 冰排入库
// @Tags 冰排
// @Accept  application/json
// @Product application/json
// @Param data body dto.IceRaftInStorageReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "删除失败"}"
// @Router /api/ice-raft [delete]
// @Security Bearer
func (e IceRaftController) InStorage(c *gin.Context) {
	s := service.IceRaft{}
	req := dto.IceRaftInStorageReq{}
	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.InStorage(&req, p)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.OK(nil, "入库成功")
}

// OutStorage 冰排出库
// @Summary 冰排出库
// @Description 冰排出库
// @Tags 冰排
// @Accept  application/json
// @Product application/json
// @Param data body dto.IceRaftOutStorageReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "删除失败"}"
// @Router /api/ice-raft [delete]
// @Security Bearer
func (e IceRaftController) OutStorage(c *gin.Context) {
	s := service.IceRaft{}
	req := dto.IceRaftOutStorageReq{}
	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.OutStorage(&req, p)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.OK(nil, "出库成功")
}

// IsOutStorage 判断冰排出库
// @Summary 判断冰排出库
// @Description 判断冰排出库
// @Tags 冰排
// @Accept  application/json
// @Product application/json
// @Param data body dto.IceRaftOutStorageReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "删除失败"}"
// @Router /api/isoutstorage [post]
// @Security Bearer
func (e IceRaftController) IsOutStorage(c *gin.Context) {
	s := service.IceRaft{}
	req := dto.IceOutStorageReq{}
	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, str := s.IsOutStorage(&req, p)
	if err != nil {
		if len(str) > 0 {
			e.Error(1201, err, err.Error())
			return
		} else {
			e.Error(500, err, err.Error())
			return
		}
	}
	e.OK(nil, str)
}

// BindCoolerBox 冰排绑定保温箱
// @Summary 冰排绑定保温箱
// @Description 冰排绑定保温箱
// @Tags 冰排
// @Accept  application/json
// @Product application/json
// @Param data body dto.IceRaftOutStorageReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "绑定成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "绑定失败"}"
// @Router /api/bindCoolerBox [post]
// @Security Bearer
func (e IceRaftController) BindCoolerBox(c *gin.Context) {
	s := service.IceRaft{}
	req := dto.BindCoolerBox{}
	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.BindCookerBox(&req, p)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.OK(nil, "绑定成功")
}

// GetCoolerBoxBindCieRaft 获取保温箱可绑定冰排
// @Summary 获取保温箱可绑定冰排
// @Description 获取保温箱可绑定冰排
// @Tags 冰排
// @Accept  application/json
// @Product application/json
// @Param data body dto.IceRaftOutStorageReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "绑定成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "绑定失败"}"
// @Router /api/bindCoolerBox [post]
// @Security Bearer
func (e IceRaftController) GetCoolerBoxBindCieRaft(c *gin.Context) {
	s := service.IceRaft{}
	req := dto.GetCoolerBoxByIceRaft{}
	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
	}
	//数据权限检查
	list := make([]string, 0)
	p := actions.GetPermissionFromContext(c)
	err = s.GetCoolerBoxBindCieRaft(&req, p, &list)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.OK(list, "获取成功")
}

// GetIceRaft 获取未入库冰排
// @Summary 获取未入库冰排
// @Description 获取未入库冰排
// @Tags 冰排
// @Accept  application/json
// @Product application/json
// @Param data body dto.IceRaftOutStorageReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "绑定成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "绑定失败"}"
// @Router /api/bindCoolerBox [post]
// @Security Bearer
func (e IceRaftController) GetIceRaft(c *gin.Context) {
	s := service.IceRaft{}
	err := e.MakeContext(c).
		MakeOrm().
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	//数据权限检查
	list := make([]model.IceRaft, 0)
	p := actions.GetPermissionFromContext(c)
	err = s.GetCoolerIceRaft(p, &list)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.OK(list, "获取成功")
}

// UnBindCoolerBox 冰排解邦
// @Summary 冰排解邦
// @Description 冰排解邦
// @Tags 冰排
// @Accept  application/json
// @Product application/json
// @Param data body dto.IceRaftOutStorageReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "绑定成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "绑定失败"}"
// @Router /api/UnbindCoolerBox [post]
// @Security Bearer
func (e IceRaftController) UnBindCoolerBox(c *gin.Context) {
	s := service.IceRaft{}
	req := dto.UnBindCoolerBox{}
	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.UnBindCookerBox(&req, p)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.OK(nil, "修改成功")
}

type IceRaftRecordController 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.IceRaft}} "{"code": 200, "data": [...]}"
// @Router /api/ice-raft [get]
// @Security Bearer
func (e IceRaftRecordController) GetPage(c *gin.Context) {
	s := service.IceRaftRecord{}
	req := dto.IceRaftRecordGetPageReq{}
	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.IceRaftRecord, 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(), "查询成功")
}

// Update 修改冰排出入库历史记录
// @Summary 修改冰排出入库历史记录
// @Description 修改冰排出入库历史记录
// @Tags 冰排
// @Accept  application/json
// @Product application/json
// @Param data body dto.IceRaftUpdateReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "添加失败"}"
// @Router /api/ice-raft [put]
// @Security Bearer
func (e IceRaftRecordController) Update(c *gin.Context) {
	s := service.IceRaftRecord{}
	req := dto.IceRaftRecordUpdateReq{}
	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.IceRaftDeleteReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "删除失败"}"
// @Router /api/ice-raft [delete]
// @Security Bearer
func (e IceRaftRecordController) Delete(c *gin.Context) {
	s := service.IceRaftRecord{}
	req := dto.IceRaftRecordDeleteReq{}
	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(), "删除成功")
}

// EndForCold 保温箱冰排结束适冷
// @Summary 保温箱冰排结束适冷
// @Description 保温箱冰排结束适冷
// @Tags 冰排
// @Accept  application/json
// @Product application/json
// @Param data body dto.IceRaftRecordEedReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "提交成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "提交成功"}"
// @Router /api/end-for-cold [post]
// @Security Bearer
func (e IceRaftRecordController) EndForCold(c *gin.Context) {
	s := service.IceRaftRecord{}
	req := dto.IceRaftRecordEedReq{}
	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.EndForCold(&req, p)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.OK(req.Id, "提交成功")
}

// StartForCold 冰排开始释冷
// @Summary 冰排开始释冷
// @Description 冰排开始释冷
// @Tags 冰排
// @Accept  application/json
// @Product application/json
// @Param data body dto.IceRaftRecordEedReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "提交成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "提交成功"}"
// @Router /api/start-for-cold [post]
// @Security Bearer
func (e IceRaftRecordController) StartForCold(c *gin.Context) {
	s := service.IceRaftRecord{}
	req := dto.IceStartbleForColfTimReq{}
	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.StartForCold(&req, p)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.OK(req, "提交成功")
}

// IceRaftRecordRecording 冰排使用记录
// @Summary 冰排使用记录
// @Description 冰排使用记录
// @Tags 冰排
// @Accept  application/json
// @Product application/json
// @Param data body dto.IceRaftRecordEedReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "提交成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "提交成功"}"
// @Router /api/recording [get]
// @Security Bearer
func (e IceRaftRecordController) IceRaftRecordRecording(c *gin.Context) {
	s := service.IceRaft{}
	req := dto.IceRaftRecordRecording{}
	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.IceRaftRecord, 0)
	var count int64

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

// ExportExecl 导出冰排使用记录
// @Summary 导出冰排使用记录
// @Description 导出冰排使用记录
// @Tags 冰排
// @Accept  application/json
// @Product application/json
// @Param data body dto.IceRaftRecordEedReq true "body"
// @Success 200 {string} string	"{"code": 200, "message": "提交成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "提交成功"}"
// @Router /api/export-execl [get]
// @Security Bearer
func (e IceRaftRecordController) ExportExecl(c *gin.Context) {
	s := service.IceRaft{}
	req := dto.IceRaftRecordRecording{}
	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.IceRaftRecord, 0)
	var count int64
	req.Page = 1
	req.PageSize = 9999
	err = s.IceRaftRecordRecording(&req, &list, &count, p)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	f := excelize.NewFile() // 设置单元格的值
	//获取当前登录用户信息,查询是否有权限
	err, sysUser := service.GetUserProfile(e.Orm, p.UserId)
	if err != nil {
		e.Error(500, err, "获取用户信息失败")
		return
	}
	if sysUser.Dept.IsIceReleaseCold {
		// 这里设置表头ÒÒ
		f.SetCellValue("Sheet1", "A1", "序号")
		f.SetCellValue("Sheet1", "B1", "冰排编号")
		f.SetCellValue("Sheet1", "C1", "入库时间")
		f.SetCellValue("Sheet1", "D1", "出库时间")
		f.SetCellValue("Sheet1", "E1", "所在位置")
		f.SetCellValue("Sheet1", "F1", "冷冻要求")
		f.SetCellValue("Sheet1", "G1", "冷冻时间")
		f.SetCellValue("Sheet1", "H1", "释冷日期")
		f.SetCellValue("Sheet1", "I1", "释冷时间(小时)")
		f.SetCellValue("Sheet1", "J1", "释冷温度")
		f.SetCellValue("Sheet1", "K1", "释冷地点")
		f.SetCellValue("Sheet1", "L1", "操作人员")
		f.SetCellValue("Sheet1", "M1", "使用日期")
		f.SetCellValue("Sheet1", "N1", "使用时间")
		f.SetCellValue("Sheet1", "O1", "使用人员")
		f.SetCellValue("Sheet1", "P1", "备注")

		// 设置列宽
		f.SetColWidth("Sheet1", "A", "A", 6)
		f.SetColWidth("Sheet1", "B", "B", 8)
		f.SetColWidth("Sheet1", "C", "C", 14)
		f.SetColWidth("Sheet1", "D", "D", 14)
		f.SetColWidth("Sheet1", "E", "E", 15)
		f.SetColWidth("Sheet1", "F", "F", 14)
		f.SetColWidth("Sheet1", "G", "G", 14)
		f.SetColWidth("Sheet1", "H", "H", 14)
		f.SetColWidth("Sheet1", "I", "J", 15)
		f.SetColWidth("Sheet1", "K", "K", 15)
		f.SetColWidth("Sheet1", "L", "K", 15)
		f.SetColWidth("Sheet1", "M", "K", 15)
		f.SetColWidth("Sheet1", "N", "K", 15)
		f.SetColWidth("Sheet1", "O", "K", 15)
		f.SetColWidth("Sheet1", "P", "K", 15)

		line := 1

		// 循环写入数据
		for i, v := range list {
			line++
			start, _ := time.Parse("2006-01-02 15:04:05", v.StartIceColdTime.String())
			end, _ := time.Parse("2006-01-02 15:04:05", v.EndForColdTime.String())
			if v.EndForColdTime.String() == "" {
				end = time.Now()
			}

			IceColdTime := end.Sub(start)
			ColdTime := fmt.Sprintf("%vh%vm", math.Floor(IceColdTime.Hours()), int(IceColdTime.Minutes())%60)

			Updstart, _ := time.Parse("2006-01-02 15:04:05", v.ReturnDate.String())
			Useend, _ := time.Parse("2006-01-02 15:04:05", v.IceUseTime.String())
			if v.ReturnDate.String() == "" {
				Updstart = time.Now()
			}
			sub := Updstart.Sub(Useend)
			subTime := fmt.Sprintf("%vh%vm", math.Floor(sub.Hours()), int(sub.Minutes())%60)
			f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
			f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.IceRafts.Code)
			f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.InStorageTime)
			f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.OutStorageTime)
			f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.CoolerBox.Name)
			f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.FreezeClaim)
			f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.FreezeDuration)
			f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.StartIceColdTime)
			f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), ColdTime)
			f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), v.SuitableForCold)
			f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), v.IceColdAddress)
			f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), v.IceColdUsers)
			f.SetCellValue("Sheet1", fmt.Sprintf("M%d", line), v.IceUseTime)
			f.SetCellValue("Sheet1", fmt.Sprintf("N%d", line), subTime)
			f.SetCellValue("Sheet1", fmt.Sprintf("O%d", line), v.IceUseUsers)
			f.SetCellValue("Sheet1", fmt.Sprintf("P%d", line), "")
		}
	} else {
		// 这里设置表头ÒÒ
		f.SetCellValue("Sheet1", "A1", "序号")
		f.SetCellValue("Sheet1", "B1", "冰排编号")
		f.SetCellValue("Sheet1", "C1", "入库时间")
		f.SetCellValue("Sheet1", "D1", "出库时间")
		f.SetCellValue("Sheet1", "E1", "所在位置")
		f.SetCellValue("Sheet1", "F1", "冷冻要求")
		f.SetCellValue("Sheet1", "G1", "冷冻时间")
		f.SetCellValue("Sheet1", "H1", "使用日期")
		f.SetCellValue("Sheet1", "I1", "使用时间")
		f.SetCellValue("Sheet1", "J1", "使用人员")
		f.SetCellValue("Sheet1", "K1", "备注")
		// 设置列宽
		f.SetColWidth("Sheet1", "A", "A", 6)
		f.SetColWidth("Sheet1", "B", "B", 8)
		f.SetColWidth("Sheet1", "C", "C", 14)
		f.SetColWidth("Sheet1", "D", "D", 15)
		f.SetColWidth("Sheet1", "E", "E", 15)
		f.SetColWidth("Sheet1", "F", "F", 15)
		f.SetColWidth("Sheet1", "G", "G", 15)
		f.SetColWidth("Sheet1", "H", "H", 15)
		f.SetColWidth("Sheet1", "I", "I", 15)
		f.SetColWidth("Sheet1", "J", "J", 15)
		f.SetColWidth("Sheet1", "K", "K", 15)

		line := 1

		// 循环写入数据
		for i, v := range list {
			line++
			Update, _ := time.Parse("2006-01-02 15:04:05", v.ReturnDate.String())
			if v.ReturnDate.String() == "" {
				Update = time.Now()
			}
			UseTime, _ := time.Parse("2006-01-02 15:04:05", v.IceUseTime.String())
			sub := Update.Sub(UseTime)
			subTime := fmt.Sprintf("%vh%vm", math.Floor(sub.Hours()), int(sub.Minutes())%60)
			f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
			f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.IceRafts.Code)
			f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.InStorageTime)
			f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.OutStorageTime)
			f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.CoolerBox.Name)
			f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.FreezeClaim)
			f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.FreezeDuration)
			f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.IceUseTime)
			f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), subTime)
			f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), v.IceUseUsers)
			f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), "")
		}
	}

	timeStr := time.Now().Format("20060102150405")
	filePath := "ofile/" + "冰排使用记录表" + timeStr + ".xlsx"
	// 保存文件
	if err = f.SaveAs(filePath); err != nil {
		logs.Error("文件失败:", err)
	}
	defer func() {
		os.Remove(filePath)
	}()
	c.Header("Content-Type", "application/vnd.ms-excel;charset=utf8")
	// PathEscape 函数对中文做处理
	c.Header("Content-Disposition", "attachment; filename="+url.PathEscape("冰排使用记录表"+timeStr+".xlsx"))
	c.Header("Content-Transfer-Encoding", "binary")
	c.File(filePath)
}
