sync_data_controller.go 3.2 KB
package controllers

//用于和企业平台和总后台进行数据通讯
import (
	"encoding/json"
	"errors"

	"github.com/astaxie/beego/logs"
	companyService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/company/service"
	syncCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/unifiedUserCenter/command"
	syncService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/unifiedUserCenter/service"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
)

type SyncDataController struct {
	BaseController
}

////Prepare 重写 BaseController 的Prepare方法
func (c *SyncDataController) Prepare() {
	c.BaseController.Prepare()
}

//SyncData 和企业后台同步数据
func (c *SyncDataController) SyncData() {
	var (
		cmd syncCmd.SyncCallbackCommand
		err error
	)
	if err = c.BindJsonData(&cmd); err != nil {
		logs.Error(err)
		c.ResponseError(errors.New("json数据解析失败"))
		return
	}
	err = syncService.NewSyncAction(cmd)
	if err != nil {
		logs.Error("同步数据异常;%s", err)
	}
	//不论子系统如何处理以及处理结果成功失败 必须返回 0
	c.ResponseData(nil)
	return
}

//CompanyAdminChance 变更公司主管(超级管理)
func (c *SyncDataController) CompanyAdminChance() {

	var (
		param syncCmd.ChanceSuperAdminCommand
		err   error
	)
	if err = c.BindJsonData(&param); err != nil {
		logs.Error(err)
		c.ResponseError(errors.New("json数据解析失败"))
		return
	}
	syncEmployeeSrv := syncService.NewSyncEmployeeService(nil)
	err = syncEmployeeSrv.ChangeSuperAdmin(param)
	if err != nil {
		logs.Error("变更公司的主管失败;%s", err)
	}
	c.ResponseData(nil)
	return
}

//AllowForidCompany 总后台调用
func (c *SyncDataController) AllowForidCompany() {
	type Paremeter struct {
		Companyid int64 `json:"company_id"`
		Status    int   `json:"status"`
	}
	var param Paremeter
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		logs.Error("json 解析失败 err:%s", err)
		c.ResponseError(errors.New("json数据解析失败"))
		return
	}
	var (
		err error
	)
	comanySrv := companyService.NewCompanyService(nil)
	switch param.Status {
	case 1:
		err = comanySrv.AllowCompany(param.Companyid)
	case 2:
		err = comanySrv.ForbidCompany(param.Companyid)
	default:
		c.ResponseError(errors.New("参数错误"))
		return
	}
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseData(nil)
	return
}

//GetCompanyInfo 总后台调用
func (c *SyncDataController) GetCompanyInfo() {
	type Paremeter struct {
		Companyid int64 `json:"company_id"`
	}
	var param Paremeter
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		logs.Error("json 解析失败 err:%s", err)
		c.ResponseError(errors.New("json数据解析失败"))
		return
	}
	var (
		err         error
		companyData *domain.Company
	)
	comanySrv := companyService.NewCompanyService(nil)
	companyData, err = comanySrv.GetCompanyData(param.Companyid)
	rspData := map[string]interface{}{
		"comopany_id": 0,
		"exist":       -1, //公司【1:存在;【-1:不存在】
		"status":      -1, //公司的启用状态【1:启用,-1:禁用】
	}
	if err != nil {
		logs.Error(err)
		c.ResponseData(rspData)
		return
	}
	if companyData.EnableIsOk() {
		rspData["status"] = 1
	}
	rspData["exist"] = 1
	c.ResponseData(rspData)
	return
}