ctr_contract_invoice.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 CtrContractInvoiceService struct {
  15. Dao *dao.CtrContractInvoiceDao
  16. Tenant string
  17. userInfo request.UserInfo
  18. }
  19. func NewCtrContractInvoiceService(ctx context.Context) (*CtrContractInvoiceService, error) {
  20. tenant, err := micro_srv.GetTenant(ctx)
  21. if err != nil {
  22. return nil, fmt.Errorf("获取组合码异常:%s", err.Error())
  23. }
  24. // 获取用户信息
  25. userInfo, err := micro_srv.GetUserInfo(ctx)
  26. if err != nil {
  27. return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
  28. }
  29. return &CtrContractInvoiceService{
  30. Dao: dao.NewCtrContractInvoiceDao(tenant),
  31. Tenant: tenant,
  32. userInfo: userInfo,
  33. }, nil
  34. }
  35. func (s CtrContractInvoiceService) List(ctx context.Context, req *model.CtrContractInvoiceListReq) (int, []*model.CtrContractInvoice, error) {
  36. dao := &s.Dao.CtrContractInvoiceDao
  37. if req.SearchText != "" {
  38. likestr := fmt.Sprintf("%%%s%%", req.SearchText)
  39. dao = dao.Where("(doc_code LIKE ? || doc_name LIKE ?)", likestr, likestr)
  40. }
  41. if req.DocType != "" {
  42. dao = dao.Where("doc_type = ?", req.DocType)
  43. }
  44. total, err := dao.Count()
  45. if err != nil {
  46. return 0, nil, err
  47. }
  48. if req.Page != nil {
  49. if req.Page.Current == 0 {
  50. req.Page.Current = 1
  51. }
  52. if req.Page.Size == 0 {
  53. req.Page.Size = 10
  54. }
  55. dao = dao.Page(req.Page.Current, req.Page.Size)
  56. }
  57. if req.OrderBy == nil {
  58. req.OrderBy = &model.OrderBy{}
  59. }
  60. if req.OrderBy.Value == "" {
  61. req.OrderBy.Value = "created_time"
  62. req.OrderBy.Type = "desc"
  63. }
  64. if req.OrderBy != nil && req.OrderBy.Value != "" {
  65. order := "asc"
  66. if req.OrderBy.Type == "desc" {
  67. order = "desc"
  68. }
  69. dao = dao.Order(req.OrderBy.Value, order)
  70. }
  71. ents := []*model.CtrContractInvoice{}
  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 CtrContractInvoiceService) Add(ctx context.Context, req *model.CtrContractInvoiceAddReq) (int, error) {
  79. validErr := gvalid.CheckStruct(ctx, req, nil)
  80. if validErr != nil {
  81. return 0, myerrors.NewMsgError(nil, validErr.Current().Error())
  82. }
  83. t, err := s.Dao.Where("doc_code = ?", req.DocCode).One()
  84. if err != nil {
  85. return 0, err
  86. }
  87. if t != nil {
  88. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("文档编码:%s 已存在", req.DocCode))
  89. }
  90. id, err := s.Dao.InsertAndGetId(model.CtrContractInvoice{
  91. CustId: req.CustId,
  92. CustName: req.CustName,
  93. ContractId: req.ContractId,
  94. ContractCode: req.ContractCode,
  95. ContractAmount: req.ContractAmount,
  96. InvoiceAmount: req.InvoiceAmount,
  97. InvoiceDate: req.InvoiceDate,
  98. InvoiceType: req.InvoiceType,
  99. ApproStatus: req.ApproStatus,
  100. InvoiceCode: req.InvoiceCode,
  101. ActualInvoiceDate: req.ActualInvoiceDate,
  102. CourierCode: req.CourierCode,
  103. Remark: req.Remark,
  104. CreatedBy: int(s.userInfo.Id),
  105. CreatedName: s.userInfo.NickName,
  106. CreatedTime: gtime.Now(),
  107. UpdatedBy: int(s.userInfo.Id),
  108. UpdatedName: s.userInfo.NickName,
  109. UpdatedTime: gtime.Now(),
  110. DeletedTime: gtime.Now(),
  111. })
  112. if err != nil {
  113. return 0, err
  114. }
  115. return int(id), err
  116. }
  117. func (s CtrContractInvoiceService) Update(ctx context.Context, req *model.CtrContractInvoiceUpdateReq) error {
  118. validErr := gvalid.CheckStruct(ctx, req, nil)
  119. if validErr != nil {
  120. return myerrors.NewMsgError(nil, validErr.Current().Error())
  121. }
  122. ent, err := s.Dao.Where("id = ?", req.Id).One()
  123. if err != nil {
  124. return err
  125. }
  126. if ent == nil {
  127. return myerrors.NewMsgError(nil, fmt.Sprintf("文档不存在: %d", req.Id))
  128. }
  129. if req.DocCode != "" {
  130. exist, err := s.Dao.Where("doc_code = ?", req.DocCode).One()
  131. if err != nil {
  132. return err
  133. }
  134. if exist != nil && exist.Id != req.Id {
  135. return myerrors.NewMsgError(nil, fmt.Sprintf("文档编码: %s 已存在", req.DocCode))
  136. }
  137. }
  138. dao := &s.Dao.CtrContractInvoiceDao
  139. toupdate := map[string]interface{}{}
  140. if req.CustId != 0 {
  141. toupdate["cust_id"] = req.CustId
  142. }
  143. if req.CustName != 0 {
  144. toupdate["cust_name"] = req.CustName
  145. }
  146. if req.ContractId != 0 {
  147. toupdate["contract_id"] = req.ContractId
  148. }
  149. if req.ContractCode != 0 {
  150. toupdate["contract_code"] = req.ContractCode
  151. }
  152. if req.ContractAmount != 0 {
  153. toupdate["contract_amount"] = req.ContractAmount
  154. }
  155. if req.InvoiceAmount != 0 {
  156. toupdate["invoice_amount"] = req.InvoiceAmount
  157. }
  158. if req.InvoiceDate != 0 {
  159. toupdate["invoice_date"] = req.InvoiceDate
  160. }
  161. if req.InvoiceType != 0 {
  162. toupdate["invoice_type"] = req.InvoiceType
  163. }
  164. if req.ApproStatus != 0 {
  165. toupdate["appro_status"] = req.ApproStatus
  166. }
  167. if req.InvoiceCode != 0 {
  168. toupdate["invoice_code"] = req.InvoiceCode
  169. }
  170. if req.ActualInvoiceDate != 0 {
  171. toupdate["actual_invoice_date"] = req.ActualInvoiceDate
  172. }
  173. if req.CourierCode != 0 {
  174. toupdate["courier_code"] = req.CourierCode
  175. }
  176. if req.Remark != nil {
  177. toupdate["remark"] = *req.Remark
  178. }
  179. if len(toupdate) != 0 {
  180. toupdate["updated_by"] = int(s.userInfo.Id)
  181. toupdate["updated_name"] = s.userInfo.NickName
  182. toupdate["updated_time"] = gtime.Now()
  183. _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
  184. if err != nil {
  185. return err
  186. }
  187. }
  188. return nil
  189. }
  190. func (s CtrContractInvoiceService) Delete(ctx context.Context, id []int) error {
  191. if len(id) == 0 {
  192. return nil
  193. }
  194. _, err := s.Dao.Where("Id IN (?)", id).Delete()
  195. return err
  196. }