tree_contract_dao.go 18 KB

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