面包店
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

193 行
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", "company", "enterprise.company_id = company.id").
  39. FindAndCount(&list)
  40. if err != nil {
  41. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  42. return
  43. }
  44. var result []md.EnterpriseListResp
  45. for _, v := range list {
  46. result = append(result, md.EnterpriseListResp{
  47. Id: v.Enterprise.Id,
  48. State: v.Enterprise.State,
  49. Name: v.Enterprise.Name,
  50. Memo: v.Enterprise.Memo,
  51. CompanyName: v.Company.Name,
  52. CompanyId: v.Company.Id,
  53. LeadName: v.Company.LeadName,
  54. LeadPhone: v.Company.LeadPhone,
  55. CreateAt: v.Enterprise.CreateAt,
  56. UpdateAt: v.Enterprise.UpdateAt,
  57. })
  58. }
  59. e.OutSuc(c, map[string]interface{}{
  60. "list": result,
  61. "total": count,
  62. "state": []map[string]interface{}{
  63. {
  64. "name": enum.EnterpriseState(enum.EnterpriseStateForNormal).String(),
  65. "value": enum.EnterpriseStateForNormal,
  66. },
  67. {
  68. "name": enum.EnterpriseState(enum.EnterpriseStateForFreeze).String(),
  69. "value": enum.EnterpriseStateForFreeze,
  70. },
  71. },
  72. }, nil)
  73. return
  74. }
  75. func EnterpriseUpdateState(c *gin.Context) {
  76. var req md.EnterpriseUpdateStateReq
  77. err := c.ShouldBindJSON(&req)
  78. if err != nil {
  79. err = validate.HandleValidateErr(err)
  80. err1 := err.(e.E)
  81. e.OutErr(c, err1.Code, err1.Error())
  82. return
  83. }
  84. enterpriseDb := db.EnterpriseDb{}
  85. enterpriseDb.Set()
  86. enterprise, err := enterpriseDb.GetEnterprise(req.Id)
  87. if err != nil {
  88. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  89. return
  90. }
  91. if enterprise == nil {
  92. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  93. return
  94. }
  95. now := time.Now()
  96. enterprise.State = req.State
  97. enterprise.UpdateAt = now.Format("2006-01-02 15:04:05")
  98. _, err = enterpriseDb.EnterpriseUpdate(enterprise, "state", "update_at")
  99. if err != nil {
  100. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  101. return
  102. }
  103. e.OutSuc(c, "success", nil)
  104. return
  105. }
  106. func EnterpriseDelete(c *gin.Context) {
  107. var req md.EnterpriseDeleteReq
  108. err := c.ShouldBindJSON(&req)
  109. if err != nil {
  110. err = validate.HandleValidateErr(err)
  111. err1 := err.(e.E)
  112. e.OutErr(c, err1.Code, err1.Error())
  113. return
  114. }
  115. err = svc.EnterpriseDelete(req.EnterpriseIds)
  116. if err != nil {
  117. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  118. return
  119. }
  120. e.OutSuc(c, "success", nil)
  121. return
  122. }
  123. func EnterpriseAdd(c *gin.Context) {
  124. var req md.EnterpriseAddReq
  125. err := c.ShouldBindJSON(&req)
  126. if err != nil {
  127. err = validate.HandleValidateErr(err)
  128. err1 := err.(e.E)
  129. e.OutErr(c, err1.Code, err1.Error())
  130. return
  131. }
  132. enterpriseDb := db.EnterpriseDb{}
  133. enterpriseDb.Set()
  134. now := time.Now()
  135. enterprise := model.Enterprise{
  136. Name: req.Name,
  137. CompanyId: req.CompanyId,
  138. State: enum.EnterpriseStateForNormal,
  139. Memo: req.Memo,
  140. CreateAt: now.Format("2006-01-02 15:04:05"),
  141. UpdateAt: now.Format("2006-01-02 15:04:05"),
  142. }
  143. _, err = enterpriseDb.EnterpriseInsert(&enterprise)
  144. if err != nil {
  145. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  146. return
  147. }
  148. e.OutSuc(c, "success", nil)
  149. return
  150. }
  151. func EnterpriseUpdate(c *gin.Context) {
  152. var req md.EnterpriseUpdateReq
  153. err := c.ShouldBindJSON(&req)
  154. if err != nil {
  155. err = validate.HandleValidateErr(err)
  156. err1 := err.(e.E)
  157. e.OutErr(c, err1.Code, err1.Error())
  158. return
  159. }
  160. enterpriseDb := db.EnterpriseDb{}
  161. enterpriseDb.Set()
  162. enterprise, err := enterpriseDb.GetEnterprise(req.Id)
  163. if err != nil {
  164. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  165. return
  166. }
  167. if enterprise == nil {
  168. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  169. return
  170. }
  171. now := time.Now()
  172. enterprise.Name = req.Name
  173. enterprise.Memo = req.Name
  174. enterprise.CompanyId = req.CompanyId
  175. enterprise.UpdateAt = now.Format("2006-01-02 15:04:05")
  176. _, err = enterpriseDb.EnterpriseUpdate(enterprise, "name", "memo", "update_at")
  177. if err != nil {
  178. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  179. return
  180. }
  181. e.OutSuc(c, "success", nil)
  182. return
  183. }