tree_contract_dao.go 15 KB

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