tree_contract_dao.go 18 KB

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