package com.sonic.shark.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.sonic.common.utils.CloudFrontSignerUtils;
import com.sonic.shark.dao.BlurryImgRecordDao;
import com.sonic.shark.domain.entity.BlurryImgRecord;
import com.sonic.shark.enums.S3BucketNameEnum;
import com.sonic.shark.lib.output.BlurryRecordOutput;
import com.sonic.shark.service.BlurryImgRecordService;
import com.sonic.shark.utils.BeanConvert;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 模糊图片访问地址生成记录表
 *
 * @author code
 */
@Slf4j
@Service
public class BlurryImgRecordServiceImpl extends ServiceImpl<BlurryImgRecordDao, BlurryImgRecord> implements BlurryImgRecordService {

    private static final Long EXP_TIME_2037 = 2114227200000L;

    @Autowired
    private CloudFrontSignerUtils cloudFrontSignerUtils;

    @Override
    public BlurryImgRecord saveRecord(S3BucketNameEnum s3BucketNameEnum, String sourceImgPath, String imgPath, String bizType, Long bizId, Long userId) throws Exception {
        BlurryImgRecord record = new BlurryImgRecord();
        record.setBizType(bizType);
        record.setBizId(bizId);
        record.setSourceImgPath(sourceImgPath);
        record.setImgPath(imgPath);
        //生成图片访问的签名链接
        String fileUrl = s3BucketNameEnum.getRootUrl() + imgPath;
        String img1Url = cloudFrontSignerUtils.signer(fileUrl + CloudFrontSignerUtils.BLURRY_IMG_468_600, new Date(EXP_TIME_2037), null);
        String img2Url = cloudFrontSignerUtils.signer(fileUrl + CloudFrontSignerUtils.BLURRY_IMG_800_800, new Date(EXP_TIME_2037), null);
        String img3Url = cloudFrontSignerUtils.signer(fileUrl + CloudFrontSignerUtils.BLURRY_ORG_IMG, new Date(EXP_TIME_2037), null);
        record.setImg1(img1Url);
        record.setImg2(img2Url);
        record.setImg3(img3Url);
        record.setCreatorId(userId);
        record.setCreateTime(LocalDateTime.now());
        save(record);
        return record;
    }

    @Override
    public List<BlurryRecordOutput> queryBlurryImgList(String bizType, List<Long> bizIdList, Boolean imgUrlBl) {
        if (StringUtils.isEmpty(bizType) || CollectionUtils.isEmpty(bizIdList)) {
            return Lists.newArrayList();
        }
        List<BlurryImgRecord> list = list(Wrappers.<BlurryImgRecord>lambdaQuery()
                .select(BlurryImgRecord::getBizId, BlurryImgRecord::getImgPath, BlurryImgRecord::getImg1, BlurryImgRecord::getImg2, BlurryImgRecord::getImg3)
                .eq(BlurryImgRecord::getBizType, bizType)
                .in(BlurryImgRecord::getBizId, bizIdList));
        List<BlurryRecordOutput> outputList = BeanConvert.copeList(list, BlurryRecordOutput.class);
        if(imgUrlBl != null && imgUrlBl) {
            S3BucketNameEnum s3BucketNameEnum = S3BucketNameEnum.BLURRY;
            for (BlurryRecordOutput output : outputList) {
                output.setImgUrl(s3BucketNameEnum.getRootUrl() + output.getImgPath());
            }
        }
        return outputList;
    }


}
