contract_section_tree_service.go 13 KB

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