tree_contract_dao.go 17 KB

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