作者 yangfu

Merge branch 'dev' of http://gitlab.fjmaimaimai.com/mmm-go/oppmg into dev

# Conflicts:
#	models/user_company.go
#	services/auth/auth.go
### 接口完成进度
### 阶段一接口完成进度
| 功能 | 完成状态 | 时间 | 接口路径 | 请求方式 |
| ---------------- | -------- | ---------- | -------------------------- | -------- |
| 登录获取 token | 完成 | 2018/12/11 | /auth/login | post |
| 短信验证码 | 未开始 | | |
| 刷新 token | 完成 | 2018/12/11 | /v1/auth/refresh_token | get |
| 切换公司 | 完成 | 2018/12/11 | /v1/auth/change_company | post |
| 获取个人基础数据 | (暂停) | | /v1/auth/me | get |
| 获取角色列表 | 完成 | | /v1/rbac/role | get |
| 添加角色 | 完成 | 2019.11.26 | /v1/rbac/role/add | post |
| 编辑角色 | 完成 | 2019.11.26 | /v1/rbac/role/edit | post |
| 删除角色 | 完成 | 2019.12.04 | /v1/rbac/role/delete | post |
| 添加角色组 | 完成 | 2019.12.04 | /v1/rbac/role_group/add | post |
| 编辑角色组 | 完成 | 2019.12.04 | /v1/rbac/role_group/edit | post |
| 删除角色组 | 完成 | 2019.12.04 | /v1/rbac/role_group/delete | post |
| 获取部门列表 | 完成 | 2019.12.5 | /v1/department | get |
| 添加部门 | 完成 | 2019.11.28 | /v1/department/add | post |
| 编辑部门 | 完成 | 2019.11.29 | /v1/department/edit | post |
| 删除部门 | 完成 | 2019.12.2 | /v1/department/delete | post |
| 获取职位列表 | 进行中 | | /v1/position | get |
| 添加职位 | 完成 | 2019.12.2 | /v1/position/add | post |
| 编辑职位 | 完成 | 2019.12.2 | /v1/position/edit | post |
| 删除职位 | 完成 | 2019.12.2 | /v1/position/delete | post |
| 添加员工 | 未开始 | | /v1/company/user | post |
| 编辑员工 | 未开始 | | /v1/company/user | post |
| 删除员工 | 未开始 | | /v1/company/user | post |
|登录-登录获取 token | 待调试 | 2018/12/11 | /auth/login | post |
|登录-短信验证码 | 未开始 | | |
|登录-刷新 token | 待调试 | 2018/12/11 | /v1/auth/refresh_token | get |
|登录-切换公司 | 待调试 | 2018/12/11 | /v1/auth/change_company | post |
|获取个人基础数据 | (暂停) | | /v1/auth/me | get |
|角色-获取角色列表 | 待调试 | | /v1/rbac/role | get |
|角色-添加角色 | 待调试 | 2019.11.26 | /v1/rbac/role/add | post |
|角色-编辑角色 | 待调试 | 2019.11.26 | /v1/rbac/role/edit | post |
|角色-删除角色 | 待调试 | 2019.12.04 | /v1/rbac/role/delete | post |
|角色-添加角色组 | 待调试 | 2019.12.04 | /v1/rbac/role_group/add | post |
|角色-编辑角色组 | 待调试 | 2019.12.04 | /v1/rbac/role_group/edit | post |
|角色-删除角色组 | 待调试 | 2019.12.04 | /v1/rbac/role_group/delete | post |
|组织架构设置-获取部门列表 | 待调试 | 2019.12.5 | /v1/department | get |
|组织架构设置-添加部门 | 待调试 | 2019.11.28 | /v1/department/add | post |
|组织架构设置-编辑部门 | 待调试 | 2019.11.29 | /v1/department/edit | post |
|组织架构设置-删除部门 | 待调试 | 2019.12.2 | /v1/department/delete | post |
|职位管理-获取职位列表 | 进行中 | | /v1/position | get |
|职位管理-添加职位 | 待调试 | 2019.12.2 | /v1/position/add | post |
|职位管理-编辑职位 | 待调试 | 2019.12.2 | /v1/position/edit | post |
|职位管理-删除职位 | 待调试 | 2019.12.2 | /v1/position/delete | post |
|员工管理-添加员工 | 未开始 | | /v1/company/user | post |
|员工管理-编辑员工 | 未开始 | | /v1/company/user | post |
|员工管理-删除员工 | 未开始 | | /v1/company/user | post |
| 机会模板管理-列表 | 进行中 | | | |
| 机会模板管理-添加 | 未开始 | | | |
| 机会模板管理-更新 | 未开始 | | | |
| 机会模板管理-修改公开状态 | 未开始 | | | |
| 机会模板管理-一级模板 | 未开始 | | | |
| 配置-评分模式设置 | 未开始 | | | |
| 公告管理 | 未开始 | | | |
| 公告管理 | 未开始 | | | |
| 公告管理 | 未开始 | | | |
| 机会&审批 模板 | 未开始 | | | |
| 机会&审批 模板 | 未开始 | | | |
| 机会&审批 模板 | 未开始 | | | |
| 机会&审批 模板 | 未开始 | | | |
| 机会类型设置 | 未开始 | | | |
| 机会类型设置 | 未开始 | | | |
| 机会类型设置 | 未开始 | | | |
| 机会类型设置 | 未开始 | | | |
... ...
... ... @@ -17,12 +17,12 @@ mysql_host = "${MYSQL_HOST||115.29.205.99}"
mysql_port = "${MYSQL_PORT||3306}"
mysql_db_name = "${MYSQL_DB_NAME||opportunity}"
##redis相关配置
redis_add_port = "127.0.0.1:6379"
redis_auth = ""
redis_add_port = "192.168.100.102:6379"
redis_auth = "123456"
##log相关配置
##out_put:"console","file"
log_output = "file"
log_filename = "${aliyun_logs_access||./log/ability.log}"
log_filename = "${aliyun_logs_access||ability.log}"
log_level = "${LOG_LEVEL||error}"
##统一用户中心相关配置
... ...
... ... @@ -12,7 +12,7 @@ type BulletinController struct {
}
//BulletinRelease
//@router /release [post]
//@router /add [post]
func (this *BulletinController) BulletinRelease() {
var msg *protocol.ResponseMessage
defer func() {
... ...
package controllers
import (
"encoding/json"
"oppmg/common/log"
"oppmg/protocol"
"oppmg/services/audit"
)
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 !(request.Type == 1 || request.Type == 2) {
// msg = protocol.BadRequestParam("1")
// log.Error("type error :%v", request.Type)
// return
//}
if b, m := this.Valid(request); !b {
msg = m
return
}
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
}
rsp, err := audit.TemplateUpdate(uid, companyId, request)
msg = protocol.NewReturnResponse(rsp, err)
return
}
//TemplateList
//@router /templateList [post]
func (this *TemplateController) TemplateList() {
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
}
rsp, err := audit.TemplateUpdate(uid, companyId, request)
msg = protocol.NewReturnResponse(rsp, err)
return
}
... ...
... ... @@ -40,6 +40,9 @@ var AuthToken = func(ctx *context.Context) {
accesstoken := ctx.Input.Header(protocol.HeaderAccessToken)
refreshToken := ctx.Input.Header(protocol.HeaderRefreshToken)
mtoken, err = serveauth.ValidJWTToken(accesstoken)
if beego.BConfig.RunMode != "prod" {
return
}
if err == nil {
storetoken, err = redisdata.GetLoginToken(mtoken.UID)
if err != nil {
... ...
package models
import (
"fmt"
"time"
"github.com/astaxie/beego/orm"
)
type AuditForm struct {
Id int `orm:"column(id);auto" description:"唯一编号"`
CompanyId int `orm:"column(company_id)" description:"表company.id 公司编号"`
AuditTemplateId int `orm:"column(audit_template_id)" description:"表audit_template.id 所属审批模板编号"`
Section int8 `orm:"column(section)" description:"输入项所属的版块(1.基础,2.附加)"`
SortNum int `orm:"column(sort_num)" description:"排序"`
Label string `orm:"column(label);size(100)" description:"标题"`
InputType string `orm:"column(input_type);size(50)" description:"输入类型 text"`
ValueList string `orm:"column(value_list);size(255)" description:"可选值列表"`
Required int8 `orm:"column(required)" description:"是否必填:【0:否】【1:是】"`
CurrentValue string `orm:"column(current_value);size(255)" description:"实际填写的值"`
Disable int8 `orm:"column(disable);null" description:"显示隐藏:【0:显示】【1:隐藏】"`
Step string `orm:"column(step);size(30)" description:"步进,输入类型是range时生效 "`
Placeholder string `orm:"column(placeholder);size(40)" description:"输入提示"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);auto_now_add" description:"创建时间"`
DeleteAt time.Time `orm:"column(delete_at);type(timestamp)" description:"更新时间"`
EnableStatus int8 `orm:"column(enable_status);null" description:"有效状态 0:无效 1:有效 "`
}
func (t *AuditForm) TableName() string {
return "audit_form"
}
func init() {
orm.RegisterModel(new(AuditForm))
}
// AddAuditForm insert a new AuditForm into database and returns
// last inserted Id on success.
func AddAuditForm(m *AuditForm) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetAuditFormById retrieves AuditForm by Id. Returns error if
// Id doesn't exist
func GetAuditFormById(id int) (v *AuditForm, err error) {
o := orm.NewOrm()
v = &AuditForm{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateAuditForm updates AuditForm by Id and returns error if
// the record to be updated doesn't exist
func UpdateAuditFormById(m *AuditForm) (err error) {
o := orm.NewOrm()
v := AuditForm{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
return
}
// DeleteAuditForm deletes AuditForm by Id and returns error if
// the record to be deleted doesn't exist
func DeleteAuditForm(id int) (err error) {
o := orm.NewOrm()
v := AuditForm{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&AuditForm{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
... ...
package models
import (
"fmt"
"time"
"github.com/astaxie/beego/orm"
)
type AuditTemplate struct {
Id int64 `orm:"column(id);auto" description:"唯一编号"`
CompanyId int `orm:"column(company_id)" description:"公司id"`
ChanceTypeId int `orm:"column(chance_type_id)" description:"机会类型 chance_type.id"`
Name string `orm:"column(name);size(20)" description:"子分类名称"`
Doc string `orm:"column(doc);size(255)" description:"说明"`
Icon string `orm:"column(icon);size(255)" description:"图标"`
NoticeType int8 `orm:"column(notice_type)" description:"通知方式"`
NoApprover int8 `orm:"column(no_approver)" description:"审核人空时:【1:自动通过】【2:转交给管理员】"`
SortNum int `orm:"column(sort_num)" description:"自定义排序编号"`
VisibleType int8 `orm:"column(visible_type)" description:"可见范围 0:所有人 1:指定部门 "`
EnableStatus int8 `orm:"column(enable_status)" description:"是否有效 1:有效 0:无效"`
CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"`
Example string `orm:"column(examplte);null" description:"示例"`
}
func (t *AuditTemplate) TableName() string {
return "audit_template"
}
func init() {
orm.RegisterModel(new(AuditTemplate))
}
// AddAuditTemplate insert a new AuditTemplate into database and returns
// last inserted Id on success.
func AddAuditTemplate(m *AuditTemplate) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetAuditTemplateById retrieves AuditTemplate by Id. Returns error if
// Id doesn't exist
func GetAuditTemplateById(id int64) (v *AuditTemplate, err error) {
o := orm.NewOrm()
v = &AuditTemplate{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateAuditTemplate updates AuditTemplate by Id and returns error if
// the record to be updated doesn't exist
func UpdateAuditTemplateById(m *AuditTemplate) (err error) {
o := orm.NewOrm()
v := AuditTemplate{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
return
}
// DeleteAuditTemplate deletes AuditTemplate by Id and returns error if
// the record to be deleted doesn't exist
func DeleteAuditTemplate(id int64) (err error) {
o := orm.NewOrm()
v := AuditTemplate{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&AuditTemplate{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
... ...
package models
import (
"fmt"
"time"
"github.com/astaxie/beego/orm"
)
type ChanceType struct {
Id int `orm:"column(id);auto"`
Name string `orm:"column(name);size(50)" description:"机会类型名称"`
Icon string `orm:"column(icon);size(500);null" description:"图标地址"`
CompanyId int `orm:"column(company_id)" description:"表company.id 公司编号"`
SortNum int `orm:"column(sort_num);null" description:"序号 公司下的序号"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间 "`
UpdateAt time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间 "`
}
func (t *ChanceType) TableName() string {
return "chance_type"
}
func init() {
orm.RegisterModel(new(ChanceType))
}
// AddChanceType insert a new ChanceType into database and returns
// last inserted Id on success.
func AddChanceType(m *ChanceType) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetChanceTypeById retrieves ChanceType by Id. Returns error if
// Id doesn't exist
func GetChanceTypeById(id int) (v *ChanceType, err error) {
o := orm.NewOrm()
v = &ChanceType{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateChanceType updates ChanceType by Id and returns error if
// the record to be updated doesn't exist
func UpdateChanceTypeById(m *ChanceType) (err error) {
o := orm.NewOrm()
v := ChanceType{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
return
}
// DeleteChanceType deletes ChanceType by Id and returns error if
// the record to be deleted doesn't exist
func DeleteChanceType(id int) (err error) {
o := orm.NewOrm()
v := ChanceType{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&ChanceType{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
func GetChanceTypeAll() (v []*ChanceType, err error) {
o := orm.NewOrm()
sql := "select * from chance_type "
if _, err = o.Raw(sql).QueryRows(&v); err == nil {
return
}
return
}
... ...
... ... @@ -11,14 +11,17 @@ const (
//InputElement 自定义表单项
type InputElement struct {
Id int `json:"id"`
Sort int `json:"sort"` //排序
Lable string `json:"lable"` //标题
InputType string `json:"input_type"` //输入类型
ValueList string `json:"value_list"` //输入候选值
Required bool `json:"required"` //是否必填
Placeholder string `json:"Placeholder"` //帮助用户填写输入字段的提示
Disable bool `json:"disable"` //"显示隐藏",
CurrentValue string `json:"current_value"` //"当前填写的值"
InputType string `json:"inputType"` //输入类型
Required int `json:"required"` //是否必填
CurrentValue string `json:"value"` //"当前填写的值"
SectionType int8 `json:"sectionType"`
ValueList string `json:"-"` //输入候选值 value_list
Placeholder string `json:"-"` //帮助用户填写输入字段的提示 Placeholder
Disable bool `json:"-"` //"显示隐藏",
}
//自定义表单
... ... @@ -72,3 +75,59 @@ func (input ValidateInputRedio) ValidateInput() error {
func (input ValidateInputRedio) ValidateConfig() error {
return nil
}
/***********审核模板管理**********/
/*TemplateAdd */
type TemplateAddRequest struct {
Template Template `json:"template"`
Example string `json:"example"` //示例
}
type TemplateAddResponse struct {
}
type Template struct {
Id int64 `json:"id"` //创建时 0
ChanceTypeId int `json:"chanceTypeId"` //机会类型编号
Name string `json:"name"`
Doc string `json:"doc"`
Icon string `json:"icon"`
InputList []*InputElement `json:"inputList"`
}
/*TemplateUpdate */
type TemplateUpdateRequest struct {
Template Template `json:"template"`
Example string `json:"example"` //示例`
}
type TemplateUpdateResponse struct {
}
/*TemplateList */
type TemplateListRequest struct {
}
type TemplateListResponse struct {
}
type ChanceType struct {
Id int `json:"id"`
Name string `json:"name"`
Icon string `json:"icon"`
Templates []*TemplateItem `json:"templates"`
}
type TemplateItem struct {
Id int64 `json:"id"` //创建时 0
Name string `json:"name"`
Doc string `json:"doc"`
Icon string `json:"icon"`
EnableStatus int8 `json:"enableStatus"` //禁用状态
Sort int `json:"sort"` //序号
VisibleType int8 `json:"visibleType"`
VisibleObject []VisibleObject `json:"visibleObject"`
}
type VisibleObject struct {
Id int `json:"id"`
Name string `json:"name,omitempty"`
Type int //1:部门 2:指定人员
}
... ...
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
}
... ...
package utils
import (
"errors"
"fmt"
"oppmg/common/log"
"oppmg/protocol"
"reflect"
"strings"
"github.com/astaxie/beego/orm"
... ... @@ -125,3 +127,110 @@ func (q *QueryDataByPage) Query(result interface{}) (pageinfo protocol.ResponseP
}
return protocol.ResponsePageInfo{CurrentPage: q.offset, TotalPage: total}, nil
}
// 更新指定表的几个列
func UpdateTableByMap(tabeleStruct interface{}, changeMap map[string]interface{}) error {
if reflect.TypeOf(tabeleStruct).Kind() != reflect.Ptr {
err := errors.New("UpdateTableByMap: tableStruct must ptr")
log.Error(err.Error())
return err
}
if len(changeMap) < 1 {
log.Info("changeMap is nil")
return nil
}
o := orm.NewOrm()
changeColumn := make([]string, 0, len(changeMap))
for i, v := range changeMap {
changeColumn = append(changeColumn, i)
if err := SetStructValueByType(tabeleStruct, i, v); err != nil {
log.Error("err:%v key:%v value:%v", err.Error(), i, v)
return err
}
}
num, err := o.Update(tabeleStruct, changeColumn...)
if err != nil {
log.Error(err.Error())
return err
}
log.Info(fmt.Sprintf("UpdateTableByMap: table:%s effect records:%d column:%v", GetTableName(tabeleStruct), num, changeColumn))
return nil
}
func UpdateTableByMapWithOrmer(o orm.Ormer, tabeleStruct interface{}, changeMap map[string]interface{}) error {
if reflect.TypeOf(tabeleStruct).Kind() != reflect.Ptr {
err := errors.New("UpdateTableByMap: tableStruct must ptr")
log.Error(err.Error())
return err
}
if len(changeMap) < 1 {
log.Info("changeMap is nil")
return nil
}
changeColumn := make([]string, 0, len(changeMap))
for i, v := range changeMap {
changeColumn = append(changeColumn, i)
if err := SetStructValueByType(tabeleStruct, i, v); err != nil {
log.Error("err:%v key:%v value:%v", err.Error(), i, v)
return err
}
}
num, err := o.Update(tabeleStruct, changeColumn...)
if err != nil {
log.Error(err.Error())
return err
}
log.Info(fmt.Sprintf("UpdateTableByMap: table:%s effect records:%d column:%v", GetTableName(tabeleStruct), num, changeColumn))
return nil
}
// 通过反射调用结构对应的TableName函数,达到返回表名的目的
func GetTableName(tableStruct interface{}) string {
m := reflect.ValueOf(tableStruct).MethodByName("TableName")
if m.IsValid() && m.Kind() == reflect.Func {
re := m.Call(nil)
for _, v := range re {
if v.IsValid() {
return v.String()
}
}
}
return "unknown"
}
// 此函数将指定的结构体成员值更新到结构体中
func SetStructValueByType(s interface{}, columnType string, columnValue interface{}) error {
columnValueV := reflect.ValueOf(columnValue)
var setValue reflect.Value
var flag = false
v := reflect.ValueOf(s)
for i, n := 0, v.Elem().NumField(); i < n; i++ {
if v.Elem().Type().Field(i).Name == columnType {
setValue = v.Elem().Field(i)
flag = true
break
}
}
if !flag {
return errors.New("struct is not type:")
} else if !setValue.CanSet() {
return errors.New("setValue.CanSet is false")
} else if setValue.Kind() != columnValueV.Kind() {
return errors.New("struct field and value of type is error")
}
switch columnValueV.Kind() {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int, reflect.Int64:
setValue.SetInt(int64(columnValueV.Int()))
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
setValue.SetUint(uint64(columnValueV.Uint()))
case reflect.Float32, reflect.Float64:
setValue.SetFloat(float64(columnValueV.Float()))
case reflect.String:
setValue.SetString(columnValueV.String())
case reflect.Struct:
setValue.Set(columnValueV)
default:
return errors.New("columnValue err for:" + columnType)
}
return nil
}
... ...