contract_section_tree_service.go 15 KB

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