123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package message
  2. import (
  3. "spaceofcheng/services/models"
  4. "spaceofcheng/services/models/course"
  5. "spaceofcheng/services/models/model"
  6. "spaceofcheng/services/utils"
  7. "strconv"
  8. )
  9. const (
  10. ISSYS = 0
  11. )
  12. // GetLocationList 获取位置字典
  13. func (m *MessageDAO) GetLocationList(issys, org_id string) ([]model.TdCmsImageLocation, error) {
  14. var locations []model.TdCmsImageLocation
  15. s := m.db.Where("status>"+strconv.Itoa(models.STATUS_DEL)).And("org_id=?", org_id)
  16. if issys != "" {
  17. s = s.And("is_sys=?", issys)
  18. }
  19. err := s.Asc("order_no").Find(&locations)
  20. return locations, err
  21. }
  22. // GetLocationMaxSort 获取最大的位置
  23. func (m *MessageDAO) GetLocationMaxSort(issys, org_id string) (int, error) {
  24. var location []map[string][]byte
  25. sql := `select max(order_no) as order_no from td_cms_image_location where is_sys=` + issys + ` and org_id ='` + org_id + `'`
  26. location, err := m.db.Query(sql)
  27. if err != nil {
  28. return 0, err
  29. }
  30. if len(location) > 0 {
  31. sort := string(location[0]["order_no"])
  32. if sort != "" {
  33. sortno, err := strconv.Atoi(string(location[0]["order_no"]))
  34. return sortno, err
  35. }
  36. return 0, err
  37. }
  38. return 0, nil
  39. }
  40. // GetLocationById 根据ID获取明细
  41. func (m *MessageDAO) GetLocationById(id string) (*model.TdCmsImageLocation, error) {
  42. var location []model.TdCmsImageLocation
  43. err := m.db.Where("status>"+strconv.Itoa(models.STATUS_DEL)).And("location_id=?", id).Find(&location)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return &location[0], nil
  48. }
  49. // AddLocation 新增
  50. func (m *MessageDAO) AddLocation(location model.TdCmsImageLocation) (*model.TdCmsImageLocation, error) {
  51. location.LocationId = utils.GetGUID()
  52. location.Status = models.STATUS_NORMAL
  53. location.IsSys = ISSYS
  54. _, err := m.db.Insert(location)
  55. return &location, err
  56. }
  57. // UpdateLocation 修改
  58. func (m *MessageDAO) UpdateLocation(location model.TdCmsImageLocation) error {
  59. var cols = []string{
  60. "location_name",
  61. "location_code",
  62. "title",
  63. "consult",
  64. "type_img",
  65. "order_no",
  66. }
  67. _, err := m.db.Cols(cols...).Where("location_id=?", location.LocationId).Update(location)
  68. return err
  69. }
  70. // DelLocation 删除
  71. func (m *MessageDAO) DelLocation(location_id string) error {
  72. var location = model.TdCmsImageLocation{
  73. LocationId: location_id,
  74. Status: models.STATUS_DEL,
  75. }
  76. var cols = []string{
  77. "status",
  78. }
  79. _, err := m.db.Cols(cols...).Where("location_id=?", location.LocationId).Update(location)
  80. return err
  81. }
  82. // UpdateLocationSort 修改顺序
  83. func (m *MessageDAO) UpdateLocationSort(loc []string) error {
  84. orderno, err := strconv.Atoi(loc[1])
  85. if err != nil {
  86. return err
  87. }
  88. var location = model.TdCmsImageLocation{
  89. LocationId: loc[0],
  90. OrderNo: orderno,
  91. }
  92. var cols = []string{
  93. "order_no",
  94. }
  95. _, err = m.db.Cols(cols...).Where("location_id=?", location.LocationId).Update(location)
  96. return err
  97. }
  98. // UpdateLocationSortByID 根据id修改之后的顺序
  99. func (m *MessageDAO) UpdateLocationSortByID(orderno int) error {
  100. sql := `update td_cms_image_location set order_no = order_no - 1 where order_no > ` + strconv.Itoa(orderno)
  101. _, err := m.db.Exec(sql)
  102. return err
  103. }
  104. // LocationInfo 详情
  105. type LocationInfo struct {
  106. model.TdCmsImageLocation `xorm:"extends"`
  107. Banners []model.TaCmsImages
  108. News []model.TaCmsNews
  109. Courses []course.CourseWithCase
  110. }
  111. // GetLocationInfoList 获取列表
  112. func (m *MessageDAO) GetLocationInfoList(orgid string) ([]LocationInfo, error) {
  113. var locations []LocationInfo
  114. sql := `select * from td_cms_image_location where status>? and location_id<>'index' and org_id=? order by order_no asc`
  115. err := m.db.Sql(sql, models.STATUS_DEL, orgid).Find(&locations)
  116. if err != nil {
  117. return nil, err
  118. }
  119. return locations, nil
  120. }