作者 唐旭辉

数据同步 功能跟进

1 package models 1 package models
2 2
3 import ( 3 import (
4 - "errors"  
5 "fmt" 4 "fmt"
6 "time" 5 "time"
7 6
@@ -29,10 +28,10 @@ func init() { @@ -29,10 +28,10 @@ func init() {
29 } 28 }
30 29
31 func (t *Position) SetRelation(parent *Position) error { 30 func (t *Position) SetRelation(parent *Position) error {
32 - if t.Id == 0 {  
33 - return errors.New("Id==0")  
34 - }  
35 - if parent == nil { 31 + // if t.Id == 0 {
  32 + // return errors.New("Id==0")
  33 + // }
  34 + if parent == nil || t.Id == 0 {
36 t.Relation = fmt.Sprintf("%d", t.Id) 35 t.Relation = fmt.Sprintf("%d", t.Id)
37 } else { 36 } else {
38 t.Relation = fmt.Sprintf("%s/%d", parent.Relation, t.Id) 37 t.Relation = fmt.Sprintf("%s/%d", parent.Relation, t.Id)
@@ -7,16 +7,17 @@ import ( @@ -7,16 +7,17 @@ import (
7 "oppmg/models" 7 "oppmg/models"
8 "oppmg/utils" 8 "oppmg/utils"
9 "strings" 9 "strings"
  10 + "time"
10 11
11 "github.com/astaxie/beego/orm" 12 "github.com/astaxie/beego/orm"
12 ) 13 )
13 14
14 type ModulePositionData struct { 15 type ModulePositionData struct {
15 - Id int64 `json:"id"`  
16 - Name string `json:"nick_name"`  
17 - ParentId int64 `json:"parent_id"`  
18 - CompanyId int64 `json:"company_id"`  
19 - Status string `json:"status"` 16 + Id int64 `json:"id"`
  17 + Name string `json:"nick_name"`
  18 + ParentId int64 `json:"parent_id"`
  19 + CompanyId int64 `json:"company_id"`
  20 + Ids []int64 `json:"ids"`
20 } 21 }
21 22
22 var _ PlatformAction = ModulePositionData{} 23 var _ PlatformAction = ModulePositionData{}
@@ -161,7 +162,75 @@ func AddPosition(data ModulePositionData) error { @@ -161,7 +162,75 @@ func AddPosition(data ModulePositionData) error {
161 } 162 }
162 return nil 163 return nil
163 } 164 }
164 -func DeletePosition(data ModulePositionData) error {  
165 165
  166 +func DeletePosition(ids []int64, ucompanyid int64) error {
  167 + //检查是否可以被删除
  168 + var (
  169 + //根据参数获取的职位
  170 + positionDelete []*models.Position
  171 + //最终需要操作的职位
  172 + toDelete = make(map[int64]*models.Position)
  173 + companyinfo *models.Company
  174 + err error
  175 + )
  176 + const (
  177 + //获取部门子集,
  178 + dataSql0 string = `SELECT id,relation FROM position WHERE relation LIKE ? AND delete_at = 0 `
  179 + )
  180 + companyinfo, err = models.GetCompanyByUCenter(ucompanyid)
  181 + if err != nil {
  182 + e := fmt.Errorf("获取公司数据失败")
  183 + log.Error(e.Error())
  184 + return e
  185 + }
  186 + for _, id := range ids {
  187 + var p *models.Position
  188 + p, err := models.GetPositionById(id)
  189 + if err != nil {
  190 + log.Error("获取职位失败Id:%d,err:%s", id, err)
  191 + continue
  192 + }
  193 + if p.CompanyId != companyinfo.Id {
  194 + log.Error("公司id不匹配")
  195 + return errors.New("公司id不匹配")
  196 + }
  197 + positionDelete = append(positionDelete, p)
  198 + toDelete[p.Id] = p
  199 + }
  200 + for _, pos := range positionDelete {
  201 + var positionsubset []models.Position
  202 + relationLike := pos.Relation + "%"
  203 + err := utils.ExecuteQueryAll(&positionsubset, dataSql0, relationLike)
  204 + if err != nil {
  205 + e := fmt.Errorf("EXECUTE SQL err:%s", err)
  206 + log.Error(e.Error())
  207 + return e
  208 + }
  209 + for k, subset := range positionsubset {
  210 + if _, ok := toDelete[subset.Id]; !ok {
  211 + toDelete[subset.Id] = &positionsubset[k]
  212 + }
  213 + }
  214 + }
  215 + var (
  216 + deleteIds []string
  217 + dataSql2 string = `update position set delete_at=%s where id IN (%s)`
  218 + nowTime string = time.Now().Format("2006-01-02 15:04:05")
  219 + )
  220 + for k, _ := range toDelete {
  221 + deleteIds = append(deleteIds, fmt.Sprint(k))
  222 + }
  223 + if len(deleteIds) == 0 {
  224 + return nil
  225 + }
  226 + dataSql2 = fmt.Sprintf(dataSql2, nowTime, strings.Join(deleteIds, ","))
  227 + o := orm.NewOrm()
  228 + o.Begin()
  229 + err = utils.ExecuteSQLWithOrmer(o, dataSql2)
  230 + if err != nil {
  231 + e := fmt.Errorf("EXCUTE SQL ERR:%s", err)
  232 + return e
  233 + }
  234 + o.Commit()
166 return nil 235 return nil
167 } 236 }