123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- package dao
- import (
- "Cold_Logistic/internal/pkg/common/global"
- "Cold_Logistic/internal/server/infra/models"
- "context"
- "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core"
- "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
- "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/util/sliceutil"
- "time"
- )
- // AddressBookStore 发件人表
- type AddressBookStore interface {
- DbBaseStore
- BatchSave(ctx context.Context, values []*models.AddressBook, omit ...string) error
- GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.AddressBook, error)
- Page(ctx context.Context, dto AddressBookPageDTO) ([]AddressBookPageVO, int64, error)
- UpdateDefaultByCreateBy(ctx context.Context, isDefault int, excludeId ...int) error
- FindByIds(ctx context.Context, ids []int) ([]models.AddressBook, error)
- }
- var _ AddressBookStore = &addressBook{}
- type addressBook struct {
- dbBase
- }
- func newAddressBook(ds *DataStore) *addressBook {
- return &addressBook{dbBase: dbBase{
- store: ds,
- baseEntity: &models.AddressBook{},
- }}
- }
- func (ds *DataStore) AddressBook() AddressBookStore {
- return newAddressBook(ds)
- }
- func (a *addressBook) BatchSave(ctx context.Context, values []*models.AddressBook, omit ...string) error {
- if len(values) == 0 {
- return nil
- }
- db := a.store.optionDB(ctx)
- tokenInfo := global.GetTokenInfoFromContext(ctx)
- for i := range values {
- if values[i].Id == 0 {
- values[i].CreatedBy = tokenInfo.AccountId
- values[i].CreatedTime.Time = time.Now()
- values[i].Deleted = models.DeleteNo
- } else {
- values[i].UpdatedBy = tokenInfo.AccountId
- values[i].UpdatedTime.Time = time.Now()
- }
- }
- return db.Table(a.baseEntity.TableName()).Omit(omit...).Save(&values).Error
- }
- func (a *addressBook) GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.AddressBook, error) {
- res := make(map[int]models.AddressBook, len(ids))
- if len(ids) == 0 {
- return res, nil
- }
- var list []models.AddressBook
- db := a.store.optionDB(ctx)
- err := db.Table(a.baseEntity.TableName()).
- Where("deleted = ?", models.DeleteNo).
- Where("id IN (?)", ids).
- Omit(omit...).
- Find(&list).Error
- if err != nil {
- return res, err
- }
- for i := range list {
- res[list[i].Id] = list[i]
- }
- return res, nil
- }
- type AddressBookPageDTO struct {
- Page core.Page
- AddressType string // 地址类型
- }
- type AddressBookPageVO struct {
- AddressId int `gorm:"column:id" json:"addressId"` //
- AddressType string `gorm:"column:address_type" json:"addressType"` // 地址类型:sender-发货人 consignee-收货人
- Name string `gorm:"column:name" json:"name"` // 姓名
- Phone string `gorm:"column:phone" json:"phone"` // 联系电话
- ProvinceId int `gorm:"column:province_id" json:"provinceId"` // 省Id
- Province string `gorm:"-" json:"province"` // 省名称
- CityId int `gorm:"column:city_id" json:"cityId"` // 市Id
- City string `gorm:"-" json:"city"` // 市名称
- RegionId int `gorm:"column:region_id" json:"regionId"` // 区Id
- Region string `gorm:"-" json:"region"` // 地区名称
- Address string `gorm:"column:address" json:"address"` // 详细地址
- IsDefault int `gorm:"column:is_default" json:"isDefault"` // 是否默认:1-是 2-否
- }
- func (a *addressBook) Page(ctx context.Context, dto AddressBookPageDTO) ([]AddressBookPageVO, int64, error) {
- db := a.store.optionDB(ctx, a.store.withAccountDataPermis(ctx, "created_by"))
- stmt := db.Model(&models.AddressBook{}).
- Where("deleted = ?", models.DeleteNo).
- Where("address_type = ?", dto.AddressType)
- stmt.Select("id, address_type, name, phone, province_id, city_id, region_id, address, is_default")
- var total int64
- var ret []AddressBookPageVO
- if err := stmt.Count(&total).Error; err != nil {
- return nil, 0, errors.WithStackOnce(err)
- }
- if total == 0 {
- return ret, total, nil
- }
- stmt.Limit(dto.Page.Size).Offset(dto.Page.Offset)
- stmt.Order("is_default ASC, id DESC")
- if err := stmt.Find(&ret).Error; err != nil {
- return nil, 0, errors.WithStackOnce(err)
- }
- regionIds := make([]int, 0, total/3)
- for _, v := range ret {
- if v.ProvinceId > 0 && !sliceutil.ContainInt(regionIds, v.ProvinceId) {
- regionIds = append(regionIds, v.ProvinceId)
- }
- if v.CityId > 0 && !sliceutil.ContainInt(regionIds, v.CityId) {
- regionIds = append(regionIds, v.CityId)
- }
- if v.RegionId > 0 && !sliceutil.ContainInt(regionIds, v.RegionId) {
- regionIds = append(regionIds, v.RegionId)
- }
- }
- regionName, err := a.store.Region().FindNameMapByIds(ctx, regionIds)
- if err != nil {
- return nil, 0, errors.WithStackOnce(err)
- }
- for i := range ret {
- ret[i].Province = regionName[ret[i].ProvinceId]
- ret[i].City = regionName[ret[i].CityId]
- ret[i].Region = regionName[ret[i].RegionId]
- }
- return ret, total, nil
- }
- // UpdateDefaultByCreateBy 修改我的默认选中
- func (a *addressBook) UpdateDefaultByCreateBy(ctx context.Context, isDefault int, excludeId ...int) error {
- accId := global.GetTokenInfoFromContext(ctx).AccountId
- ent := models.AddressBook{
- UpdatedTime: models.NewMyTime(time.Now()),
- UpdatedBy: accId,
- IsDefault: isDefault,
- }
- stmt := a.store.optionDB(ctx).Table(a.baseEntity.TableName()).
- Where("deleted = ?", models.DeleteNo).
- Where("created_by = ?", accId)
- if len(excludeId) > 0 {
- stmt.Where("id NOT IN(?)", excludeId)
- }
- err := stmt.Updates(&ent).Error
- return errors.WithStackOnce(err)
- }
- // FindByIds id批量查
- func (a *addressBook) FindByIds(ctx context.Context, ids []int) ([]models.AddressBook, error) {
- ret := make([]models.AddressBook, 0, len(ids))
- err := a.store.optionDB(ctx).Table(a.baseEntity.TableName()).
- Where("deleted = ?", models.DeleteNo).
- Where("id IN(?)", ids).Find(&ret).Error
- return ret, errors.WithStackOnce(err)
- }
|