|
|
@@ -59,34 +59,38 @@ export class FishService {
|
|
|
}
|
|
|
|
|
|
async findById(id: string): Promise<Fish> {
|
|
|
+ return this.fishRepository.findOneOrFail({ where: { id, delFlag: false } })
|
|
|
+ }
|
|
|
+
|
|
|
+ async findByIdIncludeDeleted(id: string): Promise<Fish> {
|
|
|
return this.fishRepository.findOneOrFail({ where: { id } })
|
|
|
}
|
|
|
|
|
|
async findByName(name: string): Promise<Fish | null> {
|
|
|
- return this.fishRepository.findOne({ where: { name } })
|
|
|
+ return this.fishRepository.findOne({ where: { name, delFlag: false } })
|
|
|
}
|
|
|
|
|
|
async findByUsername(username: string): Promise<Fish | null> {
|
|
|
- return this.fishRepository.findOne({ where: { username } })
|
|
|
+ return this.fishRepository.findOne({ where: { username, delFlag: false } })
|
|
|
}
|
|
|
|
|
|
async findByPhone(phone: string): Promise<Fish | null> {
|
|
|
- return this.fishRepository.findOne({ where: { phone } })
|
|
|
+ return this.fishRepository.findOne({ where: { phone, delFlag: false } })
|
|
|
}
|
|
|
|
|
|
async findByOwnerId(ownerId: number): Promise<Fish[]> {
|
|
|
- return this.fishRepository.find({ where: { ownerId } })
|
|
|
+ return this.fishRepository.find({ where: { ownerId, delFlag: false } })
|
|
|
}
|
|
|
|
|
|
async findByResult(result: ResultEnum): Promise<Fish[]> {
|
|
|
- return this.fishRepository.find({ where: { result } })
|
|
|
+ return this.fishRepository.find({ where: { result, delFlag: false } })
|
|
|
}
|
|
|
|
|
|
async list(query: ListFishQuery): Promise<PaginationResponse<Partial<Fish>>> {
|
|
|
const { page, size, id, name, username, phone, result, ownerId, ownerName, remark, createdAt, loginTime } = query
|
|
|
console.log('query: ', query)
|
|
|
|
|
|
- const whereConditions: any = {}
|
|
|
+ const whereConditions: any = { delFlag: false }
|
|
|
|
|
|
if (id) {
|
|
|
whereConditions.id = id
|
|
|
@@ -185,11 +189,46 @@ export class FishService {
|
|
|
}
|
|
|
|
|
|
async delete(id: string): Promise<void> {
|
|
|
- await this.fishRepository.delete(id)
|
|
|
+ await this.fishRepository.update(id, { delFlag: true })
|
|
|
+
|
|
|
+ try {
|
|
|
+ await this.fishFriendsRepository.update({ fishId: id }, { delFlag: true })
|
|
|
+ this.app.log.info(`Fish ${id} deleted, related FishFriends records also marked as deleted`)
|
|
|
+ } catch (error) {
|
|
|
+ this.app.log.error(`Failed to delete related FishFriends for Fish ${id}: ${error}`)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
async batchDelete(ids: string[]): Promise<void> {
|
|
|
- await this.fishRepository.delete({ id: In(ids) })
|
|
|
+ await this.fishRepository.update({ id: In(ids) }, { delFlag: true })
|
|
|
+
|
|
|
+ try {
|
|
|
+ await this.fishFriendsRepository.update({ fishId: In(ids) }, { delFlag: true })
|
|
|
+ this.app.log.info(`Fish batch deleted (${ids.length} records), related FishFriends records also marked as deleted`)
|
|
|
+ } catch (error) {
|
|
|
+ this.app.log.error(`Failed to delete related FishFriends for Fish batch: ${error}`)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async restore(id: string): Promise<void> {
|
|
|
+ await this.fishRepository.update(id, { delFlag: false })
|
|
|
+
|
|
|
+ try {
|
|
|
+ await this.fishFriendsRepository.update({ fishId: id }, { delFlag: false })
|
|
|
+ this.app.log.info(`Fish ${id} restored, related FishFriends records also restored`)
|
|
|
+ } catch (error) {
|
|
|
+ this.app.log.error(`Failed to restore related FishFriends for Fish ${id}: ${error}`)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async batchRestore(ids: string[]): Promise<void> {
|
|
|
+ await this.fishRepository.update({ id: In(ids) }, { delFlag: false })
|
|
|
+ try {
|
|
|
+ await this.fishFriendsRepository.update({ fishId: In(ids) }, { delFlag: false })
|
|
|
+ this.app.log.info(`Fish batch restored (${ids.length} records), related FishFriends records also restored`)
|
|
|
+ } catch (error) {
|
|
|
+ this.app.log.error(`Failed to restore related FishFriends for Fish batch: ${error}`)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
async batchUpdateOwner(
|
|
|
@@ -236,11 +275,11 @@ export class FishService {
|
|
|
}
|
|
|
|
|
|
async countByResult(result: ResultEnum): Promise<number> {
|
|
|
- return this.fishRepository.count({ where: { result } })
|
|
|
+ return this.fishRepository.count({ where: { result, delFlag: false } })
|
|
|
}
|
|
|
|
|
|
async countByOwnerId(ownerId: number): Promise<number> {
|
|
|
- return this.fishRepository.count({ where: { ownerId } })
|
|
|
+ return this.fishRepository.count({ where: { ownerId, delFlag: false } })
|
|
|
}
|
|
|
|
|
|
async getStatistics(ownerId?: number): Promise<{
|
|
|
@@ -281,6 +320,10 @@ export class FishService {
|
|
|
parameters.ownerId = ownerId
|
|
|
}
|
|
|
|
|
|
+ // 始终排除已删除的记录
|
|
|
+ conditions.push('fish.delFlag = :delFlag')
|
|
|
+ parameters.delFlag = false
|
|
|
+
|
|
|
if (includeTimeRange) {
|
|
|
conditions.push('fish.createdAt >= :startDate', 'fish.createdAt <= :today')
|
|
|
parameters.startDate = startDate
|
|
|
@@ -358,7 +401,7 @@ export class FishService {
|
|
|
}
|
|
|
|
|
|
async exportToExcel(query?: ListFishQuery): Promise<Buffer> {
|
|
|
- const whereConditions: any = {}
|
|
|
+ const whereConditions: any = { delFlag: false }
|
|
|
|
|
|
if (query) {
|
|
|
const { id, name, username, phone, result, ownerName, remark, createdAt, loginTime } = query
|