package service import ( "errors" "strings" "xiaoniaokuaiyan.com/xiaoniao/dal" "xiaoniaokuaiyan.com/xiaoniao/entity" "xiaoniaokuaiyan.com/xiaoniao/util" ) type UserLinkerService struct { dal.IUserLinker } func (srv *UserLinkerService) LinkerGet(id, cid int) (interface{}, error) { return srv.IUserLinker.Get(id, cid) } func (srv *UserLinkerService) LinkerDefault(cid int) (interface{}, error) { linkers, err := srv.IUserLinker.List(cid, true) if err != nil { return nil, err } if len(linkers) >= 1 { return linkers[0], nil } return nil, nil } func (srv *UserLinkerService) LinkerAdd(linker *entity.UserLinker) (interface{}, error) { if err := getCityId(linker); err != nil { return nil, errors.New("wrong city") } return srv.IUserLinker.Save(linker) } func getCityId(linker *entity.UserLinker) error { if linker.CityId == 0 { 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" db := util.GetSqlDB() var tmp = struct { CityId int `db:"city_id"` CountyId int `db:"id"` }{} cityName := strings.TrimRight(linker.City, "市") if linker.County == "" { strSql = "select id as city_id from t_city where name = ?" db.Get(&tmp, strSql, cityName) tmp.CountyId = -1 } else { db.Get(&tmp, strSql, cityName, linker.County) } if tmp.CityId > 0 { linker.CityId = tmp.CityId linker.CountyId = tmp.CountyId } else { return errors.New("wrong city") } } return nil } func (srv *UserLinkerService) LinkerUpdate(linker *entity.UserLinker) (interface{}, error) { if err := getCityId(linker); err != nil { return nil, errors.New("wrong city") } return srv.IUserLinker.Update(linker) } func (srv *UserLinkerService) LinkerList(cid int) (interface{}, error) { return srv.IUserLinker.List(cid, false) } func (srv *UserLinkerService) LinkerDel(linker *entity.UserLinker) (interface{}, error) { return srv.IUserLinker.Remove(linker) } func (srv *UserLinkerService) Relationships() (interface{}, error) { strSql := "select * from t_custom_linker_rel where status = 0;" db := util.GetSqlDB() var rels = []entity.UserLinkerRel{} err := db.Select(&rels, strSql) return rels, err }