package com.huiju.estateagents.redpack.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.excel.redPacket.RedPacketRankingListExport;
import com.huiju.estateagents.redpack.entity.TaRedPacketPerson;
import com.huiju.estateagents.redpack.mapper.TaRedPacketPersonMapper;
import com.huiju.estateagents.redpack.service.ITaRedPacketPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 红包助力报名表  服务实现类
 * </p>
 *
 * @author fxf
 * @since 2020-06-03
 */
@Service
public class TaRedPacketPersonServiceImpl extends ServiceImpl<TaRedPacketPersonMapper, TaRedPacketPerson> implements ITaRedPacketPersonService {

    @Autowired
    private TaRedPacketPersonMapper taRedPacketPersonMapper;

    @Override
    public ResponseBean getRedPacketRankingList(Integer pageNum, Integer pageSize, String activityId, String phone, Integer orgId) {
        ResponseBean responseBean = new ResponseBean();
        IPage<TaRedPacketPerson> pg = new Page<>(pageNum, pageSize);
        responseBean.addSuccess(taRedPacketPersonMapper.getRedPacketRankingList(pg, activityId, orgId, phone));
        return responseBean;
    }

    @Override
    public void exportRedPacketRankingList(String activityId, String phone, Integer orgId, HttpServletResponse response) throws Exception{
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        List<RedPacketRankingListExport> records = taRedPacketPersonMapper.exportRedPacketRankingList(activityId,orgId,phone);
        String fileName = URLEncoder.encode("排行榜", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), RedPacketRankingListExport.class).sheet("排行榜").doWrite(records);
    }


}
