contract_section_tree_service.go 11 KB

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