作者 yangfu

模板管理修改

@@ -105,3 +105,61 @@ func (this *TemplateController) TemplateList() { @@ -105,3 +105,61 @@ func (this *TemplateController) TemplateList() {
105 msg = protocol.NewReturnResponse(rsp, err) 105 msg = protocol.NewReturnResponse(rsp, err)
106 return 106 return
107 } 107 }
  108 +
  109 +//TemplateEditVisible
  110 +//@router /templateEditVisible [post]
  111 +func (this *TemplateController) TemplateEditVisible() {
  112 + var msg *protocol.ResponseMessage
  113 + defer func() {
  114 + this.ResposeJson(msg)
  115 + }()
  116 + var request *protocol.TemplateEditVisibleRequest
  117 +
  118 + if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
  119 + log.Error("json 解析失败", err)
  120 + msg = protocol.BadRequestParam("1")
  121 + return
  122 + }
  123 + uid := this.GetUserId()
  124 + companyId := this.GetCompanyId()
  125 + if companyId <= 0 {
  126 + log.Debug("companyId:%d err", companyId)
  127 + msg = protocol.BadRequestParam("1")
  128 + return
  129 + }
  130 + if b, m := this.Valid(request); !b {
  131 + msg = m
  132 + return
  133 + }
  134 + rsp, err := audit.TemplateEditVisible(uid, companyId, request)
  135 + msg = protocol.NewReturnResponse(rsp, err)
  136 +}
  137 +
  138 +//TemplateOperateCategory
  139 +//@router /templateOperateCategory [post]
  140 +func (this *TemplateController) TemplateOperateCategory() {
  141 + var msg *protocol.ResponseMessage
  142 + defer func() {
  143 + this.ResposeJson(msg)
  144 + }()
  145 + var request *protocol.TemplateOperateCategoryRequest
  146 +
  147 + if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
  148 + log.Error("json 解析失败", err)
  149 + msg = protocol.BadRequestParam("1")
  150 + return
  151 + }
  152 + uid := this.GetUserId()
  153 + companyId := this.GetCompanyId()
  154 + if companyId <= 0 {
  155 + log.Debug("companyId:%d err", companyId)
  156 + msg = protocol.BadRequestParam("1")
  157 + return
  158 + }
  159 + if b, m := this.Valid(request); !b {
  160 + msg = m
  161 + return
  162 + }
  163 + rsp, err := audit.TemplateOperateCategory(uid, companyId, request)
  164 + msg = protocol.NewReturnResponse(rsp, err)
  165 +}
@@ -82,3 +82,14 @@ func DeleteAuditForm(id int) (err error) { @@ -82,3 +82,14 @@ func DeleteAuditForm(id int) (err error) {
82 } 82 }
83 return 83 return
84 } 84 }
  85 +
  86 +// GetAuditFormById retrieves AuditForm by Id. Returns error if
  87 +// Id doesn't exist
  88 +func GetAuditFormByTemplateId(id int) (v []*AuditForm, err error) {
  89 + o := orm.NewOrm()
  90 + sql := `select * from audit_form where audit_template_id =? and enable_status=1 order by sort_num`
  91 + if _, err = o.Raw(sql, id).QueryRows(&v); err == nil {
  92 + return v, nil
  93 + }
  94 + return nil, err
  95 +}
@@ -18,12 +18,23 @@ type AuditTemplate struct { @@ -18,12 +18,23 @@ type AuditTemplate struct {
18 NoApprover int8 `orm:"column(no_approver)" description:"审核人空时:【1:自动通过】【2:转交给管理员】"` 18 NoApprover int8 `orm:"column(no_approver)" description:"审核人空时:【1:自动通过】【2:转交给管理员】"`
19 SortNum int `orm:"column(sort_num)" description:"自定义排序编号"` 19 SortNum int `orm:"column(sort_num)" description:"自定义排序编号"`
20 VisibleType int8 `orm:"column(visible_type)" description:"可见范围 0:所有人 1:指定部门 "` 20 VisibleType int8 `orm:"column(visible_type)" description:"可见范围 0:所有人 1:指定部门 "`
  21 + VisibleObject string `orm:"column(visible_object)" description:"可见的对象 部门 指定人 json"`
21 EnableStatus int8 `orm:"column(enable_status)" description:"是否有效 1:有效 0:无效"` 22 EnableStatus int8 `orm:"column(enable_status)" description:"是否有效 1:有效 0:无效"`
22 CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"` 23 CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
23 UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"` 24 UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
24 - Example string `orm:"column(examplte);null" description:"示例"` 25 + Example string `orm:"column(example);null" description:"示例"`
25 } 26 }
26 27
  28 +const (
  29 + VisibleTypeAll = 0
  30 + VisibleTypeDepartment = 1
  31 +)
  32 +
  33 +const (
  34 + VisibleObject_Department = 1
  35 + VisibleObject_User = 2
  36 +)
  37 +
