作者 yangfu

审核模板启用 删除

... ... @@ -20,7 +20,8 @@ type BaseController struct {
func (this *BaseController) Options() {
this.AllowCross() //允许跨域
this.Data["json"] = map[string]interface{}{"status": 204, "message": "ok", "moreinfo": ""}
this.Ctx.ResponseWriter.WriteHeader(204)
this.Data["json"] = map[string]interface{}{}
this.ServeJSON()
}
... ... @@ -36,6 +37,7 @@ func (this *BaseController) Prepare() {
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "*")
if this.Ctx.Input.Method() == "OPTIONS" {
this.Ctx.ResponseWriter.WriteHeader(204)
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
//this.Ctx.WriteString("")
return
... ...
... ... @@ -11,7 +11,7 @@ type TemplateController struct {
BaseController
}
//TemplateAdd
//TemplateAdd 模板新增
//@router /templateAdd [post]
func (this *TemplateController) TemplateAdd() {
var msg *protocol.ResponseMessage
... ... @@ -46,7 +46,7 @@ func (this *TemplateController) TemplateAdd() {
return
}
//TemplateUpdate
//TemplateUpdate 模板更新
//@router /templateUpdate [post]
func (this *TemplateController) TemplateUpdate() {
var msg *protocol.ResponseMessage
... ... @@ -76,7 +76,7 @@ func (this *TemplateController) TemplateUpdate() {
return
}
//TemplateList
//TemplateList 模板列表
//@router /templateList [post]
func (this *TemplateController) TemplateList() {
var msg *protocol.ResponseMessage
... ... @@ -106,7 +106,7 @@ func (this *TemplateController) TemplateList() {
return
}
//TemplateGet
//TemplateGet 模板获取
//@router /templateGet [post]
func (this *TemplateController) TemplateGet() {
var msg *protocol.ResponseMessage
... ... @@ -135,7 +135,7 @@ func (this *TemplateController) TemplateGet() {
msg = protocol.NewReturnResponse(rsp, err)
}
//TemplateEditVisible
//TemplateEditVisible 编辑模板可见范围
//@router /templateEditVisible [post]
func (this *TemplateController) TemplateEditVisible() {
var msg *protocol.ResponseMessage
... ... @@ -193,7 +193,7 @@ func (this *TemplateController) TemplateEditSort() {
msg = protocol.NewReturnResponse(rsp, err)
}
//TemplateOperateCategory
//TemplateOperateCategory 编辑一级分类
//@router /templateOperateCategory [post]
func (this *TemplateController) TemplateOperateCategory() {
var msg *protocol.ResponseMessage
... ... @@ -221,3 +221,119 @@ func (this *TemplateController) TemplateOperateCategory() {
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)
}
... ...
... ... @@ -175,3 +175,32 @@ type SortItem struct {
Id int `json:"id"`
SortNum int `json:"sort_num"`
}
/*TemplateEditEnable 模板启用*/
type TemplateEditEnableRequest struct {
TemplateId int `json:"id" valid:"Required"`
Enabled int8 `json:"enabled"` //启用状态 1 启用 0 禁用
}
type TemplateEditEnableResponse struct {
}
/*TemplateDelete 删除模板*/
type TemplateDeleteRequest struct {
TemplateId int `json:"id" valid:"Required"`
}
type TemplateDeleteResponse struct {
}
/*TemplateDeleteCategory 删除一级分类*/
type TemplateDeleteCategoryRequest struct {
ChanceTypeId int `json:"id" valid:"Required"`
}
type TemplateDeleteCategoryResponse struct {
}
/*CategoryEditSort 一级分类排序*/
type CategoryEditSortRequest struct {
List []SortItem `json:"list"` //需要排序的列表
}
type CategoryEditSortResponse struct {
}
... ...
... ... @@ -37,6 +37,10 @@ var errmessge ErrorMap = map[string]string{
"10047": "只能删除没有成员的部门,需要先删除部门下的员工,再删除该部门",
//用户中心相关
"10051": "无法从远端接口获取公司数据",
//模板相关
"10061": "请先删除该分类下的二级分类",
"10062": "该分类已被使用无法删除",
"10063": "该分类已被使用无法禁用",
}
//错误码转换 ,兼容需要
... ...
... ... @@ -68,9 +68,13 @@ func init() {
beego.NSRouter("/list", &controllers.TemplateController{}, "post:TemplateList"),
beego.NSRouter("/get", &controllers.TemplateController{}, "post:TemplateGet"),
beego.NSRouter("/update", &controllers.TemplateController{}, "post:TemplateUpdate"),
beego.NSRouter("/delete", &controllers.TemplateController{}, "post:TemplateDelete"),
beego.NSRouter("/editVisible", &controllers.TemplateController{}, "post:TemplateEditVisible"),
beego.NSRouter("/editEnable", &controllers.TemplateController{}, "post:TemplateEditEnable"),
beego.NSRouter("/editSort", &controllers.TemplateController{}, "post:TemplateEditSort"),
beego.NSRouter("/operateCategory", &controllers.TemplateController{}, "post:TemplateOperateCategory"),
beego.NSRouter("/deleteCategory", &controllers.TemplateController{}, "post:TemplateDeleteCategory"),
beego.NSRouter("/sortCategory", &controllers.TemplateController{}, "post:CategoryEditSort"),
),
)
... ...
... ... @@ -297,6 +297,111 @@ func TemplateEditSort(uid, companyId int64, request *protocol.TemplateEditSortRe
return
}
//获取模板详情
func TemplateGet(uid, companyId int64, request *protocol.TemplateGetRequest) (rsp *protocol.TemplateGetResponse, err error) {
var (
template *models.AuditTemplate
auditForm []*models.AuditForm
)
rsp = &protocol.TemplateGetResponse{}
if template, err = models.GetAuditTemplateById(int64(request.Id)); err != nil {
log.Error("template_id:%v 不存在 ,err:%v", request.Id, err.Error())
return
}
{
rsp.Template = protocol.Template{
Id: template.Id,
ChanceTypeId: template.ChanceTypeId,
Name: template.Name,
Doc: template.Doc,
Icon: template.Icon,
}
rsp.Example = template.Example
if auditForm, err = models.GetAuditFormByTemplateId(int(template.Id)); err != nil {
if err == sql.ErrNoRows {
err = nil
}
return
}
for i := range auditForm {
input := auditForm[i]
rsp.Template.InputList = append(rsp.Template.InputList, &protocol.InputElement{
Id: input.Id,
Sort: input.SortNum,
Lable: input.InputType,
Required: int(input.Required),
SectionType: input.Section,
})
}
}
return
}
//模板删除
func TemplateDelete(uid, companyId int64, request *protocol.TemplateDeleteRequest) (rsp *protocol.TemplateDeleteResponse, err error) {
var (
template *models.AuditTemplate
num int
sql1 string = `select count(0) from chance where chance_type_id = ? limit 1`
)
if template, err = models.GetAuditTemplateById(int64(request.TemplateId)); err != nil {
log.Error(err.Error())
return
}
if template.CompanyId != int(companyId) {
err = protocol.NewErrWithMessage("10027")
return
}
//2.判断是否有对应已经发布的机会
if err = utils.ExecuteQueryOne(&num, sql1, request.TemplateId); err == nil && num > 0 {
err = protocol.NewErrWithMessage("10062")
return
}
//删除数据
if err = models.DeleteAuditTemplate(int64(request.TemplateId)); err != nil {
log.Error(err.Error())
return
}
rsp = &protocol.TemplateDeleteResponse{}
return
}
//模板启用
func TemplateEditEnable(uid, companyId int64, request *protocol.TemplateEditEnableRequest) (rsp *protocol.TemplateEditEnableResponse, err error) {
var (
template *models.AuditTemplate
num int
sql1 string = `select count(0) from chance where chance_type_id = ? limit 1`
)
if template, err = models.GetAuditTemplateById(int64(request.TemplateId)); err != nil {
log.Error(err.Error())
return
}
if template.EnableStatus == request.Enabled {
return
}
if template.CompanyId != int(companyId) {
err = protocol.NewErrWithMessage("10027")
return
}
//2.判断是否有对应已经发布的机会
if request.Enabled == 0 {
if err = utils.ExecuteQueryOne(&num, sql1, request.TemplateId); err == nil && num > 0 {
err = protocol.NewErrWithMessage("10063")
return
}
}
rsp = &protocol.TemplateEditEnableResponse{}
if err = utils.UpdateTableByMap(template, map[string]interface{}{"EnableStatus": request.Enabled}); err != nil {
log.Error(err.Error())
return
}
return
}
//操作一级分类
func TemplateOperateCategory(uid, companyId int64, request *protocol.TemplateOperateCategoryRequest) (rsp *protocol.TemplateOperateCategoryResponse, err error) {
var (
... ... @@ -312,7 +417,7 @@ func TemplateOperateCategory(uid, companyId int64, request *protocol.TemplateOpe
err = protocol.NewErrWithMessage("10027")
log.Error("template_id:%v companyId:%v want:%v not equal.", request.Id, companyId, chanceType.CompanyId)
}
if err = utils.UpdateTableByMap(chanceType, map[string]interface{}{"Name": request.Name, "Icon": request.Icon, "UpdateAt": time.Now()}); err != nil {
if err = utils.UpdateTableByMap(chanceType, map[string]interface{}{"Name": request.Name, "Icon": request.Icon, "SortNum": chanceType.SortNum, "UpdateAt": time.Now()}); err != nil {
log.Error(err.Error())
return
}
... ... @@ -332,44 +437,70 @@ func TemplateOperateCategory(uid, companyId int64, request *protocol.TemplateOpe
return
}
//获取模板详情
func TemplateGet(uid, companyId int64, request *protocol.TemplateGetRequest) (rsp *protocol.TemplateGetResponse, err error) {
//删除一级分类
func TemplateDeleteCategory(uid, companyId int64, request *protocol.TemplateDeleteCategoryRequest) (rsp *protocol.TemplateDeleteCategoryResponse, err error) {
var (
template *models.AuditTemplate
auditForm []*models.AuditForm
chanceType *models.ChanceType
num int
sql1 string = `select count(0) from from audit_template where chance_type_id =? limit 1`
sql2 string = `select count(0) from chance where chance_type_id = ? limit 1`
)
rsp = &protocol.TemplateGetResponse{}
if template, err = models.GetAuditTemplateById(int64(request.Id)); err != nil {
log.Error("template_id:%v 不存在 ,err:%v", request.Id, err.Error())
if chanceType, err = models.GetChanceTypeById(request.ChanceTypeId); err != nil {
log.Error(err.Error())
return
}
if chanceType.CompanyId != int(companyId) {
err = protocol.NewErrWithMessage("10027")
return
}
{
rsp.Template = protocol.Template{
Id: template.Id,
ChanceTypeId: template.ChanceTypeId,
Name: template.Name,
Doc: template.Doc,
Icon: template.Icon,
}
rsp.Example = template.Example
//1.判断分类下面是否有模板
if err = utils.ExecuteQueryOne(&num, sql1, request.ChanceTypeId); err == nil && num > 0 {
err = protocol.NewErrWithMessage("10061")
return
}
//2.判断是否有对应已经发布的机会
if err = utils.ExecuteQueryOne(&num, sql2, request.ChanceTypeId); err == nil && num > 0 {
err = protocol.NewErrWithMessage("10062")
return
}
if auditForm, err = models.GetAuditFormByTemplateId(int(template.Id)); err != nil {
if err == sql.ErrNoRows {
err = nil
}
//删除数据
if err = models.DeleteChanceType(request.ChanceTypeId); err != nil {
log.Error(err.Error())
return
}
rsp = &protocol.TemplateDeleteCategoryResponse{}
return
}
//一级分类排序
func CategoryEditSort(uid, companyId int64, request *protocol.CategoryEditSortRequest) (rsp *protocol.CategoryEditSortResponse, err error) {
var (
template *models.ChanceType
)
orm := orm2.NewOrm()
for i := range request.List {
item := request.List[i]
if template, err = models.GetChanceTypeById(item.Id); err != nil {
log.Error(err.Error())
continue
}
if template.CompanyId != int(companyId) {
log.Debug(fmt.Sprintf("template.companyId input:%v want:%v not equal.", companyId, template.CompanyId))
err = protocol.NewErrWithMessage("10027")
return
}
for i := range auditForm {
input := auditForm[i]
rsp.Template.InputList = append(rsp.Template.InputList, &protocol.InputElement{
Id: input.Id,
Sort: input.SortNum,
Lable: input.InputType,
Required: int(input.Required),
SectionType: input.Section,
})
if item.SortNum == template.SortNum {
continue
}
if err = utils.UpdateTableByMapWithOrmer(orm, template, map[string]interface{}{"SortNum": item.SortNum}); err != nil {
log.Error(err.Error())
orm.Rollback()
return
}
}
orm.Commit()
rsp = &protocol.CategoryEditSortResponse{}
return
}
... ...