package com.xhpc.activity.service.impl;


import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import com.xhpc.activity.mapper.XhpcDeviceMessageMapper;
import com.xhpc.activity.mapper.XhpcHistoryOrderMapper;
import com.xhpc.activity.service.OrderLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Service
public class OrderLogServiceImpl implements OrderLogService {

    @Resource
    XhpcHistoryOrderMapper historyOrderMapper;


    @Resource
    XhpcDeviceMessageMapper deviceMessageMapper;


    @Override
    public List<Map<String, Object>> getOrderPage(Map<String, Object> params){
        return historyOrderMapper.getOrderPage(params);
    }


    @Override
    public List<Map<String, Object>> getOrderDetailPage(String orderId){
        return deviceMessageMapper.selectListByChargeOrderNo(orderId);
    }


    @Override
    public void export(HttpServletResponse response, Map<String, Object> params) throws Exception{
        List<Map<String, Object>> list = deviceMessageMapper.getOrderMessageByParams(params);

        BigExcelWriter writer = ExcelUtil.getBigWriter("temp_" + System.currentTimeMillis() + ".xlsx");
        writer.addHeaderAlias("serialNumber", "订单号");
        writer.addHeaderAlias("sourceName", "订单来源");
//        writer.addHeaderAlias("chargingDegree", "充电电量");
//        writer.addHeaderAlias("amountCharged", "总金额");
//        writer.addHeaderAlias("chargingTime", "充电时长");
//        writer.addHeaderAlias("startSOc", "充电启始soc");
//        writer.addHeaderAlias("endSoc", "结束soc");
//        writer.addHeaderAlias("startTime", "充电开始时间");
//        writer.addHeaderAlias("endTime", "充电结束时间");
//        writer.addHeaderAlias("power", "电桩功率(KW)");
        writer.addHeaderAlias("content", "充电报文");
        writer.addHeaderAlias("remark", "备注描述");
        writer.addHeaderAlias("createTime", "发送时间");

        writer.setOnlyAlias(true);
        writer.write(list, true);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        ServletOutputStream out = response.getOutputStream();

        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
    }
}
