contract_section_tree_service.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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) *viewmodels.TreeSectionContract {
  17. dataList := s.treeContractDao.GetAll(bidsectionId, projectId)
  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) GetSectionTreeContract(attribution string, bidsectionId int, projectId int) []*viewmodels.Contracts {
  26. s.treeContractDao.GetAttribution(attribution, bidsectionId, projectId)
  27. return nil
  28. }
  29. // 设置合同项目节初始数据-根据模板导入
  30. func (s *contractService) SetSection(templateNumber int, bidsectionId int, projectId int) error {
  31. // 获得模板数据
  32. templateTree := make([]*lib.ItemSectionTemplateTree, 0)
  33. if templateNumber == 1 {
  34. templateTree = lib.NewItemSectionData().TemplateList1
  35. } else if templateNumber == 2 {
  36. templateTree = lib.NewItemSectionData().TemplateList2
  37. } else {
  38. return errors.New("找不到合同项目节模板")
  39. }
  40. // 1.组合数据-写入库中
  41. sectionTreeList := make([]*models.CmTreeContracts, 0)
  42. for _, item := range templateTree {
  43. section := &models.CmTreeContracts{}
  44. section.TreeId = item.Id
  45. section.ParentId = item.ParentId
  46. section.ProjectId = projectId
  47. section.BidsectionId = bidsectionId
  48. section.Name = item.Name
  49. section.Depth = item.Depth
  50. section.Serial = item.Serial
  51. section.Attribution = item.Attribution
  52. section.Code = fmt.Sprintf("%s%d", item.Attribution, item.Serial)
  53. section.CreateTime = time.Now()
  54. section.ContractPrice = "0"
  55. section.ContractReturned = "0"
  56. section.ContractsPaid = "0"
  57. sectionTreeList = append(sectionTreeList, section)
  58. }
  59. err := s.treeContractDao.CreateAll(sectionTreeList)
  60. if err != nil {
  61. log.Println("设置合同项目节模板错误 err=", err)
  62. return errors.New("设置合同项目节模板错误")
  63. }
  64. return nil
  65. }
  66. // 新增项目节
  67. func (s *contractService) SectionAdd(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int) error {
  68. // 1.验证项目节ID
  69. treeId, err := utils.GetDecryptId(sectionData.Id)
  70. if err != nil {
  71. return err
  72. }
  73. sectionFather := s.treeContractDao.Get(treeId, bidsectionId, projectId)
  74. if sectionFather.Id == 0 {
  75. return errors.New("未找到合同项目节")
  76. }
  77. // 2 获得最大序号
  78. // 2-1 孩子节点
  79. childrenList := s.treeContractDao.GetChildren(treeId, bidsectionId, projectId)
  80. // 2-1 最大序号
  81. serial := 1
  82. if len(childrenList) != 0 {
  83. serial = childrenList[len(childrenList)-1].Serial + 1
  84. }
  85. // 2-2 归属
  86. attribution := fmt.Sprintf("%s%d-", sectionFather.Attribution, sectionFather.Serial)
  87. code := fmt.Sprintf("%s%d", attribution, serial)
  88. // 新增项目节
  89. sectionCM := &models.CmTreeContracts{}
  90. sectionCM.ParentId = sectionFather.TreeId
  91. sectionCM.Name = sectionData.Name
  92. sectionCM.Depth = sectionFather.Depth + 1
  93. sectionCM.Serial = serial
  94. sectionCM.Attribution = attribution
  95. sectionCM.Code = code
  96. sectionCM.ProjectId = projectId
  97. sectionCM.BidsectionId = bidsectionId
  98. sectionCM.ContractPrice = "0"
  99. sectionCM.ContractReturned = "0"
  100. sectionCM.ContractsPaid = "0"
  101. err = s.treeContractDao.Create(sectionCM)
  102. if err != nil {
  103. return errors.New("新增失败")
  104. }
  105. return nil
  106. }
  107. // 保存名称或者编号
  108. func (s *contractService) SectionSave(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int) error {
  109. // 1.验证项目节ID
  110. treeId, err := utils.GetDecryptId(sectionData.Id)
  111. if err != nil {
  112. return err
  113. }
  114. section := s.treeContractDao.Get(treeId, bidsectionId, projectId)
  115. if section.Id == 0 {
  116. return errors.New("未找到合同项目节")
  117. }
  118. // 2.保存
  119. sectionCM := &models.CmTreeContracts{}
  120. sectionCM.Name = sectionData.Name
  121. sectionCM.Id = section.Id
  122. err = s.treeContractDao.Save(sectionCM, []string{"Name"})
  123. if err != nil {
  124. return errors.New("保存失败")
  125. }
  126. return nil
  127. }
  128. // 项目节删除
  129. func (s *contractService) SectionDelete(treeId int, bidsectionId int, projectId int) error {
  130. // 1.验证项目节ID
  131. section := s.treeContractDao.Get(treeId, bidsectionId, projectId)
  132. if section.Id == 0 {
  133. return errors.New("未找到合同项目节")
  134. }
  135. err := s.treeContractDao.Delete(section)
  136. if err != nil {
  137. return err
  138. }
  139. return nil
  140. }
  141. // 项目节的层级移动
  142. func (s *contractService) MoveDepth(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int) error {
  143. // 1.验证项目节ID
  144. treeId, err := utils.GetDecryptId(sectionData.Id)
  145. if err != nil {
  146. return err
  147. }
  148. section := s.treeContractDao.Get(treeId, bidsectionId, projectId)
  149. if section.Id == 0 {
  150. return errors.New("未找到合同项目节")
  151. }
  152. // 2.层级降级-同级有前一个兄弟节点
  153. elderBrother := &models.CmTreeContracts{}
  154. if sectionData.Operation == "downDepth" {
  155. // 获得前一个兄弟节点
  156. elderBrotherList := s.treeContractDao.GetElderBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId)
  157. if len(elderBrotherList) == 0 {
  158. return errors.New("项目节不能降级")
  159. }
  160. elderBrother = &elderBrotherList[0]
  161. } else if sectionData.Operation == "upDepth" { // 3.层级升级-只要有父亲都能升级
  162. // 获得父亲节点
  163. if section.ParentId == 0 {
  164. return errors.New("项目节不能升级")
  165. }
  166. } else {
  167. return errors.New("参数错误")
  168. }
  169. // 4.执行升降级
  170. err = s.treeContractDao.MoveDepth(section, elderBrother, sectionData.Operation, bidsectionId, projectId)
  171. if err != nil {
  172. return err
  173. }
  174. return nil
  175. }
  176. // 项目节的排序移动
  177. func (s *contractService) MoveSerial(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int) error {
  178. // 1.验证项目节ID
  179. treeId, err := utils.GetDecryptId(sectionData.Id)
  180. if err != nil {
  181. return err
  182. }
  183. section := s.treeContractDao.Get(treeId, bidsectionId, projectId)
  184. if section.Id == 0 {
  185. return errors.New("未找到合同项目节")
  186. }
  187. // 2.下移
  188. brother := &models.CmTreeContracts{}
  189. if sectionData.Operation == "downSerial" {
  190. // 获得下一个兄弟
  191. youngerBrotherList := s.treeContractDao.GetYoungerBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId)
  192. if len(youngerBrotherList) == 0 {
  193. return errors.New("项目节不能下移")
  194. }
  195. brother = &youngerBrotherList[0]
  196. } else if sectionData.Operation == "upSerial" {
  197. // 获得上一个兄弟
  198. elderBrotherList := s.treeContractDao.GetElderBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId)
  199. if len(elderBrotherList) == 0 {
  200. return errors.New("项目节不能上移")
  201. }
  202. brother = &elderBrotherList[0]
  203. } else {
  204. return errors.New("参数错误")
  205. }
  206. // 4.执行升降级
  207. err = s.treeContractDao.MoveSerial(section, brother, sectionData.Operation, bidsectionId, projectId)
  208. if err != nil {
  209. return err
  210. }
  211. return nil
  212. }
  213. // 构造项目节树
  214. func (s *contractService) makeSectionTreeView(dataList []models.CmTreeContracts) []*viewmodels.TreeSectionContract {
  215. sectionList := make([]*viewmodels.TreeSectionContract, 0)
  216. // 生成根
  217. sectionRoot := &viewmodels.TreeSectionContract{}
  218. id, _ := comm.AesEncrypt(strconv.Itoa(0), conf.SignSecret)
  219. parentId, _ := comm.AesEncrypt(strconv.Itoa(-1), conf.SignSecret)
  220. sectionRoot.Id = id
  221. sectionRoot.Name = "root"
  222. sectionRoot.ParentId = parentId
  223. sectionList = append(sectionList, sectionRoot)
  224. for _, data := range dataList {
  225. section := s.makeSectionView(&data)
  226. sectionList = append(sectionList, section)
  227. }
  228. return sectionList
  229. }
  230. // 构造一个项目节View
  231. func (s *contractService) makeSectionView(data *models.CmTreeContracts) *viewmodels.TreeSectionContract {
  232. section := &viewmodels.TreeSectionContract{}
  233. id, _ := comm.AesEncrypt(strconv.Itoa(data.TreeId), conf.SignSecret)
  234. parentId, _ := comm.AesEncrypt(strconv.Itoa(data.ParentId), conf.SignSecret)
  235. projectId, _ := comm.AesEncrypt(strconv.Itoa(data.ProjectId), conf.SignSecret)
  236. contractId, _ := comm.AesEncrypt(strconv.Itoa(data.ContractId), conf.SignSecret)
  237. bidsectionId, _ := comm.AesEncrypt(strconv.Itoa(data.BidsectionId), conf.SignSecret)
  238. section.Id = id
  239. section.Name = data.Name
  240. section.ParentId = parentId
  241. section.Depth = data.Depth + 1
  242. section.Serial = data.Serial
  243. section.Attribution = data.Attribution
  244. section.Code = data.Code
  245. section.ProjectId = projectId
  246. section.BidsectionId = bidsectionId
  247. section.ContractId = contractId
  248. section.ElderBrother = true
  249. section.IsEnd = false
  250. section.Name = data.Name
  251. section.ContractCode = data.ContractCode
  252. section.ContractPrice = data.ContractPrice
  253. section.ContractReturned = data.ContractReturned
  254. section.ContractsPaid = data.ContractsPaid
  255. section.ContractStatus = data.ContractStatus
  256. section.CreateTime = data.CreateTime.Format(conf.SysTimeform)
  257. return section
  258. }