contract_section_tree_service.go 14 KB

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