tree_contract_dao.go 15 KB

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