From 739914ab0d8553c4f39fc8d0b3c3119899fba9b7 Mon Sep 17 00:00:00 2001 From: snowgitea Date: Wed, 1 Jul 2026 17:57:42 +0800 Subject: [PATCH] chore: src/modules/user/service/address.ts --- src/modules/user/service/address.ts | 63 +++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/modules/user/service/address.ts diff --git a/src/modules/user/service/address.ts b/src/modules/user/service/address.ts new file mode 100644 index 0000000..a478640 --- /dev/null +++ b/src/modules/user/service/address.ts @@ -0,0 +1,63 @@ +import { Init, Inject, Provide } from '@midwayjs/core'; +import { BaseService } from '@cool-midway/core'; +import { Equal, Repository } from 'typeorm'; +import { UserAddressEntity } from '../entity/address'; +import { InjectEntityModel } from '@midwayjs/typeorm'; + +/** + * 地址 + */ +@Provide() +export class UserAddressService extends BaseService { + @InjectEntityModel(UserAddressEntity) + userAddressEntity: Repository; + + @Inject() + ctx; + + @Init() + async init() { + await super.init(); + this.setEntity(this.userAddressEntity); + } + + /** + * 列表信息 + */ + async list() { + return this.userAddressEntity + .createQueryBuilder() + .where('userId = :userId ', { userId: this.ctx.user.id }) + .addOrderBy('isDefault', 'DESC') + .getMany(); + } + + /** + * 修改之后 + * @param data + * @param type + */ + async modifyAfter(data: any, type: 'add' | 'update' | 'delete') { + if (type == 'add' || type == 'update') { + if (data.isDefault) { + await this.userAddressEntity + .createQueryBuilder() + .update() + .set({ isDefault: false }) + .where('userId = :userId ', { userId: this.ctx.user.id }) + .andWhere('id != :id', { id: data.id }) + .execute(); + } + } + } + + /** + * 默认地址 + */ + async default(userId) { + return await this.userAddressEntity.findOneBy({ + userId: Equal(userId), + isDefault: true, + }); + } +}