// 模块控制接口-项目
// 秒寻科技
// zt 2024-02-03

package projecthandler

import (
	"ipsomc/core/dao/mysql/mysqlmodel"
	"ipsomc/core/gin/bind"
	"ipsomc/core/gin/resp"
	"ipsomc/module/project/projectapi"
	"ipsomc/module/project/projectlogic"
	"ipsomc/module/project/projectmodel"

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

type ProjectHan struct {
	bindApi    bind.BindApi
	projectApi projectapi.ProjectApi
	projectLgc projectlogic.ProjectLgc
}

// 增加
func (obj *ProjectHan) AddProjectHan(c *gin.Context) {
	dataModel := new(projectmodel.Project)

	//把前端数据绑定到模型
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据错误")
		return
	}

	//保存数据
	if err := obj.projectLgc.AddProjectLgc(dataModel); err != nil {
		resp.RespFail(c, err.Error())
		return
	}

	//返回数据
	resp.RespData(c, dataModel)
}

// 修改
func (obj *ProjectHan) UpdateProjectHan(c *gin.Context) {
	dataModel := new(projectmodel.Project)
	dataMap := make(map[string]interface{})

	//把前端数据绑定到模型
	if err := obj.bindApi.Bind2Map(c, dataModel, dataMap); err != nil {
		resp.RespFail(c, err.Error())
		return
	}

	//保存数据
	if err := obj.projectApi.UpdateProject(dataModel, dataMap); err != nil {
		resp.RespFail(c, err.Error())
		return
	}

	//返回数据
	resp.RespOk(c)
}

// 删除
func (obj *ProjectHan) DeleteProjectHan(c *gin.Context) {
	dataModel := new(projectmodel.Project)

	//把前端数据绑定到模型
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据解析错误")
		return
	}

	if err := obj.projectApi.DeleteProject(dataModel); err != nil {
		resp.RespFail(c, err.Error())
		return
	}

	//返回数据
	resp.RespOk(c)
}

// 查询单条记录
func (obj *ProjectHan) GetProjectByIdHan(c *gin.Context) {
	dataModel := new(projectmodel.Project)

	//把前端数据绑定到模型
	if err := obj.bindApi.Bind2Model(c, dataModel); err != nil {
		resp.RespFail(c, "数据解析错误")
		return
	}

	if err := obj.projectApi.GetProjectById(dataModel); err != nil {
		resp.RespFail(c, err.Error())
		return
	}

	//返回数据
	resp.RespData(c, dataModel)
}

// 查询
func (obj *ProjectHan) GetProjectListHan(c *gin.Context) {
	dataModel := new(projectmodel.Project)
	listParam := new(mysqlmodel.ListParam)
	dataList := []projectmodel.Project{}

	//把前端数据绑定到模型
	if err := obj.bindApi.Bind2Query(c, listParam); err != nil {
		resp.RespFail(c, "数据解析错误")
		return
	}

	if err := obj.projectApi.GetProjectList(dataModel, listParam, &dataList); err != nil {
		resp.RespFail(c, err.Error())
		return
	}

	resp.RespList(c, dataList)
}

// 分页查询
func (obj *ProjectHan) GetProjectPageListHan(c *gin.Context) {
	dataModel := new(projectmodel.Project)
	pageListParam := new(mysqlmodel.PageListParam)
	dataList := []projectmodel.Project{}

	//把前端数据绑定到模型
	if err := obj.bindApi.Bind2PageQuery(c, pageListParam); err != nil {
		resp.RespFail(c, "数据解析错误")
		return
	}

	pageListData, err := obj.projectApi.GetProjectPageList(dataModel, pageListParam, &dataList)
	if err != nil {
		resp.RespFail(c, err.Error())
		return
	}

	resp.RespList(c, pageListData)
}
