product_code.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package services
  2. import (
  3. "Medical_ERP/common/actions"
  4. cDto "Medical_ERP/common/dto"
  5. "Medical_ERP/common/global"
  6. db "Medical_ERP/common/initialize"
  7. model2 "Medical_ERP/common/model"
  8. "Medical_ERP/dto"
  9. "Medical_ERP/models"
  10. "errors"
  11. "github.com/beego/beego/v2/core/logs"
  12. "gorm.io/gorm"
  13. )
  14. type ProductCode struct {
  15. }
  16. func ProductCodeNameScopes(name string) func(db *gorm.DB) *gorm.DB {
  17. return func(db *gorm.DB) *gorm.DB {
  18. if len(name) == 0 {
  19. return db
  20. }
  21. return db.Where("product like ? OR enterprise like ? OR batch_number like ?", "%"+name+"%", "%"+name+"%", "%"+name+"%")
  22. }
  23. }
  24. // Insert 创建Enterprise对象
  25. func (e *ProductCode) CreateProductCode(c *dto.GenProductCodePageReq, deptId int) error {
  26. var err error
  27. var data models.ProductCode
  28. err = db.DB.Where("product = ? AND enterprise = ? AND spec = ? AND batch_number = ? AND dept_id = ?",
  29. c.Product, c.Enterprise, c.Spec, c.BatchNumber, deptId).First(&data).Error
  30. if err != nil {
  31. if errors.Is(err, gorm.ErrRecordNotFound) {
  32. err = db.DB.Create(&models.ProductCode{
  33. Product: c.Product,
  34. Enterprise: c.Enterprise,
  35. Spec: c.Spec,
  36. BatchNumber: c.BatchNumber,
  37. ControlBy: model2.ControlBy{
  38. DeptId: deptId,
  39. },
  40. }).Error
  41. if err != nil {
  42. logs.Error("db error: %s", err)
  43. return errors.New("创建产品条码信息失败")
  44. }
  45. } else {
  46. logs.Error("db error: %s", err)
  47. return errors.New("查询产品条码信息失败")
  48. }
  49. }
  50. return nil
  51. }
  52. func (e *ProductCode) GetPage(c *dto.ProductCodePageReq, list *[]models.ProductCode, count *int64, p *actions.DataPermission) error {
  53. var err error
  54. var data models.ProductCode
  55. err = db.DB.Model(&data).
  56. Scopes(
  57. ProductCodeNameScopes(c.Name),
  58. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  59. actions.Permission(data.TableName(), p),
  60. ).
  61. Find(list).
  62. Count(count).Error
  63. if err != nil {
  64. logs.Error("db error: %s ", err)
  65. return global.GetFailedErr
  66. }
  67. return nil
  68. }