package com.xhpc.activity.service.impl;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.xhpc.common.core.web.service.BaseService;
import com.xhpc.common.enums.StationDeviceEnum;
import com.xhpc.activity.mapper.XhpcChargingPileMapper;
import com.xhpc.activity.mapper.XhpcDeviceMessageMapper;
import com.xhpc.activity.service.PileLogService;
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.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class PileLogServiceImpl extends BaseService implements PileLogService {

    @Resource
    XhpcChargingPileMapper pileMapper;

    @Resource
    XhpcDeviceMessageMapper deviceMessageMapper;

    @Resource
    private TokenService tokenService;
    @Override
    public List<Map<String, Object>> getPilePage(Map<String, Object> params){
        LoginUser loginUser = tokenService.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        Long userId = loginUser.getUserid();
        //桩的统计、该时段金额
        List<Map<String, Object>> list = new ArrayList<>();

        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            if(UserTypeUtil.SYS_SUPER_TYPE_ONE == loginUser.getSuperType()){
                params.put("tenantId",loginUser.getTenantId());
                return  pileMapper.selectXhpcChargingPileList(params);
            }
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                Long logOperatorId = sysUser.getOperatorId();
                params.put("logOperatorId",logOperatorId);
                params.put("status",1);
                //运营商看自己的场站
                list = pileMapper.selectXhpcChargingPileList(params);
            }else{
                params.put("status",2);
                params.put("logOperatorId",userId);
                //查询赋值的场站
                list = pileMapper.selectXhpcChargingPileList(params);
            }
        }else{
            startPage();
            params.put("status", 0);
            list = pileMapper.selectXhpcChargingPileList(params);
        }

        return list;

    }


    @Override
    public List<Map<String, Object>> getPileRunLogPage(String pileId, String startTime, String endTime){
        return deviceMessageMapper.selectListByTypeAndSerialNumber( pileId, startTime, endTime);
    }

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

        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.addHeaderAlias("chargingStationName", "场站名称");
        writer.addHeaderAlias("chargingPileName", "充电桩名称");
        writer.addHeaderAlias("serialNumber", "桩编码");
        writer.addHeaderAlias("brandModel", "规则型号");
        writer.addHeaderAlias("pileType", "电桩类型");
        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);
    }
}
