package controllers

import (
	"encoding/json"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/system"
	"hongze/hz_crm_api/services/flow"
)

// 审批配置
type SysFlowController struct {
	BaseAuthController
}

// @Title 获取审批配置详情
// @Description 获取审批配置详情接口
// @Param   FlowId   query   int  true       "节点id"
// @Success 200 {object} system.ApprovalFlowItem
// @router /flow/detail [get]
func (this *SysFlowController) Detail() {
	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
	}
	flowId, _ := this.GetInt("FlowId")
	if flowId <= 0 {
		br.Msg = "参数错误"
		br.ErrMsg = "请传入审批节点"
		return
	}
	item, err := flow.GetApprovalFlowDetail(flowId)
	if err != nil {
		br.Msg = "获取信息失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = item
}

// @Title 编辑审批配置
// @Description 编辑审批配置接口
// @Param	request	body system.SysRoleAddReq true "type json string"
// @Success 200 流程配置成功
// @router /flow/edit [post]
func (this *SysFlowController) Edit() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req system.EditFlowReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.FlowId <= 0 {
		br.Msg = "审批配置不能为空"
		return
	}
	if len(req.NodeList) <= 0 {
		br.Msg = "节点配置不能为空"
		return
	}
	err = flow.EditFlow(req)
	if err != nil {
		br.Msg = "流程配置失败"
		br.ErrMsg = "流程配置失败,Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "流程配置成功"
}

// @Title 获取用户列表
// @Description 获取审批配置详情接口
// @Param   Keyword   query   string  true       "关键字"
// @Success 200 {object} system.FlowUserResp
// @router /flow/user [get]
func (this *SysFlowController) 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
	}
	keyword := this.GetString("Keyword")
	if keyword == "" {
		br.Msg = "参数错误"
		br.ErrMsg = "请传入搜索关键字"
		return
	}

	//获取用户列表
	var condition string
	var pars []interface{}
	condition += ` AND (real_name LIKE '%` + keyword + `%' OR admin_name LIKE '%` + keyword + `%' OR mobile LIKE '%` + keyword + `%' )  `
	adminList, err := system.GetSysuserList(condition, pars, 0, 100)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取用户列表失败，Err:" + err.Error()
		return
	}

	//获取角色列表
	var roleCondition string
	var rolePars []interface{}
	roleCondition += ` AND role_name LIKE '%` + keyword + `%'  `

	roleList, err := system.GetSysRoleList(roleCondition, rolePars, 0, 100)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取角色列表失败，Err:" + err.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = system.FlowUserResp{
		AdminList: adminList,
		RoleList:  roleList,
	}
}
