contract_section_tree_service.go 12 KB

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