package cygx

import (
	"encoding/json"
	"hongze/hz_crm_api/controllers"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/cygx"
	"hongze/hz_crm_api/services"
	"time"
)

// 权益小程序
type IndustryMapController struct {
	controllers.BaseAuthController
}

// @Title 行业图谱
// @Description 行业图谱接口
// @Success 200 {object} cygx.CygxIndustryMapResp
// @router /industry/map/list [get]
func (this *IndustryMapController) List() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	list, err := cygx.GetCygxIndustryMapByParentId(0)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	rootNode := list[0]
	allNodes, err := cygx.GetCygxIndustryMapAll()
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	services.MakeTree(allNodes, rootNode)
	resp := new(cygx.CygxIndustryMapResp)
	resp.Node = rootNode
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 新增图谱
// @Description 新增图谱接口
// @Param	request	body cygx.IndustryMapAddReq true "type json string"
// @Success Ret=200 新增图谱成功
// @router /industry/map/add [post]
func (this *IndustryMapController) IndustryMapAdd() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req cygx.IndustryMapAddReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	item := new(cygx.CygxIndustryMap)
	item.IndustryMapName = req.IndustryMapName
	item.ParentId = req.IndustryMapId
	item.CreateTime = time.Now()
	item.ModifyTime = time.Now()
	item.Level = req.Level + 1
	err = cygx.AddCygxIndustryMap(item)
	if err != nil {
		br.Msg = "新增失败"
		br.ErrMsg = "新增失败 Err:" + req.IndustryMapName
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "新增成功"
	br.IsAddLog = true
}

// @Title 编辑图谱
// @Description 编辑图谱接口
// @Param	request	body cygx.IndustryMapEditReq true "type json string"
// @Success Ret=200 新增图谱成功
// @router /industry/map/edit [post]
func (this *IndustryMapController) IndustryMapEdit() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req cygx.IndustryMapEditReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	err = cygx.EditCygxIndustryMap(&req)
	if err != nil {
		br.Msg = "修改失败"
		br.ErrMsg = "修改失败 Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "修改成功"
	br.IsAddLog = true
}

// @Title 删除图谱
// @Description 删除图谱接口
// @Param	request	body cygx.IndustryMapDeleteReq true "type json string"
// @Success Ret=200 删除成功
// @router /industry/map/delete [post]
func (this *IndustryMapController) IndustryMapDelete() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req cygx.IndustryMapDeleteReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	err = cygx.DeleteCygxIndustryMap(req.IndustryMapId)
	if err != nil {
		br.Msg = "删除失败"
		br.ErrMsg = "删除失败 Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "删除成功"
	br.IsAddLog = true
}
