作者 tangxvhui

机会模板增加 自查内容

package models
import (
"time"
"github.com/astaxie/beego/orm"
)
type AuditCheck struct {
Id int64 `orm:"column(id);pk"`
Pid int64 `orm:"column(pid)"`
TemplateId int64 `orm:"column(template_id)" description:"模板id"`
Title string `orm:"column(title);size(100)" description:"标题"`
Items string `orm:"column(items)" description:"选项数据json格式"`
Enable int8 `orm:"column(enable)" description:"是否有效【0:无效】【1:有效】"`
CreateTime time.Time `orm:"column(create_time);type(timestamp)"`
}
func (t *AuditCheck) TableName() string {
return "audit_check"
}
func init() {
orm.RegisterModel(new(AuditCheck))
}
//AuditCheckItems 自查内容选项
// AuditCheck 表items存储的json结构
type AuditCheckItems struct {
Items []AuditCheckItem `json:"items"`
}
type AuditCheckItem struct {
Item string `json:"item"` //选项内容
NeedOther int `json:"needOther"` //是否需填写其他的内容【1:需要】【2:不需要】
}
//AuditCheck 表enable字段含义 是否有效【0:无效】【1:有效】
const (
AUDITCHECK_ENABLE_NO int8 = 0
AUDITCHECK_ENABLE_YES int8 = 1
)
// AddAuditCheck insert a new AuditCheck into database and returns
// last inserted Id on success.
func AddAuditCheck(m *AuditCheck) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetAuditCheckById retrieves AuditCheck by Id. Returns error if
// Id doesn't exist
func GetAuditCheckById(id int64) (v *AuditCheck, err error) {
o := orm.NewOrm()
v = &AuditCheck{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// DeleteAuditCheck deletes AuditCheck by Id and returns error if
// the record to be deleted doesn't exist
// func DeleteAuditCheck(id int64) (err error) {
// o := orm.NewOrm()
// v := AuditCheck{Id: id}
// // ascertain id exists in the database
// if err = o.Read(&v); err == nil {
// var num int64
// if num, err = o.Delete(&AuditCheck{Id: id}); err == nil {
// fmt.Println("Number of records deleted in database:", num)
// }
// }
// return
// }
func GetAuditCheckByTemplate(templateId int64) ([]AuditCheck, error) {
var (
data []AuditCheck
err error
)
o := orm.NewOrm()
_, err = o.QueryTable(&AuditCheck{}).
Filter("template_id", templateId).
Filter("enable", AUDITCHECK_ENABLE_YES).
All(&data)
if err == orm.ErrNoRows {
return data, nil
}
if err != nil {
return nil, err
}
return data, nil
}
func DeleteAuditCheckByTempelate(templateId int64, om orm.Ormer) error {
var (
err error
)
_, err = om.QueryTable(&AuditCheck{}).
Filter("template_id", templateId).
Filter("enable", AUDITCHECK_ENABLE_YES).
Update(orm.Params{
"enable": AUDITCHECK_ENABLE_NO,
})
return err
}
... ...
... ... @@ -79,11 +79,21 @@ func (input ValidateInputRedio) ValidateConfig() error {
/***********审核模板管理**********/
/*TemplateAdd */
//TemplateSelfCheck 机会模板自查内容设定
type TemplateSelfCheck struct {
Id int64 `json:"id"`
Pid int64 `json:"pid"`
Title string `json:"title"` //标题
Child []TemplateSelfCheck `json:"child"` //下级维度
}
type TemplateAddRequest struct {
Template Template `json:"template"`
Example string `json:"example"` //示例
Videos []string `json:"videos"` //视频
AuditFlowConfig AuditFlowConfig `json:"auditFlowConfig"`
Template Template `json:"template"`
Example string `json:"example"` //示例
Videos []string `json:"videos"` //视频
AuditFlowConfig AuditFlowConfig `json:"auditFlowConfig"`
SelfCheck []TemplateSelfCheck `json:"selfCheck"`
}
type TemplateAddResponse struct {
}
... ... @@ -114,10 +124,11 @@ type ProcessConfig struct {
/*TemplateUpdate */
type TemplateUpdateRequest struct {
Template Template `json:"template"`
Example string `json:"example"` //示例`
Videos []string `json:"videos"` //视频
AuditFlowConfig AuditFlowConfig `json:"auditFlowConfig"`
Template Template `json:"template"`
Example string `json:"example"` //示例`
Videos []string `json:"videos"` //视频
AuditFlowConfig AuditFlowConfig `json:"auditFlowConfig"`
SelfCheck []TemplateSelfCheck `json:"selfCheck"` //自查内容
}
type TemplateUpdateResponse struct {
}
... ... @@ -181,10 +192,11 @@ type TemplateGetRequest struct {
Id int `json:"id" valid:"Required"`
}
type TemplateGetResponse struct {
Template Template `json:"template"`
Example string `json:"example"` //示例`
Videos []string `json:"videos"` //视频
AuditFlowConfig AuditFlowConfig `json:"auditFlowConfig"`
Template Template `json:"template"`
Example string `json:"example"` //示例`
Videos []string `json:"videos"` //视频
AuditFlowConfig AuditFlowConfig `json:"auditFlowConfig"`
SelfCheck []TemplateSelfCheck `json:"selfCheck"` //自查内容
}
/*TemplateEditSort */
... ...
... ... @@ -8,6 +8,7 @@ import (
"oppmg/models"
"oppmg/protocol"
"oppmg/utils"
"oppmg/utils/idworker"
"strings"
"time"
... ... @@ -129,11 +130,82 @@ func TemplateAdd(uid, companyId int64, request *protocol.TemplateAddRequest) (rs
}
}
}
//添加自查内容
err = addSelfCheckData(templateId, request.SelfCheck, orm)
if err != nil {
log.Error("模板设置,添加自查内容失败,err:%s", err)
orm.Rollback()
return nil, protocol.NewErrWithMessage("1")
}
orm.Commit()
rsp = &protocol.TemplateAddResponse{}
return
}
//SetSelfCheckData 机会模板设置-自查内容设置
func addSelfCheckData(templateId int64, data []protocol.TemplateSelfCheck, om orm2.Ormer) error {
var (
addSelfCheck []models.AuditCheck
err error
)
defaultItem := models.AuditCheckItems{
Items: []models.AuditCheckItem{
models.AuditCheckItem{Item: "是", NeedOther: 0},
models.AuditCheckItem{Item: "否", NeedOther: 1},
models.AuditCheckItem{Item: "不清楚", NeedOther: 0},
},
}
defaultItemJson, _ := json.Marshal(defaultItem)
nowTime := time.Now()
for _, v := range data {
mId := idworker.NextId()
m := models.AuditCheck{
Id: mId,
Pid: 0,
TemplateId: templateId,
Title: v.Title,
Enable: models.AUDITCHECK_ENABLE_YES,
CreateTime: nowTime,
Items: string(defaultItemJson),
}
addSelfCheck = append(addSelfCheck, m)
if len(v.Child) > 0 {
for _, vv := range v.Child {
childId := idworker.NextId()
childDdata := models.AuditCheck{
Id: childId,
Pid: mId,
TemplateId: templateId,
Title: vv.Title,
Enable: models.AUDITCHECK_ENABLE_YES,
CreateTime: nowTime,
Items: string(defaultItemJson),
}
addSelfCheck = append(addSelfCheck, childDdata)
}
}
}
_, err = om.InsertMulti(10, addSelfCheck)
return err
}
func editSelfCheckData(templateId int64, data []protocol.TemplateSelfCheck, om orm2.Ormer) error {
var (
err error
)
err = models.DeleteAuditCheckByTempelate(templateId, om)
if err != nil {
log.Error("软删除audit_check数据失败,err;%s", err)
return err
}
err = addSelfCheckData(templateId, data, om)
if err != nil {
log.Error("添加audit_check数据失败,err:%s", err)
return err
}
return nil
}
func jsonAssertMarsh(v interface{}) string {
if data, e := json.Marshal(v); e != nil {
log.Error(fmt.Sprintf("%v %v", e.Error(), v))
... ... @@ -255,7 +327,12 @@ func TemplateUpdate(uid, companyId int64, request *protocol.TemplateUpdateReques
}
}
}
err = editSelfCheckData(template.Id, request.SelfCheck, orm)
if err != nil {
log.Error("更新audit_check数据失败:err:%s", err)
orm.Rollback()
return rsp, protocol.NewErrWithMessage("1")
}
orm.Commit()
rsp = &protocol.TemplateUpdateResponse{}
return
... ...