package com.ruoyi.ibs.custmap.service;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.ibs.custmap.domain.dto.CustMapCircleExcelDTO;
import com.ruoyi.ibs.custmap.domain.dto.CustMapExcelDTO;
import com.ruoyi.ibs.custmap.domain.dto.CustMapPolygonExcelDTO;
import com.ruoyi.ibs.custmap.domain.entity.CustInfoRadar;
import com.ruoyi.ibs.custmap.domain.vo.CustMapExcelVO;
import com.ruoyi.ibs.custmap.mapper.CustMapMapper;
import com.ruoyi.ibs.dashboard.service.FileOptService;
import com.ruoyi.ibs.dashboard.service.NotificationDownloadService;
import com.ruoyi.ibs.task.domain.entity.ImportExportTask;
import com.ruoyi.ibs.task.mapper.ImportExportTaskMapper;
import com.ruoyi.ibs.task.service.ImportExportTaskService;
import com.ruoyi.system.enums.OssFileEnum;
import com.ruoyi.system.service.OssFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @Author 吴凯程
 * @Date 2025/5/15
 **/
@Service
@Slf4j
@EnableAsync
public class CustMapExportService {

    private final String CUST_MAP_EXPORT_REQUEST = "CUST_MAP_EXPORT_REQUEST_";
    @Resource
    private CustMapMapper custMapMapper;
    @Resource
    private RedisCache redisCache;
    @Resource
    private NotificationDownloadService notificationDownloadService;
    @Resource
    private FileOptService fileOptService;
    @Resource
    private ImportExportTaskService importExportTaskService;
    @Resource
    private ImportExportTaskMapper importExportTaskMapper;


    public Boolean checkCustMapExportRequest() {
        return redisCache.hasKey(CUST_MAP_EXPORT_REQUEST + SecurityUtils.getUsername());
    }

    @Async
    public void exportCustMapList(CustMapExcelDTO custMapExcelDTO) {
        // 使用redis控制每个用户不可同时多次执行导出
        if (redisCache.hasKey(CUST_MAP_EXPORT_REQUEST + custMapExcelDTO.getUserName())) {
            throw new ServiceException("请勿重复导出");
        }
        redisCache.setCacheObject(CUST_MAP_EXPORT_REQUEST + custMapExcelDTO.getUserName(), custMapExcelDTO, 15, TimeUnit.MINUTES);
        String fileNotification = notificationDownloadService.createFileNotification(custMapExcelDTO.getUserName());
        int pageNum = 1;
        int pageSize = Math.min(10000, custMapExcelDTO.getTotal());
        int pageCount = (int) Math.ceil(custMapExcelDTO.getTotal() / pageSize);
        List<CustMapExcelVO> custMapExcelVOS = new ArrayList<>();
        do {
            PageHelper.startPage(pageNum, pageSize);
            List<CustMapExcelVO> custMapVOS = new ArrayList<>();
            if(custMapExcelDTO instanceof CustMapCircleExcelDTO){
                custMapVOS = custMapMapper.exportCustMapListByCircle((CustMapCircleExcelDTO) custMapExcelDTO);
            }else{
                custMapVOS = custMapMapper.exportCustMapListByPolygon((CustMapPolygonExcelDTO) custMapExcelDTO);
            }
            custMapExcelVOS.addAll(custMapVOS);
            pageNum++;
        } while (pageNum <= pageCount);
//        String filePath = fileOptService.saveFile("地图拓客", custMapExcelVOS, CustMapExcelVO.class);
        String filePath = importExportTaskService.exportExcelAndUpload(custMapExcelVOS, CustMapExcelVO.class, "地图拓客", OssFileEnum.CUST_MAP_EXPORT, custMapExcelDTO.getUserName());
        notificationDownloadService.sendFileNotification(fileNotification, custMapExcelDTO.getUserName(), filePath);
        redisCache.deleteObject(CUST_MAP_EXPORT_REQUEST + custMapExcelDTO.getUserName());
    }

    public List<CustMapExcelVO> exportCustMapList(List<Integer> ids) {
        List<CustInfoRadar> custInfoRadars = custMapMapper.selectBatchIds(ids);
        return custInfoRadars.stream().map(custInfoRadar -> {
            CustMapExcelVO custMapExcelVO = new CustMapExcelVO();
            BeanUtils.copyProperties(custInfoRadar, custMapExcelVO);
            return custMapExcelVO;
        }).collect(Collectors.toList());
    }
}
