作者 唐旭辉

数据同步 功能跟进

package models
import (
"errors"
"fmt"
"time"
... ... @@ -29,10 +28,10 @@ func init() {
}
func (t *Position) SetRelation(parent *Position) error {
if t.Id == 0 {
return errors.New("Id==0")
}
if parent == nil {
// if t.Id == 0 {
// return errors.New("Id==0")
// }
if parent == nil || t.Id == 0 {
t.Relation = fmt.Sprintf("%d", t.Id)
} else {
t.Relation = fmt.Sprintf("%s/%d", parent.Relation, t.Id)
... ...
... ... @@ -7,16 +7,17 @@ import (
"oppmg/models"
"oppmg/utils"
"strings"
"time"
"github.com/astaxie/beego/orm"
)
type ModulePositionData struct {
Id int64 `json:"id"`
Name string `json:"nick_name"`
ParentId int64 `json:"parent_id"`
CompanyId int64 `json:"company_id"`
Status string `json:"status"`
Id int64 `json:"id"`
Name string `json:"nick_name"`
ParentId int64 `json:"parent_id"`
CompanyId int64 `json:"company_id"`
Ids []int64 `json:"ids"`
}
var _ PlatformAction = ModulePositionData{}
... ... @@ -161,7 +162,75 @@ func AddPosition(data ModulePositionData) error {
}
return nil
}
func DeletePosition(data ModulePositionData) error {
func DeletePosition(ids []int64, ucompanyid int64) error {
//检查是否可以被删除
var (
//根据参数获取的职位
positionDelete []*models.Position
//最终需要操作的职位
toDelete = make(map[int64]*models.Position)
companyinfo *models.Company
err error
)
const (
//获取部门子集,
dataSql0 string = `SELECT id,relation FROM position WHERE relation LIKE ? AND delete_at = 0 `
)
companyinfo, err = models.GetCompanyByUCenter(ucompanyid)
if err != nil {
e := fmt.Errorf("获取公司数据失败")
log.Error(e.Error())
return e
}
for _, id := range ids {
var p *models.Position
p, err := models.GetPositionById(id)
if err != nil {
log.Error("获取职位失败Id:%d,err:%s", id, err)
continue
}
if p.CompanyId != companyinfo.Id {
log.Error("公司id不匹配")
return errors.New("公司id不匹配")
}
positionDelete = append(positionDelete, p)
toDelete[p.Id] = p
}
for _, pos := range positionDelete {
var positionsubset []models.Position
relationLike := pos.Relation + "%"
err := utils.ExecuteQueryAll(&positionsubset, dataSql0, relationLike)
if err != nil {
e := fmt.Errorf("EXECUTE SQL err:%s", err)
log.Error(e.Error())
return e
}
for k, subset := range positionsubset {
if _, ok := toDelete[subset.Id]; !ok {
toDelete[subset.Id] = &positionsubset[k]
}
}
}
var (
deleteIds []string
dataSql2 string = `update position set delete_at=%s where id IN (%s)`
nowTime string = time.Now().Format("2006-01-02 15:04:05")
)
for k, _ := range toDelete {
deleteIds = append(deleteIds, fmt.Sprint(k))
}
if len(deleteIds) == 0 {
return nil
}
dataSql2 = fmt.Sprintf(dataSql2, nowTime, strings.Join(deleteIds, ","))
o := orm.NewOrm()
o.Begin()
err = utils.ExecuteSQLWithOrmer(o, dataSql2)
if err != nil {
e := fmt.Errorf("EXCUTE SQL ERR:%s", err)
return e
}
o.Commit()
return nil
}
... ...