contract_section_tree_service.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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) GetSectionTreeContract(attribution string, bidsectionId int, projectId int, treeType int) []*viewmodels.Contracts {
  26. s.treeContractDao.GetAttribution(attribution, bidsectionId, projectId, treeType)
  27. return nil
  28. }
  29. // 设置合同项目节初始数据-根据模板导入
  30. func (s *contractService) SetSection(templateNumber int, bidsectionId int, projectId int, treeType 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.TreeType = treeType
  46. section.ParentId = item.ParentId
  47. section.ProjectId = projectId
  48. section.BidsectionId = bidsectionId
  49. section.Name = item.Name
  50. section.Depth = item.Depth
  51. section.Serial = item.Serial
  52. section.Attribution = item.Attribution
  53. section.Code = fmt.Sprintf("%s%d", item.Attribution, item.Serial)
  54. section.CreateTime = time.Now()
  55. section.ContractPrice = "0"
  56. section.ContractReturned = "0"
  57. section.ContractsPaid = "0"
  58. sectionTreeList = append(sectionTreeList, section)
  59. }
  60. err := s.treeContractDao.CreateAll(sectionTreeList)
  61. if err != nil {
  62. log.Println("设置合同项目节模板错误 err=", err)
  63. return errors.New("设置合同项目节模板错误")
  64. }
  65. return nil
  66. }
  67. // 新增项目节
  68. func (s *contractService) SectionAdd(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error {
  69. // 1.验证项目节ID
  70. treeId, err := utils.GetDecryptId(sectionData.Id)
  71. if err != nil {
  72. return err
  73. }
  74. sectionFather := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  75. if sectionFather.Id == 0 {
  76. return errors.New("未找到合同项目节")
  77. }
  78. // 1-1 深度为>=2才能新增项目节
  79. // if sectionFather.Depth < 3 {
  80. // return errors.New("请在项目节第三层开始编辑")
  81. // }
  82. // 1-2 项目节是否有合同
  83. if sectionFather.ContractId != 0 {
  84. return errors.New("该项目节已存在合同")
  85. }
  86. // 2 获得最大序号
  87. // 2-1 孩子节点
  88. childrenList := s.treeContractDao.GetChildren(treeId, bidsectionId, projectId, treeType)
  89. // 2-1 最大序号
  90. serial := 1
  91. if len(childrenList) != 0 {
  92. serial = childrenList[len(childrenList)-1].Serial + 1
  93. }
  94. // 2-2 归属
  95. attribution := fmt.Sprintf("%s%d-", sectionFather.Attribution, sectionFather.Serial)
  96. code := fmt.Sprintf("%s%d", attribution, serial)
  97. // 新增项目节
  98. sectionCM := &models.CmTreeContracts{}
  99. sectionCM.TreeId = treeType
  100. sectionCM.ParentId = sectionFather.TreeId
  101. sectionCM.Name = sectionData.Name
  102. sectionCM.Depth = sectionFather.Depth + 1
  103. sectionCM.Serial = serial
  104. sectionCM.Attribution = attribution
  105. sectionCM.Code = code
  106. sectionCM.ProjectId = projectId
  107. sectionCM.BidsectionId = bidsectionId
  108. sectionCM.ContractPrice = "0"
  109. sectionCM.ContractReturned = "0"
  110. sectionCM.ContractsPaid = "0"
  111. err = s.treeContractDao.Create(sectionCM)
  112. if err != nil {
  113. return err
  114. }
  115. return nil
  116. }
  117. // 保存名称
  118. func (s *contractService) SectionSave(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error {
  119. // 1.验证项目节ID
  120. treeId, err := utils.GetDecryptId(sectionData.Id)
  121. if err != nil {
  122. return err
  123. }
  124. section := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  125. if section.Id == 0 {
  126. return errors.New("未找到合同项目节")
  127. }
  128. // 1-1 深度为>=2才能新增项目节
  129. // if section.Depth < 2 {
  130. // return errors.New("请在项目节第三层开始编辑")
  131. // }
  132. // 2.保存
  133. sectionCM := &models.CmTreeContracts{}
  134. sectionCM.Name = sectionData.Name
  135. sectionCM.Id = section.Id
  136. err = s.treeContractDao.Save(sectionCM, []string{"Name"})
  137. if err != nil {
  138. return errors.New("保存失败")
  139. }
  140. return nil
  141. }
  142. // 更新序号
  143. func (s *contractService) UpdateSerial(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error {
  144. // 1.验证项目节ID
  145. treeId, err := utils.GetDecryptId(sectionData.Id)
  146. if err != nil {
  147. return err
  148. }
  149. section := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  150. if section.Id == 0 {
  151. return errors.New("未找到合同项目节")
  152. }
  153. // 1-1 深度为>=2才能新增项目节
  154. // if section.Depth < 2 {
  155. // return errors.New("请在项目节第三层开始编辑")
  156. // }
  157. err = s.treeContractDao.UpdateSerial(section, sectionData.Serial, treeType)
  158. if err != nil {
  159. return errors.New("更新失败")
  160. }
  161. return nil
  162. }
  163. // 项目节删除
  164. func (s *contractService) SectionDelete(treeId int, bidsectionId int, projectId int, treeType int) error {
  165. // 1.验证项目节ID
  166. section := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  167. if section.Id == 0 {
  168. return errors.New("未找到合同项目节")
  169. }
  170. // 1-1 深度为>=2才能新增项目节
  171. // if section.Depth < 2 {
  172. // return errors.New("请在项目节第三层开始编辑")
  173. // }
  174. // 1-2 有合同的不能编辑(包含孩子节点)
  175. contractList := s.GetSectionTreeContract(section.Attribution, section.BidsectionId, section.ProjectId, treeType)
  176. if len(contractList) != 0 {
  177. return errors.New("该项目节存在合同")
  178. }
  179. err := s.treeContractDao.Delete(section)
  180. if err != nil {
  181. return err
  182. }
  183. return nil
  184. }
  185. // 项目节的层级移动
  186. func (s *contractService) MoveDepth(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error {
  187. // 1.验证项目节ID
  188. treeId, err := utils.GetDecryptId(sectionData.Id)
  189. if err != nil {
  190. return err
  191. }
  192. section := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  193. if section.Id == 0 {
  194. return errors.New("未找到合同项目节")
  195. }
  196. // 1-1 深度为>=2才能新增项目节
  197. // if section.Depth < 2 {
  198. // return errors.New("请在项目节第三层开始编辑")
  199. // }
  200. // 1-2 有合同的不能编辑(包含孩子节点)
  201. contractList := s.GetSectionTreeContract(section.Attribution, section.BidsectionId, section.ProjectId, treeType)
  202. if len(contractList) != 0 {
  203. return errors.New("该项目节已存在合同")
  204. }
  205. // 2.层级降级-同级有前一个兄弟节点
  206. elderBrother := &models.CmTreeContracts{}
  207. if sectionData.Operation == "downDepth" {
  208. // 获得前一个兄弟节点
  209. elderBrotherList := s.treeContractDao.GetElderBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId, treeType)
  210. if len(elderBrotherList) == 0 {
  211. return errors.New("项目节不能降级")
  212. }
  213. elderBrother = &elderBrotherList[0]
  214. } else if sectionData.Operation == "upDepth" { // 3.层级升级-只要有父亲都能升级
  215. // 2-1 父节点深度为2不能升级
  216. // if (section.Depth - 1) < 2 {
  217. // return errors.New("请在项目节第三层开始编辑")
  218. // }
  219. // 获得父亲节点
  220. if section.ParentId == 0 {
  221. return errors.New("项目节不能升级")
  222. }
  223. } else {
  224. return errors.New("参数错误")
  225. }
  226. // 4.执行升降级
  227. err = s.treeContractDao.MoveDepth(section, elderBrother, sectionData.Operation, bidsectionId, projectId, treeType)
  228. if err != nil {
  229. return err
  230. }
  231. return nil
  232. }
  233. // 项目节的排序移动
  234. func (s *contractService) MoveSerial(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error {
  235. // 1.验证项目节ID
  236. treeId, err := utils.GetDecryptId(sectionData.Id)
  237. if err != nil {
  238. return err
  239. }
  240. section := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  241. if section.Id == 0 {
  242. return errors.New("未找到合同项目节")
  243. }
  244. // 1-1 深度为>=2才能新增项目节
  245. // if section.Depth < 2 {
  246. // return errors.New("请在项目节第三层开始编辑")
  247. // }
  248. // 1-2 有合同的不能编辑(包含孩子节点)
  249. contractList := s.GetSectionTreeContract(section.Attribution, section.BidsectionId, section.ProjectId, treeType)
  250. if len(contractList) != 0 {
  251. return errors.New("该项目节已存在合同")
  252. }
  253. // 2.下移
  254. brother := &models.CmTreeContracts{}
  255. if sectionData.Operation == "downSerial" {
  256. // 获得下一个兄弟
  257. youngerBrotherList := s.treeContractDao.GetYoungerBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId, treeType)
  258. if len(youngerBrotherList) == 0 {
  259. return errors.New("项目节不能下移")
  260. }
  261. brother = &youngerBrotherList[0]
  262. } else if sectionData.Operation == "upSerial" {
  263. // 获得上一个兄弟
  264. elderBrotherList := s.treeContractDao.GetElderBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId, treeType)
  265. if len(elderBrotherList) == 0 {
  266. return errors.New("项目节不能上移")
  267. }
  268. brother = &elderBrotherList[0]
  269. } else {
  270. return errors.New("参数错误")
  271. }
  272. // 4.执行升降级
  273. err = s.treeContractDao.MoveSerial(section, brother, sectionData.Operation, bidsectionId, projectId, treeType)
  274. if err != nil {
  275. return err
  276. }
  277. return nil
  278. }
  279. // 构造项目节树
  280. func (s *contractService) makeSectionTreeView(dataList []models.CmTreeContracts) []*viewmodels.TreeSectionContract {
  281. sectionList := make([]*viewmodels.TreeSectionContract, 0)
  282. // 生成根
  283. sectionRoot := &viewmodels.TreeSectionContract{}
  284. id, _ := comm.AesEncrypt(strconv.Itoa(0), conf.SignSecret)
  285. parentId, _ := comm.AesEncrypt(strconv.Itoa(-1), conf.SignSecret)
  286. sectionRoot.Id = id
  287. sectionRoot.Name = "root"
  288. sectionRoot.ParentId = parentId
  289. sectionList = append(sectionList, sectionRoot)
  290. for _, data := range dataList {
  291. section := s.makeSectionView(&data)
  292. sectionList = append(sectionList, section)
  293. }
  294. return sectionList
  295. }
  296. // 构造一个项目节View
  297. func (s *contractService) makeSectionView(data *models.CmTreeContracts) *viewmodels.TreeSectionContract {
  298. section := &viewmodels.TreeSectionContract{}
  299. id, _ := comm.AesEncrypt(strconv.Itoa(data.TreeId), conf.SignSecret)
  300. parentId, _ := comm.AesEncrypt(strconv.Itoa(data.ParentId), conf.SignSecret)
  301. projectId, _ := comm.AesEncrypt(strconv.Itoa(data.ProjectId), conf.SignSecret)
  302. contractId, _ := comm.AesEncrypt(strconv.Itoa(data.ContractId), conf.SignSecret)
  303. bidsectionId, _ := comm.AesEncrypt(strconv.Itoa(data.BidsectionId), conf.SignSecret)
  304. section.Id = id
  305. section.Name = data.Name
  306. section.ParentId = parentId
  307. section.Depth = data.Depth + 1
  308. section.Serial = data.Serial
  309. section.Attribution = data.Attribution
  310. section.Code = data.Code
  311. section.ProjectId = projectId
  312. section.BidsectionId = bidsectionId
  313. section.ContractId = contractId
  314. section.ElderBrother = true
  315. section.IsEnd = false
  316. section.Name = data.Name
  317. section.ContractName = data.ContractName
  318. section.ContractCode = data.ContractCode
  319. section.ContractPrice = data.ContractPrice
  320. section.ContractReturned = data.ContractReturned
  321. section.ContractsPaid = data.ContractsPaid
  322. section.ContractStatus = data.ContractStatus
  323. section.ContractLocking = data.ContractLocking
  324. section.CreateTime = data.CreateTime.Format(conf.SysTimeform)
  325. return section
  326. }