tree_contract_dao.go 17 KB

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