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.XhpcWorkHistoryOrderMapper;
import com.xhpc.activity.service.XhpcWorkHistoryOrderService;
import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.web.service.BaseService;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author yuyang
 * @date 2022/8/5 9:23
 */
@Service
public class XhpcWorkHistoryOrderServiceImpl extends BaseService implements XhpcWorkHistoryOrderService {

    @Resource
    XhpcWorkHistoryOrderMapper xhpcWorkHistoryOrderMapper;
    @Resource
    private TokenService tokenService;

    @Override
    public List<Map<String, Object>> getListPage(Map<String, Object> params) {


        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        //params.put("tenantId", loginUser.getTenantId());
        //桩的统计、该时段金额
        if (UserTypeUtil.USER_ID !=sysUserId) {
            if (UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())) {
                params.put("status", 1);
                //运营商看自己的场站
            } else {
                params.put("status", 2);
                //查询赋值的场站
            }
        } else {
            params.put("status", 0);
        }
        startPage();
        return xhpcWorkHistoryOrderMapper.getListPage(params);

    }

    @Override
    public void export(HttpServletResponse response, Map<String, Object> params) throws IOException {
        LoginUser loginUser = tokenService.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        //params.put("tenantId", loginUser.getTenantId());
        //桩的统计、该时段金额
        if(UserTypeUtil.SYS_USER_TYPE_FOUR.equals(sysUser.getUserType())){
            //运维管理人员
            params.put("status", 3);
            params.put("userId",sysUserId);
        }else if (UserTypeUtil.USER_ID !=sysUserId) {
            if (UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())) {
                Long logOperatorId = sysUser.getOperatorId();
                params.put("status", 1);
                params.put("userId",logOperatorId);
                //运营商看自己的场站
            } else {
                params.put("status", 2);
                params.put("userId",sysUserId);
                //查询赋值的场站
            }
        } else {
            params.put("status", 0);
        }
        List<Map<String, Object>> list= xhpcWorkHistoryOrderMapper.getListPage(params);

        BigExcelWriter writer = ExcelUtil.getBigWriter("HistoryOrder_" + System.currentTimeMillis() + ".xlsx");
        writer.addHeaderAlias("historyOrderId", "历史订单ID");
        writer.addHeaderAlias("serialNumber", "订单号");
        writer.addHeaderAlias("internetSerialNumber", "流量方订单号");
        writer.addHeaderAlias("chargingStationName", "电站名称");
        writer.addHeaderAlias("pileSerialNumber", "桩编号");
        writer.addHeaderAlias("terminalName", "终端名称");
        writer.addHeaderAlias("operatorId", "运营商ID");
        writer.addHeaderAlias("operatorName", "运营商名称");
        writer.addHeaderAlias("power", "功率");
        writer.addHeaderAlias("type", "桩类型(1直流（慢） 2交流（快）");
        writer.addHeaderAlias("orderType", "状态1 自动结算 2 平台结算");
        writer.addHeaderAlias("startSoc", "开始soc");
        writer.addHeaderAlias("endSoc", "结束soc");
        writer.addHeaderAlias("powerPriceTotal", "总电费");
        writer.addHeaderAlias("servicePriceTotal", "总服务费");
        writer.addHeaderAlias("totalPrice", "订单总价");
        writer.addHeaderAlias("activityPowerPriceTotal", "活动总电费");
        writer.addHeaderAlias("activityServicePriceTotal", "活动总服务费");
        writer.addHeaderAlias("activityTotalPrice", "活动总金额");
        writer.addHeaderAlias("promotionDiscount", "电站活动抵扣总金额");
        writer.addHeaderAlias("actPrice", "实际用户支付金额");
        writer.addHeaderAlias("internetCommission", "流量方总金额抽成");
        writer.addHeaderAlias("internetSvcCommission", "流量方服务费抽成");
        writer.addHeaderAlias("internetDegreeCommission", "流量方电量抽成");
        writer.addHeaderAlias("platformCommission", "平台总金额抽成");
        writer.addHeaderAlias("platformSvcCommisssion", "平台服务费抽成");
        writer.addHeaderAlias("operationCommission", "运维总抽成");
        writer.addHeaderAlias("operationSvcCommission", "运维服务费抽成");
        writer.addHeaderAlias("actPowerPrice", "运营商电费");
        writer.addHeaderAlias("actServicePrice", "运营商服务费");
        writer.addHeaderAlias("actSumPrice", "运营商总金额");
//        writer.addHeaderAlias("internetSerialNumber", "电站名称");
        writer.addHeaderAlias("totalPrice", "订单总价");
        writer.addHeaderAlias("actPrice", "实际价格");
//        writer.addHeaderAlias("confirmResult", "历史订单ID");
        writer.addHeaderAlias("chargingTime", "累计充电时间");
        writer.addHeaderAlias("chargingDegree", "充电度数");
        writer.addHeaderAlias("startTime", "开始充电时间");
        writer.addHeaderAlias("endTime", "结束充电时间");
        writer.addHeaderAlias("updateTime", "结算时间");
        writer.addHeaderAlias("chargingModeName", "订单来源");
        writer.addHeaderAlias("stopReasonEvcs", "停止原因");
        writer.addHeaderAlias("sourceName", "用户类型");

//        writer.addHeaderAlias("plateNum", "电站名称");
//        writer.addHeaderAlias("disputeOrderStatus", "桩编号");

        // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
        writer.setOnlyAlias(true);
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(list, true);
        //out为OutputStream，需要写出到的目标流
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
//        //response为HttpServletResponse对象
//        response.setContentType("application/vnd.ms-excel;charset=utf-8");
//        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
//        response.setHeader("Content-Disposition", "attachment;filename=历史订单列表.xls");
        ServletOutputStream out = response.getOutputStream();

        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
        //此处记得关闭输出Servlet流
        IoUtil.close(out);

    }

}
