contract_section_tree_service.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "log"
  6. "strconv"
  7. "time"
  8. "go.mod/comm"
  9. "go.mod/conf"
  10. "go.mod/lib"
  11. "go.mod/models"
  12. "go.mod/web/utils"
  13. "go.mod/web/viewmodels"
  14. )
  15. // 获得合同项目节
  16. func (s *contractService) GetSecionTree() *viewmodels.TreeSectionContract {
  17. dataList := s.treeContractDao.GetAll()
  18. sectionList := s.makeSectionTreeView(dataList)
  19. // Node := sectionRoot //父节点
  20. Node := sectionList[0] //父节点
  21. comm.MakeSectionContract(sectionList, Node)
  22. return Node
  23. }
  24. // 获得合同项目节-不包含合同
  25. func (s *contractService) GetSecionTreeNotContract(bidsectionId int, projectId int, treeType int) *viewmodels.TreeSectionContract {
  26. dataList := s.treeContractDao.GetAllNotContract(bidsectionId, projectId, treeType)
  27. sectionList := s.makeSectionTreeView(dataList)
  28. // Node := sectionRoot //父节点
  29. Node := sectionList[0] //父节点
  30. comm.MakeSectionContract(sectionList, Node)
  31. return Node
  32. }
  33. // 获得项目节树和孩子们下的合同数据-未使用
  34. func (s *contractService) GetSectionTreeContract(attribution string) []*viewmodels.Contracts {
  35. s.treeContractDao.GetAttribution(attribution)
  36. return nil
  37. }
  38. // 设置合同项目节初始数据-根据模板导入
  39. func (s *contractService) SetSection(templateNumber int) error {
  40. // 获得模板数据
  41. templateTree := make([]*lib.ItemSectionTemplateTree, 0)
  42. if templateNumber == 1 {
  43. templateTree = lib.NewItemSectionData().TemplateList1
  44. } else if templateNumber == 2 {
  45. templateTree = lib.NewItemSectionData().TemplateList2
  46. } else {
  47. return errors.New("找不到合同项目节模板")
  48. }
  49. // 1.组合数据-写入库中
  50. sectionTreeList := make([]*models.CmTreeContracts, 0)
  51. for _, item := range templateTree {
  52. section := &models.CmTreeContracts{}
  53. section.TreeId = item.Id
  54. section.ParentId = item.ParentId
  55. section.ProjectId = 1
  56. section.BidsectionId = 1
  57. section.Name = item.Name
  58. section.Depth = item.Depth
  59. section.Serial = item.Serial
  60. section.Attribution = item.Attribution
  61. section.Code = fmt.Sprintf("%s%d", item.Attribution, item.Serial)
  62. section.CreateTime = time.Now()
  63. // err := s.treeContractDao.Create(section)
  64. // if err != nil {
  65. // log.Println("设置合同项目节模板错误 err=", err)
  66. // }
  67. sectionTreeList = append(sectionTreeList, section)
  68. }
  69. err := s.treeContractDao.CreateAll(sectionTreeList)
  70. if err != nil {
  71. log.Println("设置合同项目节模板错误 err=", err)
  72. return errors.New("设置合同项目节模板错误")
  73. }
  74. return nil
  75. }
  76. // 新增项目节
  77. func (s *contractService) ContractSectionAdd(sectionData *viewmodels.TreeSectionContract) (*models.CmTreeContracts, error) {
  78. // 1.验证项目节ID
  79. treeId, err := utils.GetDecryptId(sectionData.Id)
  80. if err != nil {
  81. return nil, err
  82. }
  83. sectionFather := s.treeContractDao.Get(treeId)
  84. if sectionFather.Id == 0 {
  85. return nil, errors.New("未找到项目节")
  86. }
  87. // 1-1 深度为>=2才能新增项目节
  88. // if sectionFather.Depth < 3 {
  89. // return errors.New("请在项目节第三层开始编辑")
  90. // }
  91. // 1-2 项目节是否有合同
  92. if sectionFather.ContractId != 0 {
  93. return nil, errors.New("合同项禁止添加子项")
  94. }
  95. // 2 获得最大序号
  96. // 2-1 孩子节点
  97. childrenList := s.treeContractDao.GetChildren(treeId)
  98. // 2-2.检查是否可以添加项目节
  99. // 新建合同,项目层级必须在第二层以下,当第一层没有孩子可以添加合同
  100. if sectionFather.Depth == 0 {
  101. if len(childrenList) != 0 {
  102. return nil, errors.New("该项目节不允许添加合同,请在下一层级新建合同")
  103. }
  104. }
  105. // 2-1 最大序号
  106. serial := 1
  107. if len(childrenList) != 0 {
  108. serial = childrenList[len(childrenList)-1].Serial + 1
  109. }
  110. // 2-2 归属
  111. attribution := fmt.Sprintf("%s%d-", sectionFather.Attribution, sectionFather.Serial)
  112. code := fmt.Sprintf("%s%d", attribution, serial)
  113. // 2-3获得新增ID
  114. lastId := s.treeContractDao.GetLastId()
  115. // 新增项目节
  116. sectionCM := &models.CmTreeContracts{}
  117. sectionCM.Id = lastId.Id + 1
  118. sectionCM.TreeId = lastId.Id + 1
  119. sectionCM.ParentId = sectionFather.TreeId
  120. sectionCM.Name = sectionData.Name
  121. sectionCM.Depth = sectionFather.Depth + 1
  122. sectionCM.Serial = serial
  123. sectionCM.Attribution = attribution
  124. sectionCM.Code = code
  125. data, err := s.treeContractDao.Create(sectionCM)
  126. if err != nil {
  127. return nil, err
  128. }
  129. return data, nil
  130. }
  131. // 新增项目节
  132. func (s *contractService) SectionAdd(sectionData *viewmodels.TreeSectionContract) (*models.CmTreeContracts, error) {
  133. // 1.验证项目节ID
  134. treeId, err := utils.GetDecryptId(sectionData.Id)
  135. if err != nil {
  136. return nil, err
  137. }
  138. sectionFather := s.treeContractDao.Get(treeId)
  139. if sectionFather.Id == 0 {
  140. return nil, errors.New("未找到合同项目节")
  141. }
  142. // 1-1 深度为>=2才能新增项目节
  143. // if sectionFather.Depth < 3 {
  144. // return errors.New("请在项目节第三层开始编辑")
  145. // }
  146. // 1-2 项目节是否有合同
  147. if sectionFather.ContractId != 0 {
  148. return nil, errors.New("合同项禁止添加子项")
  149. }
  150. // 2 获得最大序号
  151. // 2-1 孩子节点
  152. childrenList := s.treeContractDao.GetChildren(treeId)
  153. // 2-1 最大序号
  154. serial := 1
  155. if len(childrenList) != 0 {
  156. serial = childrenList[len(childrenList)-1].Serial + 1
  157. }
  158. // 2-2 归属
  159. attribution := fmt.Sprintf("%s%d-", sectionFather.Attribution, sectionFather.Serial)
  160. code := fmt.Sprintf("%s%d", attribution, serial)
  161. // 2-3获得新增ID
  162. lastId := s.treeContractDao.GetLastId()
  163. // 新增项目节
  164. sectionCM := &models.CmTreeContracts{}
  165. sectionCM.Id = lastId.Id + 1
  166. sectionCM.TreeId = lastId.Id + 1
  167. sectionCM.ParentId = sectionFather.TreeId
  168. sectionCM.Name = sectionData.Name
  169. sectionCM.Depth = sectionFather.Depth + 1
  170. sectionCM.Serial = serial
  171. sectionCM.Attribution = attribution
  172. sectionCM.Code = code
  173. data, err := s.treeContractDao.Create(sectionCM)
  174. if err != nil {
  175. return nil, err
  176. }
  177. return data, nil
  178. }
  179. // 保存名称
  180. func (s *contractService) SectionSave(sectionData *viewmodels.TreeSectionContract) error {
  181. // 1.验证项目节ID
  182. treeId, err := utils.GetDecryptId(sectionData.Id)
  183. if err != nil {
  184. return err
  185. }
  186. section := s.treeContractDao.Get(treeId)
  187. if section.Id == 0 {
  188. return errors.New("未找到合同项目节")
  189. }
  190. if section.Name == sectionData.Name {
  191. return nil
  192. }
  193. // 1-1 深度为>=2才能新增项目节
  194. // if section.Depth < 2 {
  195. // return errors.New("请在项目节第三层开始编辑")
  196. // }
  197. // 2.保存
  198. sectionCM := &models.CmTreeContracts{}
  199. sectionCM.Name = sectionData.Name
  200. sectionCM.Id = section.Id
  201. err = s.treeContractDao.Save(sectionCM, []string{"Name"})
  202. if err != nil {
  203. return errors.New("保存失败")
  204. }
  205. return nil
  206. }
  207. // 更新序号
  208. func (s *contractService) UpdateSerial(sectionData *viewmodels.TreeSectionContract) error {
  209. // 1.验证项目节ID
  210. treeId, err := utils.GetDecryptId(sectionData.Id)
  211. if err != nil {
  212. return err
  213. }
  214. section := s.treeContractDao.Get(treeId)
  215. if section.Id == 0 {
  216. return errors.New("未找到合同项目节")
  217. }
  218. if section.Serial == sectionData.Serial {
  219. return nil
  220. }
  221. // 1-1 深度为>=2才能新增项目节
  222. // if section.Depth < 2 {
  223. // return errors.New("请在项目节第三层开始编辑")
  224. // }
  225. err = s.treeContractDao.UpdateSerial(section, sectionData.Serial)
  226. if err != nil {
  227. return errors.New("更新失败")
  228. }
  229. return nil
  230. }
  231. // 项目节删除
  232. func (s *contractService) SectionDelete(treeId int) error {
  233. // 1.验证项目节ID
  234. section := s.treeContractDao.Get(treeId)
  235. if section.Id == 0 {
  236. return errors.New("未找到合同项目节")
  237. }
  238. // 1-1 深度为>=1才能新增项目节
  239. if section.Depth < 1 {
  240. return errors.New("请在项目节第三层开始编辑")
  241. }
  242. // 1-2 有合同的不能编辑(包含孩子节点)
  243. contractList := s.treeContractDao.GetAttributionContract(section)
  244. if len(contractList) != 0 {
  245. return errors.New("该项目节存在合同")
  246. }
  247. err := s.treeContractDao.Delete(section)
  248. if err != nil {
  249. return err
  250. }
  251. return nil
  252. }
  253. // 项目节的层级移动
  254. func (s *contractService) MoveDepth(sectionData *viewmodels.TreeSectionContract) error {
  255. // 1.验证项目节ID
  256. treeId, err := utils.GetDecryptId(sectionData.Id)
  257. if err != nil {
  258. return err
  259. }
  260. section := s.treeContractDao.Get(treeId)
  261. if section.Id == 0 {
  262. return errors.New("未找到合同项目节")
  263. }
  264. // 1-1 深度为>=2才能新增项目节
  265. // if section.Depth < 2 {
  266. // return errors.New("请在项目节第三层开始编辑")
  267. // }
  268. // 1-2 有合同的不能编辑(包含孩子节点)
  269. // contractList := s.GetSectionTreeContract(section.Attribution, section.BidsectionId)
  270. // if len(contractList) != 0 {
  271. // return errors.New("该项目节已存在合同")
  272. // }
  273. // 2.层级降级-同级有前一个兄弟节点
  274. elderBrother := &models.CmTreeContracts{}
  275. if sectionData.Operation == "downDepth" {
  276. // 获得前一个兄弟节点
  277. elderBrotherList := s.treeContractDao.GetElderBrother(section.Serial, section.Depth, section.ParentId)
  278. if len(elderBrotherList) == 0 {
  279. return errors.New("项目节不能降级")
  280. }
  281. elderBrother = &elderBrotherList[0]
  282. // 前一个兄弟节点 不能有合同
  283. if elderBrother.ContractId != 0 {
  284. return errors.New(elderBrother.Code + "已存在合同,不能降级")
  285. }
  286. } else if sectionData.Operation == "upDepth" { // 3.层级升级-只要有父亲都能升级
  287. // 2-1 父节点深度为2不能升级
  288. // if (section.Depth - 1) < 2 {
  289. // return errors.New("请在项目节第三层开始编辑")
  290. // }
  291. // 获得父亲节点
  292. if section.ParentId == 0 {
  293. return errors.New("项目节不能升级")
  294. }
  295. } else {
  296. return errors.New("参数错误")
  297. }
  298. // 4.执行升降级
  299. err = s.treeContractDao.MoveDepth(section, elderBrother, sectionData.Operation)
  300. if err != nil {
  301. return err
  302. }
  303. return nil
  304. }
  305. // 项目节的排序移动
  306. func (s *contractService) MoveSerial(sectionData *viewmodels.TreeSectionContract) error {
  307. // 1.验证项目节ID
  308. treeId, err := utils.GetDecryptId(sectionData.Id)
  309. if err != nil {
  310. return err
  311. }
  312. section := s.treeContractDao.Get(treeId)
  313. if section.Id == 0 {
  314. return errors.New("未找到合同项目节")
  315. }
  316. // 1-1 深度为>=2才能新增项目节
  317. // if section.Depth < 2 {
  318. // return errors.New("请在项目节第三层开始编辑")
  319. // }
  320. // 1-2 有合同的不能编辑(包含孩子节点)
  321. // contractList := s.GetSectionTreeContract(section.Attribution, section.BidsectionId, section.ProjectId, treeType)
  322. // if len(contractList) != 0 {
  323. // return errors.New("该项目节已存在合同")
  324. // }
  325. // 2.下移
  326. brother := &models.CmTreeContracts{}
  327. if sectionData.Operation == "downSerial" {
  328. // 获得下一个兄弟
  329. youngerBrotherList := s.treeContractDao.GetYoungerBrother(section.Serial, section.Depth, section.ParentId)
  330. if len(youngerBrotherList) == 0 {
  331. return errors.New("项目节不能下移")
  332. }
  333. brother = &youngerBrotherList[0]
  334. } else if sectionData.Operation == "upSerial" {
  335. // 获得上一个兄弟
  336. elderBrotherList := s.treeContractDao.GetElderBrother(section.Serial, section.Depth, section.ParentId)
  337. if len(elderBrotherList) == 0 {
  338. return errors.New("项目节不能上移")
  339. }
  340. brother = &elderBrotherList[0]
  341. } else {
  342. return errors.New("参数错误")
  343. }
  344. // 4.执行升降级
  345. err = s.treeContractDao.MoveSerial(section, brother, sectionData.Operation)
  346. if err != nil {
  347. return err
  348. }
  349. return nil
  350. }
  351. // 构造项目节树
  352. func (s *contractService) makeSectionTreeView(dataList []models.CmTreeContracts) []*viewmodels.TreeSectionContract {
  353. sectionList := make([]*viewmodels.TreeSectionContract, 0)
  354. // 生成根
  355. sectionRoot := &viewmodels.TreeSectionContract{}
  356. id, _ := comm.AesEncrypt(strconv.Itoa(0), conf.SignSecret)
  357. parentId, _ := comm.AesEncrypt(strconv.Itoa(-1), conf.SignSecret)
  358. sectionRoot.Id = id
  359. sectionRoot.Name = "root"
  360. sectionRoot.ParentId = parentId
  361. // sectionRoot.Children = make([]*viewmodels.TreeSectionContract, 0)
  362. sectionList = append(sectionList, sectionRoot)
  363. for _, data := range dataList {
  364. section := s.makeSectionView(&data)
  365. sectionList = append(sectionList, section)
  366. }
  367. return sectionList
  368. }
  369. // 构造一个项目节View
  370. func (s *contractService) makeSectionView(data *models.CmTreeContracts) *viewmodels.TreeSectionContract {
  371. section := &viewmodels.TreeSectionContract{}
  372. id, _ := comm.AesEncrypt(strconv.Itoa(data.TreeId), conf.SignSecret)
  373. parentId, _ := comm.AesEncrypt(strconv.Itoa(data.ParentId), conf.SignSecret)
  374. contractId, _ := comm.AesEncrypt(strconv.Itoa(data.ContractId), conf.SignSecret)
  375. section.Id = id
  376. section.Name = data.Name
  377. section.ParentId = parentId
  378. section.Depth = data.Depth + 1
  379. section.Serial = data.Serial
  380. section.Attribution = data.Attribution
  381. section.Code = data.Code
  382. section.ContractId = contractId
  383. // section.Children = make([]*viewmodels.TreeSectionContract, 0)
  384. section.ElderBrother = true
  385. section.IsEnd = false
  386. section.Name = data.Name
  387. section.CreateTime = data.CreateTime.Format(conf.SysTimeform)
  388. return section
  389. }