contract_section_tree_service.go 12 KB

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