package admin

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"server/dao"
	"server/global"
	"server/model/common/request"
	"server/model/common/response"
	"strconv"
)

type DepartmentApi struct {
}

func (da *DepartmentApi) QueryAllDep(c *gin.Context) {
	deps, err := departmentService.QueryAllDep()
	if err != nil {
		response.FailWithMessage("查询失败", c)
		global.GVA_LOG.Error("QueryAllDep ==" + err.Error())
		return
	}
	response.OkWithData(deps, c)
}

func (da *DepartmentApi) QueryDepByStart(c *gin.Context) {
	deps, err := departmentService.QueryDepByStart()
	if err != nil {
		response.FailWithMessage("查询失败", c)
		global.GVA_LOG.Error("QueryDepByStart ==" + err.Error())
		return
	}
	response.OkWithData(deps, c)
}

func (da *DepartmentApi) CreateDep(c *gin.Context) {
	department := dao.Department{}
	err := c.ShouldBindJSON(&department)
	if err != nil {
		global.GVA_LOG.Error("CreateDep ==" + err.Error())
		response.FailWithMessage("错误", c)
		return
	}
	err = departmentService.CreateDep(department)
	if err != nil {
		global.GVA_LOG.Error("CreateDep ==" + err.Error())
		response.FailWithMessage("错误", c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

func (da *DepartmentApi) UpdateDep(c *gin.Context) {
	department := dao.Department{}
	err := c.ShouldBindJSON(&department)
	if err != nil {
		global.GVA_LOG.Error("UpdateDep ==" + err.Error())
		response.FailWithMessage("错误", c)
		return
	}
	err = departmentService.UpdateDep(department)
	if err != nil {
		global.GVA_LOG.Error("UpdateDep ==" + err.Error())
		response.FailWithMessage("错误", c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

func (da *DepartmentApi) UpdateDepStatus(c *gin.Context) {
	department := dao.Department{}
	err := c.ShouldBindJSON(&department)
	if err != nil {
		global.GVA_LOG.Error("UpdateDepStatus ==" + err.Error())
		response.FailWithMessage("错误", c)
		return
	}
	err = departmentService.UpdateDepStatus(department.Id, department.IsStart)
	if err != nil {
		global.GVA_LOG.Error("UpdateDepStatus ==" + err.Error())
		response.FailWithMessage("错误", c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

func (da *DepartmentApi) UpdateUserDep(c *gin.Context) {
	ids := request.Ids{}
	err := c.ShouldBindJSON(&ids)
	if err != nil {
		global.GVA_LOG.Error("UpdateUserDep ==" + err.Error())
		response.FailWithMessage("错误", c)
		return
	}
	err = departmentService.UpdateUserDep(ids.Ids, ids.Id)
	if err != nil {
		global.GVA_LOG.Error("UpdateUserDep ==" + err.Error())
		response.FailWithMessage("错误", c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

func (da *DepartmentApi) DeleteDep(c *gin.Context) {
	id := c.Param("depId")
	depId, _ := strconv.Atoi(id)
	err := departmentService.DeleteDep(depId)
	if err != nil {
		global.GVA_LOG.Error("DeleteDep ==" + err.Error())
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// FilesUpload 文件上传
func (da *DepartmentApi) FilesUpload(c *gin.Context) {
	form, err := c.MultipartForm()
	files := form.File["file"]
	fmt.Println(files)
	if err != nil {
		response.FailWithMessage("失败", c)
		//ignore
		return
	}
	cs := c.PostForm("cs")

	fmt.Println(cs)
	for _, file := range files {
		dst := file.Filename
		// gin 简单做了封装,拷贝了文件流
		if err := c.SaveUploadedFile(file, "./uploads/file/"+dst); err != nil {
			// ignore
		}
	}
	response.Ok(c)
}

func (da *DepartmentApi) XiaZai(c *gin.Context) {
	c.Header("Content-Type", "application/octet-stream")              // 表示是文件流，唤起浏览器下载，一般设置了这个，就要设置文件名
	c.Header("Content-Disposition", "attachment; filename="+"牛逼.png") // 用来指定下载下来的文件名
	c.Header("Content-Transfer-Encoding", "binary")                   // 表示传输过程中的编码形式，乱码问题可能就是因为它
	c.File("uploads/file/04-原理篇.zip")
	response.Ok(c)
}
