ctr_contract_append.go 5.1 KB

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