contract_service.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * @description: 合同数据相关操作
  3. * @Author: CP
  4. * @Date: 2020-10-27 11:28:24
  5. * @FilePath: \construction_management\services\contract_service.go
  6. */
  7. package services
  8. import (
  9. "errors"
  10. "fmt"
  11. "log"
  12. "strconv"
  13. "time"
  14. "github.com/kataras/iris/v12"
  15. "go.mod/comm"
  16. "go.mod/conf"
  17. "go.mod/dao"
  18. "go.mod/datasource"
  19. "go.mod/lib"
  20. "go.mod/models"
  21. "go.mod/web/utils"
  22. "go.mod/web/viewmodels"
  23. )
  24. //定义项目用户Service接口
  25. type ContractService interface {
  26. ValidRuleDepth(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  27. ValidRuleTemplate(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  28. // Create(viewBidAccount viewmodels.BidAccount, projectId int) error
  29. // Delete(viewBidAccount viewmodels.BidAccount, projectId int) error
  30. GetAll()
  31. GetSeciontTree(bidsectionId int, projectId int) *viewmodels.TreeSectionContract
  32. SetSection(templateNumber int, bidsectionId int, projectIdInt int) error
  33. MoveDepth(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int) error
  34. MoveSerial(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int) error
  35. }
  36. //返回service操作类
  37. type contractService struct {
  38. treeContractDao *dao.TreeContractDao
  39. // dao *dao.ProjectAccountDao
  40. // projectDao *dao.ProjectDao
  41. // bidsectionDao *dao.BidsectionDao
  42. // bidAccountDao *dao.BidAccountDao
  43. }
  44. //创建项目用户service
  45. func NewContractService() ContractService {
  46. return &contractService{
  47. treeContractDao: dao.NewTreeContractDao(datasource.InstanceDbMaster()),
  48. // projectAccountDao: dao.NewProjectAccountDao(datasource.InstanceDbMaster()),
  49. // projectDao: dao.NewProjectDao(datasource.InstanceDbMaster()),
  50. // bidsectionDao: dao.NewBidsectionDao(datasource.InstanceDbMaster()),
  51. // bidAccountDao: dao.NewBidAccountDao(datasource.InstanceDbMaster()),
  52. }
  53. }
  54. // 升级降级规则验证
  55. func (s *contractService) ValidRuleDepth(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  56. treeSectionVaild := &viewmodels.TreeSectionContract{}
  57. err := ctx.ReadJSON(treeSectionVaild)
  58. if err != nil {
  59. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  60. return treeSectionVaild, err
  61. }
  62. err = treeSectionVaild.ValidateDepth()
  63. if err != nil {
  64. log.Println("参数验证错误, error=", err)
  65. return treeSectionVaild, err
  66. }
  67. return treeSectionVaild, nil
  68. }
  69. // 模板规则验证
  70. func (s *contractService) ValidRuleTemplate(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  71. treeSectionVaild := &viewmodels.TreeSectionContract{}
  72. err := ctx.ReadJSON(treeSectionVaild)
  73. if err != nil {
  74. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  75. return treeSectionVaild, err
  76. }
  77. err = treeSectionVaild.ValidateTemplate()
  78. if err != nil {
  79. log.Println("参数验证错误, error=", err)
  80. return treeSectionVaild, err
  81. }
  82. return treeSectionVaild, nil
  83. }
  84. // 获得合同标段内容
  85. func (s *contractService) GetAll() {
  86. }
  87. // 获得合同项目节
  88. func (s *contractService) GetSeciontTree(bidsectionId int, projectId int) *viewmodels.TreeSectionContract {
  89. sectionList := make([]*viewmodels.TreeSectionContract, 0)
  90. dataList := s.treeContractDao.GetAll(bidsectionId, projectId)
  91. // 生成根
  92. sectionRoot := &viewmodels.TreeSectionContract{}
  93. id, _ := comm.AesEncrypt(strconv.Itoa(0), conf.SignSecret)
  94. parentId, _ := comm.AesEncrypt(strconv.Itoa(-1), conf.SignSecret)
  95. sectionRoot.Id = id
  96. sectionRoot.Name = "root"
  97. sectionRoot.ParentId = parentId
  98. sectionList = append(sectionList, sectionRoot)
  99. for _, data := range dataList {
  100. section := &viewmodels.TreeSectionContract{}
  101. id, _ := comm.AesEncrypt(strconv.Itoa(data.TreeId), conf.SignSecret)
  102. parentId, _ := comm.AesEncrypt(strconv.Itoa(data.ParentId), conf.SignSecret)
  103. projectId, _ := comm.AesEncrypt(strconv.Itoa(data.ProjectId), conf.SignSecret)
  104. contractId, _ := comm.AesEncrypt(strconv.Itoa(data.ContractId), conf.SignSecret)
  105. section.Id = id
  106. section.Name = data.Name
  107. section.ParentId = parentId
  108. section.Depth = data.Depth + 1
  109. section.Serial = data.Serial
  110. section.Attribution = data.Attribution
  111. section.Code = data.Code
  112. section.ProjectId = projectId
  113. section.ContractId = contractId
  114. section.Name = data.Name
  115. section.ContractCode = data.ContractCode
  116. section.ContractPrice = data.ContractPrice
  117. section.ContractReturned = data.ContractReturned
  118. section.ContractsPaid = data.ContractsPaid
  119. section.ContractStatus = data.ContractStatus
  120. section.CreateTime = data.CreateTime.Format(conf.SysTimeform)
  121. sectionList = append(sectionList, section)
  122. }
  123. Node := sectionRoot //父节点
  124. comm.MakeSectionContract(sectionList, Node)
  125. return Node
  126. }
  127. // 设置合同项目节初始数据-根据模板导入
  128. func (s *contractService) SetSection(templateNumber int, bidsectionId int, projectId int) error {
  129. // 获得模板数据
  130. templateTree := make([]*lib.ItemSectionTemplateTree, 0)
  131. if templateNumber == 1 {
  132. templateTree = lib.NewItemSectionData().TemplateList1
  133. } else if templateNumber == 2 {
  134. templateTree = lib.NewItemSectionData().TemplateList2
  135. } else {
  136. return errors.New("找不到合同项目节模板")
  137. }
  138. // 1.组合数据-写入库中
  139. sectionTreeList := make([]*models.CmTreeContracts, 0)
  140. for _, item := range templateTree {
  141. section := &models.CmTreeContracts{}
  142. section.TreeId = item.Id
  143. section.ParentId = item.ParentId
  144. section.ProjectId = projectId
  145. section.BidsectionId = bidsectionId
  146. section.Name = item.Name
  147. section.Depth = item.Depth
  148. section.Serial = item.Serial
  149. section.Attribution = item.Attribution
  150. section.Code = fmt.Sprintf("%s%d", item.Attribution, item.Serial)
  151. section.CreateTime = time.Now()
  152. section.ContractPrice = "0"
  153. section.ContractReturned = "0"
  154. section.ContractsPaid = "0"
  155. sectionTreeList = append(sectionTreeList, section)
  156. }
  157. err := s.treeContractDao.CreateAll(sectionTreeList)
  158. if err != nil {
  159. log.Println("设置合同项目节模板错误 err=", err)
  160. return errors.New("设置合同项目节模板错误")
  161. }
  162. return nil
  163. }
  164. // 项目节的层级移动
  165. func (s *contractService) MoveDepth(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int) error {
  166. // 1.验证项目节ID
  167. treeId, err := utils.GetDecryptId(sectionData.Id)
  168. if err != nil {
  169. return err
  170. }
  171. section := s.treeContractDao.Get(treeId, bidsectionId, projectId)
  172. if section.Id == 0 {
  173. return errors.New("未找到合同项目节")
  174. }
  175. // 2.层级降级-同级有前一个兄弟节点
  176. elderBrother := &models.CmTreeContracts{}
  177. if sectionData.Operation == "downDepth" {
  178. // 获得前一个兄弟节点
  179. elderBrotherList := s.treeContractDao.GetElderBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId)
  180. if len(elderBrotherList) == 0 {
  181. return errors.New("项目节不能降级")
  182. }
  183. elderBrother = &elderBrotherList[0]
  184. } else if sectionData.Operation == "upDepth" { // 3.层级升级-只要有父亲都能升级
  185. // 获得父亲节点
  186. if section.ParentId == 0 {
  187. return errors.New("项目节不能升级")
  188. }
  189. } else {
  190. return errors.New("参数错误")
  191. }
  192. // 4.执行升降级
  193. err = s.treeContractDao.MoveDepth(section, elderBrother, sectionData.Operation, bidsectionId, projectId)
  194. if err != nil {
  195. return err
  196. }
  197. return nil
  198. }
  199. // 项目节的排序移动
  200. func (s *contractService) MoveSerial(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int) error {
  201. // 1.验证项目节ID
  202. treeId, err := utils.GetDecryptId(sectionData.Id)
  203. if err != nil {
  204. return err
  205. }
  206. section := s.treeContractDao.Get(treeId, bidsectionId, projectId)
  207. if section.Id == 0 {
  208. return errors.New("未找到合同项目节")
  209. }
  210. // 2.下移
  211. brother := &models.CmTreeContracts{}
  212. if sectionData.Operation == "downSerial" {
  213. // 获得下一个兄弟
  214. youngerBrotherList := s.treeContractDao.GetYoungerBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId)
  215. if len(youngerBrotherList) == 0 {
  216. return errors.New("项目节不能下移")
  217. }
  218. brother = &youngerBrotherList[0]
  219. } else if sectionData.Operation == "upSerial" {
  220. // 获得上一个兄弟
  221. elderBrotherList := s.treeContractDao.GetElderBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId)
  222. if len(elderBrotherList) == 0 {
  223. return errors.New("项目节不能上移")
  224. }
  225. brother = &elderBrotherList[0]
  226. } else {
  227. return errors.New("参数错误")
  228. }
  229. // 4.执行升降级
  230. err = s.treeContractDao.MoveSerial(section, brother, sectionData.Operation, bidsectionId, projectId)
  231. if err != nil {
  232. return err
  233. }
  234. return nil
  235. }