tree_contract_dao.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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) GetAllNotContract(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) GetAttributionContract(attribution string, projectId int, bidsectionId int, treeType int) []models.CmTreeContracts {
  171. datalist := make([]models.CmTreeContracts, 0)
  172. err := d.engine.
  173. Asc("serial").
  174. Where("attribution like ? and project_id=? and bidsection_id=? and tree_type=? and contract_id!=0", attribution+"%", projectId, bidsectionId, treeType).
  175. Find(&datalist)
  176. if err != nil {
  177. return datalist
  178. } else {
  179. return datalist
  180. }
  181. }
  182. // 项目节升降级
  183. func (d *TreeContractDao) MoveDepth(section *models.CmTreeContracts, elderBrother *models.CmTreeContracts, operation string, bidsectionId int, projectId int, treeType int) error {
  184. session := d.engine.NewSession()
  185. defer session.Close()
  186. err := session.Begin()
  187. if err != nil {
  188. return errors.New("操作失败-db")
  189. }
  190. // 降级
  191. if operation == "downDepth" {
  192. // 1.前一个兄弟节点
  193. // fmt.Println(elderBrother)
  194. // 2.把节点移动到兄弟节点的下级,成为兄弟的孩子
  195. // 2-1 节点的父亲为兄弟的ID
  196. // 2-2 节点的深度 +1
  197. // 2-3 节点归属为兄弟归属+序号
  198. attribution := fmt.Sprintf("%s%d-", elderBrother.Attribution, elderBrother.Serial)
  199. // 2-4 序号 没有孩子序号为1,有孩子 最大孩子序号+1
  200. // 2-4-1 获得上一位哥的孩子们
  201. elderBrotherChildren := d.GetChildren(elderBrother.TreeId, bidsectionId, projectId, treeType)
  202. serial := 1
  203. if len(elderBrotherChildren) != 0 {
  204. serial = elderBrotherChildren[len(elderBrotherChildren)-1].Serial + 1
  205. }
  206. // 2-5 项目节编号
  207. // 原编号
  208. // section.Code
  209. // 移动后编号
  210. moveCode := fmt.Sprintf("%s%d", attribution, serial)
  211. _, err = session.Exec("UPDATE cm_tree_contracts SET `parent_id` = ?,attribution= ? , serial = ? ,`code` = replace(`code`, '"+section.Code+"', '"+moveCode+"')"+
  212. ",`depth` =`depth` + ? where id = ? and tree_type=? ", elderBrother.TreeId, attribution, serial, 1, section.Id, treeType)
  213. if err != nil {
  214. session.Rollback()
  215. return errors.New("降级失败")
  216. }
  217. // 3.更新节点下的归属
  218. // 3-1 节点的所有孩子的归属
  219. // 原节点 孩子的归属
  220. attributionChildren := fmt.Sprintf("%s%d-", section.Attribution, section.Serial)
  221. // 降级后的 孩子归属
  222. moveAttributionChildren := fmt.Sprintf("%s%d-", attribution, serial)
  223. // 3-2 降级 深度+1
  224. _, err = session.Exec("UPDATE cm_tree_contracts SET "+
  225. "`depth` =`depth` + ? where attribution like ? and project_id=? and bidsection_id=? and tree_type=? ", 1, attributionChildren+"%", projectId, bidsectionId, treeType)
  226. if err != nil {
  227. session.Rollback()
  228. return errors.New("降级失败")
  229. }
  230. // 3-3--替换 归属和编号
  231. // "`attribution` = replace(`attribution`, '"+attributionChildren+"', '"+moveAttributionChildren+"') ,`code` = replace(`code`, '"+section.Code+"', '"+moveCode+"'),"
  232. err = d.replaceContractAttribution(session, attributionChildren, moveAttributionChildren, section.Code, moveCode, projectId, bidsectionId, treeType)
  233. if err != nil {
  234. session.Rollback()
  235. return errors.New("降级失败")
  236. }
  237. } else if operation == "upDepth" {
  238. // 升级
  239. // 1.父亲节点
  240. sectionFather := d.Get(section.ParentId, bidsectionId, projectId, treeType)
  241. if sectionFather.Id == 0 {
  242. session.Rollback()
  243. return errors.New("升级-未找到上级项目节")
  244. }
  245. // 2.原节点的父亲ID字段升级为爷爷ID
  246. // 2-1 升级 深度-1
  247. // 2-2 序号 爷爷的孩子们的 序号+1
  248. grandpaChildren := d.GetChildren(sectionFather.ParentId, bidsectionId, projectId, treeType)
  249. serial := 1
  250. if len(grandpaChildren) != 0 {
  251. serial = grandpaChildren[len(grandpaChildren)-1].Serial + 1
  252. }
  253. // 2-3 归属 原父亲的归属
  254. // 移动后编号-需替换原编号 节点2-1-1 升级后的父节点归属 2- 加上爷爷最大孩子的序号+1
  255. moveCode := fmt.Sprintf("%s%d", sectionFather.Attribution, serial)
  256. // 升级的项目节
  257. _, err = session.Exec("UPDATE cm_tree_contracts SET `parent_id` = ?,attribution= ? , serial = ? ,`code` = replace(`code`, '"+section.Code+"', '"+moveCode+"')"+
  258. ",`depth` =`depth` - ? where id = ? and tree_type=? ", sectionFather.ParentId, sectionFather.Attribution, serial, 1, section.Id, treeType)
  259. if err != nil {
  260. session.Rollback()
  261. return errors.New("升级失败")
  262. }
  263. // 3.更新节点下的归属,深度
  264. // 原节点 孩子的归属
  265. attributionChildren := fmt.Sprintf("%s%d-", section.Attribution, section.Serial)
  266. // 升级后的 孩子归属
  267. moveAttributionChildren := fmt.Sprintf("%s%d-", sectionFather.Attribution, serial)
  268. // 深度 -1
  269. _, err = session.Exec("UPDATE cm_tree_contracts SET `attribution` = replace(`attribution`, '"+attributionChildren+"', '"+moveAttributionChildren+"') "+
  270. ",`depth` =`depth` - ? where attribution like ? and project_id=? and bidsection_id=? and tree_type=? ", 1, attributionChildren+"%", projectId, bidsectionId, treeType)
  271. if err != nil {
  272. session.Rollback()
  273. return errors.New("升级失败")
  274. }
  275. // 3-1
  276. err = d.replaceContractAttribution(session, attributionChildren, moveAttributionChildren, section.Code, moveCode, projectId, bidsectionId, treeType)
  277. if err != nil {
  278. session.Rollback()
  279. return errors.New("升级失败")
  280. }
  281. } else {
  282. return errors.New("参数错误")
  283. }
  284. err = session.Commit()
  285. if err != nil {
  286. session.Rollback()
  287. return errors.New("操作失败-db")
  288. }
  289. return nil
  290. }
  291. // 合同项目节上下移动
  292. func (d *TreeContractDao) MoveSerial(section *models.CmTreeContracts, brother *models.CmTreeContracts, operation string, bidsectionId int, projectId int, treeType int) error {
  293. session := d.engine.NewSession()
  294. defer session.Close()
  295. err := session.Begin()
  296. if err != nil {
  297. return errors.New("操作失败-db")
  298. }
  299. //1.上下移
  300. // 1.项目节序号替换为兄弟序号
  301. _, 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)
  302. if err != nil {
  303. session.Rollback()
  304. return errors.New("移动失败")
  305. }
  306. // 兄弟序号替换为项目节序号
  307. _, 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)
  308. if err != nil {
  309. session.Rollback()
  310. return errors.New("移动失败")
  311. }
  312. //2.项目节孩子们 归属设置
  313. // 原节点 孩子的归属
  314. attributionChildren := fmt.Sprintf("%s%d-", section.Attribution, section.Serial)
  315. // 移动后的 孩子归属和编号
  316. moveAttributionChildren := fmt.Sprintf("%s%d-", brother.Attribution, brother.Serial)
  317. err = d.replaceContractAttribution(session, attributionChildren, moveAttributionChildren, section.Code, brother.Code, projectId, bidsectionId, treeType)
  318. if err != nil {
  319. session.Rollback()
  320. return errors.New("移动失败")
  321. }
  322. // _, err = session.Exec("UPDATE cm_tree_contracts SET `attribution` = replace(`attribution`, '"+attributionChildren+"', '"+moveAttributionChildren+"') "+
  323. // "`code` = replace(`code`, '"+attributionChildren+"', '"+moveAttributionChildren+"') where attribution like ? and project_id=? and bidsection_id=? ", attributionChildren+"%", projectId, bidsectionId)
  324. // if err != nil {
  325. // session.Rollback()
  326. // return errors.New("移动失败")
  327. // }
  328. // 3.兄弟节点孩子们 归属设置
  329. // 兄弟节点 孩子的归属
  330. attributionChildren = fmt.Sprintf("%s%d-", brother.Attribution, brother.Serial)
  331. // 移动后的 孩子归属
  332. moveAttributionChildren = fmt.Sprintf("%s%d-", section.Attribution, section.Serial)
  333. err = d.replaceContractAttribution(session, attributionChildren, moveAttributionChildren, brother.Code, section.Code, projectId, bidsectionId, treeType)
  334. if err != nil {
  335. session.Rollback()
  336. return errors.New("移动失败")
  337. }
  338. err = session.Commit()
  339. if err != nil {
  340. session.Rollback()
  341. return errors.New("操作失败-db")
  342. }
  343. return nil
  344. }
  345. // 修改项目节序号
  346. func (d *TreeContractDao) UpdateSerial(section *models.CmTreeContracts, serial int, treeType int) error {
  347. session := d.engine.NewSession()
  348. defer session.Close()
  349. err := session.Begin()
  350. if err != nil {
  351. return errors.New("操作失败-db")
  352. }
  353. // 1.更新项目节序号和项目节编号
  354. moveCode := fmt.Sprintf("%s%d", section.Attribution, serial)
  355. _, err = session.Exec("UPDATE cm_tree_contracts SET serial = ? , `code` = ? where id = ? and tree_type=? ", serial, moveCode, section.Id, treeType)
  356. if err != nil {
  357. session.Rollback()
  358. log.Println("合同项目节序号更新 error=", err)
  359. return errors.New("更新序号失败")
  360. }
  361. // 2.更新项目节子孙们的序号和归属
  362. // 2-1归属 项目节 孩子归属
  363. attributionChildren := fmt.Sprintf("%s%d-", section.Attribution, section.Serial)
  364. // 2-2 序号更新后的 孩子归属
  365. moveAttributionChildren := fmt.Sprintf("%s%d-", section.Attribution, serial)
  366. // 2-3 项目节 孩子的编号
  367. code := fmt.Sprintf("%s%d", section.Attribution, section.Serial)
  368. err = d.replaceContractAttribution(session, attributionChildren, moveAttributionChildren, code, moveCode, section.ProjectId, section.BidsectionId, treeType)
  369. if err != nil {
  370. session.Rollback()
  371. log.Println("合同项目节序号更新 error=", err)
  372. return errors.New("更新序号失败")
  373. }
  374. err = session.Commit()
  375. if err != nil {
  376. session.Rollback()
  377. return errors.New("操作失败-db")
  378. }
  379. return nil
  380. }
  381. // 插入多条数据
  382. func (d *TreeContractDao) CreateAll(data []*models.CmTreeContracts) error {
  383. session := d.engine.NewSession()
  384. defer session.Close()
  385. err := session.Begin()
  386. if err != nil {
  387. return errors.New("新增失败-db")
  388. }
  389. // 新增
  390. for _, item := range data {
  391. _, err := session.Insert(item)
  392. if err != nil {
  393. log.Println(" error=", err)
  394. session.Rollback()
  395. return errors.New("新增失败")
  396. }
  397. }
  398. err = session.Commit()
  399. if err != nil {
  400. session.Rollback()
  401. return errors.New("新增失败-db")
  402. }
  403. return nil
  404. }
  405. // 新增项目节
  406. func (d *TreeContractDao) Create(data *models.CmTreeContracts) (*models.CmTreeContracts, error) {
  407. session := d.engine.NewSession()
  408. defer session.Close()
  409. err := session.Begin()
  410. if err != nil {
  411. return nil, errors.New("新增失败-db")
  412. }
  413. _, err = session.Insert(data)
  414. if err != nil {
  415. log.Println(" error=", err)
  416. return nil, errors.New("新增失败")
  417. }
  418. // 插入成功后,ID自动赋值到data.Id里
  419. // fmt.Println("=======================================")
  420. // fmt.Println(data)
  421. // 更新合同节树ID
  422. // data.TreeId = data.Id
  423. // _, err = session.Id(data.Id).Update(data)
  424. // if err != nil {
  425. // log.Println(" error=", err)
  426. // return errors.New("新增失败")
  427. // }
  428. err = session.Commit()
  429. if err != nil {
  430. session.Rollback()
  431. return nil, errors.New("新增失败-db")
  432. }
  433. return data, nil
  434. }
  435. // 保存项目节
  436. func (d *TreeContractDao) Save(section *models.CmTreeContracts, columns []string) error {
  437. _, err := d.engine.Id(section.Id).MustCols(columns...).Update(section)
  438. if err != nil {
  439. return errors.New("保存失败")
  440. }
  441. return nil
  442. }
  443. // 删除项目节
  444. func (d *TreeContractDao) Delete(section *models.CmTreeContracts) error {
  445. session := d.engine.NewSession()
  446. defer session.Close()
  447. err := session.Begin()
  448. if err != nil {
  449. return errors.New("删除失败-db")
  450. }
  451. // 1. 删除项目节
  452. _, err = session.Where("id = ? ", section.Id).Delete(section)
  453. if err != nil {
  454. session.Rollback()
  455. return errors.New("删除失败")
  456. }
  457. // 孩子们的归属
  458. attribution := fmt.Sprintf("%s%d-", section.Attribution, section.Serial)
  459. // 2. 删除项目节孩子们
  460. _, 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)
  461. if err != nil {
  462. session.Rollback()
  463. return errors.New("删除失败")
  464. }
  465. err = session.Commit()
  466. if err != nil {
  467. session.Rollback()
  468. return errors.New("删除失败-db")
  469. }
  470. return nil
  471. }
  472. //替换项目节归属
  473. func (d *TreeContractDao) replaceContractAttribution(session *xorm.Session, attributionChildren string, moveAttributionChildren string, code string, moveCode string, projectId int, bidsectionId int, treeType int) error {
  474. // 1.获得需要替换的数据
  475. sectionData := d.GetAttribution(attributionChildren, projectId, bidsectionId, treeType)
  476. if len(sectionData) == 0 {
  477. return nil
  478. }
  479. attributionSql := " attribution = case id "
  480. codeSql := " code = case id "
  481. idList := make([]int, 0)
  482. for _, item := range sectionData {
  483. // section := &models.CmTreeContracts{}
  484. // section.Id = item.Id
  485. // 替换归属
  486. attributionSql += fmt.Sprintf("when %d then '%s' ", item.Id, strings.Replace(item.Attribution, attributionChildren, moveAttributionChildren, 1))
  487. //section.Attribution = strings.Replace(item.Attribution, attributionChildren, moveAttributionChildren, 1)
  488. // 替换编号
  489. codeSql += fmt.Sprintf("when %d then '%s' ", item.Id, strings.Replace(item.Code, code, moveCode, 1))
  490. // section.Code = strings.Replace(item.Code, code, moveCode, 1)
  491. idList = append(idList, item.Id)
  492. }
  493. attributionSql += " end, "
  494. codeSql += " end "
  495. id := strings.Replace(strings.Trim(fmt.Sprint(idList), "[]"), " ", ",", -1)
  496. sql := "update cm_tree_contracts set " + attributionSql + codeSql + " WHERE id IN (" + id + ")"
  497. _, err := session.Exec(sql)
  498. if err != nil {
  499. log.Println("替换项目节归属, error=", err)
  500. return err
  501. }
  502. return nil
  503. }