面包店
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 

192 rindas
4.7 KiB

  1. package hdl
  2. import (
  3. "applet/app/admin/enum"
  4. "applet/app/admin/lib/validate"
  5. "applet/app/admin/md"
  6. "applet/app/admin/svc"
  7. "applet/app/db"
  8. "applet/app/db/model"
  9. "applet/app/e"
  10. "github.com/gin-gonic/gin"
  11. "time"
  12. )
  13. func EnterpriseList(c *gin.Context) {
  14. var req md.EnterpriseListReq
  15. err := c.ShouldBindJSON(&req)
  16. if err != nil {
  17. err = validate.HandleValidateErr(err)
  18. err1 := err.(e.E)
  19. e.OutErr(c, err1.Code, err1.Error())
  20. return
  21. }
  22. enterpriseDb := db.EnterpriseDb{}
  23. enterpriseDb.Set()
  24. engine := enterpriseDb.Db.Desc("enterprise.id").Limit(req.Limit, (req.Page-1)*req.Limit)
  25. if req.Name != "" {
  26. engine.And("enterprise.name like ?", "%"+req.Name+"%")
  27. }
  28. if req.Memo != "" {
  29. engine.And("enterprise.memo like ?", "%"+req.Memo+"%")
  30. }
  31. if req.LeadPhone != "" {
  32. engine.And("company.lead_phone like ?", "%"+req.LeadPhone+"%")
  33. }
  34. if req.LeadName != "" {
  35. engine.And("company.lead_name like ?", "%"+req.LeadName+"%")
  36. }
  37. var list []db.EnterpriseWithCompany
  38. count, err := engine.Join("LEFT", "grade", "enterprise.id = grade.enterprise_id").
  39. Join("LEFT", "class", "class.grade_id = grade.id").
  40. FindAndCount(&list)
  41. if err != nil {
  42. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  43. return
  44. }
  45. var result []md.EnterpriseListResp
  46. for _, v := range list {
  47. result = append(result, md.EnterpriseListResp{
  48. Id: v.Enterprise.Id,
  49. State: v.Enterprise.State,
  50. Name: v.Enterprise.Name,
  51. Memo: v.Enterprise.Memo,
  52. LeadName: v.Company.LeadName,
  53. LeadPhone: v.Company.LeadPhone,
  54. CreateAt: v.Enterprise.CreateAt,
  55. UpdateAt: v.Enterprise.UpdateAt,
  56. })
  57. }
  58. e.OutSuc(c, map[string]interface{}{
  59. "list": list,
  60. "total": count,
  61. "state": []map[string]interface{}{
  62. {
  63. "name": enum.EnterpriseState(enum.EnterpriseStateForNormal).String(),
  64. "value": enum.EnterpriseStateForNormal,
  65. },
  66. {
  67. "name": enum.EnterpriseState(enum.EnterpriseStateForFreeze).String(),
  68. "value": enum.EnterpriseStateForFreeze,
  69. },
  70. },
  71. }, nil)
  72. return
  73. }
  74. func EnterpriseUpdateState(c *gin.Context) {
  75. var req md.EnterpriseUpdateStateReq
  76. err := c.ShouldBindJSON(&req)
  77. if err != nil {
  78. err = validate.HandleValidateErr(err)
  79. err1 := err.(e.E)
  80. e.OutErr(c, err1.Code, err1.Error())
  81. return
  82. }
  83. enterpriseDb := db.EnterpriseDb{}
  84. enterpriseDb.Set()
  85. enterprise, err := enterpriseDb.GetEnterprise(req.Id)
  86. if err != nil {
  87. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  88. return
  89. }
  90. if enterprise == nil {
  91. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  92. return
  93. }
  94. now := time.Now()
  95. enterprise.State = req.State
  96. enterprise.UpdateAt = now.Format("2006-01-02 15:04:05")
  97. _, err = enterpriseDb.EnterpriseUpdate(enterprise, "state", "update_at")
  98. if err != nil {
  99. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  100. return
  101. }
  102. e.OutSuc(c, "success", nil)
  103. return
  104. }
  105. func EnterpriseDelete(c *gin.Context) {
  106. var req md.EnterpriseDeleteReq
  107. err := c.ShouldBindJSON(&req)
  108. if err != nil {
  109. err = validate.HandleValidateErr(err)
  110. err1 := err.(e.E)
  111. e.OutErr(c, err1.Code, err1.Error())
  112. return
  113. }
  114. err = svc.EnterpriseDelete(req.EnterpriseIds)
  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. }
  122. func EnterpriseAdd(c *gin.Context) {
  123. var req md.EnterpriseAddReq
  124. err := c.ShouldBindJSON(&req)
  125. if err != nil {
  126. err = validate.HandleValidateErr(err)
  127. err1 := err.(e.E)
  128. e.OutErr(c, err1.Code, err1.Error())
  129. return
  130. }
  131. enterpriseDb := db.EnterpriseDb{}
  132. enterpriseDb.Set()
  133. now := time.Now()
  134. enterprise := model.Enterprise{
  135. Name: req.Name,
  136. CompanyId: req.CompanyId,
  137. State: enum.EnterpriseStateForNormal,
  138. Memo: req.Memo,
  139. CreateAt: now.Format("2006-01-02 15:04:05"),
  140. UpdateAt: now.Format("2006-01-02 15:04:05"),
  141. }
  142. _, err = enterpriseDb.EnterpriseInsert(&enterprise)
  143. if err != nil {
  144. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  145. return
  146. }
  147. e.OutSuc(c, "success", nil)
  148. return
  149. }
  150. func EnterpriseUpdate(c *gin.Context) {
  151. var req md.EnterpriseUpdateReq
  152. err := c.ShouldBindJSON(&req)
  153. if err != nil {
  154. err = validate.HandleValidateErr(err)
  155. err1 := err.(e.E)
  156. e.OutErr(c, err1.Code, err1.Error())
  157. return
  158. }
  159. enterpriseDb := db.EnterpriseDb{}
  160. enterpriseDb.Set()
  161. enterprise, err := enterpriseDb.GetEnterprise(req.Id)
  162. if err != nil {
  163. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  164. return
  165. }
  166. if enterprise == nil {
  167. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  168. return
  169. }
  170. now := time.Now()
  171. enterprise.Name = req.Name
  172. enterprise.Memo = req.Name
  173. enterprise.CompanyId = req.CompanyId
  174. enterprise.UpdateAt = now.Format("2006-01-02 15:04:05")
  175. _, err = enterpriseDb.EnterpriseUpdate(enterprise, "name", "memo", "update_at")
  176. if err != nil {
  177. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  178. return
  179. }
  180. e.OutSuc(c, "success", nil)
  181. return
  182. }