|
|
package audit
|
|
|
|
|
|
import (
|
|
|
"database/sql"
|
|
|
"fmt"
|
|
|
orm2 "github.com/astaxie/beego/orm"
|
|
|
"oppmg/common/log"
|
|
|
"oppmg/models"
|
|
|
"oppmg/protocol"
|
|
|
"oppmg/utils"
|
|
|
"strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
func TemplateAdd(uid, companyId int64, request *protocol.TemplateAddRequest) (rsp *protocol.TemplateAddResponse, err error) {
|
|
|
var (
|
|
|
auditForm *models.AuditForm
|
|
|
template *models.AuditTemplate
|
|
|
templateId int64
|
|
|
)
|
|
|
//判断机会类型是否存在
|
|
|
if _, err = models.GetChanceTypeById(request.Template.ChanceTypeId); err != nil {
|
|
|
log.Error("chance_type_id:%v 不存在 ,err:%v", request.Template.ChanceTypeId, err.Error())
|
|
|
return
|
|
|
}
|
|
|
orm := orm2.NewOrm()
|
|
|
//模板
|
|
|
{
|
|
|
template = &models.AuditTemplate{
|
|
|
CompanyId: int(companyId),
|
|
|
Name: request.Template.Name,
|
|
|
Doc: request.Template.Doc,
|
|
|
Icon: request.Template.Icon,
|
|
|
NoApprover: 1, //TODO:配置
|
|
|
SortNum: 0,
|
|
|
VisibleType: int8(0),
|
|
|
EnableStatus: 1,
|
|
|
Example: request.Example,
|
|
|
CreateAt: time.Now(),
|
|
|
UpdateAt: time.Now(),
|
|
|
}
|
|
|
if templateId, err = orm.Insert(template); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
orm.Rollback()
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
//表单
|
|
|
{
|
|
|
for i := range request.Template.InputList {
|
|
|
input := request.Template.InputList[i]
|
|
|
auditForm = &models.AuditForm{
|
|
|
CompanyId: int(companyId),
|
|
|
AuditTemplateId: int(templateId),
|
|
|
Section: input.SectionType,
|
|
|
SortNum: input.Sort,
|
|
|
Label: input.Lable,
|
|
|
InputType: input.InputType,
|
|
|
Required: int8(input.Required),
|
|
|
CreateAt: time.Now(),
|
|
|
EnableStatus: 1,
|
|
|
}
|
|
|
if _, err = orm.Insert(auditForm); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
orm.Rollback()
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//审核配置
|
|
|
|
|
|
orm.Commit()
|
|
|
rsp = &protocol.TemplateAddResponse{}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
func TemplateUpdate(uid, companyId int64, request *protocol.TemplateUpdateRequest) (rsp *protocol.TemplateUpdateResponse, err error) {
|
|
|
var (
|
|
|
template *models.AuditTemplate
|
|
|
|
|
|
sql1 = `update audit_form set enable_status=0,delete_at=now() where audit_template_id=? and id not in(%v)`
|
|
|
ids []string
|
|
|
)
|
|
|
//TODO:判断是否有在进行的机会
|
|
|
//判断机会类型是否存在
|
|
|
if _, err = models.GetChanceTypeById(request.Template.ChanceTypeId); err != nil {
|
|
|
log.Error("chance_type_id:%v 不存在 ,err:%v", request.Template.ChanceTypeId, err.Error())
|
|
|
return
|
|
|
}
|
|
|
if template, err = models.GetAuditTemplateById(request.Template.Id); err != nil {
|
|
|
log.Error("template_id:%v 不存在 ,err:%v", request.Template.Id, err.Error())
|
|
|
return
|
|
|
}
|
|
|
orm := orm2.NewOrm()
|
|
|
//模板
|
|
|
{
|
|
|
template.Name = request.Template.Name
|
|
|
template.Doc = request.Template.Doc
|
|
|
template.Icon = request.Template.Icon
|
|
|
template.NoApprover = 1
|
|
|
template.Example = request.Example
|
|
|
template.UpdateAt = time.Now()
|
|
|
if err = models.UpdateAuditTemplateById(template); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
orm.Rollback()
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
//表单
|
|
|
{
|
|
|
for i := range request.Template.InputList {
|
|
|
input := request.Template.InputList[i]
|
|
|
if input.Id == 0 {
|
|
|
continue
|
|
|
}
|
|
|
ids = append(ids, fmt.Sprintf("%v", input.Id))
|
|
|
}
|
|
|
if err = utils.ExecuteSQLWithOrmer(orm, fmt.Sprintf(sql1, strings.Join(ids, ",")), template.Id); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
orm.Rollback()
|
|
|
return
|
|
|
}
|
|
|
for i := range request.Template.InputList {
|
|
|
input := request.Template.InputList[i]
|
|
|
if err = insertOrUpdateInput(orm, companyId, int(template.Id), input); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
orm.Rollback()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//审核配置
|
|
|
|
|
|
orm.Commit()
|
|
|
rsp = &protocol.TemplateUpdateResponse{}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//更新 或 插入表单
|
|
|
func insertOrUpdateInput(orm orm2.Ormer, companyId int64, templateId int, input *protocol.InputElement) (err error) {
|
|
|
var (
|
|
|
auditForm *models.AuditForm
|
|
|
)
|
|
|
if input.Id == 0 {
|
|
|
auditForm = &models.AuditForm{
|
|
|
CompanyId: int(companyId),
|
|
|
AuditTemplateId: int(templateId),
|
|
|
Section: input.SectionType,
|
|
|
SortNum: input.Sort,
|
|
|
Label: input.Lable,
|
|
|
InputType: input.InputType,
|
|
|
Required: int8(input.Required),
|
|
|
CreateAt: time.Now(),
|
|
|
EnableStatus: 1,
|
|
|
}
|
|
|
if _, err = orm.Insert(auditForm); err != nil {
|
|
|
log.Error(err.Error())
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
if auditForm, err = models.GetAuditFormById(input.Id); err != nil {
|
|
|
if err == sql.ErrNoRows {
|
|
|
err = nil
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
{
|
|
|
updateMap := map[string]interface{}{
|
|
|
"Label": input.Lable,
|
|
|
"InputType": input.InputType,
|
|
|
"Required": int8(input.Required),
|
|
|
}
|
|
|
if err = utils.UpdateTableByMapWithOrmer(orm, auditForm, updateMap); err != nil {
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
func TemplateList(uid, companyId int64, request *protocol.TemplateListRequest) (rsp *protocol.TemplateListResponse, err error) {
|
|
|
var ()
|
|
|
rsp = &protocol.TemplateListResponse{}
|
|
|
return
|
|
|
} |
...
|
...
|
|