package jnpf.attendance.service.impl;

import cn.hutool.core.collection.CollUtil;
import jnpf.attendance.mapper.AttendanceClockInPicMapper;
import jnpf.attendance.service.AttendanceClockInPicService;
import jnpf.base.service.SuperServiceImpl;
import jnpf.entity.attendance.AttendanceClockInPic;
import jnpf.model.attendance.vo.ClockInPicVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 外勤打卡图片服务
 *
 * @author yanwenfu
 * @create 2023-11-21
 */
@Slf4j
@Service
public class AttendanceClockInPicServiceImpl extends SuperServiceImpl<AttendanceClockInPicMapper, AttendanceClockInPic> implements AttendanceClockInPicService {
    @Override
    public Map<String, List<AttendanceClockInPic>> getClockInPicByIds(List<String> ids, List<String> appIds) {
        if (CollUtil.isEmpty(ids)) {
            return new HashMap<>();
        }
        List<AttendanceClockInPic> clockInPics = this.lambdaQuery()
                .and(x -> x.in(AttendanceClockInPic::getClockInId, ids)
                        .or()
                        .in(CollUtil.isNotEmpty(appIds), AttendanceClockInPic::getApprovalCode, appIds))
                .orderByDesc(AttendanceClockInPic::getCreatorTime)
                .eq(AttendanceClockInPic::getDeleteMark, Boolean.FALSE)
                .list();
        return CollUtil.isNotEmpty(clockInPics) ? clockInPics.stream().collect(Collectors.groupingBy(AttendanceClockInPic::getClockInId)) : new HashMap<>();
    }

    @Override
    public List<ClockInPicVo> getClockInPicList(String clockInId) {
        List<AttendanceClockInPic> clockInPics = this.lambdaQuery()
                .eq(AttendanceClockInPic::getClockInId, clockInId)
                .orderByDesc(AttendanceClockInPic::getCreatorTime)
                .eq(AttendanceClockInPic::getDeleteMark, Boolean.FALSE)
                .list();
        return CollUtil.isNotEmpty(clockInPics) ? clockInPics.stream().map(item -> ClockInPicVo.builder()
                .clockInId(item.getClockInId())
                .picUrl(item.getPicUrl())
                .build()).collect(Collectors.toList()) : CollUtil.newArrayList();
    }
}
