|
|
@@ -9,6 +9,10 @@ import { UserQrCode } from '../entities/user-qr-code.entity'
|
|
|
import { User } from '../entities/user.entity'
|
|
|
import { PaginationResponse } from '../dto/common.dto'
|
|
|
import { FileService } from './file.service'
|
|
|
+import { PersonInfoDto } from '../dto/qr-code.dto'
|
|
|
+import { PetInfoDto } from '../dto/qr-code.dto'
|
|
|
+import { GoodsInfoDto } from '../dto/qr-code.dto'
|
|
|
+import { LinkInfoDto } from '../dto/qr-code.dto'
|
|
|
|
|
|
export class QrCodeService {
|
|
|
private qrCodeRepository: Repository<QrCode>
|
|
|
@@ -162,12 +166,10 @@ export class QrCodeService {
|
|
|
info = await this.linkInfoRepository.findOne({ where: { qrCodeId: entity.id } })
|
|
|
}
|
|
|
|
|
|
- // 先设置 isVisible 状态
|
|
|
if (info) {
|
|
|
isVisible = info.isVisible ?? true
|
|
|
}
|
|
|
|
|
|
- // 处理图片签名URL
|
|
|
if (info && 'photoUrl' in info && info.photoUrl) {
|
|
|
try {
|
|
|
if (!info.photoUrl.startsWith('http')) {
|
|
|
@@ -183,7 +185,6 @@ export class QrCodeService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 如果不是管理员且 isVisible 为 false,只返回图片 URL
|
|
|
if (!isAdmin && info && info.isVisible === false) {
|
|
|
const photoUrl = 'photoUrl' in info ? info.photoUrl : ''
|
|
|
info = { photoUrl }
|
|
|
@@ -247,6 +248,8 @@ export class QrCodeService {
|
|
|
isActivated?: boolean | string,
|
|
|
startDate?: string,
|
|
|
endDate?: string,
|
|
|
+ ownerId?: number,
|
|
|
+ ownerName?: string,
|
|
|
page: number = 0,
|
|
|
pageSize: number = 20
|
|
|
): Promise<PaginationResponse<any>> {
|
|
|
@@ -278,6 +281,20 @@ export class QrCodeService {
|
|
|
baseQueryBuilder.andWhere('qrCode.createdAt <= :end', { end })
|
|
|
}
|
|
|
|
|
|
+ if (ownerId || ownerName) {
|
|
|
+ baseQueryBuilder
|
|
|
+ .leftJoin(UserQrCode, 'uq', 'uq.qrCodeId = qrCode.id AND uq.isDeleted = :isDeleted', { isDeleted: false })
|
|
|
+ .leftJoin(User, 'owner', 'owner.id = uq.userId')
|
|
|
+
|
|
|
+ if (ownerId) {
|
|
|
+ baseQueryBuilder.andWhere('owner.id = :ownerId', { ownerId })
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ownerName) {
|
|
|
+ baseQueryBuilder.andWhere('owner.name = :ownerName', { ownerName })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
const [qrCodes, total] = await baseQueryBuilder
|
|
|
.clone()
|
|
|
.skip(page * pageSize)
|
|
|
@@ -329,6 +346,40 @@ export class QrCodeService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ async updateQrCode(
|
|
|
+ id: number,
|
|
|
+ maintenanceCode?: string,
|
|
|
+ remark?: string,
|
|
|
+ info?: PersonInfoDto | PetInfoDto | GoodsInfoDto | LinkInfoDto
|
|
|
+ ): Promise<void> {
|
|
|
+ await this.app.dataSource.transaction(async transactionalEntityManager => {
|
|
|
+ const qrCode = await transactionalEntityManager.findOne(QrCode, { where: { id } })
|
|
|
+ if (!qrCode) {
|
|
|
+ throw new Error('二维码不存在')
|
|
|
+ }
|
|
|
+
|
|
|
+ if (maintenanceCode) {
|
|
|
+ qrCode.maintenanceCode = maintenanceCode
|
|
|
+ }
|
|
|
+
|
|
|
+ if (remark !== undefined) {
|
|
|
+ qrCode.remark = remark
|
|
|
+ }
|
|
|
+
|
|
|
+ // 关联信息
|
|
|
+ if (info) {
|
|
|
+ await this.updateQrCodeInfo(transactionalEntityManager, qrCode, info)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!qrCode.isActivated) {
|
|
|
+ qrCode.isActivated = true
|
|
|
+ qrCode.activatedAt = new Date()
|
|
|
+ }
|
|
|
+
|
|
|
+ await transactionalEntityManager.save(QrCode, qrCode)
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 根据日期获取二维码列表(用于下载)
|
|
|
*/
|
|
|
@@ -475,27 +526,6 @@ export class QrCodeService {
|
|
|
await this.userQrCodeRepository.save(userQrCode)
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 更新二维码信息
|
|
|
- */
|
|
|
- async updateQrCode(qrCode: string, maintenanceCode: string, data: { remark?: string }): Promise<QrCode> {
|
|
|
- const isValid = await this.verifyMaintenanceCode(qrCode, maintenanceCode)
|
|
|
- if (!isValid) {
|
|
|
- throw new Error('维护码错误')
|
|
|
- }
|
|
|
-
|
|
|
- const entity = await this.qrCodeRepository.findOne({ where: { qrCode } })
|
|
|
- if (!entity) {
|
|
|
- throw new Error('二维码不存在')
|
|
|
- }
|
|
|
-
|
|
|
- if (data.remark !== undefined) {
|
|
|
- entity.remark = data.remark
|
|
|
- }
|
|
|
-
|
|
|
- return await this.qrCodeRepository.save(entity)
|
|
|
- }
|
|
|
-
|
|
|
async getUserQrCodes(userId: number, page: number = 0, pageSize: number = 20): Promise<PaginationResponse<any>> {
|
|
|
const [content, total] = await this.userQrCodeRepository
|
|
|
.createQueryBuilder('userQrCode')
|
|
|
@@ -567,4 +597,51 @@ export class QrCodeService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新二维码关联信息
|
|
|
+ */
|
|
|
+ private async updateQrCodeInfo(
|
|
|
+ transactionalEntityManager: any,
|
|
|
+ qrCode: QrCode,
|
|
|
+ info: PersonInfoDto | PetInfoDto | GoodsInfoDto | LinkInfoDto
|
|
|
+ ): Promise<void> {
|
|
|
+ switch (qrCode.qrType) {
|
|
|
+ case QrType.PERSON:
|
|
|
+ await this.updateOrCreateInfo(transactionalEntityManager, PersonInfo, qrCode.id, info as PersonInfoDto)
|
|
|
+ break
|
|
|
+ case QrType.PET:
|
|
|
+ await this.updateOrCreateInfo(transactionalEntityManager, PetInfo, qrCode.id, info as PetInfoDto)
|
|
|
+ break
|
|
|
+ case QrType.GOODS:
|
|
|
+ await this.updateOrCreateInfo(transactionalEntityManager, GoodsInfo, qrCode.id, info as GoodsInfoDto)
|
|
|
+ break
|
|
|
+ case QrType.LINK:
|
|
|
+ await this.updateOrCreateInfo(transactionalEntityManager, LinkInfo, qrCode.id, info as LinkInfoDto)
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新或创建 Info 方法
|
|
|
+ */
|
|
|
+ private async updateOrCreateInfo<T>(
|
|
|
+ transactionalEntityManager: any,
|
|
|
+ entityClass: new () => T,
|
|
|
+ qrCodeId: number,
|
|
|
+ info: any
|
|
|
+ ): Promise<void> {
|
|
|
+ const repository = transactionalEntityManager.getRepository(entityClass)
|
|
|
+ const existingInfo = await repository.findOne({ where: { qrCodeId } })
|
|
|
+
|
|
|
+ if (existingInfo) {
|
|
|
+ await repository.update((existingInfo as any).id, info)
|
|
|
+ } else {
|
|
|
+ const newInfo = repository.create({
|
|
|
+ ...info,
|
|
|
+ qrCodeId
|
|
|
+ })
|
|
|
+ await repository.save(newInfo)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|