contract_section_tree_service.go 13 KB

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