27 func (t *AuditTemplate) TableName() string { 38 func (t *AuditTemplate) TableName() string {
28 return "audit_template" 39 return "audit_template"
29 } 40 }
@@ -80,3 +91,14 @@ func DeleteAuditTemplate(id int64) (err error) { @@ -80,3 +91,14 @@ func DeleteAuditTemplate(id int64) (err error) {
80 } 91 }
81 return 92 return
82 } 93 }
  94 +
  95 +func GetAuditTemplateByTypeId(chanceTypeId int) (v []*AuditTemplate, err error) {
  96 + o := orm.NewOrm()
  97 + _, err = o.QueryTable(&AuditTemplate{}).
  98 + Filter("chance_type_id", chanceTypeId).All(&v)
  99 +
  100 + if err == orm.ErrNoRows {
  101 + return v, nil
  102 + }
  103 + return
  104 +}
@@ -82,3 +82,12 @@ func GetChanceTypeAll() (v []*ChanceType, err error) { @@ -82,3 +82,12 @@ func GetChanceTypeAll() (v []*ChanceType, err error) {
82 } 82 }
83 return 83 return
84 } 84 }
  85 +
  86 +func GetChanceTypeByCompany(companyId int) (v []*ChanceType, err error) {
  87 + o := orm.NewOrm()
  88 + sql := "select * from chance_type where company_id=?"
  89 + if _, err = o.Raw(sql, companyId).QueryRows(&v); err == nil {
  90 + return
  91 + }
  92 + return
  93 +}
@@ -154,3 +154,15 @@ func GetDepartmentByCompanyId(companyId int64) ([]Department, error) { @@ -154,3 +154,15 @@ func GetDepartmentByCompanyId(companyId int64) ([]Department, error) {
154 All(&result) 154 All(&result)
155 return result, err 155 return result, err
156 } 156 }
  157 +
  158 +func GetDepartmentByIds(departmentIds []int64) ([]Department, error) {
  159 + var (
  160 + result []Department
  161 + err error
  162 + )
  163 + o := orm.NewOrm()
  164 + _, err = o.QueryTable(&Department{}).
  165 + Filter("id__in", departmentIds).
  166 + All(&result)
  167 + return result, err
  168 +}
