
package com.yami.shop.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.PointsFailureRecord;
import com.yami.shop.bean.vo.PointsFailureRecordVO;
import com.yami.shop.bean.vo.RecordExportVO;
import com.yami.shop.dao.PointsFailureRecordMapper;
import com.yami.shop.service.PointsFailureRecordService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 积分充值失败记录 Service实现类
 *
 * @author fbj
 * @since 2025-09-XX
 */
@Service
@AllArgsConstructor
public class PointsFailureRecordServiceImpl extends ServiceImpl<PointsFailureRecordMapper, PointsFailureRecord> implements PointsFailureRecordService {

    private final PointsFailureRecordMapper pointsFailureRecordMapper;

    @Override
    public void exportPointsRecharge(HttpServletResponse response, PointsFailureRecord pointsFailureRecord) {
        // 查询数据

            List<PointsFailureRecord> dataList = pointsFailureRecordMapper.selectList(new LambdaQueryWrapper<PointsFailureRecord>()
                    .eq(PointsFailureRecord::getRechargeStatus, 0)
                    .eq(ObjectUtils.isNotEmpty(pointsFailureRecord.getCode()), PointsFailureRecord::getCode, pointsFailureRecord.getCode())
                    .last("LIMIT 1000")
            );
            // 转换为导出VO
            List<RecordExportVO> exportData = new ArrayList<>();
            for (PointsFailureRecord record : dataList) {
                RecordExportVO exportVO = new RecordExportVO();
                exportVO.setPoints(record.getPoints());
                exportVO.setChannelName(record.getChannelName());
                exportVO.setUserName(record.getUserName());
                exportVO.setUserPhone(record.getUserPhone());
                exportVO.setExpiryDate(record.getExpiryDate());
                exportVO.setReasonForFailure(record.getReasonForFailure());
                exportData.add(exportVO);
            }
        try {
            // 设置响应头
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("积分导入失败数据导出", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 写入Excel
            EasyExcel.write(response.getOutputStream(), RecordExportVO.class)
                    .sheet("积分导入失败数据")
                    .doWrite(exportData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<PointsFailureRecordVO> groupByCode(PointsFailureRecord pointsFailureRecord) {
        List<PointsFailureRecordVO> list = pointsFailureRecordMapper.groupByCode(pointsFailureRecord);
        return list;
    }
}
