tree_contract_dao.go 18 KB

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