소스 검색

feat: 批量编辑

x1ongzhu 1 년 전
부모
커밋
f1b7e6d4d4
3개의 변경된 파일39개의 추가작업 그리고 15개의 파일을 삭제
  1. 3 3
      src/device/device.controller.ts
  2. 33 12
      src/device/device.service.ts
  3. 3 0
      src/device/entities/device.entity.ts

+ 3 - 3
src/device/device.controller.ts

@@ -29,8 +29,8 @@ export class DeviceController {
         return await this.deviceService.pinCountry(body.ids, body.country)
     }
 
-    @Post('/:id')
-    async updateDevice(@Param('id') id: string, @Body() data: any) {
-        return await this.deviceService.updateDevice1(id, data.pinCountry, data.clashProfile)
+    @Post('/update')
+    async updateDevice(@Body() data: any) {
+        return await this.deviceService.updateDevice1(data)
     }
 }

+ 33 - 12
src/device/device.service.ts

@@ -66,19 +66,40 @@ export class DeviceService {
         }
     }
 
-    async updateDevice1(id: string, country: string, clashProfile: string) {
-        let device = await this.deviceRepository.findOneBy({ id })
-        if (device) {
-            device.pinCountry = country
-            device.clashProfile = clashProfile
-            if (device.pinCountry && !device.clashProfile) {
-                const profiles = await this.ipmoyuProvider.genProfiles(device.pinCountry, 1)
-                device.clashProfile = profiles[0]
-            } else if (device.pinCountry === '') {
-                device.clashProfile = ''
-            }
-            await this.deviceRepository.save(device)
+    async updateDevice1({
+        id,
+        pinCountry,
+        matchCountry,
+        clashProfile
+    }: {
+        id: string | string[]
+        pinCountry: string
+        matchCountry: Boolean
+        clashProfile: string
+    }) {
+        if (!id) {
+            return
+        }
+        let devices = []
+        if (Array.isArray(id)) {
+            devices = await this.deviceRepository.findBy({ id: In(id) })
+        } else {
+            devices = [await this.deviceRepository.findOneBy({ id })]
         }
+        await Promise.all(
+            devices.map(async (device) => {
+                device.pinCountry = pinCountry
+                device.matchCountry = matchCountry
+                device.clashProfile = clashProfile
+                if (device.pinCountry && !device.clashProfile) {
+                    const profiles = await this.ipmoyuProvider.genProfiles(device.pinCountry, 1)
+                    device.clashProfile = profiles[0]
+                } else if (device.pinCountry === '') {
+                    device.clashProfile = ''
+                }
+                return await this.deviceRepository.save(device)
+            })
+        )
     }
 
     async pinCountry(ids: number[], country: string) {

+ 3 - 0
src/device/entities/device.entity.ts

@@ -34,4 +34,7 @@ export class Device {
 
     @Column({ nullable: true })
     currentCountry: string
+
+    @Column({ default: false })
+    matchCountry: boolean
 }