userlinker_service.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package service
  2. import (
  3. "errors"
  4. "strings"
  5. "xiaoniaokuaiyan.com/xiaoniao/dal"
  6. "xiaoniaokuaiyan.com/xiaoniao/entity"
  7. "xiaoniaokuaiyan.com/xiaoniao/util"
  8. )
  9. type UserLinkerService struct {
  10. dal.IUserLinker
  11. }
  12. func (srv *UserLinkerService) LinkerGet(id, cid int) (interface{}, error) {
  13. return srv.IUserLinker.Get(id, cid)
  14. }
  15. func (srv *UserLinkerService) LinkerDefault(cid int) (interface{}, error) {
  16. linkers, err := srv.IUserLinker.List(cid, true)
  17. if err != nil {
  18. return nil, err
  19. }
  20. if len(linkers) >= 1 {
  21. return linkers[0], nil
  22. }
  23. return nil, nil
  24. }
  25. func (srv *UserLinkerService) LinkerAdd(linker *entity.UserLinker) (interface{}, error) {
  26. if err := getCityId(linker); err != nil {
  27. return nil, errors.New("wrong city")
  28. }
  29. return srv.IUserLinker.Save(linker)
  30. }
  31. func getCityId(linker *entity.UserLinker) error {
  32. if linker.CityId == 0 {
  33. strSql := "select t2.id, city_id from t_city t1 left join t_county t2 on t1.id = t2.city_id where t1.name = ? and t2.name = ? limit 1"
  34. db := util.GetSqlDB()
  35. var tmp = struct {
  36. CityId int `db:"city_id"`
  37. CountyId int `db:"id"`
  38. }{}
  39. cityName := strings.TrimRight(linker.City, "市")
  40. if linker.County == "" {
  41. strSql = "select id as city_id from t_city where name = ?"
  42. db.Get(&tmp, strSql, cityName)
  43. tmp.CountyId = -1
  44. } else {
  45. db.Get(&tmp, strSql, cityName, linker.County)
  46. }
  47. if tmp.CityId > 0 {
  48. linker.CityId = tmp.CityId
  49. linker.CountyId = tmp.CountyId
  50. } else {
  51. return errors.New("wrong city")
  52. }
  53. }
  54. return nil
  55. }
  56. func (srv *UserLinkerService) LinkerUpdate(linker *entity.UserLinker) (interface{}, error) {
  57. if err := getCityId(linker); err != nil {
  58. return nil, errors.New("wrong city")
  59. }
  60. return srv.IUserLinker.Update(linker)
  61. }
  62. func (srv *UserLinkerService) LinkerList(cid int) (interface{}, error) {
  63. return srv.IUserLinker.List(cid, false)
  64. }
  65. func (srv *UserLinkerService) LinkerDel(linker *entity.UserLinker) (interface{}, error) {
  66. return srv.IUserLinker.Remove(linker)
  67. }
  68. func (srv *UserLinkerService) Relationships() (interface{}, error) {
  69. strSql := "select * from t_custom_linker_rel where status = 0;"
  70. db := util.GetSqlDB()
  71. var rels = []entity.UserLinkerRel{}
  72. err := db.Select(&rels, strSql)
  73. return rels, err
  74. }