tree_contract_dao.go 19 KB

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