ctr_contract_append.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package service
  2. import (
  3. "context"
  4. dao "dashoo.cn/micro/app/dao/contract"
  5. model "dashoo.cn/micro/app/model/contract"
  6. "database/sql"
  7. "fmt"
  8. "dashoo.cn/opms_libary/micro_srv"
  9. "dashoo.cn/opms_libary/myerrors"
  10. "dashoo.cn/opms_libary/request"
  11. "github.com/gogf/gf/os/gtime"
  12. "github.com/gogf/gf/util/gvalid"
  13. )
  14. type CtrContractAppendService struct {
  15. Dao *dao.CtrContractAppendDao
  16. ContractDao *dao.CtrContractDao
  17. Tenant string
  18. userInfo request.UserInfo
  19. }
  20. func NewCtrContractAppendService(ctx context.Context) (*CtrContractAppendService, error) {
  21. tenant, err := micro_srv.GetTenant(ctx)
  22. if err != nil {
  23. return nil, fmt.Errorf("获取组合码异常:%s", err.Error())
  24. }
  25. // 获取用户信息
  26. userInfo, err := micro_srv.GetUserInfo(ctx)
  27. if err != nil {
  28. return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
  29. }
  30. return &CtrContractAppendService{
  31. Dao: dao.NewCtrContractAppendDao(tenant),
  32. ContractDao: dao.NewCtrContractDao(tenant),
  33. Tenant: tenant,
  34. userInfo: userInfo,
  35. }, nil
  36. }
  37. func (s CtrContractAppendService) List(ctx context.Context, req *model.CtrContractAppendListReq) (int, []*model.CtrContractAppend, error) {
  38. dao := &s.Dao.CtrContractAppendDao
  39. if req.FileName != "" {
  40. likestr := fmt.Sprintf("%%%s%%", req.FileName)
  41. dao = dao.Where("file_name LIKE ?", likestr)
  42. }
  43. if req.ContractId != 0 {
  44. dao = dao.Where("contract_id = ?", req.ContractId)
  45. }
  46. if req.BeginTime != "" {
  47. dao = dao.Where("created_time > ?", req.BeginTime)
  48. }
  49. if req.EndTime != "" {
  50. dao = dao.Where("created_time < ?", req.EndTime)
  51. }
  52. total, err := dao.Count()
  53. if err != nil {
  54. return 0, nil, err
  55. }
  56. if req.PageNum != 0 {
  57. dao = dao.Page(req.GetPage())
  58. }
  59. orderby := "created_time desc"
  60. if req.OrderBy != "" {
  61. orderby = req.OrderBy
  62. }
  63. dao = dao.Order(orderby)
  64. ents := []*model.CtrContractAppend{}
  65. err = dao.Structs(&ents)
  66. if err != nil && err != sql.ErrNoRows {
  67. return 0, nil, err
  68. }
  69. return total, ents, err
  70. }
  71. func (s CtrContractAppendService) Add(ctx context.Context, req *model.CtrContractAppendAddReq) (int, error) {
  72. validErr := gvalid.CheckStruct(ctx, req, nil)
  73. if validErr != nil {
  74. return 0, myerrors.NewMsgError(nil, validErr.Current().Error())
  75. }
  76. ent, err := s.ContractDao.Where("id = ?", req.ContractId).One()
  77. if err != nil {
  78. return 0, err
  79. }
  80. if ent == nil {
  81. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("合同不存在: %d", req.ContractId))
  82. }
  83. id, err := s.Dao.InsertAndGetId(model.CtrContractAppend{
  84. ContractId: req.ContractId,
  85. FileName: req.FileName,
  86. FileType: req.FileType,
  87. FileUrl: req.FileUrl,
  88. Remark: req.Remark,
  89. CreatedBy: s.userInfo.Id,
  90. CreatedName: s.userInfo.NickName,
  91. CreatedTime: gtime.Now(),
  92. UpdatedBy: s.userInfo.Id,
  93. UpdatedName: s.userInfo.NickName,
  94. UpdatedTime: gtime.Now(),
  95. DeletedTime: gtime.Now(),
  96. })
  97. if err != nil {
  98. return 0, err
  99. }
  100. return int(id), err
  101. }
  102. func (s CtrContractAppendService) Update(ctx context.Context, req *model.CtrContractAppendUpdateReq) error {
  103. validErr := gvalid.CheckStruct(ctx, req, nil)
  104. if validErr != nil {
  105. return myerrors.NewMsgError(nil, validErr.Current().Error())
  106. }
  107. ent, err := s.Dao.Where("id = ?", req.Id).One()
  108. if err != nil {
  109. return err
  110. }
  111. if ent == nil {
  112. return myerrors.NewMsgError(nil, fmt.Sprintf("文档不存在: %d", req.Id))
  113. }
  114. if req.ContractId != 0 {
  115. ent, err := s.ContractDao.Where("id = ?", req.ContractId).One()
  116. if err != nil {
  117. return err
  118. }
  119. if ent == nil {
  120. return myerrors.NewMsgError(nil, fmt.Sprintf("合同不存在: %d", req.ContractId))
  121. }
  122. }
  123. dao := &s.Dao.CtrContractAppendDao
  124. toupdate := map[string]interface{}{}
  125. if req.ContractId != 0 {
  126. toupdate["contract_id"] = req.ContractId
  127. }
  128. if req.FileName != "" {
  129. toupdate["file_name"] = req.FileName
  130. }
  131. if req.FileType != "" {
  132. toupdate["file_type"] = req.FileType
  133. }
  134. if req.FileUrl != "" {
  135. toupdate["file_url"] = req.FileUrl
  136. }
  137. if req.Remark != nil {
  138. toupdate["remark"] = *req.Remark
  139. }
  140. if len(toupdate) != 0 {
  141. toupdate["updated_by"] = s.userInfo.Id
  142. toupdate["updated_name"] = s.userInfo.NickName
  143. toupdate["updated_time"] = gtime.Now()
  144. _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
  145. if err != nil {
  146. return err
  147. }
  148. }
  149. return nil
  150. }
  151. func (s CtrContractAppendService) Delete(ctx context.Context, id []int) error {
  152. if len(id) == 0 {
  153. return nil
  154. }
  155. _, err := s.Dao.Where("Id IN (?)", id).Delete()
  156. return err
  157. }