invoice_service.go 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package service
  2. import (
  3. "Xcx_New/models"
  4. "github.com/jinzhu/gorm"
  5. "strconv"
  6. )
  7. func GetInvoiceByOrderId(orgId int64, orderID int64) (*models.ServeInvoice, error) {
  8. var invoice models.ServeInvoice
  9. orderLike := "%," + strconv.FormatInt(orderID, 10) + ",%"
  10. err := readUserDb.Model(&models.ServeInvoice{}).Where("org_id=? and orders LIKE ?", orgId, orderLike).First(&invoice).Error
  11. if err == gorm.ErrRecordNotFound {
  12. return nil, nil
  13. }
  14. if err != nil {
  15. return nil, err
  16. }
  17. return &invoice, nil
  18. }
  19. func CreateInvoice(m *models.ServeInvoice) error {
  20. err := writeUserDb.Create(m).Error
  21. return err
  22. }
  23. func FindUnInvoiceOrders(orgID int64) (list []*models.ServeOrder, err error) {
  24. // 这一段是查询出还没有开票的订单
  25. // SELECT so.* from sgj_users.sgj_serve_order as so WHERE NOT EXISTS
  26. // (select o.id
  27. // from sgj_users.sgj_serve_order as o
  28. // JOIN sgj_users.sgj_serve_invoice as i ON FIND_IN_SET(o.id, i.orders)
  29. // WHERE o.org_id=13 and o.order_status=2 and o.status=1 and so.id=o.id )
  30. err = readUserDb.Table("sgj_serve_order as so").Where("so.org_id=? and so.order_status=2 and so.status=1", orgID).Where("NOT EXISTS (?)", readUserDb.Table("sgj_serve_order as o").Joins("JOIN sgj_serve_invoice as i ON FIND_IN_SET(o.id, i.orders)").Where("o.org_id=? and o.order_status=2 and o.status=1 and i.status=1 and so.id=o.id", orgID).Select("o.id").QueryExpr()).Find(&list).Error
  31. return
  32. }
  33. func FindInvoices(orgID int64) (list []*models.ServeInvoice, err error) {
  34. err = readUserDb.Model(&models.ServeInvoice{}).Where("org_id=? and status=1", orgID).Find(&list).Error
  35. return
  36. }
  37. func FindServeOrdersByIDs(orgId int64, ids []int64) (hads []*models.ServeOrder, nos []*models.ServeOrder, err error) {
  38. err = readUserDb.Table("sgj_serve_order as so").Where("so.id IN (?) and so.org_id=? and so.order_status=2 and so.status=1", ids, orgId).Where("EXISTS (?)", readUserDb.Table("sgj_serve_order as o").Joins("JOIN sgj_serve_invoice as i ON FIND_IN_SET(o.id, i.orders)").Where("so.id IN (?) and o.org_id=? and o.order_status=2 and o.status=1 and i.status=1 and so.id=o.id", ids, orgId).Select("o.id").QueryExpr()).Find(&hads).Error
  39. if err != nil {
  40. return
  41. }
  42. err = readUserDb.Table("sgj_serve_order as so").Where("so.id IN (?) and so.org_id=? and so.order_status=2 and so.status=1", ids, orgId).Where("NOT EXISTS (?)", readUserDb.Table("sgj_serve_order as o").Joins("JOIN sgj_serve_invoice as i ON FIND_IN_SET(o.id, i.orders)").Where("so.id IN (?) and o.org_id=? and o.order_status=2 and o.status=1 and i.status=1 and so.id=o.id", ids, orgId).Select("o.id").QueryExpr()).Find(&nos).Error
  43. if err != nil {
  44. return
  45. }
  46. return
  47. }