tree_contract_dao.go 17 KB

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