contract_section_tree_service.go 13 KB

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