contract_section_tree_service.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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) ContractSectionAdd(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-2.检查是否可以添加项目节
  103. // 新建合同,项目层级必须在第二层以下,当第一层没有孩子可以添加合同
  104. if sectionFather.Depth == 0 {
  105. if len(childrenList) != 0 {
  106. return nil, errors.New("该项目节不允许添加合同,请在下一层级新建合同")
  107. }
  108. }
  109. // 2-1 最大序号
  110. serial := 1
  111. if len(childrenList) != 0 {
  112. serial = childrenList[len(childrenList)-1].Serial + 1
  113. }
  114. // 2-2 归属
  115. attribution := fmt.Sprintf("%s%d-", sectionFather.Attribution, sectionFather.Serial)
  116. code := fmt.Sprintf("%s%d", attribution, serial)
  117. // 2-3获得新增ID
  118. lastId := s.treeContractDao.GetLastId()
  119. // 新增项目节
  120. sectionCM := &models.CmTreeContracts{}
  121. sectionCM.Id = lastId.Id + 1
  122. sectionCM.TreeId = lastId.Id + 1
  123. sectionCM.TreeType = treeType
  124. sectionCM.ParentId = sectionFather.TreeId
  125. sectionCM.Name = sectionData.Name
  126. sectionCM.Depth = sectionFather.Depth + 1
  127. sectionCM.Serial = serial
  128. sectionCM.Attribution = attribution
  129. sectionCM.Code = code
  130. sectionCM.ProjectId = projectId
  131. sectionCM.BidsectionId = bidsectionId
  132. sectionCM.ContractPrice = "0"
  133. sectionCM.ContractReturned = "0"
  134. sectionCM.ContractsPaid = "0"
  135. data, err := s.treeContractDao.Create(sectionCM)
  136. if err != nil {
  137. return nil, err
  138. }
  139. return data, nil
  140. }
  141. // 新增项目节
  142. func (s *contractService) SectionAdd(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) (*models.CmTreeContracts, error) {
  143. // 1.验证项目节ID
  144. treeId, err := utils.GetDecryptId(sectionData.Id)
  145. if err != nil {
  146. return nil, err
  147. }
  148. sectionFather := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  149. if sectionFather.Id == 0 {
  150. return nil, errors.New("未找到合同项目节")
  151. }
  152. // 1-1 深度为>=2才能新增项目节
  153. // if sectionFather.Depth < 3 {
  154. // return errors.New("请在项目节第三层开始编辑")
  155. // }
  156. // 1-2 项目节是否有合同
  157. if sectionFather.ContractId != 0 {
  158. return nil, errors.New("合同项禁止添加子项")
  159. }
  160. // 2 获得最大序号
  161. // 2-1 孩子节点
  162. childrenList := s.treeContractDao.GetChildren(treeId, bidsectionId, projectId, treeType)
  163. // 2-1 最大序号
  164. serial := 1
  165. if len(childrenList) != 0 {
  166. serial = childrenList[len(childrenList)-1].Serial + 1
  167. }
  168. // 2-2 归属
  169. attribution := fmt.Sprintf("%s%d-", sectionFather.Attribution, sectionFather.Serial)
  170. code := fmt.Sprintf("%s%d", attribution, serial)
  171. // 2-3获得新增ID
  172. lastId := s.treeContractDao.GetLastId()
  173. // 新增项目节
  174. sectionCM := &models.CmTreeContracts{}
  175. sectionCM.Id = lastId.Id + 1
  176. sectionCM.TreeId = lastId.Id + 1
  177. sectionCM.TreeType = treeType
  178. sectionCM.ParentId = sectionFather.TreeId
  179. sectionCM.Name = sectionData.Name
  180. sectionCM.Depth = sectionFather.Depth + 1
  181. sectionCM.Serial = serial
  182. sectionCM.Attribution = attribution
  183. sectionCM.Code = code
  184. sectionCM.ProjectId = projectId
  185. sectionCM.BidsectionId = bidsectionId
  186. sectionCM.ContractPrice = "0"
  187. sectionCM.ContractReturned = "0"
  188. sectionCM.ContractsPaid = "0"
  189. data, err := s.treeContractDao.Create(sectionCM)
  190. if err != nil {
  191. return nil, err
  192. }
  193. return data, nil
  194. }
  195. // 保存名称
  196. func (s *contractService) SectionSave(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error {
  197. // 1.验证项目节ID
  198. treeId, err := utils.GetDecryptId(sectionData.Id)
  199. if err != nil {
  200. return err
  201. }
  202. section := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  203. if section.Id == 0 {
  204. return errors.New("未找到合同项目节")
  205. }
  206. if section.Name == sectionData.Name {
  207. return nil
  208. }
  209. // 1-1 深度为>=2才能新增项目节
  210. // if section.Depth < 2 {
  211. // return errors.New("请在项目节第三层开始编辑")
  212. // }
  213. // 2.保存
  214. sectionCM := &models.CmTreeContracts{}
  215. sectionCM.Name = sectionData.Name
  216. sectionCM.Id = section.Id
  217. err = s.treeContractDao.Save(sectionCM, []string{"Name"})
  218. if err != nil {
  219. return errors.New("保存失败")
  220. }
  221. return nil
  222. }
  223. // 保存编号
  224. func (s *contractService) SectionCode(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error {
  225. // 1.验证项目节ID
  226. treeId, err := utils.GetDecryptId(sectionData.Id)
  227. if err != nil {
  228. return err
  229. }
  230. section := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  231. if section.Id == 0 {
  232. return errors.New("未找到合同项目节")
  233. }
  234. if section.Code == sectionData.Code {
  235. return nil
  236. }
  237. // 1-1 深度为>=2才能新增项目节
  238. // if section.Depth < 2 {
  239. // return errors.New("请在项目节第三层开始编辑")
  240. // }
  241. // 2.保存
  242. sectionCM := &models.CmTreeContracts{}
  243. sectionCM.Code = sectionData.Code
  244. sectionCM.Id = section.Id
  245. err = s.treeContractDao.Save(sectionCM, []string{"Code"})
  246. if err != nil {
  247. return errors.New("保存失败")
  248. }
  249. return nil
  250. }
  251. // 更新序号
  252. func (s *contractService) UpdateSerial(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error {
  253. // 1.验证项目节ID
  254. treeId, err := utils.GetDecryptId(sectionData.Id)
  255. if err != nil {
  256. return err
  257. }
  258. section := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  259. if section.Id == 0 {
  260. return errors.New("未找到合同项目节")
  261. }
  262. if section.Serial == sectionData.Serial {
  263. return nil
  264. }
  265. // 1-1 深度为>=2才能新增项目节
  266. // if section.Depth < 2 {
  267. // return errors.New("请在项目节第三层开始编辑")
  268. // }
  269. err = s.treeContractDao.UpdateSerial(section, sectionData.Serial, treeType)
  270. if err != nil {
  271. return errors.New("更新失败")
  272. }
  273. return nil
  274. }
  275. // 项目节删除
  276. func (s *contractService) SectionDelete(treeId int, bidsectionId int, projectId int, treeType int) error {
  277. // 1.验证项目节ID
  278. section := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  279. if section.Id == 0 {
  280. return errors.New("未找到合同项目节")
  281. }
  282. // 1-1 深度为>=1才能新增项目节
  283. if section.Depth < 1 {
  284. return errors.New("请在项目节第三层开始编辑")
  285. }
  286. // 1-2 有合同的不能编辑(包含孩子节点)
  287. contractList := s.treeContractDao.GetAttributionContract(section, treeType)
  288. if len(contractList) != 0 {
  289. return errors.New("该项目节存在合同")
  290. }
  291. err := s.treeContractDao.Delete(section)
  292. if err != nil {
  293. return err
  294. }
  295. return nil
  296. }
  297. // 项目节的层级移动
  298. func (s *contractService) MoveDepth(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error {
  299. // 1.验证项目节ID
  300. treeId, err := utils.GetDecryptId(sectionData.Id)
  301. if err != nil {
  302. return err
  303. }
  304. section := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  305. if section.Id == 0 {
  306. return errors.New("未找到合同项目节")
  307. }
  308. // 1-1 深度为>=2才能新增项目节
  309. // if section.Depth < 2 {
  310. // return errors.New("请在项目节第三层开始编辑")
  311. // }
  312. // 1-2 有合同的不能编辑(包含孩子节点)
  313. contractList := s.GetSectionTreeContract(section.Attribution, section.BidsectionId, section.ProjectId, treeType)
  314. if len(contractList) != 0 {
  315. return errors.New("该项目节已存在合同")
  316. }
  317. // 2.层级降级-同级有前一个兄弟节点
  318. elderBrother := &models.CmTreeContracts{}
  319. if sectionData.Operation == "downDepth" {
  320. // 获得前一个兄弟节点
  321. elderBrotherList := s.treeContractDao.GetElderBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId, treeType)
  322. if len(elderBrotherList) == 0 {
  323. return errors.New("项目节不能降级")
  324. }
  325. elderBrother = &elderBrotherList[0]
  326. // 前一个兄弟节点 不能有合同
  327. if elderBrother.ContractId != 0 {
  328. return errors.New(elderBrother.Code + "已存在合同,不能降级")
  329. }
  330. } else if sectionData.Operation == "upDepth" { // 3.层级升级-只要有父亲都能升级
  331. // 2-1 父节点深度为2不能升级
  332. // if (section.Depth - 1) < 2 {
  333. // return errors.New("请在项目节第三层开始编辑")
  334. // }
  335. // 获得父亲节点
  336. if section.ParentId == 0 {
  337. return errors.New("项目节不能升级")
  338. }
  339. } else {
  340. return errors.New("参数错误")
  341. }
  342. // 4.执行升降级
  343. err = s.treeContractDao.MoveDepth(section, elderBrother, sectionData.Operation, bidsectionId, projectId, treeType)
  344. if err != nil {
  345. return err
  346. }
  347. return nil
  348. }
  349. // 项目节的排序移动
  350. func (s *contractService) MoveSerial(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error {
  351. // 1.验证项目节ID
  352. treeId, err := utils.GetDecryptId(sectionData.Id)
  353. if err != nil {
  354. return err
  355. }
  356. section := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  357. if section.Id == 0 {
  358. return errors.New("未找到合同项目节")
  359. }
  360. // 1-1 深度为>=2才能新增项目节
  361. // if section.Depth < 2 {
  362. // return errors.New("请在项目节第三层开始编辑")
  363. // }
  364. // 1-2 有合同的不能编辑(包含孩子节点)
  365. contractList := s.GetSectionTreeContract(section.Attribution, section.BidsectionId, section.ProjectId, treeType)
  366. if len(contractList) != 0 {
  367. return errors.New("该项目节已存在合同")
  368. }
  369. // 2.下移
  370. brother := &models.CmTreeContracts{}
  371. if sectionData.Operation == "downSerial" {
  372. // 获得下一个兄弟
  373. youngerBrotherList := s.treeContractDao.GetYoungerBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId, treeType)
  374. if len(youngerBrotherList) == 0 {
  375. return errors.New("项目节不能下移")
  376. }
  377. brother = &youngerBrotherList[0]
  378. } else if sectionData.Operation == "upSerial" {
  379. // 获得上一个兄弟
  380. elderBrotherList := s.treeContractDao.GetElderBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId, treeType)
  381. if len(elderBrotherList) == 0 {
  382. return errors.New("项目节不能上移")
  383. }
  384. brother = &elderBrotherList[0]
  385. } else {
  386. return errors.New("参数错误")
  387. }
  388. // 4.执行升降级
  389. err = s.treeContractDao.MoveSerial(section, brother, sectionData.Operation, bidsectionId, projectId, treeType)
  390. if err != nil {
  391. return err
  392. }
  393. return nil
  394. }
  395. // 构造项目节树
  396. func (s *contractService) makeSectionTreeView(dataList []models.CmTreeContracts) []*viewmodels.TreeSectionContract {
  397. sectionList := make([]*viewmodels.TreeSectionContract, 0)
  398. // 生成根
  399. sectionRoot := &viewmodels.TreeSectionContract{}
  400. id, _ := comm.AesEncrypt(strconv.Itoa(0), conf.SignSecret)
  401. parentId, _ := comm.AesEncrypt(strconv.Itoa(-1), conf.SignSecret)
  402. sectionRoot.Id = id
  403. sectionRoot.Name = "root"
  404. sectionRoot.ParentId = parentId
  405. // sectionRoot.Children = make([]*viewmodels.TreeSectionContract, 0)
  406. sectionList = append(sectionList, sectionRoot)
  407. for _, data := range dataList {
  408. section := s.makeSectionView(&data)
  409. sectionList = append(sectionList, section)
  410. }
  411. return sectionList
  412. }
  413. // 构造一个项目节View
  414. func (s *contractService) makeSectionView(data *models.CmTreeContracts) *viewmodels.TreeSectionContract {
  415. section := &viewmodels.TreeSectionContract{}
  416. id, _ := comm.AesEncrypt(strconv.Itoa(data.TreeId), conf.SignSecret)
  417. parentId, _ := comm.AesEncrypt(strconv.Itoa(data.ParentId), conf.SignSecret)
  418. projectId, _ := comm.AesEncrypt(strconv.Itoa(data.ProjectId), conf.SignSecret)
  419. contractId, _ := comm.AesEncrypt(strconv.Itoa(data.ContractId), conf.SignSecret)
  420. bidsectionId, _ := comm.AesEncrypt(strconv.Itoa(data.BidsectionId), conf.SignSecret)
  421. section.Id = id
  422. section.Name = data.Name
  423. section.ParentId = parentId
  424. section.Depth = data.Depth + 1
  425. section.Serial = data.Serial
  426. section.Attribution = data.Attribution
  427. section.Code = data.Code
  428. section.ProjectId = projectId
  429. section.BidsectionId = bidsectionId
  430. section.ContractId = contractId
  431. // section.Children = make([]*viewmodels.TreeSectionContract, 0)
  432. section.ElderBrother = true
  433. section.IsEnd = false
  434. section.Name = data.Name
  435. section.ContractName = data.ContractName
  436. section.ContractCode = data.ContractCode
  437. section.ContractPrice = data.ContractPrice
  438. section.ContractReturned = data.ContractReturned
  439. section.ContractsPaid = data.ContractsPaid
  440. section.ContractStatus = data.ContractStatus
  441. section.ContractLocking = data.ContractLocking
  442. section.CreateTime = data.CreateTime.Format(conf.SysTimeform)
  443. //
  444. section.Title = fmt.Sprintf("%s%d ", data.Attribution, data.Serial) + data.Name
  445. section.Key = id
  446. section.Value = id
  447. return section
  448. }