contract_section_tree_service.go 15 KB

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