template.go 10.0 KB
package controllers

import (
	"encoding/json"
	"oppmg/common/log"
	"oppmg/models"
	"oppmg/protocol"
	"oppmg/services/audit"
)

//TemplateController 机会模板
type TemplateController struct {
	BaseController
}

//TemplateAdd 模板新增
//@router /templateAdd [post]
func (this *TemplateController) TemplateAdd() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.ResposeJson(msg)
	}()
	var request *protocol.TemplateAddRequest

	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
		log.Error("json 解析失败", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	uid := this.GetUserId()
	companyId := this.GetCompanyId()
	if companyId <= 0 {
		log.Debug("companyId:%d err", companyId)
		msg = protocol.BadRequestParam("1")
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	if len([]rune(request.Template.Name)) > 30 {
		msg = protocol.BadRequestParam("10070")
		return
	}
	if len([]rune(request.Template.Doc)) > 30 {
		msg = protocol.BadRequestParam("10272")
		return
	}
	{
		//审批人配置
		v := request.AuditFlowConfig.NoApprover
		if !(v == models.NoApproverPass || v == models.NoApproverToAdmin) {
			msg = protocol.BadRequestParam("10068")
			return
		}
		if len(request.AuditFlowConfig.ProcessConfig) == 0 {
			msg = protocol.BadRequestParam("10069")
			return
		}
	}
	{
		if msg = audit.ValidFormList(request.Template.InputList); msg.Errno != 0 {
			log.Error("valid fail ,code:%v msg:%v", msg.OriginErrno, msg.Errmsg)
			return
		}

		if msg = audit.ValidAuditFlowConfig(request.AuditFlowConfig); msg.Errno != 0 {
			log.Error("valid fail ,code:%v msg:%v", msg.OriginErrno, msg.Errmsg)
			return
		}
	}
	if len(request.Videos) == 0 {
		request.Videos = make([]string, 0)
	}
	rsp, err := audit.TemplateAdd(uid, companyId, request)
	msg = protocol.NewReturnResponse(rsp, err)
	return
}

//TemplateUpdate 模板更新
//@router /templateUpdate [post]
func (this *TemplateController) TemplateUpdate() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.ResposeJson(msg)
	}()
	var request *protocol.TemplateUpdateRequest

	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
		log.Error("json 解析失败", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	uid := this.GetUserId()
	companyId := this.GetCompanyId()
	if companyId <= 0 {
		log.Debug("companyId:%d err", companyId)
		msg = protocol.BadRequestParam("1")
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	if len([]rune(request.Template.Name)) > 30 {
		msg = protocol.BadRequestParam("10070")
		return
	}
	if len([]rune(request.Template.Doc)) > 30 {
		msg = protocol.BadRequestParam("10272")
		return
	}
	{
		if msg = audit.ValidFormList(request.Template.InputList); msg.Errno != 0 {
			log.Error("valid fail ,code:%v msg:%v", msg.OriginErrno, msg.Errmsg)
			return
		}

		if msg = audit.ValidAuditFlowConfig(request.AuditFlowConfig); msg.Errno != 0 {
			log.Error("valid fail ,code:%v msg:%v", msg.OriginErrno, msg.Errmsg)
			return
		}
	}
	if len(request.Videos) == 0 {
		request.Videos = make([]string, 0)
	}
	rsp, err := audit.TemplateUpdate(uid, companyId, request)
	msg = protocol.NewReturnResponse(rsp, err)
	return
}

//func checkApproveConfig()(err error){
//
//}

//TemplateList 模板列表
//@router /templateList [post]
func (this *TemplateController) TemplateList() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.ResposeJson(msg)
	}()
	var request *protocol.TemplateListRequest

	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
		log.Error("json 解析失败", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	uid := this.GetUserId()
	companyId := this.GetCompanyId()
	if companyId <= 0 {
		log.Debug("companyId:%d err", companyId)
		msg = protocol.BadRequestParam("1")
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	rsp, err := audit.TemplateList(uid, companyId, request)
	msg = protocol.NewReturnResponse(rsp, err)
	return
}

//TemplateGet 模板获取
//@router /templateGet [post]
func (this *TemplateController) TemplateGet() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.ResposeJson(msg)
	}()
	var request *protocol.TemplateGetRequest

	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
		log.Error("json 解析失败", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	uid := this.GetUserId()
	companyId := this.GetCompanyId()
	if companyId <= 0 {
		log.Debug("companyId:%d err", companyId)
		msg = protocol.BadRequestParam("1")
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	rsp, err := audit.TemplateGet(uid, companyId, request)
	msg = protocol.NewReturnResponse(rsp, err)
}

//TemplateEditVisible 编辑模板可见范围
//@router /templateEditVisible [post]
func (this *TemplateController) TemplateEditVisible() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.ResposeJson(msg)
	}()
	var request *protocol.TemplateEditVisibleRequest

	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
		log.Error("json 解析失败", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	uid := this.GetUserId()
	companyId := this.GetCompanyId()
	if companyId <= 0 {
		log.Debug("companyId:%d err", companyId)
		msg = protocol.BadRequestParam("1")
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	rsp, err := audit.TemplateEditVisible(uid, companyId, request)
	msg = protocol.NewReturnResponse(rsp, err)
}

//TemplateEditSort 编辑模板排序
//@router /templateEditSort [post]
func (this *TemplateController) TemplateEditSort() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.ResposeJson(msg)
	}()
	var request *protocol.TemplateEditSortRequest

	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
		log.Error("json 解析失败", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	uid := this.GetUserId()
	companyId := this.GetCompanyId()
	if companyId <= 0 {
		log.Debug("companyId:%d err", companyId)
		msg = protocol.BadRequestParam("1")
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	rsp, err := audit.TemplateEditSort(uid, companyId, request)
	msg = protocol.NewReturnResponse(rsp, err)
}

//TemplateOperateCategory 编辑一级分类
//@router /templateOperateCategory [post]
func (this *TemplateController) TemplateOperateCategory() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.ResposeJson(msg)
	}()
	var request *protocol.TemplateOperateCategoryRequest

	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
		log.Error("json 解析失败", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	uid := this.GetUserId()
	companyId := this.GetCompanyId()
	if companyId <= 0 {
		log.Debug("companyId:%d err", companyId)
		msg = protocol.BadRequestParam("1")
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	if len([]rune(request.Name)) > 10 {
		msg = protocol.BadRequestParam("10070")
		return
	}
	rsp, err := audit.TemplateOperateCategory(uid, companyId, request)
	msg = protocol.NewReturnResponse(rsp, err)
}

//TemplateDeleteCategory 删除一级分类
//@router /templateDeleteCategory [post]
func (this *TemplateController) TemplateDeleteCategory() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.ResposeJson(msg)
	}()
	var request *protocol.TemplateDeleteCategoryRequest

	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
		log.Error("json 解析失败", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	uid := this.GetUserId()
	companyId := this.GetCompanyId()
	if companyId <= 0 {
		log.Debug("companyId:%d err", companyId)
		msg = protocol.BadRequestParam("1")
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	rsp, err := audit.TemplateDeleteCategory(uid, companyId, request)
	msg = protocol.NewReturnResponse(rsp, err)
}

//TemplateDelete  模板删除
//@router /templateDelete [post]
func (this *TemplateController) TemplateDelete() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.ResposeJson(msg)
	}()
	var request *protocol.TemplateDeleteRequest

	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
		log.Error("json 解析失败", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	uid := this.GetUserId()
	companyId := this.GetCompanyId()
	if companyId <= 0 {
		log.Debug("companyId:%d err", companyId)
		msg = protocol.BadRequestParam("1")
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	rsp, err := audit.TemplateDelete(uid, companyId, request)
	msg = protocol.NewReturnResponse(rsp, err)
}

//TemplateEditEnable  模板启用
//@router /templateEditEnable [post]
func (this *TemplateController) TemplateEditEnable() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.ResposeJson(msg)
	}()
	var request *protocol.TemplateEditEnableRequest

	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
		log.Error("json 解析失败", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	uid := this.GetUserId()
	companyId := this.GetCompanyId()
	if companyId <= 0 {
		log.Debug("companyId:%d err", companyId)
		msg = protocol.BadRequestParam("1")
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	rsp, err := audit.TemplateEditEnable(uid, companyId, request)
	msg = protocol.NewReturnResponse(rsp, err)
}

//CategoryEditSort 一级分类排序
//@router /categoryEditSort [post]
func (this *TemplateController) CategoryEditSort() {
	var msg *protocol.ResponseMessage
	defer func() {
		this.ResposeJson(msg)
	}()
	var request *protocol.CategoryEditSortRequest

	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
		log.Error("json 解析失败", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	uid := this.GetUserId()
	companyId := this.GetCompanyId()
	if companyId <= 0 {
		log.Debug("companyId:%d err", companyId)
		msg = protocol.BadRequestParam("1")
		return
	}
	if b, m := this.Valid(request); !b {
		msg = m
		return
	}
	rsp, err := audit.CategoryEditSort(uid, companyId, request)
	msg = protocol.NewReturnResponse(rsp, err)
}