house.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package customer
  2. import (
  3. "zhiyuan/pkg/app"
  4. "zhiyuan/pkg/utils"
  5. "zhiyuan/services/admin"
  6. "zhiyuan/services/aftersale"
  7. "zhiyuan/services/form"
  8. "zhiyuan/services/structs"
  9. "zhiyuan/services/user"
  10. "github.com/gin-gonic/gin"
  11. )
  12. func HouseList(c *gin.Context) {
  13. houseList := make([]*structs.HouseList, 0)
  14. if _, err := user.GetHouseList(map[string]interface{}{"user_id": c.GetInt("userID"), "deleted_at": 0}, nil, app.Page{}, &houseList); err != nil {
  15. app.Error(c, err.Error())
  16. return
  17. }
  18. adminIds := make([]int, 0)
  19. adminMap := make(map[int]string)
  20. for _, v := range houseList {
  21. if v.Designer > 0 {
  22. adminIds = append(adminIds, v.Designer)
  23. }
  24. if v.Supervisor > 0 {
  25. adminIds = append(adminIds, v.Supervisor)
  26. }
  27. }
  28. if len(adminIds) > 0 {
  29. if adminList, err := admin.GetAdmins(map[string]interface{}{"id in": adminIds}, nil, app.Page{}, nil); err == nil {
  30. for _, v := range adminList {
  31. adminMap[v.ID] = v.Username
  32. }
  33. }
  34. }
  35. typeList := make([]*structs.WarrantyType, 0)
  36. aftersale.GetTypeList(map[string]interface{}{"pid": 0, "`show`": 1}, nil, app.Page{}, &typeList)
  37. for _, v := range houseList {
  38. if v.Designer > 0 {
  39. v.DesignerName = adminMap[v.Designer]
  40. }
  41. if v.Supervisor > 0 {
  42. v.SupervisorName = adminMap[v.Supervisor]
  43. }
  44. warrantyType := make([]*structs.WarrantyType, 0)
  45. utils.DeepCopy(&warrantyType, &typeList)
  46. warrantyStart := utils.ToInt64(v.WarrantyStart)
  47. if warrantyStart > 0 {
  48. for _, t := range warrantyType {
  49. user.GetWarrantyPeriod(warrantyStart, t, v)
  50. }
  51. v.WarrantyStart = utils.Date(warrantyStart, "YYYY-MM-DD")
  52. }
  53. v.WarrantyType = warrantyType
  54. }
  55. app.Success(c, houseList)
  56. }
  57. func HouseEdit(c *gin.Context) {
  58. id := utils.StrTo(c.Param("id")).MustInt()
  59. if id <= 0 {
  60. app.Error(c, "house id must be a number")
  61. return
  62. }
  63. var form form.HouseAddByCustomer
  64. if app.Bind(c, &form) != nil {
  65. return
  66. }
  67. form.UserID = c.GetInt("userID")
  68. if err := user.HouseEditByCustomer(form, id); err != nil {
  69. app.ErrorMsg(c, err.Error(), nil)
  70. return
  71. }
  72. app.Success(c, nil)
  73. }
  74. func HouseAdd(c *gin.Context) {
  75. var form form.HouseAddByCustomer
  76. if app.Bind(c, &form) != nil {
  77. return
  78. }
  79. form.UserID = c.GetInt("userID")
  80. houseID, err := user.HouseAddByCustomer(form)
  81. if err != nil {
  82. app.ErrorMsg(c, err.Error(), nil)
  83. return
  84. }
  85. app.Success(c, gin.H{"house_id": houseID})
  86. }