contract_section_tree_service.go 15 KB

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