From c19f1f7e21c93d1f13b4f44a8a615f65af577559 Mon Sep 17 00:00:00 2001
From: wangzhibo <wangzhibo@shsening.com>
Date: 星期日, 16 八月 2026 15:10:24 +0800
Subject: [PATCH] 添加 android 端接口

---
 src/modules/push/service/sorterattendance.ts |  104 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 101 insertions(+), 3 deletions(-)

diff --git a/src/modules/push/service/sorterattendance.ts b/src/modules/push/service/sorterattendance.ts
index 1546c35..0852926 100644
--- a/src/modules/push/service/sorterattendance.ts
+++ b/src/modules/push/service/sorterattendance.ts
@@ -1,14 +1,112 @@
 import { Provide } from '@midwayjs/core';
-import { BaseService } from '@cool-midway/core';
+import { BaseService, CoolCommException } from '@cool-midway/core';
 import { InjectEntityModel } from '@midwayjs/typeorm';
 import { Repository } from 'typeorm';
+import * as moment from 'moment';
 import { PushSorterAttendanceEntity } from '../entity/sorterattendance';
+import { BasicdataSorterEntity } from '../../basicdata/entity/sorter';
 
 /**
- * 鍒嗘嫞鍛樺仴搴峰憡璀︽湇鍔�
+ * 鍒嗘嫞鍛樼鍒扮閫�
  */
 @Provide()
 export class PushSorterAttendanceService extends BaseService {
   @InjectEntityModel(PushSorterAttendanceEntity)
   pushSorterAttendanceEntity: Repository<PushSorterAttendanceEntity>;
-}
\ No newline at end of file
+
+  @InjectEntityModel(BasicdataSorterEntity)
+  basicdataSorterEntity: Repository<BasicdataSorterEntity>;
+
+  async getSorter(sorterId: string) {
+    const sorter = await this.basicdataSorterEntity.findOneBy({
+      businessId: String(sorterId || '').trim(),
+    });
+    if (!sorter || sorter.status !== 0) {
+      throw new CoolCommException('鍒嗘嫞鍛樿处鍙蜂笉鍙敤');
+    }
+    return sorter;
+  }
+
+  private locationOf(param: any) {
+    if (param.longitude == null || param.latitude == null) {
+      throw new CoolCommException('缂哄皯瀹氫綅淇℃伅');
+    }
+    return `${Number(param.longitude)},${Number(param.latitude)}`;
+  }
+
+  private today() {
+    return moment().format('YYYY-MM-DD');
+  }
+
+  private async todayRow(idCard: string) {
+    return this.pushSorterAttendanceEntity
+      .createQueryBuilder('a')
+      .where('a.idCard = :idCard', { idCard })
+      .andWhere('a.workDate = :workDate', { workDate: this.today() })
+      .getOne();
+  }
+
+  async checkIn(param: any) {
+    const sorter = await this.getSorter(param.sorterId);
+    const loc = this.locationOf(param);
+    const now = new Date();
+    const workDate = this.today();
+    let row = await this.todayRow(sorter.businessId);
+    if (row?.checkInTime) {
+      throw new CoolCommException('浠婃棩宸茬鍒�');
+    }
+    if (!row) {
+      row = this.pushSorterAttendanceEntity.create({
+        idCard: sorter.businessId,
+        departmentId: sorter.departmentId,
+        uploadTime: now,
+        workDate: workDate as any,
+        checkInTime: now,
+        checkInLocation: loc,
+        workHours: 0,
+        status: '缂哄崱',
+      });
+    } else {
+      row.departmentId = sorter.departmentId;
+      row.uploadTime = now;
+      row.checkInTime = now;
+      row.checkInLocation = loc;
+    }
+    await this.pushSorterAttendanceEntity.save(row);
+    return {
+      message: '绛惧埌鎴愬姛',
+      workDate,
+      checkInTime: now,
+      checkInLocation: loc,
+    };
+  }
+
+  async checkOut(param: any) {
+    const sorter = await this.getSorter(param.sorterId);
+    const loc = this.locationOf(param);
+    const now = new Date();
+    const workDate = this.today();
+    const row = await this.todayRow(sorter.businessId);
+    if (!row?.checkInTime) {
+      throw new CoolCommException('璇峰厛涓婄彮绛惧埌');
+    }
+    if (row.checkOutTime) {
+      throw new CoolCommException('浠婃棩宸茬閫�');
+    }
+    const start = new Date(row.checkInTime).getTime();
+    const hours = Math.max(0, (now.getTime() - start) / 3600000);
+    row.uploadTime = now;
+    row.checkOutTime = now;
+    row.checkOutLocation = loc;
+    row.workHours = Math.round(hours * 100) / 100;
+    row.status = '姝e父';
+    await this.pushSorterAttendanceEntity.save(row);
+    return {
+      message: '绛鹃��鎴愬姛',
+      workDate,
+      checkOutTime: now,
+      checkOutLocation: loc,
+      workHours: row.workHours,
+    };
+  }
+}

--
Gitblit v1.9.1