contract_section_tree_service.go 12 KB

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