contract_section_tree_service.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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.GetChildrenBySection(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. sectionCM.ContractDeductionTotal = "0"
  190. data, err := s.treeContractDao.Create(sectionCM)
  191. if err != nil {
  192. return nil, err
  193. }
  194. return data, nil
  195. }
  196. // 保存名称
  197. func (s *contractService) SectionSave(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error {
  198. // 1.验证项目节ID
  199. treeId, err := utils.GetDecryptId(sectionData.Id)
  200. if err != nil {
  201. return err
  202. }
  203. section := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  204. if section.Id == 0 {
  205. return errors.New("未找到合同项目节")
  206. }
  207. if section.Name == sectionData.Name {
  208. return nil
  209. }
  210. // 1-1 深度为>=1才能新增项目节
  211. if section.Depth == 0 {
  212. return errors.New("请在项目节第二层开始编辑")
  213. }
  214. // 2.保存
  215. sectionCM := &models.CmTreeContracts{}
  216. sectionCM.Name = sectionData.Name
  217. sectionCM.Id = section.Id
  218. err = s.treeContractDao.Save(sectionCM, []string{"Name"})
  219. if err != nil {
  220. return errors.New("保存失败")
  221. }
  222. return nil
  223. }
  224. // 更新序号
  225. func (s *contractService) UpdateSerial(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error {
  226. // 1.验证项目节ID
  227. treeId, err := utils.GetDecryptId(sectionData.Id)
  228. if err != nil {
  229. return err
  230. }
  231. section := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  232. if section.Id == 0 {
  233. return errors.New("未找到合同项目节")
  234. }
  235. if section.Serial == sectionData.Serial {
  236. return nil
  237. }
  238. // 1-1 深度为>=1才能新增项目节
  239. if section.Depth == 0 {
  240. return errors.New("请在项目节第二层开始编辑")
  241. }
  242. err = s.treeContractDao.UpdateSerial(section, sectionData.Serial, treeType)
  243. if err != nil {
  244. return errors.New("更新失败")
  245. }
  246. return nil
  247. }
  248. // 项目节删除
  249. func (s *contractService) SectionDelete(treeId int, bidsectionId int, projectId int, treeType int) error {
  250. // 1.验证项目节ID
  251. section := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  252. if section.Id == 0 {
  253. return errors.New("未找到合同项目节")
  254. }
  255. // 1-1 深度为>=1才能新增项目节
  256. if section.Depth == 0 {
  257. return errors.New("请在项目节第二层开始编辑")
  258. }
  259. // 1-2 有合同的不能编辑(包含孩子节点)
  260. contractList := s.treeContractDao.GetAttributionContract(section, treeType)
  261. if len(contractList) != 0 {
  262. return errors.New("该项目节存在合同")
  263. }
  264. err := s.treeContractDao.Delete(section)
  265. if err != nil {
  266. return err
  267. }
  268. return nil
  269. }
  270. // 项目节的层级移动
  271. func (s *contractService) MoveDepth(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error {
  272. // 1.验证项目节ID
  273. treeId, err := utils.GetDecryptId(sectionData.Id)
  274. if err != nil {
  275. return err
  276. }
  277. section := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  278. if section.Id == 0 {
  279. return errors.New("未找到合同项目节")
  280. }
  281. // 1-1 深度为>=1才能新增项目节
  282. if section.Depth == 0 {
  283. return errors.New("请在项目节第二层开始编辑")
  284. }
  285. // 1-2 有合同的不能编辑(包含孩子节点)
  286. contractList := s.GetSectionTreeContract(section.Attribution, section.BidsectionId, section.ProjectId, treeType)
  287. if len(contractList) != 0 {
  288. return errors.New("该项目节已存在合同")
  289. }
  290. // 2.层级降级-同级有前一个兄弟节点
  291. elderBrother := &models.CmTreeContracts{}
  292. if sectionData.Operation == "downDepth" {
  293. // 获得前一个兄弟节点
  294. elderBrotherList := s.treeContractDao.GetElderBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId, treeType)
  295. if len(elderBrotherList) == 0 {
  296. return errors.New("项目节不能降级")
  297. }
  298. elderBrother = &elderBrotherList[0]
  299. // 前一个兄弟节点 不能有合同
  300. if elderBrother.ContractId != 0 {
  301. return errors.New(elderBrother.Code + "已是合同,不能降级")
  302. }
  303. } else if sectionData.Operation == "upDepth" { // 3.层级升级-只要有父亲都能升级
  304. // 2-1 父节点深度为2不能升级
  305. // if (section.Depth - 1) < 2 {
  306. // return errors.New("请在项目节第三层开始编辑")
  307. // }
  308. // 获得父亲节点
  309. if section.ParentId == 0 {
  310. return errors.New("项目节不能升级")
  311. }
  312. } else {
  313. return errors.New("参数错误")
  314. }
  315. // 4.执行升降级
  316. err = s.treeContractDao.MoveDepth(section, elderBrother, sectionData.Operation, bidsectionId, projectId, treeType)
  317. if err != nil {
  318. return err
  319. }
  320. return nil
  321. }
  322. // 项目节的排序移动
  323. func (s *contractService) MoveSerial(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error {
  324. // 1.验证项目节ID
  325. treeId, err := utils.GetDecryptId(sectionData.Id)
  326. if err != nil {
  327. return err
  328. }
  329. section := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  330. if section.Id == 0 {
  331. return errors.New("未找到合同项目节")
  332. }
  333. // 1-1 深度为>=1才能新增项目节
  334. if section.Depth == 0 {
  335. return errors.New("请在项目节第二层开始编辑")
  336. }
  337. // 1-2 有合同的不能编辑(包含孩子节点)
  338. contractList := s.GetSectionTreeContract(section.Attribution, section.BidsectionId, section.ProjectId, treeType)
  339. if len(contractList) != 0 {
  340. return errors.New("该项目节已存在合同")
  341. }
  342. // 2.下移
  343. brother := &models.CmTreeContracts{}
  344. if sectionData.Operation == "downSerial" {
  345. // 获得下一个兄弟
  346. youngerBrotherList := s.treeContractDao.GetYoungerBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId, treeType)
  347. if len(youngerBrotherList) == 0 {
  348. return errors.New("项目节不能下移")
  349. }
  350. brother = &youngerBrotherList[0]
  351. } else if sectionData.Operation == "upSerial" {
  352. // 获得上一个兄弟
  353. elderBrotherList := s.treeContractDao.GetElderBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId, treeType)
  354. if len(elderBrotherList) == 0 {
  355. return errors.New("项目节不能上移")
  356. }
  357. brother = &elderBrotherList[0]
  358. } else {
  359. return errors.New("参数错误")
  360. }
  361. // 4.执行升降级
  362. err = s.treeContractDao.MoveSerial(section, brother, sectionData.Operation, bidsectionId, projectId, treeType)
  363. if err != nil {
  364. return err
  365. }
  366. return nil
  367. }
  368. // 构造项目节树
  369. func (s *contractService) makeSectionTreeView(dataList []models.CmTreeContracts) []*viewmodels.TreeSectionContract {
  370. sectionList := make([]*viewmodels.TreeSectionContract, 0)
  371. // 生成根
  372. sectionRoot := &viewmodels.TreeSectionContract{}
  373. id, _ := comm.AesEncrypt(strconv.Itoa(0), conf.SignSecret)
  374. parentId, _ := comm.AesEncrypt(strconv.Itoa(-1), conf.SignSecret)
  375. sectionRoot.Id = id
  376. sectionRoot.Name = "root"
  377. sectionRoot.ParentId = parentId
  378. // sectionRoot.Children = make([]*viewmodels.TreeSectionContract, 0)
  379. sectionList = append(sectionList, sectionRoot)
  380. for _, data := range dataList {
  381. section := s.makeSectionView(&data)
  382. sectionList = append(sectionList, section)
  383. }
  384. return sectionList
  385. }
  386. // 构造一个项目节View
  387. func (s *contractService) makeSectionView(data *models.CmTreeContracts) *viewmodels.TreeSectionContract {
  388. section := &viewmodels.TreeSectionContract{}
  389. id, _ := comm.AesEncrypt(strconv.Itoa(data.TreeId), conf.SignSecret)
  390. parentId, _ := comm.AesEncrypt(strconv.Itoa(data.ParentId), conf.SignSecret)
  391. projectId, _ := comm.AesEncrypt(strconv.Itoa(data.ProjectId), conf.SignSecret)
  392. contractId, _ := comm.AesEncrypt(strconv.Itoa(data.ContractId), conf.SignSecret)
  393. bidsectionId, _ := comm.AesEncrypt(strconv.Itoa(data.BidsectionId), conf.SignSecret)
  394. section.Id = id
  395. section.Name = data.Name
  396. section.ParentId = parentId
  397. section.Depth = data.Depth + 1
  398. section.Serial = data.Serial
  399. section.Attribution = data.Attribution
  400. section.Code = data.Code
  401. section.ProjectId = projectId
  402. section.BidsectionId = bidsectionId
  403. section.ContractId = contractId
  404. // section.Children = make([]*viewmodels.TreeSectionContract, 0)
  405. section.ElderBrother = true
  406. section.IsEnd = false
  407. section.Name = data.Name
  408. section.ContractName = data.ContractName
  409. section.ContractCode = data.ContractCode
  410. section.ContractPrice = data.ContractPrice
  411. section.ContractReturned = data.ContractReturned
  412. section.ContractsPaid = data.ContractsPaid
  413. section.ContractStatus = data.ContractStatus
  414. section.ContractLocking = data.ContractLocking
  415. section.CreateTime = data.CreateTime.Format(conf.SysTimeform)
  416. section.ContractDeductionTotal = data.ContractDeductionTotal
  417. section.SettlementCode = data.SettlementCode
  418. //
  419. section.Title = fmt.Sprintf("%s%d ", data.Attribution, data.Serial) + data.Name
  420. section.Key = id
  421. section.Value = id
  422. return section
  423. }