// 模块控制接口-公司
// 秒寻科技
// zt 2024-02-01

package companyhandler

import (
	"ipsomc/core/company/companyapi"
	"ipsomc/core/company/companylogic"
	"ipsomc/core/company/companymodel"
	"ipsomc/core/dao/mysql/mysqlmodel"
	"ipsomc/core/gin/bind"
	"ipsomc/core/gin/resp"

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

type CompanyHand struct {
	companyLgc companylogic.CompanyLgc
	companyApi companyapi.CompanyApi
	bindApi    bind.BindApi
}

// 公司注册，包括注册公司资料、系统管理员和管理员角色
func (obj *CompanyHand) CompanyRegisterHan(c *gin.Context) {
	dataModel := new(companymodel.Company)

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

	//注册
	if err := obj.companyLgc.CompanyRegister(dataModel); err != nil {
		resp.RespFail(c, err.Error())
		return
	}

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

// 新增公司
func (obj *CompanyHand) AddCompanyHan(c *gin.Context) {
	dataModel := new(companymodel.Company)

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

	//新增
	if err := obj.companyApi.AddCompany(dataModel); err != nil {
		resp.RespFail(c, err.Error())
		return
	}

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

// 修改
func (obj *CompanyHand) UpdateCompanyHan(c *gin.Context) {
	dataModel := new(companymodel.Company)
	dataMap := make(map[string]interface{})

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

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

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

// 删除
func (obj *CompanyHand) DeleteCompanyHan(c *gin.Context) {
	dataModel := new(companymodel.Company)

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

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

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

// 查询单条记录
func (obj *CompanyHand) GetCompanyByIdHan(c *gin.Context) {
	dataModel := new(companymodel.Company)

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

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

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

// 分页查询
func (obj *CompanyHand) GetCompanyPageListHan(c *gin.Context) {
	pageListParam := new(mysqlmodel.PageListParam)
	dataModel := new(companymodel.Company)
	dataList := []companymodel.Company{}

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

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

	resp.RespList(c, pageListData)
}
