contract_section_tree_service.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 {
  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.Id = section.Id
  208. err = s.treeContractDao.Save(sectionCM, []string{"Name"})
  209. if err != nil {
  210. return errors.New("保存失败")
  211. }
  212. return nil
  213. }
  214. // 更新序号
  215. func (s *contractService) UpdateSerial(sectionData *viewmodels.TreeSectionContract) error {
  216. // 1.验证项目节ID
  217. treeId, err := utils.GetDecryptId(sectionData.Id)
  218. if err != nil {
  219. return err
  220. }
  221. section := s.treeContractDao.Get(treeId)
  222. if section.Id == 0 {
  223. return errors.New("未找到合同项目节")
  224. }
  225. if section.Serial == sectionData.Serial {
  226. return nil
  227. }
  228. // 1-1 深度为>=2才能新增项目节
  229. // if section.Depth < 2 {
  230. // return errors.New("请在项目节第三层开始编辑")
  231. // }
  232. err = s.treeContractDao.UpdateSerial(section, sectionData.Serial)
  233. if err != nil {
  234. return errors.New("更新失败")
  235. }
  236. return nil
  237. }
  238. // 项目节删除
  239. func (s *contractService) SectionDelete(treeId int) error {
  240. // 1.验证项目节ID
  241. section := s.treeContractDao.Get(treeId)
  242. if section.Id == 0 {
  243. return errors.New("未找到合同项目节")
  244. }
  245. // 1-1 深度为>=1才能新增项目节
  246. if section.Depth < 1 {
  247. return errors.New("请在项目节第三层开始编辑")
  248. }
  249. // 1-2 有合同的不能编辑(包含孩子节点)
  250. contractList := s.treeContractDao.GetAttributionContract(section)
  251. if len(contractList) != 0 {
  252. return errors.New("该项目节存在合同")
  253. }
  254. err := s.treeContractDao.Delete(section)
  255. if err != nil {
  256. return err
  257. }
  258. return nil
  259. }
  260. // 项目节的层级移动
  261. func (s *contractService) MoveDepth(sectionData *viewmodels.TreeSectionContract) error {
  262. // 1.验证项目节ID
  263. treeId, err := utils.GetDecryptId(sectionData.Id)
  264. if err != nil {
  265. return err
  266. }
  267. section := s.treeContractDao.Get(treeId)
  268. if section.Id == 0 {
  269. return errors.New("未找到合同项目节")
  270. }
  271. // 1-1 深度为>=2才能新增项目节
  272. // if section.Depth < 2 {
  273. // return errors.New("请在项目节第三层开始编辑")
  274. // }
  275. // 1-2 有合同的不能编辑(包含孩子节点)
  276. // contractList := s.GetSectionTreeContract(section.Attribution, section.BidsectionId)
  277. // if len(contractList) != 0 {
  278. // return errors.New("该项目节已存在合同")
  279. // }
  280. // 2.层级降级-同级有前一个兄弟节点
  281. elderBrother := &models.CmTreeContracts{}
  282. if sectionData.Operation == "downDepth" {
  283. // 获得前一个兄弟节点
  284. elderBrotherList := s.treeContractDao.GetElderBrother(section.Serial, section.Depth, section.ParentId)
  285. if len(elderBrotherList) == 0 {
  286. return errors.New("项目节不能降级")
  287. }
  288. elderBrother = &elderBrotherList[0]
  289. // 前一个兄弟节点 不能有合同
  290. if elderBrother.ContractId != 0 {
  291. return errors.New(elderBrother.Code + "已存在合同,不能降级")
  292. }
  293. } else if sectionData.Operation == "upDepth" { // 3.层级升级-只要有父亲都能升级
  294. // 2-1 父节点深度为2不能升级
  295. // if (section.Depth - 1) < 2 {
  296. // return errors.New("请在项目节第三层开始编辑")
  297. // }
  298. // 获得父亲节点
  299. if section.ParentId == 0 {
  300. return errors.New("项目节不能升级")
  301. }
  302. } else {
  303. return errors.New("参数错误")
  304. }
  305. // 4.执行升降级
  306. err = s.treeContractDao.MoveDepth(section, elderBrother, sectionData.Operation)
  307. if err != nil {
  308. return err
  309. }
  310. return nil
  311. }
  312. // 项目节的排序移动
  313. func (s *contractService) MoveSerial(sectionData *viewmodels.TreeSectionContract) error {
  314. // 1.验证项目节ID
  315. treeId, err := utils.GetDecryptId(sectionData.Id)
  316. if err != nil {
  317. return err
  318. }
  319. section := s.treeContractDao.Get(treeId)
  320. if section.Id == 0 {
  321. return errors.New("未找到合同项目节")
  322. }
  323. // 1-1 深度为>=2才能新增项目节
  324. // if section.Depth < 2 {
  325. // return errors.New("请在项目节第三层开始编辑")
  326. // }
  327. // 1-2 有合同的不能编辑(包含孩子节点)
  328. // contractList := s.GetSectionTreeContract(section.Attribution, section.BidsectionId, section.ProjectId, treeType)
  329. // if len(contractList) != 0 {
  330. // return errors.New("该项目节已存在合同")
  331. // }
  332. // 2.下移
  333. brother := &models.CmTreeContracts{}
  334. if sectionData.Operation == "downSerial" {
  335. // 获得下一个兄弟
  336. youngerBrotherList := s.treeContractDao.GetYoungerBrother(section.Serial, section.Depth, section.ParentId)
  337. if len(youngerBrotherList) == 0 {
  338. return errors.New("项目节不能下移")
  339. }
  340. brother = &youngerBrotherList[0]
  341. } else if sectionData.Operation == "upSerial" {
  342. // 获得上一个兄弟
  343. elderBrotherList := s.treeContractDao.GetElderBrother(section.Serial, section.Depth, section.ParentId)
  344. if len(elderBrotherList) == 0 {
  345. return errors.New("项目节不能上移")
  346. }
  347. brother = &elderBrotherList[0]
  348. } else {
  349. return errors.New("参数错误")
  350. }
  351. // 4.执行升降级
  352. err = s.treeContractDao.MoveSerial(section, brother, sectionData.Operation)
  353. if err != nil {
  354. return err
  355. }
  356. return nil
  357. }
  358. // 构造项目节树
  359. func (s *contractService) makeSectionTreeView(dataList []models.CmTreeContracts) []*viewmodels.TreeSectionContract {
  360. sectionList := make([]*viewmodels.TreeSectionContract, 0)
  361. // 生成根
  362. sectionRoot := &viewmodels.TreeSectionContract{}
  363. id, _ := comm.AesEncrypt(strconv.Itoa(0), conf.SignSecret)
  364. parentId, _ := comm.AesEncrypt(strconv.Itoa(-1), conf.SignSecret)
  365. sectionRoot.Id = id
  366. sectionRoot.Name = "root"
  367. sectionRoot.ParentId = parentId
  368. // sectionRoot.Children = make([]*viewmodels.TreeSectionContract, 0)
  369. sectionList = append(sectionList, sectionRoot)
  370. for _, data := range dataList {
  371. section := s.makeSectionView(&data)
  372. sectionList = append(sectionList, section)
  373. }
  374. return sectionList
  375. }
  376. // 构造一个项目节View
  377. func (s *contractService) makeSectionView(data *models.CmTreeContracts) *viewmodels.TreeSectionContract {
  378. section := &viewmodels.TreeSectionContract{}
  379. id, _ := comm.AesEncrypt(strconv.Itoa(data.TreeId), conf.SignSecret)
  380. parentId, _ := comm.AesEncrypt(strconv.Itoa(data.ParentId), conf.SignSecret)
  381. contractId, _ := comm.AesEncrypt(strconv.Itoa(data.ContractId), conf.SignSecret)
  382. section.Id = id
  383. section.Name = data.Name
  384. section.ParentId = parentId
  385. section.Depth = data.Depth + 1
  386. section.Serial = data.Serial
  387. section.Attribution = data.Attribution
  388. section.Code = data.Code
  389. section.ContractId = contractId
  390. // section.Children = make([]*viewmodels.TreeSectionContract, 0)
  391. section.ElderBrother = true
  392. section.IsEnd = false
  393. section.Name = data.Name
  394. section.CreateTime = data.CreateTime.Format(conf.SysTimeform)
  395. return section
  396. }
  397. // 更新项目节-合同id
  398. func (s *contractService) UpdateContractId(id int, contractId int) error {
  399. err := s.treeContractDao.UpdateContract(id, contractId)
  400. return err
  401. }