面包店
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hdl_company.go 2.7 KiB

8 달 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package hdl
  2. import (
  3. "applet/app/admin/enum"
  4. "applet/app/admin/lib/validate"
  5. "applet/app/admin/md"
  6. "applet/app/db"
  7. "applet/app/db/model"
  8. "applet/app/e"
  9. "applet/app/utils"
  10. "github.com/gin-gonic/gin"
  11. "time"
  12. )
  13. func CompanyList(c *gin.Context) {
  14. companyDb := db.CompanyDb{}
  15. companyDb.Set()
  16. companies, err := companyDb.FindCompany(0, 0)
  17. if err != nil {
  18. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  19. return
  20. }
  21. e.OutSuc(c, map[string]interface{}{
  22. "list": companies,
  23. "state": []map[string]interface{}{
  24. {
  25. "name": "正常",
  26. "value": enum.CompanyStateForNormal,
  27. },
  28. {
  29. "name": "冻结",
  30. "value": enum.CompanyStateForFreeze,
  31. },
  32. },
  33. }, nil)
  34. return
  35. }
  36. func CompanyAdd(c *gin.Context) {
  37. var req md.CompanyAddReq
  38. err := c.ShouldBindJSON(&req)
  39. if err != nil {
  40. err = validate.HandleValidateErr(err)
  41. err1 := err.(e.E)
  42. e.OutErr(c, err1.Code, err1.Error())
  43. return
  44. }
  45. companyDb := db.CompanyDb{}
  46. companyDb.Set()
  47. now := time.Now()
  48. company := model.Company{
  49. Name: req.Name,
  50. State: enum.CompanyStateForNormal,
  51. Memo: req.Memo,
  52. LeadName: req.LeadName,
  53. LeadPhone: req.LeadPhone,
  54. CreateAt: now.Format("2006-01-02 15:04:05"),
  55. UpdateAt: now.Format("2006-01-02 15:04:05"),
  56. }
  57. _, err = companyDb.CompanyInsert(&company)
  58. if err != nil {
  59. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  60. return
  61. }
  62. e.OutSuc(c, "success", nil)
  63. return
  64. }
  65. func CompanyUpdate(c *gin.Context) {
  66. var req md.CompanyUpdateReq
  67. err := c.ShouldBindJSON(&req)
  68. if err != nil {
  69. err = validate.HandleValidateErr(err)
  70. err1 := err.(e.E)
  71. e.OutErr(c, err1.Code, err1.Error())
  72. return
  73. }
  74. companyDb := db.CompanyDb{}
  75. companyDb.Set()
  76. company, err := companyDb.GetCompany(req.Id)
  77. if err != nil {
  78. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  79. return
  80. }
  81. if company == nil {
  82. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  83. return
  84. }
  85. now := time.Now()
  86. company.Name = req.Name
  87. company.LeadName = req.LeadName
  88. company.LeadPhone = req.LeadPhone
  89. if req.State != 0 {
  90. company.State = req.State
  91. }
  92. company.UpdateAt = now.Format("2006-01-02 15:04:05")
  93. _, err = companyDb.CompanyUpdate(company, "name", "memo", "state", "lead_name", "lead_phone", "update_at")
  94. if err != nil {
  95. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  96. return
  97. }
  98. e.OutSuc(c, "success", nil)
  99. return
  100. }
  101. func CompanyDelete(c *gin.Context) {
  102. id := c.Param("id")
  103. companyDb := db.CompanyDb{}
  104. companyDb.Set()
  105. company, err := companyDb.GetCompany(utils.StrToInt(id))
  106. if err != nil {
  107. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  108. return
  109. }
  110. if company == nil {
  111. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  112. return
  113. }
  114. _, err = companyDb.CompanyDelete(id)
  115. if err != nil {
  116. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  117. return
  118. }
  119. e.OutSuc(c, "success", nil)
  120. return
  121. }