ctr_contract_append.go 5.0 KB

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