contract_section_tree_service.go 12 KB

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