tree_contract_dao.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. * @description: 合同项目节相关数据库操作
  3. * @Author: CP
  4. * @Date: 2020-11-02 11:37:32
  5. * @FilePath: \construction_management\dao\tree_contract_dao.go
  6. */
  7. package dao
  8. import (
  9. "errors"
  10. "fmt"
  11. "log"
  12. "strings"
  13. "github.com/go-xorm/xorm"
  14. "go.mod/models"
  15. )
  16. //数据库操作引擎
  17. type TreeContractDao struct {
  18. engine *xorm.Engine
  19. }
  20. //获得一个DAO对象
  21. func NewTreeContractDao(engine *xorm.Engine) *TreeContractDao {
  22. return &TreeContractDao{
  23. engine: engine,
  24. }
  25. }
  26. // 获得本项目的合同项目节
  27. func (d *TreeContractDao) Get(treeId int, bidsectionId int, projectId int, treeType int) *models.CmTreeContracts {
  28. data := &models.CmTreeContracts{}
  29. _, err := d.engine.
  30. Where("tree_id=? and bidsection_id =? and project_id=? and tree_type=?", treeId, bidsectionId, projectId, treeType).
  31. Get(data)
  32. if err != nil {
  33. data.Id = 0
  34. return data
  35. }
  36. return data
  37. }
  38. // 获得项目下的项目节
  39. func (d *TreeContractDao) GetAll(bidsectionId int, projectId int, treeType int) []models.CmTreeContracts {
  40. datalist := make([]models.CmTreeContracts, 0)
  41. err := d.engine.
  42. Asc("id").
  43. Where("bidsection_id =? and project_id=? and tree_type=?", bidsectionId, projectId, treeType).
  44. Limit(5000, 0).
  45. Find(&datalist)
  46. if err != nil {
  47. return datalist
  48. } else {
  49. return datalist
  50. }
  51. }
  52. // 根据项目ID和表单ID数组获得数据
  53. func (d *TreeContractDao) GetByProjectIdInBidsectionId(bids string, projectId int) []models.CmTreeContracts {
  54. datalist := make([]models.CmTreeContracts, 0)
  55. d.engine.
  56. Asc("id").
  57. Where(" project_id=? and bidsection_id IN ("+bids+")", projectId).
  58. Find(&datalist)
  59. return datalist
  60. }
  61. // 获得项目下的项目节不包含合同
  62. func (d *TreeContractDao) GetAllNotContract(bidsectionId int, projectId int, treeType int) []models.CmTreeContracts {
  63. datalist := make([]models.CmTreeContracts, 0)
  64. err := d.engine.
  65. Asc("id").
  66. Where("bidsection_id =? and project_id=? and tree_type=? and contract_id=0", bidsectionId, projectId, treeType).
  67. Limit(5000, 0).
  68. Find(&datalist)
  69. if err != nil {
  70. return datalist
  71. } else {
  72. return datalist
  73. }
  74. }
  75. // 获得最新ID
  76. func (d *TreeContractDao) GetLastId() *models.CmTreeContracts {
  77. data := &models.CmTreeContracts{}
  78. _, err := d.engine.
  79. Desc("id").
  80. Get(data)
  81. if err != nil {
  82. data.Id = 0
  83. return data
  84. }
  85. return data
  86. }
  87. // 获得项目节所有合同
  88. func (d *TreeContractDao) GetContractAll(bidsectionId int, projectId int) []models.CmTreeContracts {
  89. datalist := make([]models.CmTreeContracts, 0)
  90. err := d.engine.
  91. Asc("id").
  92. Where("bidsection_id =? and project_id=? and contract_id!=0 ", bidsectionId, projectId).
  93. Find(&datalist)
  94. if err != nil {
  95. return datalist
  96. } else {
  97. return datalist
  98. }
  99. }
  100. // 获得标段 项目节中已有的合同
  101. func (d *TreeContractDao) GetContract(bidsectionId int, projectId int, treeType int) []models.CmTreeContracts {
  102. datalist := make([]models.CmTreeContracts, 0)
  103. err := d.engine.
  104. Asc("id").
  105. Where("bidsection_id =? and project_id=? and contract_id!=0 and tree_type=?", bidsectionId, projectId, treeType).
  106. Find(&datalist)
  107. if err != nil {
  108. return datalist
  109. } else {
  110. return datalist
  111. }
  112. }
  113. // 获得节点的孩子
  114. func (d *TreeContractDao) GetChildren(parentId int, bidsectionId int, projectId int, treeType int) []models.CmTreeContracts {
  115. datalist := make([]models.CmTreeContracts, 0)
  116. err := d.engine.
  117. Asc("serial").
  118. Where("parent_id=? and bidsection_id=? and project_id=? and tree_type=?", parentId, bidsectionId, projectId, treeType).
  119. Find(&datalist)
  120. if err != nil {
  121. return datalist
  122. } else {
  123. return datalist
  124. }
  125. }
  126. // 获得节点的孩子-是项目节
  127. func (d *TreeContractDao) GetChildrenBySection(parentId int, bidsectionId int, projectId int, treeType int) []models.CmTreeContracts {
  128. datalist := make([]models.CmTreeContracts, 0)
  129. err := d.engine.
  130. Asc("serial").
  131. Where("parent_id=? and bidsection_id=? and project_id=? and tree_type=? and contract_id=0", parentId, bidsectionId, projectId, treeType).
  132. Find(&datalist)
  133. if err != nil {
  134. return datalist
  135. } else {
  136. return datalist
  137. }
  138. }
  139. //根据序号和深度获得前一个兄弟节点
  140. func (d *TreeContractDao) GetElderBrother(serial int, depth int, parentId int, bidsectionId int, projectId int, treeType int) []models.CmTreeContracts {
  141. datalist := make([]models.CmTreeContracts, 0)
  142. err := d.engine.
  143. Desc("serial").
  144. Where("serial < ? and depth = ? and parent_id =? and bidsection_id=? and project_id=? and tree_type=?", serial, depth, parentId, bidsectionId, projectId, treeType).
  145. Find(&datalist)
  146. if err != nil {
  147. return datalist
  148. } else {
  149. return datalist
  150. }
  151. }
  152. //根据序号和深度获得后一个兄弟节点
  153. func (d *TreeContractDao) GetYoungerBrother(serial int, depth int, parentId int, bidsectionId int, projectId int, treeType int) []models.CmTreeContracts {
  154. datalist := make([]models.CmTreeContracts, 0)
  155. err := d.engine.
  156. Asc("serial").
  157. Where("serial > ? and depth = ? and parent_id =? and bidsection_id=? and project_id=? and tree_type=?", serial, depth, parentId, bidsectionId, projectId, treeType).
  158. Find(&datalist)
  159. if err != nil {
  160. return datalist
  161. } else {
  162. return datalist
  163. }
  164. }
  165. // 获得最后一条项目节
  166. func (d *TreeContractDao) GetLast(projectId int, treeType int) *models.CmTreeContracts {
  167. data := &models.CmTreeContracts{}
  168. _, err := d.engine.
  169. Desc("id").
  170. Where("project_id=? and tree_type=?", projectId, treeType).
  171. Get(data)
  172. if err != nil {
  173. data.Id = 0
  174. return data
  175. }
  176. return data
  177. }
  178. // 获得谋归属下的项目节
  179. func (d *TreeContractDao) GetAttribution(attribution string, projectId int, bidsectionId int, treeType int) []models.CmTreeContracts {
  180. datalist := make([]models.CmTreeContracts, 0)
  181. err := d.engine.
  182. Asc("serial").
  183. Where("attribution like ? and project_id=? and bidsection_id=? and tree_type=?", attribution+"%", projectId, bidsectionId, treeType).
  184. Find(&datalist)
  185. if err != nil {
  186. return datalist
  187. } else {
  188. return datalist
  189. }
  190. }
  191. // 获得谋归属下的项目节-合同
  192. func (d *TreeContractDao) GetAttributionContract(section *models.CmTreeContracts, treeType int) []models.CmTreeContracts {
  193. // attribution := section.Attribution
  194. attribution := fmt.Sprintf("%s-", section.Code)
  195. projectId := section.ProjectId
  196. bidsectionId := section.BidsectionId
  197. datalist := make([]models.CmTreeContracts, 0)
  198. err := d.engine.
  199. Asc("serial").
  200. Where("(attribution like ? or id =? ) and project_id=? and bidsection_id=? and tree_type=? and contract_id!=0", attribution+"%", section.Id, projectId, bidsectionId, treeType).
  201. Find(&datalist)
  202. if err != nil {
  203. return datalist
  204. } else {
  205. return datalist
  206. }
  207. }
  208. // 项目节升降级
  209. func (d *TreeContractDao) MoveDepth(section *models.CmTreeContracts, elderBrother *models.CmTreeContracts, operation string, bidsectionId int, projectId int, treeType int) error {
  210. session := d.engine.NewSession()
  211. defer session.Close()
  212. err := session.Begin()
  213. if err != nil {
  214. return errors.New("操作失败-db")
  215. }
  216. // 降级
  217. if operation == "downDepth" {
  218. // 1.前一个兄弟节点
  219. // fmt.Println(elderBrother)
  220. // 2.把节点移动到兄弟节点的下级,成为兄弟的孩子
  221. // 2-1 节点的父亲为兄弟的ID
  222. // 2-2 节点的深度 +1
  223. // 2-3 节点归属为兄弟归属+序号
  224. attribution := fmt.Sprintf("%s%d-", elderBrother.Attribution, elderBrother.Serial)
  225. // 2-4 序号 没有孩子序号为1,有孩子 最大孩子序号+1
  226. // 2-4-1 获得上一位哥的孩子们
  227. elderBrotherChildren := d.GetChildren(elderBrother.TreeId, bidsectionId, projectId, treeType)
  228. serial := 1
  229. if len(elderBrotherChildren) != 0 {
  230. serial = elderBrotherChildren[len(elderBrotherChildren)-1].Serial + 1
  231. }
  232. // 2-5 项目节编号
  233. // 原编号
  234. // section.Code
  235. // 移动后编号
  236. moveCode := fmt.Sprintf("%s%d", attribution, serial)
  237. _, err = session.Exec("UPDATE cm_tree_contracts SET `parent_id` = ?,attribution= ? , serial = ? ,`code` = replace(`code`, '"+section.Code+"', '"+moveCode+"')"+
  238. ",`depth` =`depth` + ? where id = ? and tree_type=? ", elderBrother.TreeId, attribution, serial, 1, section.Id, treeType)
  239. if err != nil {
  240. session.Rollback()
  241. return errors.New("降级失败")
  242. }
  243. // 3.更新节点下的归属
  244. // 3-1 节点的所有孩子的归属
  245. // 原节点 孩子的归属
  246. attributionChildren := fmt.Sprintf("%s%d-", section.Attribution, section.Serial)
  247. // 降级后的 孩子归属
  248. moveAttributionChildren := fmt.Sprintf("%s%d-", attribution, serial)
  249. // 3-2 降级 深度+1
  250. _, err = session.Exec("UPDATE cm_tree_contracts SET "+
  251. "`depth` =`depth` + ? where attribution like ? and project_id=? and bidsection_id=? and tree_type=? ", 1, attributionChildren+"%", projectId, bidsectionId, treeType)
  252. if err != nil {
  253. session.Rollback()
  254. return errors.New("降级失败")
  255. }
  256. // 3-3--替换 归属和编号
  257. // "`attribution` = replace(`attribution`, '"+attributionChildren+"', '"+moveAttributionChildren+"') ,`code` = replace(`code`, '"+section.Code+"', '"+moveCode+"'),"
  258. err = d.replaceContractAttribution(session, attributionChildren, moveAttributionChildren, section.Code, moveCode, projectId, bidsectionId, treeType)
  259. if err != nil {
  260. session.Rollback()
  261. return errors.New("降级失败")
  262. }
  263. } else if operation == "upDepth" {
  264. // 升级
  265. // 1.父亲节点
  266. sectionFather := d.Get(section.ParentId, bidsectionId, projectId, treeType)
  267. if sectionFather.Id == 0 {
  268. session.Rollback()
  269. return errors.New("升级-未找到上级项目节")
  270. }
  271. // 2.原节点的父亲ID字段升级为爷爷ID
  272. // 2-1 升级 深度-1
  273. // 2-2 序号 爷爷的孩子们的 序号+1
  274. grandpaChildren := d.GetChildren(sectionFather.ParentId, bidsectionId, projectId, treeType)
  275. serial := 1
  276. if len(grandpaChildren) != 0 {
  277. serial = grandpaChildren[len(grandpaChildren)-1].Serial + 1
  278. }
  279. // 2-3 归属 原父亲的归属
  280. // 移动后编号-需替换原编号 节点2-1-1 升级后的父节点归属 2- 加上爷爷最大孩子的序号+1
  281. moveCode := fmt.Sprintf("%s%d", sectionFather.Attribution, serial)
  282. // 升级的项目节
  283. _, err = session.Exec("UPDATE cm_tree_contracts SET `parent_id` = ?,attribution= ? , serial = ? ,`code` = replace(`code`, '"+section.Code+"', '"+moveCode+"')"+
  284. ",`depth` =`depth` - ? where id = ? and tree_type=? ", sectionFather.ParentId, sectionFather.Attribution, serial, 1, section.Id, treeType)
  285. if err != nil {
  286. session.Rollback()
  287. return errors.New("升级失败")
  288. }
  289. // 3.更新节点下的归属,深度
  290. // 原节点 孩子的归属
  291. attributionChildren := fmt.Sprintf("%s%d-", section.Attribution, section.Serial)
  292. // 升级后的 孩子归属
  293. moveAttributionChildren := fmt.Sprintf("%s%d-", sectionFather.Attribution, serial)
  294. // 深度 -1
  295. _, err = session.Exec("UPDATE cm_tree_contracts SET `attribution` = replace(`attribution`, '"+attributionChildren+"', '"+moveAttributionChildren+"') "+
  296. ",`depth` =`depth` - ? where attribution like ? and project_id=? and bidsection_id=? and tree_type=? ", 1, attributionChildren+"%", projectId, bidsectionId, treeType)
  297. if err != nil {
  298. session.Rollback()
  299. return errors.New("升级失败")
  300. }
  301. // 3-1
  302. err = d.replaceContractAttribution(session, attributionChildren, moveAttributionChildren, section.Code, moveCode, projectId, bidsectionId, treeType)
  303. if err != nil {
  304. session.Rollback()
  305. return errors.New("升级失败")
  306. }
  307. } else {
  308. return errors.New("参数错误")
  309. }
  310. err = session.Commit()
  311. if err != nil {
  312. session.Rollback()
  313. return errors.New("操作失败-db")
  314. }
  315. return nil
  316. }
  317. // 合同项目节上下移动
  318. func (d *TreeContractDao) MoveSerial(section *models.CmTreeContracts, brother *models.CmTreeContracts, operation string, bidsectionId int, projectId int, treeType int) error {
  319. session := d.engine.NewSession()
  320. defer session.Close()
  321. err := session.Begin()
  322. if err != nil {
  323. return errors.New("操作失败-db")
  324. }
  325. //1.上下移
  326. // 1.项目节序号替换为兄弟序号
  327. _, err = session.Exec("UPDATE cm_tree_contracts SET serial = ? , `code` = replace(`code`, '"+section.Code+"', '"+brother.Code+"') where id = ? and tree_type=? ", brother.Serial, section.Id, treeType)
  328. if err != nil {
  329. session.Rollback()
  330. return errors.New("移动失败")
  331. }
  332. // 兄弟序号替换为项目节序号
  333. _, err = session.Exec("UPDATE cm_tree_contracts SET serial = ? , `code` = replace(`code`, '"+brother.Code+"', '"+section.Code+"') where id = ? and tree_type=? ", section.Serial, brother.Id, treeType)
  334. if err != nil {
  335. session.Rollback()
  336. return errors.New("移动失败")
  337. }
  338. //2.项目节孩子们 归属设置
  339. // 原节点 孩子的归属
  340. attributionChildren := fmt.Sprintf("%s%d-", section.Attribution, section.Serial)
  341. // 移动后的 孩子归属和编号
  342. moveAttributionChildren := fmt.Sprintf("%s%d-", brother.Attribution, brother.Serial)
  343. err = d.replaceContractAttribution(session, attributionChildren, moveAttributionChildren, section.Code, brother.Code, projectId, bidsectionId, treeType)
  344. if err != nil {
  345. session.Rollback()
  346. return errors.New("移动失败")
  347. }
  348. // _, err = session.Exec("UPDATE cm_tree_contracts SET `attribution` = replace(`attribution`, '"+attributionChildren+"', '"+moveAttributionChildren+"') "+
  349. // "`code` = replace(`code`, '"+attributionChildren+"', '"+moveAttributionChildren+"') where attribution like ? and project_id=? and bidsection_id=? ", attributionChildren+"%", projectId, bidsectionId)
  350. // if err != nil {
  351. // session.Rollback()
  352. // return errors.New("移动失败")
  353. // }
  354. // 3.兄弟节点孩子们 归属设置
  355. // 兄弟节点 孩子的归属
  356. attributionChildren = fmt.Sprintf("%s%d-", brother.Attribution, brother.Serial)
  357. // 移动后的 孩子归属
  358. moveAttributionChildren = fmt.Sprintf("%s%d-", section.Attribution, section.Serial)
  359. err = d.replaceContractAttribution(session, attributionChildren, moveAttributionChildren, brother.Code, section.Code, projectId, bidsectionId, treeType)
  360. if err != nil {
  361. session.Rollback()
  362. return errors.New("移动失败")
  363. }
  364. err = session.Commit()
  365. if err != nil {
  366. session.Rollback()
  367. return errors.New("操作失败-db")
  368. }
  369. return nil
  370. }
  371. // 修改项目节序号
  372. func (d *TreeContractDao) UpdateSerial(section *models.CmTreeContracts, serial int, treeType int) error {
  373. session := d.engine.NewSession()
  374. defer session.Close()
  375. err := session.Begin()
  376. if err != nil {
  377. return errors.New("操作失败-db")
  378. }
  379. // 1.更新项目节序号和项目节编号
  380. moveCode := fmt.Sprintf("%s%d", section.Attribution, serial)
  381. _, err = session.Exec("UPDATE cm_tree_contracts SET serial = ? , `code` = ? where id = ? and tree_type=? ", serial, moveCode, section.Id, treeType)
  382. if err != nil {
  383. session.Rollback()
  384. log.Println("合同项目节序号更新 error=", err)
  385. return errors.New("更新序号失败")
  386. }
  387. // 2.更新项目节子孙们的序号和归属
  388. // 2-1归属 项目节 孩子归属
  389. attributionChildren := fmt.Sprintf("%s%d-", section.Attribution, section.Serial)
  390. // 2-2 序号更新后的 孩子归属
  391. moveAttributionChildren := fmt.Sprintf("%s%d-", section.Attribution, serial)
  392. // 2-3 项目节 孩子的编号
  393. code := fmt.Sprintf("%s%d", section.Attribution, section.Serial)
  394. err = d.replaceContractAttribution(session, attributionChildren, moveAttributionChildren, code, moveCode, section.ProjectId, section.BidsectionId, treeType)
  395. if err != nil {
  396. session.Rollback()
  397. log.Println("合同项目节序号更新 error=", err)
  398. return errors.New("更新序号失败")
  399. }
  400. err = session.Commit()
  401. if err != nil {
  402. session.Rollback()
  403. return errors.New("操作失败-db")
  404. }
  405. return nil
  406. }
  407. // 插入多条数据
  408. func (d *TreeContractDao) CreateAll(data []*models.CmTreeContracts) error {
  409. session := d.engine.NewSession()
  410. defer session.Close()
  411. err := session.Begin()
  412. if err != nil {
  413. return errors.New("新增失败-db")
  414. }
  415. // 新增
  416. for _, item := range data {
  417. _, err := session.Insert(item)
  418. if err != nil {
  419. log.Println(" error=", err)
  420. session.Rollback()
  421. return errors.New("新增失败")
  422. }
  423. }
  424. err = session.Commit()
  425. if err != nil {
  426. session.Rollback()
  427. return errors.New("新增失败-db")
  428. }
  429. return nil
  430. }
  431. // 新增项目节
  432. func (d *TreeContractDao) Create(data *models.CmTreeContracts) (*models.CmTreeContracts, error) {
  433. session := d.engine.NewSession()
  434. defer session.Close()
  435. err := session.Begin()
  436. if err != nil {
  437. return nil, errors.New("新增失败-db")
  438. }
  439. _, err = session.Insert(data)
  440. if err != nil {
  441. log.Println(" error=", err)
  442. return nil, errors.New("新增失败")
  443. }
  444. // 插入成功后,ID自动赋值到data.Id里
  445. // fmt.Println("=======================================")
  446. // fmt.Println(data)
  447. // 更新合同节树ID
  448. // data.TreeId = data.Id
  449. // _, err = session.Id(data.Id).Update(data)
  450. // if err != nil {
  451. // log.Println(" error=", err)
  452. // return errors.New("新增失败")
  453. // }
  454. err = session.Commit()
  455. if err != nil {
  456. session.Rollback()
  457. return nil, errors.New("新增失败-db")
  458. }
  459. return data, nil
  460. }
  461. // 保存项目节
  462. func (d *TreeContractDao) Save(section *models.CmTreeContracts, columns []string) error {
  463. _, err := d.engine.Id(section.Id).MustCols(columns...).Update(section)
  464. if err != nil {
  465. return errors.New("保存失败")
  466. }
  467. return nil
  468. }
  469. // 删除项目节
  470. func (d *TreeContractDao) Delete(section *models.CmTreeContracts) error {
  471. session := d.engine.NewSession()
  472. defer session.Close()
  473. err := session.Begin()
  474. if err != nil {
  475. return errors.New("删除失败-db")
  476. }
  477. // 1. 删除项目节
  478. _, err = session.Where("id = ? ", section.Id).Delete(section)
  479. if err != nil {
  480. session.Rollback()
  481. return errors.New("删除失败")
  482. }
  483. // 孩子们的归属
  484. attribution := fmt.Sprintf("%s%d-", section.Attribution, section.Serial)
  485. // 2. 删除项目节孩子们
  486. _, err = session.Exec("DELETE FROM `cm_tree_contracts` WHERE attribution like ? and project_id=? and bidsection_id=? and tree_type=? ", attribution+"%", section.ProjectId, section.BidsectionId, section.TreeType)
  487. if err != nil {
  488. session.Rollback()
  489. return errors.New("删除失败")
  490. }
  491. err = session.Commit()
  492. if err != nil {
  493. session.Rollback()
  494. return errors.New("删除失败-db")
  495. }
  496. return nil
  497. }
  498. //替换项目节归属
  499. func (d *TreeContractDao) replaceContractAttribution(session *xorm.Session, attributionChildren string, moveAttributionChildren string, code string, moveCode string, projectId int, bidsectionId int, treeType int) error {
  500. // 1.获得需要替换的数据
  501. sectionData := d.GetAttribution(attributionChildren, projectId, bidsectionId, treeType)
  502. if len(sectionData) == 0 {
  503. return nil
  504. }
  505. attributionSql := " attribution = case id "
  506. codeSql := " code = case id "
  507. idList := make([]int, 0)
  508. for _, item := range sectionData {
  509. // section := &models.CmTreeContracts{}
  510. // section.Id = item.Id
  511. // 替换归属
  512. attributionSql += fmt.Sprintf("when %d then '%s' ", item.Id, strings.Replace(item.Attribution, attributionChildren, moveAttributionChildren, 1))
  513. //section.Attribution = strings.Replace(item.Attribution, attributionChildren, moveAttributionChildren, 1)
  514. // 替换编号
  515. codeSql += fmt.Sprintf("when %d then '%s' ", item.Id, strings.Replace(item.Code, code, moveCode, 1))
  516. // section.Code = strings.Replace(item.Code, code, moveCode, 1)
  517. idList = append(idList, item.Id)
  518. }
  519. attributionSql += " end, "
  520. codeSql += " end "
  521. id := strings.Replace(strings.Trim(fmt.Sprint(idList), "[]"), " ", ",", -1)
  522. sql := "update cm_tree_contracts set " + attributionSql + codeSql + " WHERE id IN (" + id + ")"
  523. _, err := session.Exec(sql)
  524. if err != nil {
  525. log.Println("替换项目节归属, error=", err)
  526. return err
  527. }
  528. return nil
  529. }