contract_section_tree_service.go 13 KB

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