import { CoolController, BaseController } from '@cool-midway/core';
import { PushSorterAttendanceEntity } from '../../entity/sorterattendance';
import { PushSorterAttendanceService } from '../../service/sorterattendance';
import { BaseSysDepartmentEntity } from '../../../base/entity/sys/department'
import { WithDeptFilterWhere } from '../../../base/middleware/with-dept-filter-where'

/**
 * 分拣员轨迹管理
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: PushSorterAttendanceEntity,
  service: PushSorterAttendanceService,
  pageQueryOp: {
    select: ['a.*', 'b.name as departmentName'],
    join: [
      {
        entity: BaseSysDepartmentEntity,
        alias: 'b',
        condition: 'a.departmentId = b.id',
        type: 'leftJoin',
      },
    ],
    fieldEq: ['a.sorterId', 'a.departmentId'],
    where: async ctx => {
      const { startTime, endTime } = ctx.request.body;
      const conditions:any[][] = [];
      if (startTime && endTime) {
        conditions.push(['a.uploadTime BETWEEN :startTime AND :endTime', { startTime, endTime }]);
      }
      conditions.push(...(await WithDeptFilterWhere(ctx, { alias: 'a', field: 'departmentId' })));
      return conditions;
    },
  },
})
export class PushSorterAttendanceController extends BaseController {}