@@ -16,7 +16,7 @@ type InputElement struct { @@ -16,7 +16,7 @@ type InputElement struct {
16 Lable string `json:"lable"` //标题 16 Lable string `json:"lable"` //标题
17 InputType string `json:"inputType"` //输入类型 17 InputType string `json:"inputType"` //输入类型
18 Required int `json:"required"` //是否必填 18 Required int `json:"required"` //是否必填
19 - CurrentValue string `json:"value"` //"当前填写的值" 19 + CurrentValue string `json:"-"` //"当前填写的值"
20 SectionType int8 `json:"sectionType"` 20 SectionType int8 `json:"sectionType"`
21 21
22 ValueList string `json:"-"` //输入候选值 value_list 22 ValueList string `json:"-"` //输入候选值 value_list
@@ -106,9 +106,10 @@ type TemplateUpdateResponse struct { @@ -106,9 +106,10 @@ type TemplateUpdateResponse struct {
106 type TemplateListRequest struct { 106 type TemplateListRequest struct {
107 } 107 }
108 type TemplateListResponse struct { 108 type TemplateListResponse struct {
  109 + List []*TemplateList `json:"list"`
109 } 110 }
110 111
111 -type ChanceType struct { 112 +type TemplateList struct {
112 Id int `json:"id"` 113 Id int `json:"id"`
113 Name string `json:"name"` 114 Name string `json:"name"`
114 Icon string `json:"icon"` 115 Icon string `json:"icon"`
@@ -127,7 +128,33 @@ type TemplateItem struct { @@ -127,7 +128,33 @@ type TemplateItem struct {
127 } 128 }
128 129
129 type VisibleObject struct { 130 type VisibleObject struct {
  131 + Id string `json:"id"`
  132 + Name string `json:"name",omitempty`
  133 + Type int `json:"type"` //1:部门 2:指定人员
  134 +}
  135 +
  136 +/*TemplateEditVisible */
  137 +type TemplateEditVisibleRequest struct {
  138 + Id int `json:"id"` //模板编号
  139 + VisibleObject []VisibleObject `json:"visibleObject" valid:"Required"`
  140 +}
  141 +type TemplateEditVisibleResponse struct {
  142 +}
  143 +
  144 +/*TemplateAddCategory */
  145 +type TemplateOperateCategoryRequest struct {
130 Id int `json:"id"` 146 Id int `json:"id"`
131 - Name string `json:"name,omitempty"`  
132 - Type int //1:部门 2:指定人员 147 + Name string `json:"name"`
  148 + Icon string `json:"icon"`
  149 +}
  150 +type TemplateOperateCategoryResponse struct {
  151 +}
  152 +
  153 +/*TemplateGet */
  154 +type TemplateGetRequest struct {
  155 + Id int `json:"id" valid:"Required"`
  156 +}
  157 +type TemplateGetResponse struct {
  158 + Template Template `json:"template"`
  159 + Example string `json:"example"` //示例`
133 } 160 }
  1 +package agg
  2 +
  3 +import (
  4 + "encoding/json"
  5 + "fmt"
  6 + orm2 "github.com/astaxie/beego/orm"
  7 + "oppmg/common/log"
  8 + "oppmg/models"
  9 + "oppmg/protocol"
  10 + "strconv"
  11 +)
  12 +
  13 +func getUsers(jsonData string) (v []models.User, err error) {
  14 + var vObjs []protocol.VisibleObject
  15 + var ids []int64
  16 + var id int64
  17 + if err = json.Unmarshal([]byte(jsonData), &vObjs); err != nil {
  18 + return
  19 + }
  20 + for i := 0; i < len(vObjs); i++ {
  21 + if vObjs[i].Type != models.VisibleObject_User {
  22 + continue
  23 + }
  24 + if id, err = strconv.ParseInt(vObjs[i].Id, 10, 64); err != nil {
  25 + return
  26 + }
  27 + if id == 0 {
  28 + continue
  29 + }
  30 + ids = append(ids, id)
  31 + }
  32 + if len(ids) == 0 {
  33 + return
  34 + }
  35 + return models.GetUserNameByIds(ids)
  36 +}
  37 +
  38 +func getDepartments(jsonData string) (v []models.Department, err error) {
  39 + var vObjs []protocol.VisibleObject
  40 + var ids []int64
  41 + var id int64
  42 + if err = json.Unmarshal([]byte(jsonData), &vObjs); err != nil {
  43 + return
  44 + }
  45 + for i := 0; i < len(vObjs); i++ {
  46 + if vObjs[i].Type != models.VisibleObject_Department {
  47 + continue
  48 + }
  49 + if id, err = strconv.ParseInt(vObjs[i].Id, 10, 64); err != nil {
  50 + return
  51 + }
  52 + if id == 0 {
  53 + continue
  54 + }
  55 + ids = append(ids, id)
  56 + }
  57 + if len(ids) == 0 {
  58 + return
  59 + }
  60 + return models.GetDepartmentByIds(ids)
  61 +}
  62 +
  63 +func GetVisibleObject(jsonData string) (v []protocol.VisibleObject, err error) {
  64 + var (
  65 + users []models.User
  66 + departments []models.Department
  67 + )
  68 + if len(jsonData) == 0 {
  69 + return
  70 + }
  71 + if departments, err = getDepartments(jsonData); err != nil && err != orm2.ErrNoRows {
  72 + log.Error(err.Error())
  73 + return
  74 + }
  75 + for i := range departments {
  76 + v = append(v, protocol.VisibleObject{
  77 + Id: fmt.Sprintf("%v", departments[i].Id),
  78 + Name: departments[i].Name,
  79 + Type: models.VisibleObject_Department,
  80 + })
  81 + }
  82 + if users, err = getUsers(jsonData); err != nil && err != orm2.ErrNoRows {
  83 + log.Error(err.Error())
  84 + return
  85 + }
  86 + for i := range users {
  87 + v = append(v, protocol.VisibleObject{
  88 + Id: fmt.Sprintf("%v", users[i].Id),
  89 + Name: users[i].NickName,
  90 + Type: models.VisibleObject_User,
  91 + })
  92 + }
  93 +
  94 + return
  95 +}
@@ -2,16 +2,19 @@ package audit @@ -2,16 +2,19 @@ package audit
2 2
3 import ( 3 import (
4 "database/sql" 4 "database/sql"
  5 + "encoding/json"
5 "fmt" 6 "fmt"
6 orm2 "github.com/astaxie/beego/orm" 7 orm2 "github.com/astaxie/beego/orm"
7 "oppmg/common/log" 8 "oppmg/common/log"
8 "oppmg/models" 9 "oppmg/models"
9 "oppmg/protocol" 10 "oppmg/protocol"
  11 + "oppmg/services/agg"
10 "oppmg/utils" 12 "oppmg/utils"
11 "strings" 13 "strings"
12 "time" 14 "time"
13 ) 15 )
14 16
  17 +//添加模板
15 func TemplateAdd(uid, companyId int64, request *protocol.TemplateAddRequest) (rsp *protocol.TemplateAddResponse, err error) { 18 func TemplateAdd(uid, companyId int64, request *protocol.TemplateAddRequest) (rsp *protocol.TemplateAddResponse, err error) {
16 var ( 19 var (
17 auditForm *models.AuditForm 20 auditForm *models.AuditForm
@@ -27,6 +30,7 @@ func TemplateAdd(uid, companyId int64, request *protocol.TemplateAddRequest) (rs @@ -27,6 +30,7 @@ func TemplateAdd(uid, companyId int64, request *protocol.TemplateAddRequest) (rs
27 //模板 30 //模板
28 { 31 {
29 template = &models.AuditTemplate{ 32 template = &models.AuditTemplate{
  33 + ChanceTypeId: request.Template.ChanceTypeId,
30 CompanyId: int(companyId), 34 CompanyId: int(companyId),
31 Name: request.Template.Name, 35 Name: request.Template.Name,
32 Doc: request.Template.Doc, 36 Doc: request.Template.Doc,
@@ -74,6 +78,7 @@ func TemplateAdd(uid, companyId int64, request *protocol.TemplateAddRequest) (rs @@ -74,6 +78,7 @@ func TemplateAdd(uid, companyId int64, request *protocol.TemplateAddRequest) (rs
74 return 78 return
75 } 79 }
76 80
  81 +//模板更新
77 func TemplateUpdate(uid, companyId int64, request *protocol.TemplateUpdateRequest) (rsp *protocol.TemplateUpdateResponse, err error) { 82 func TemplateUpdate(uid, companyId int64, request *protocol.TemplateUpdateRequest) (rsp *protocol.TemplateUpdateResponse, err error) {
78 var ( 83 var (
79 template *models.AuditTemplate 84 template *models.AuditTemplate
@@ -176,8 +181,158 @@ func insertOrUpdateInput(orm orm2.Ormer, companyId int64, templateId int, input @@ -176,8 +181,158 @@ func insertOrUpdateInput(orm orm2.Ormer, companyId int64, templateId int, input
176 return 181 return
177 } 182 }
178 183
  184 +//模板列表
179 func TemplateList(uid, companyId int64, request *protocol.TemplateListRequest) (rsp *protocol.TemplateListResponse, err error) { 185 func TemplateList(uid, companyId int64, request *protocol.TemplateListRequest) (rsp *protocol.TemplateListResponse, err error) {
180 - var () 186 + var (
  187 + chanceTypes []*models.ChanceType
  188 + templates []*models.AuditTemplate
  189 + )
  190 + if chanceTypes, err = models.GetChanceTypeByCompany(int(companyId)); err != nil {
  191 + log.Error("get:%v msg:%v", companyId, err.Error())
  192 + return
  193 + }
181 rsp = &protocol.TemplateListResponse{} 194 rsp = &protocol.TemplateListResponse{}
  195 + for i := 0; i < len(chanceTypes); i++ {
  196 + t := chanceTypes[i]
  197 + item := &protocol.TemplateList{
  198 + Id: t.Id,
  199 + Name: t.Name,
  200 + Icon: t.Icon,
  201 + }
  202 + rsp.List = append(rsp.List, item)
  203 + if templates, err = models.GetAuditTemplateByTypeId(t.Id); err != nil {
  204 + log.Error(err.Error())
  205 + continue
  206 + }
  207 + for j := range templates {
  208 + temp := templates[j]
  209 + var vObj []protocol.VisibleObject
  210 + //TODO:可见
  211 + //1.检查可见性
  212 +
  213 + //2.可见对象 VisibleObject
  214 + if temp.VisibleType == models.VisibleTypeDepartment {
  215 + if vObj, err = agg.GetVisibleObject(temp.VisibleObject); err != nil {
  216 + log.Error(err.Error())
  217 + return
  218 + }
  219 + }
  220 + tempItem := &protocol.TemplateItem{
  221 + Id: temp.Id,
  222 + Name: temp.Name,
  223 + Doc: temp.Doc,
  224 + Icon: temp.Icon,
  225 + EnableStatus: temp.EnableStatus,
  226 + Sort: temp.SortNum,
  227 + VisibleType: temp.VisibleType,
  228 + VisibleObject: vObj,
  229 + }
  230 + item.Templates = append(item.Templates, tempItem)
  231 + }
  232 + }
  233 +
  234 + return
  235 +}
  236 +
  237 +//编辑可见范围
  238 +func TemplateEditVisible(uid, companyId int64, request *protocol.TemplateEditVisibleRequest) (rsp *protocol.TemplateEditVisibleResponse, err error) {
  239 + var (
  240 + template *models.AuditTemplate
  241 + data []byte
  242 + )
  243 + rsp = &protocol.TemplateEditVisibleResponse{}
  244 + if template, err = models.GetAuditTemplateById(int64(request.Id)); err != nil {
  245 + log.Error(err.Error())
  246 + return
  247 + }
  248 + if template.CompanyId != int(companyId) {
  249 + err = protocol.NewErrWithMessage("10027")
  250 + log.Error("template_id:%v companyId:%v want:%v not equal.", request.Id, companyId, template.CompanyId)
  251 + return
  252 + }
  253 + if data, err = json.Marshal(request.VisibleObject); err != nil {
  254 + log.Error(err.Error())
  255 + return
  256 + }
  257 + if err = utils.UpdateTableByMap(template, map[string]interface{}{"VisibleObject": string(data)}); err != nil {
  258 + log.Error(err.Error())
  259 + return
  260 + }
  261 + return
  262 +}
  263 +
  264 +//操作一级分类
  265 +func TemplateOperateCategory(uid, companyId int64, request *protocol.TemplateOperateCategoryRequest) (rsp *protocol.TemplateOperateCategoryResponse, err error) {
  266 + var (
  267 + chanceType *models.ChanceType
  268 + )
  269 + rsp = &protocol.TemplateOperateCategoryResponse{}
  270 + if request.Id > 0 { //编辑
  271 + if chanceType, err = models.GetChanceTypeById(request.Id); err != nil {
  272 + log.Error(err.Error())
  273 + return
  274 + }
  275 + if chanceType.CompanyId != int(companyId) {
  276 + err = protocol.NewErrWithMessage("10027")
  277 + log.Error("template_id:%v companyId:%v want:%v not equal.", request.Id, companyId, chanceType.CompanyId)
  278 + }
  279 + if err = utils.UpdateTableByMap(chanceType, map[string]interface{}{"Name": request.Name, "Icon": request.Icon, "UpdateAt": time.Now()}); err != nil {
  280 + log.Error(err.Error())
  281 + return
  282 + }
  283 + return
  284 + }
  285 + chanceType = &models.ChanceType{
  286 + Name: request.Name,
  287 + Icon: request.Icon,
  288 + CompanyId: int(companyId),
  289 + CreateAt: time.Now(),
  290 + UpdateAt: time.Now(),
  291 + }
  292 + if _, err = models.AddChanceType(chanceType); err != nil {
  293 + log.Error(err.Error())
  294 + }
  295 + return
  296 +}
  297 +
  298 +//获取模板详情
  299 +func TemplateGet(uid, companyId int64, request *protocol.TemplateGetRequest) (rsp *protocol.TemplateGetResponse, err error) {
  300 + var (
  301 + template *models.AuditTemplate
  302 + auditForm []*models.AuditForm
  303 + )
  304 + rsp = &protocol.TemplateGetResponse{}
  305 + if template, err = models.GetAuditTemplateById(int64(request.Id)); err != nil {
  306 + log.Error("template_id:%v 不存在 ,err:%v", request.Id, err.Error())
  307 + return
  308 + }
  309 +
  310 + {
  311 + rsp.Template = protocol.Template{
  312 + Id: template.Id,
  313 + ChanceTypeId: template.ChanceTypeId,
  314 + Name: template.Name,
  315 + Doc: template.Doc,
  316 + Icon: template.Icon,
  317 + }
  318 + rsp.Example = template.Example
  319 +
  320 + if auditForm, err = models.GetAuditFormByTemplateId(int(template.Id)); err != nil {
  321 + if err == sql.ErrNoRows {
  322 + err = nil
  323 + }
  324 + return
  325 + }
  326 + for i := range auditForm {
  327 + input := auditForm[i]
  328 + rsp.Template.InputList = append(rsp.Template.InputList, &protocol.InputElement{
  329 + Id: input.Id,
  330 + Sort: input.SortNum,
  331 + Lable: input.InputType,
  332 + Required: int(input.Required),
  333 + SectionType: input.Section,
  334 + })
  335 + }
  336 + }
182 return 337 return
183 } 338 }