ctr_contract_append.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. DeletedTime: gtime.Now(),
  105. }
  106. var id int
  107. txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
  108. appendId, err := tx.InsertAndGetId("ctr_contract_append", contractAppend)
  109. if err != nil {
  110. return err
  111. }
  112. err = s.ctrSrv.AddDynamicsByCurrentUser(tx, int(req.ContractId), "创建合同附件", map[string]interface{}{
  113. "id": appendId,
  114. "fileName": contractAppend.FileName,
  115. "fileType": contractAppend.FileType,
  116. "fileUrl": contractAppend.FileUrl,
  117. })
  118. if err != nil {
  119. return err
  120. }
  121. id = int(appendId)
  122. return nil
  123. })
  124. return id, txerr
  125. }
  126. func (s CtrContractAppendService) Update(ctx context.Context, req *model.CtrContractAppendUpdateReq) error {
  127. validErr := gvalid.CheckStruct(ctx, req, nil)
  128. if validErr != nil {
  129. return myerrors.TipsError(validErr.Current().Error())
  130. }
  131. ent, err := s.Dao.Where("id = ?", req.Id).One()
  132. if err != nil {
  133. return err
  134. }
  135. if ent == nil {
  136. return myerrors.TipsError(fmt.Sprintf("合同附件不存在: %d", req.Id))
  137. }
  138. if req.ContractId != 0 {
  139. ent, err := s.ContractDao.Where("id = ?", req.ContractId).One()
  140. if err != nil {
  141. return err
  142. }
  143. if ent == nil {
  144. return myerrors.TipsError(fmt.Sprintf("合同不存在: %d", req.ContractId))
  145. }
  146. }
  147. dao := &s.Dao.CtrContractAppendDao
  148. toupdate := map[string]interface{}{}
  149. if req.ContractId != 0 {
  150. toupdate["contract_id"] = req.ContractId
  151. }
  152. if req.FileName != "" {
  153. toupdate["file_name"] = req.FileName
  154. }
  155. if req.FileType != "" {
  156. toupdate["file_type"] = req.FileType
  157. }
  158. if req.FileUrl != "" {
  159. toupdate["file_url"] = req.FileUrl
  160. }
  161. if req.Remark != nil {
  162. toupdate["remark"] = *req.Remark
  163. }
  164. if len(toupdate) != 0 {
  165. toupdate["updated_by"] = s.userInfo.Id
  166. toupdate["updated_name"] = s.userInfo.NickName
  167. toupdate["updated_time"] = gtime.Now()
  168. _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
  169. if err != nil {
  170. return err
  171. }
  172. }
  173. return nil
  174. }
  175. func (s CtrContractAppendService) Delete(ctx context.Context, id []int) error {
  176. if len(id) == 0 {
  177. return nil
  178. }
  179. _, err := s.Dao.Where("Id IN (?)", id).Delete()
  180. return err
  181. }