tree_contract_dao.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 bidsection_id=? and project_id=?", serial, depth, 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 bidsection_id=? and project_id=?", serial, depth, 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
  134. // 2-5 项目节编号
  135. // 原编号
  136. // section.Code
  137. // 移动后编号
  138. moveCode := attribution + "1"
  139. _, err = session.Exec("UPDATE cm_tree_contracts SET `parent_id` = ?,attribution= ? , serial = ? ,`code` = replace(`code`, '"+section.Code+"', '"+moveCode+"')"+
  140. ",`depth` =`depth` + ? where id = ? ", elderBrother.TreeId, attribution, 1, 1, section.Id)
  141. if err != nil {
  142. session.Rollback()
  143. return errors.New("降级失败")
  144. }
  145. // 3.更新节点下的归属
  146. // 3-1 节点的所有孩子的归属
  147. // 原节点 孩子的归属
  148. attributionChildren := fmt.Sprintf("%s%d-", section.Attribution, section.Serial)
  149. // 降级后的 孩子归属
  150. moveAttributionChildren := fmt.Sprintf("%s%d-", attribution, 1)
  151. // 3-2 降级 深度+1
  152. _, err = session.Exec("UPDATE cm_tree_contracts SET "+
  153. "`depth` =`depth` + ? where attribution like ? and project_id=? and bidsection_id=? ", 1, attributionChildren+"%", projectId, bidsectionId)
  154. if err != nil {
  155. session.Rollback()
  156. return errors.New("降级失败")
  157. }
  158. // 3-3--替换 归属和编号
  159. // "`attribution` = replace(`attribution`, '"+attributionChildren+"', '"+moveAttributionChildren+"') ,`code` = replace(`code`, '"+section.Code+"', '"+moveCode+"'),"
  160. err = d.replaceContractAttribution(session, attributionChildren, moveAttributionChildren, section.Code, moveCode, projectId, bidsectionId)
  161. if err != nil {
  162. session.Rollback()
  163. return errors.New("降级失败")
  164. }
  165. } else if operation == "upDepth" {
  166. // 升级
  167. // 1.父亲节点
  168. sectionFather := d.Get(section.ParentId, bidsectionId, projectId)
  169. if sectionFather.Id == 0 {
  170. session.Rollback()
  171. return errors.New("升级-未找到上级项目节")
  172. }
  173. // 2.原节点的父亲ID字段升级为爷爷ID
  174. // 2-1 升级 深度-1
  175. // 2-2 序号 原父亲的序号+1
  176. // 2-3 归属 原父亲的归属
  177. // 移动后编号-需替换原编号 节点2-1-1 升级后的父节点归属 2- 加上父节点的序号+1 1+1=2 2-2
  178. moveCode := fmt.Sprintf("%s%d", sectionFather.Attribution, sectionFather.Serial+1)
  179. _, err = session.Exec("UPDATE cm_tree_contracts SET `parent_id` = ?,attribution= ? , serial = ? ,`code` = replace(`code`, '"+section.Code+"', '"+moveCode+"')"+
  180. ",`depth` =`depth` - ? where id = ? ", sectionFather.ParentId, sectionFather.Attribution, sectionFather.Serial+1, 1, section.Id)
  181. if err != nil {
  182. session.Rollback()
  183. return errors.New("升级失败")
  184. }
  185. // 3.更新节点下的归属,深度
  186. // 原节点 孩子的归属
  187. attributionChildren := fmt.Sprintf("%s%d-", section.Attribution, section.Serial)
  188. // 升级后的 孩子归属
  189. moveAttributionChildren := fmt.Sprintf("%s%d-", sectionFather.Attribution, sectionFather.Serial+1)
  190. // 深度 -1
  191. _, err = session.Exec("UPDATE cm_tree_contracts SET `attribution` = replace(`attribution`, '"+attributionChildren+"', '"+moveAttributionChildren+"') "+
  192. ",`depth` =`depth` - ? where attribution like ? and project_id=? and bidsection_id=? ", 1, attributionChildren+"%", projectId, bidsectionId)
  193. if err != nil {
  194. session.Rollback()
  195. return errors.New("升级失败")
  196. }
  197. // 3-1
  198. err = d.replaceContractAttribution(session, attributionChildren, moveAttributionChildren, section.Code, moveCode, projectId, bidsectionId)
  199. if err != nil {
  200. session.Rollback()
  201. return errors.New("升级失败")
  202. }
  203. } else {
  204. return errors.New("参数错误")
  205. }
  206. err = session.Commit()
  207. if err != nil {
  208. session.Rollback()
  209. return errors.New("操作失败-db")
  210. }
  211. return nil
  212. }
  213. // 合同项目节上下移动
  214. func (d *TreeContractDao) MoveSerial(section *models.CmTreeContracts, brother *models.CmTreeContracts, operation string, bidsectionId int, projectId int) error {
  215. session := d.engine.NewSession()
  216. defer session.Close()
  217. err := session.Begin()
  218. if err != nil {
  219. return errors.New("操作失败-db")
  220. }
  221. //1.上下移
  222. // 1.项目节序号替换为兄弟序号
  223. _, err = session.Exec("UPDATE cm_tree_contracts SET serial = ? , `code` = replace(`code`, '"+section.Code+"', '"+brother.Code+"') where id = ? ", brother.Serial, section.Id)
  224. if err != nil {
  225. session.Rollback()
  226. return errors.New("移动失败")
  227. }
  228. // 兄弟序号替换为项目节序号
  229. _, err = session.Exec("UPDATE cm_tree_contracts SET serial = ? , `code` = replace(`code`, '"+brother.Code+"', '"+section.Code+"') where id = ? ", section.Serial, brother.Id)
  230. if err != nil {
  231. session.Rollback()
  232. return errors.New("移动失败")
  233. }
  234. //2.项目节孩子们 归属设置
  235. // 原节点 孩子的归属
  236. attributionChildren := fmt.Sprintf("%s%d-", section.Attribution, section.Serial)
  237. // 移动后的 孩子归属和编号
  238. moveAttributionChildren := fmt.Sprintf("%s%d-", brother.Attribution, brother.Serial)
  239. err = d.replaceContractAttribution(session, attributionChildren, moveAttributionChildren, section.Code, brother.Code, projectId, bidsectionId)
  240. if err != nil {
  241. session.Rollback()
  242. return errors.New("移动失败")
  243. }
  244. // _, err = session.Exec("UPDATE cm_tree_contracts SET `attribution` = replace(`attribution`, '"+attributionChildren+"', '"+moveAttributionChildren+"') "+
  245. // "`code` = replace(`code`, '"+attributionChildren+"', '"+moveAttributionChildren+"') where attribution like ? and project_id=? and bidsection_id=? ", attributionChildren+"%", projectId, bidsectionId)
  246. // if err != nil {
  247. // session.Rollback()
  248. // return errors.New("移动失败")
  249. // }
  250. // 3.兄弟节点孩子们 归属设置
  251. // 兄弟节点 孩子的归属
  252. attributionChildren = fmt.Sprintf("%s%d-", brother.Attribution, brother.Serial)
  253. // 移动后的 孩子归属
  254. moveAttributionChildren = fmt.Sprintf("%s%d-", section.Attribution, section.Serial)
  255. err = d.replaceContractAttribution(session, attributionChildren, moveAttributionChildren, brother.Code, section.Code, projectId, bidsectionId)
  256. if err != nil {
  257. session.Rollback()
  258. return errors.New("移动失败")
  259. }
  260. // _, err = session.Exec("UPDATE cm_tree_contracts SET `attribution` = replace(`attribution`, '"+attributionChildren+"', '"+moveAttributionChildren+"') "+
  261. // "`code` = replace(`code`, '"+attributionChildren+"', '"+moveAttributionChildren+"') where attribution like ? and project_id=? and bidsection_id=? ", attributionChildren+"%", projectId, bidsectionId)
  262. // if err != nil {
  263. // session.Rollback()
  264. // return errors.New("移动失败")
  265. // }
  266. //
  267. // 上移
  268. // if operation == "upSerial" {
  269. // // 1.上一个大兄弟
  270. // // fmt.Println(brother)
  271. // // 2.项目节 序号 兄弟的序号
  272. // _, err = session.Exec("UPDATE cm_tree_contracts SET serial = ? "+
  273. // " where id = ? ", brother.Serial, section.Id)
  274. // if err != nil {
  275. // session.Rollback()
  276. // return errors.New("上移失败")
  277. // }
  278. // // 3.兄弟的序号为 项目节序号
  279. // _, err = session.Exec("UPDATE cm_tree_contracts SET serial = ? "+
  280. // " where id = ? ", section.Serial, brother.Id)
  281. // if err != nil {
  282. // session.Rollback()
  283. // return errors.New("上移失败")
  284. // }
  285. // } else if operation == "downSerial" {
  286. // // 下移
  287. // // 1.下一个兄弟
  288. // // fmt.Println(brother)
  289. // // 2.项目节 序号 兄弟的序号
  290. // } else {
  291. // return errors.New("参数错误")
  292. // }
  293. err = session.Commit()
  294. if err != nil {
  295. session.Rollback()
  296. return errors.New("操作失败-db")
  297. }
  298. return nil
  299. }
  300. // 插入多条数据
  301. func (d *TreeContractDao) CreateAll(data []*models.CmTreeContracts) error {
  302. _, err := d.engine.Insert(data)
  303. return err
  304. }
  305. //替换项目节归属
  306. func (d *TreeContractDao) replaceContractAttribution(session *xorm.Session, attributionChildren string, moveAttributionChildren string, code string, moveCode string, projectId int, bidsectionId int) error {
  307. // 1.获得需要替换的数据
  308. sectionData := d.GetAttribution(attributionChildren, projectId, bidsectionId)
  309. if len(sectionData) == 0 {
  310. return nil
  311. }
  312. attributionSql := " attribution = case id "
  313. codeSql := " code = case id "
  314. idList := make([]int, 0)
  315. for _, item := range sectionData {
  316. // section := &models.CmTreeContracts{}
  317. // section.Id = item.Id
  318. // 替换归属
  319. attributionSql += fmt.Sprintf("when %d then '%s' ", item.Id, strings.Replace(item.Attribution, attributionChildren, moveAttributionChildren, 1))
  320. //section.Attribution = strings.Replace(item.Attribution, attributionChildren, moveAttributionChildren, 1)
  321. // 替换编号
  322. codeSql += fmt.Sprintf("when %d then '%s' ", item.Id, strings.Replace(item.Code, code, moveCode, 1))
  323. // section.Code = strings.Replace(item.Code, code, moveCode, 1)
  324. idList = append(idList, item.Id)
  325. }
  326. attributionSql += " end, "
  327. codeSql += " end "
  328. id := strings.Replace(strings.Trim(fmt.Sprint(idList), "[]"), " ", ",", -1)
  329. sql := "update cm_tree_contracts set " + attributionSql + codeSql + " WHERE id IN (" + id + ")"
  330. _, err := session.Exec(sql)
  331. if err != nil {
  332. log.Println("替换项目节归属, error=", err)
  333. return err
  334. }
  335. return nil
  336. }