tree_contract_dao.go 18 KB

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