contract_section_tree_service.go 13 KB

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