package com.bonus.app.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.bonus.app.mapper.AppIndexMapper;
import com.bonus.app.service.IAppIndexService;
import com.bonus.common.core.constant.HttpStatus;
import com.bonus.common.core.constant.SecurityConstants;
import com.bonus.common.core.domain.R;
import com.bonus.common.core.utils.encryption.Sm4Utils;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.entity.app.AppParamsDto;
import com.bonus.common.entity.app.vo.CarouseVo;
import com.bonus.common.entity.bracelet.vo.PersonVo;
import com.bonus.system.api.RemoteFileService;
import com.bonus.system.api.domain.SysFile;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

/**
 * @className:AppIndexServiceImpl
 * @author:cwchen
 * @date:2024-08-21-15:56
 * @version:1.0
 * @description:App首页
 */
@Service(value = "IAppIndexService")
@Slf4j
public class AppIndexServiceImpl implements IAppIndexService {

    @Resource(name = "AppIndexMapper")
    private AppIndexMapper mapper;

    @Resource
    private RemoteFileService remoteFileService;

    @Resource(name = "testTaskExecutor")
    private ThreadPoolTaskExecutor testTaskExecutor;

    @Override
    public List<CarouseVo> carouselList(AppParamsDto dto) {
        dto.setSourceType("5");
        List<CarouseVo> list = new ArrayList<>();
        List<Future> futureList = new ArrayList<>();
        List<CarouseVo> newList = new ArrayList<>();
        try {
            list = mapper.carouselList(dto);
            for (CarouseVo vo : list) {
                Future<CarouseVo> future = testTaskExecutor.submit(new Callable<CarouseVo>() {
                    @Override
                    public CarouseVo call() throws Exception {
                        String imageBase64 = getImageBase64(vo.getFilePath());
                        vo.setBase64Url(imageBase64);
                        return vo;
                    }
                });
                futureList.add(future);
            }
            for (Future<CarouseVo> future : futureList) {
                CarouseVo vo = future.get();
                newList.add(vo);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return newList;
    }

    /**
     * 获取图片的base64
     *
     * @param filePath
     * @return String
     * @author cwchen
     * @date 2024/8/20 9:18
     */
    public String getImageBase64(String filePath) {
        R<SysFile> result = remoteFileService.getImgBase64(filePath, SecurityConstants.INNER);
        if (result != null && result.getCode() == HttpStatus.SUCCESS && result.getData() != null) {
            String jsonString = JSON.toJSONString(result.getData());
            JSONObject item = JSON.parseObject(jsonString);
            String base64 = item.getString("url");
            return base64;
        }
        return null;
    }
}