package jnpf.attendance.service.impl;

import cn.hutool.core.collection.CollUtil;
import jnpf.attendance.mapper.AttendanceCloudAlbumMapper;
import jnpf.attendance.service.AttendanceCloudAlbumService;
import jnpf.base.UserInfo;
import jnpf.base.service.SuperServiceImpl;
import jnpf.entity.attendance.AttendanceCloudAlbum;
import jnpf.enums.attendance.CloudAlbumTypeEnum;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import jnpf.util.attendance.OcsWatermarkUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description: 考勤云相册表
 * @Author: shiTou(他是小石头)
 * @Date: 2024-10-30 10:26
 */
@Slf4j
@Service
public class AttendanceCloudAlbumServiceImpl extends SuperServiceImpl<AttendanceCloudAlbumMapper, AttendanceCloudAlbum> implements AttendanceCloudAlbumService {
    @Override
    public List<String> getDataList() {
        List<AttendanceCloudAlbum> cloudAlbumList = this.lambdaQuery().select(AttendanceCloudAlbum::getPicUrl)
                .eq(AttendanceCloudAlbum::getUserId, UserProvider.getUser().getUserId())
                .ge(AttendanceCloudAlbum::getDeleteTime, new Date())
                .orderByDesc(AttendanceCloudAlbum::getCreatorTime)
                .eq(AttendanceCloudAlbum::getDeleteMark, Boolean.FALSE)
                .list();
        return CollUtil.isNotEmpty(cloudAlbumList) ? cloudAlbumList.stream().map(AttendanceCloudAlbum::getPicUrl).collect(Collectors.toList()) : CollUtil.newArrayList();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchSave(List<String> picUrlList) {
        List<AttendanceCloudAlbum> cloudAlbumList = CollUtil.newArrayList();
        if (CollUtil.isEmpty(picUrlList)) {
            return;
        }
        Date newDate = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(newDate);
        calendar.add(Calendar.DAY_OF_MONTH, 7);
        Date dateSevenDaysAfter = calendar.getTime();
        cloudAlbumList.addAll(picUrlList.stream().map(item -> {
            AttendanceCloudAlbum cloudAlbum = new AttendanceCloudAlbum();
            cloudAlbum.setUserId(UserProvider.getUser().getUserId());
            cloudAlbum.setType(CloudAlbumTypeEnum.FIELD_PERSONNEL.getCode());
            cloudAlbum.setPicUrl(item);
            cloudAlbum.setCreatorTime(newDate);
            cloudAlbum.setDeleteTime(dateSevenDaysAfter);
            return cloudAlbum;
        }).collect(Collectors.toList()));
        if (CollUtil.isNotEmpty(cloudAlbumList)) {
            this.saveBatch(cloudAlbumList);
        }
    }

    @Override
    public String getUserWatermark() {
        UserInfo userInfo = UserProvider.getUser();
        return OcsWatermarkUtils.watermarkByStr(Objects.isNull(userInfo) ?
                null : StringUtil.isNotEmpty(userInfo.getUserName()) ? userInfo.getUserName() :
                StringUtil.isNotEmpty(userInfo.getNickName()) ? userInfo.getNickName() : null);
    }
}
