package com.xhpc.order.service.impl;


import cn.hutool.core.date.DateUtil;
import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.order.domain.XhpcStatisticsStation;
import com.xhpc.order.domain.XhpcStatisticsTimeInterval;
import com.xhpc.order.mapper.XhpcStatisticsServiceMapper;
import com.xhpc.order.service.IXhpcHistoryOrderService;
import com.xhpc.order.service.IXhpcStatisticsService;

import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import java.util.*;

/**
 * @author yuyang
 * @date 2021/8/31 16:22
 * @Version 1.0
 */
@Service
public class XhpcStatisticsServiceImpl implements IXhpcStatisticsService {

    @Autowired
    private XhpcStatisticsServiceMapper xhpcStatisticsServiceMapper;
    @Autowired
    private IXhpcHistoryOrderService xhpcHistoryOrderService;
    @Override
    public List<Map<String, Object>> getTimeIntervalPage(String chargingStationIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type) {
        if (getJudge(internetUserId, operatorId, type)){
            return new ArrayList<>();
        }
        List<String> chargingStationId =new ArrayList<>();
        if(!"".equals(chargingStationIds) && chargingStationIds !=null){
            chargingStationId= Arrays.asList(chargingStationIds.split(","));
        }
        //获取登陆用户
        Long logUserId = SecurityUtils.getUserId();
        List<Map<String, Object>> list =new ArrayList<>();
        if(userId !=1){
            Map<String, Object> landUser = xhpcHistoryOrderService.getLandUser(logUserId);
            if(landUser !=null){
                if(landUser.get("userType") !=null){
                    if("01".equals(landUser.get("userType").toString())){
                        Long logOperatorId = Long.valueOf(landUser.get("operatorId").toString());
                        //运营商看自己的场站
                        list = xhpcStatisticsServiceMapper.getTimeIntervalPage(chargingStationId, internetUserId, operatorId, startTime, endTime, 1,logOperatorId);
                    }else{
                        //查询赋值的场站
                        list = xhpcStatisticsServiceMapper.getTimeIntervalPage(chargingStationId, internetUserId, operatorId, startTime, endTime, 2,logUserId);
                    }
                }
            }
        }else{
            list =xhpcStatisticsServiceMapper.getTimeIntervalPage(chargingStationId, internetUserId, operatorId, startTime, endTime, 0,userId);
        }
        List<Map<String, Object>> page =new ArrayList<>();
        if(list !=null && list.size()>0){
            for (int i = 1; i <=24 ; i++) {
                boolean fan = true;
                for (Map<String, Object> map:list) {
                    int status = (int)map.get("status");
                    if(status==i){
                        page.add(map);
                        fan=false;
                        break;
                    }
                }
                if(fan){
                    if(i==1){
                        page.add(getStatistics("00:00-01:00"));
                    }else if(i==2){
                        page.add(getStatistics("01:00-02:00"));
                    }else if(i==3){
                        page.add(getStatistics("02:00-03:00"));
                    }else if(i==4){
                        page.add(getStatistics("03:00-04:00"));
                    }else if(i==5){
                        page.add(getStatistics("04:00-05:00"));
                    }else if(i==6){
                        page.add(getStatistics("05:00-06:00"));
                    }else if(i==7){
                        page.add(getStatistics("06:00-07:00"));
                    }else if(i==8){
                        page.add(getStatistics("07:00-08:00"));
                    }else if(i==9){
                        page.add(getStatistics("08:00-09:00"));
                    }else if(i==10){
                        page.add(getStatistics("09:00-10:00"));
                    }else if(i==11){
                        page.add(getStatistics("10:00-11:00"));
                    }else if(i==12){
                        page.add(getStatistics("11:00-12:00"));
                    }else if(i==13){
                        page.add(getStatistics("12:00-13:00"));
                    }else if(i==14){
                        page.add(getStatistics("13:00-14:00"));
                    }else if(i==15){
                        page.add(getStatistics("14:00-15:00"));
                    }else if(i==16){
                        page.add(getStatistics("15:00-16:00"));
                    }else if(i==17){
                        page.add(getStatistics("16:00-17:00"));
                    }else if(i==18){
                        page.add(getStatistics("17:00-18:00"));
                    }else if(i==19){
                        page.add(getStatistics("18:00-19:00"));
                    }else if(i==20){
                        page.add(getStatistics("19:00-20:00"));
                    }else if(i==21){
                        page.add(getStatistics("20:00-21:00"));
                    }else if(i==22){
                        page.add(getStatistics("21:00-22:00"));
                    }else if(i==23){
                        page.add(getStatistics("22:00-23:00"));
                    }else if(i==24){
                        page.add(getStatistics("23:00-24:00"));
                    }
                }
            }
        }else{
            page.add(getStatistics("00:00-01:00"));
            page.add(getStatistics("01:00-02:00"));
            page.add(getStatistics("02:00-03:00"));
            page.add(getStatistics("03:00-04:00"));
            page.add(getStatistics("04:00-05:00"));
            page.add(getStatistics("05:00-06:00"));
            page.add(getStatistics("06:00-07:00"));
            page.add(getStatistics("07:00-08:00"));
            page.add(getStatistics("08:00-09:00"));
            page.add(getStatistics("09:00-10:00"));
            page.add(getStatistics("10:00-11:00"));
            page.add(getStatistics("11:00-12:00"));
            page.add(getStatistics("12:00-13:00"));
            page.add(getStatistics("13:00-14:00"));
            page.add(getStatistics("14:00-15:00"));
            page.add(getStatistics("15:00-16:00"));
            page.add(getStatistics("16:00-17:00"));
            page.add(getStatistics("17:00-18:00"));
            page.add(getStatistics("18:00-19:00"));
            page.add(getStatistics("19:00-20:00"));
            page.add(getStatistics("20:00-21:00"));
            page.add(getStatistics("21:00-22:00"));
            page.add(getStatistics("22:00-23:00"));
            page.add(getStatistics("23:00-24:00"));
        }
        return page;
    }
    @Override
    public List<Map<String, Object>> getDateIntervalPage(String chargingStationIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type) {
        if (getJudge(internetUserId, operatorId, type)){
            return new ArrayList<>();
        }
        List<String> chargingStationId =new ArrayList<>();
        if(!"".equals(chargingStationIds) && chargingStationIds !=null){
            chargingStationId= Arrays.asList(chargingStationIds.split(","));
        }

        //获取登陆用户
        Long logUserId = SecurityUtils.getUserId();
        List<Map<String, Object>> list =new ArrayList<>();
        if(userId !=1){
            Map<String, Object> landUser = xhpcHistoryOrderService.getLandUser(logUserId);
            if(landUser !=null){
                if(landUser.get("userType") !=null){
                    if("01".equals(landUser.get("userType").toString())){
                        Long logOperatorId = Long.valueOf(landUser.get("operatorId").toString());
                        //运营商看自己的场站
                        list = xhpcStatisticsServiceMapper.getDateIntervalPage(chargingStationId, internetUserId, operatorId, startTime, endTime, 1,logOperatorId);
                    }else{
                        //查询赋值的场站
                        list = xhpcStatisticsServiceMapper.getDateIntervalPage(chargingStationId, internetUserId, operatorId, startTime, endTime, 2,logUserId);
                    }
                }
            }
        }else{
            list =xhpcStatisticsServiceMapper.getDateIntervalPage(chargingStationId, internetUserId, operatorId, startTime, endTime, 0,userId);
        }

        return list;
    }

    @Override
    public List<Map<String, Object>> getStationIntervalPage(String chargingStationIds, String operatorIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type) {
        if (getJudge(internetUserId, operatorId, type)){
            return new ArrayList<>();
        }
        List<String> chargingStationId =new ArrayList<>();
        if(!"".equals(chargingStationIds) && chargingStationIds !=null){
            chargingStationId= Arrays.asList(chargingStationIds.split(","));
        }
        List<String> operatorUserId =new ArrayList<>();
        if(!"".equals(operatorIds) && operatorIds !=null){
            operatorUserId= Arrays.asList(operatorIds.split(","));
        }

        //获取登陆用户
        Long logUserId = SecurityUtils.getUserId();
        List<Map<String, Object>> list =new ArrayList<>();
        if(userId !=1){
            Map<String, Object> landUser = xhpcHistoryOrderService.getLandUser(logUserId);
            if(landUser !=null){
                if(landUser.get("userType") !=null){
                    if("01".equals(landUser.get("userType").toString())){
                        Long logOperatorId = Long.valueOf(landUser.get("operatorId").toString());
                        //运营商看自己的场站
                        list = xhpcStatisticsServiceMapper.getStationIntervalPage(chargingStationId,operatorUserId,internetUserId,operatorId,startTime,endTime,1,logOperatorId);
                    }else{
                        //查询赋值的场站
                        list = xhpcStatisticsServiceMapper.getStationIntervalPage(chargingStationId,operatorUserId,internetUserId,operatorId,startTime,endTime,2,logUserId);
                    }
                }
            }
        }else{
            list =xhpcStatisticsServiceMapper.getStationIntervalPage(chargingStationId,operatorUserId,internetUserId,operatorId,startTime,endTime,0,userId);
        }


        return list;
    }

    @Override
    public List<Map<String, Object>> getOperatorIntervalPage(String operatorIds, Long operatorId, String startTime, String endTime, Long userId, Integer type) {
        if(getJudge(null,operatorId,type)){
            return new ArrayList<>();
        }
        List<String> operatorUserId =new ArrayList<>();
        if(!"".equals(operatorIds) && operatorIds !=null){
            operatorUserId= Arrays.asList(operatorIds.split(","));
        }

        //获取登陆用户
        Long logUserId = SecurityUtils.getUserId();
        List<Map<String, Object>> list =new ArrayList<>();
        if(userId !=1){
            Map<String, Object> landUser = xhpcHistoryOrderService.getLandUser(logUserId);
            if(landUser !=null){
                if(landUser.get("userType") !=null){
                    if("01".equals(landUser.get("userType").toString())){
                        Long logOperatorId = Long.valueOf(landUser.get("operatorId").toString());
                        //运营商看自己的场站
                        list = xhpcStatisticsServiceMapper.getOperatorIntervalPage(operatorUserId, operatorId, startTime, endTime, 1,logOperatorId);
                    }else{
                        //查询赋值的场站
                        list = xhpcStatisticsServiceMapper.getOperatorIntervalPage(operatorUserId, operatorId, startTime, endTime, 2,logUserId);
                    }
                }
            }
        }else{
            list =xhpcStatisticsServiceMapper.getOperatorIntervalPage(operatorUserId, operatorId, startTime, endTime, 0,userId);
        }

        return list;
    }

    @Override
    public List<Map<String, Object>> getInternetUserIntervalPage(String internetUserIds, Long internetUserId, String startTime, String endTime, Long userId, Integer type) {
        if(getJudge(internetUserId,null,type)){
            return new ArrayList<>();
        }
        List<String> internetIds =new ArrayList<>();
        if(!"".equals(internetUserIds) && internetUserIds !=null){
            internetIds= Arrays.asList(internetUserIds.split(","));
        }
//        if(("".equals(startTime) && "".equals(endTime)) ||(startTime==null && endTime==null)){
//            startTime =date(1);
//            endTime =date(2);
//        }
        return xhpcStatisticsServiceMapper.getInternetUserIntervalPage(internetIds, internetUserId, startTime, endTime, type,userId);
    }

    @Override
    public List<Map<String, Object>> getTerminalIntervalPage(String chargingStationId, String terminalId, Long operatorId, String startTime, String endTime, Long userId, Integer type) {
        if(getJudge(null,operatorId,type)){
            return new ArrayList<>();
        }

        //获取登陆用户
        Long logUserId = SecurityUtils.getUserId();
        List<Map<String, Object>> list =new ArrayList<>();
        if(userId !=1){
            Map<String, Object> landUser = xhpcHistoryOrderService.getLandUser(logUserId);
            if(landUser !=null){
                if(landUser.get("userType") !=null){
                    if("01".equals(landUser.get("userType").toString())){
                        Long logOperatorId = Long.valueOf(landUser.get("operatorId").toString());
                        //运营商看自己的场站
                        list = xhpcStatisticsServiceMapper.getTerminalIntervalPage(chargingStationId, terminalId, operatorId, startTime, endTime, type,logOperatorId);
                    }else{
                        //查询赋值的场站
                        list = xhpcStatisticsServiceMapper.getTerminalIntervalPage(chargingStationId, terminalId, operatorId, startTime, endTime, type,logUserId);
                    }
                }
            }
        }else{
            list =xhpcStatisticsServiceMapper.getTerminalIntervalPage(chargingStationId, terminalId, operatorId, startTime, endTime, type,userId);
        }


        return list;
    }

    @Override
    public AjaxResult getChargingStationList(Long operatorId, Long userId, Integer type) {

        //获取登陆用户
        Long logUserId = SecurityUtils.getUserId();
        if(userId !=1){
            Map<String, Object> landUser = xhpcHistoryOrderService.getLandUser(logUserId);
            if(landUser !=null){
                if(landUser.get("userType") !=null){
                    if("01".equals(landUser.get("userType").toString())){
                        Long logOperatorId = Long.valueOf(landUser.get("operatorId").toString());
                        //运营商看自己的场站
                        return AjaxResult.success(xhpcStatisticsServiceMapper.getChargingStationList(operatorId, logOperatorId, 1));
                    }else{
                        //查询赋值的场站
                        return AjaxResult.success(xhpcStatisticsServiceMapper.getChargingStationList(operatorId, logUserId, 2));
                    }
                }
            }
        }else{
            return AjaxResult.success(xhpcStatisticsServiceMapper.getChargingStationList(operatorId, userId, 0));
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getOperatorList(Long operatorId, Long userId, Integer type) {
        //获取登陆用户
        Long logUserId = SecurityUtils.getUserId();
        if(userId !=1){
            Map<String, Object> landUser = xhpcHistoryOrderService.getLandUser(logUserId);
            if(landUser !=null){
                if(landUser.get("userType") !=null){
                    if("01".equals(landUser.get("userType").toString())){
                        Long logOperatorId = Long.valueOf(landUser.get("operatorId").toString());
                        //运营商看自己的场站
                        return AjaxResult.success(xhpcStatisticsServiceMapper.getOperatorList(operatorId, logOperatorId, 1));
                    }else{
                        //查询赋值的场站
                        return AjaxResult.success(xhpcStatisticsServiceMapper.getOperatorList(operatorId, logUserId, 2));
                    }
                }
            }
        }else{
            AjaxResult.success(xhpcStatisticsServiceMapper.getOperatorList(operatorId, userId, 0));
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getInternetUserIList(Long internetUserId, Long userId, Integer type) {
        return AjaxResult.success(xhpcStatisticsServiceMapper.getInternetUserIList(internetUserId, userId, type));
    }

    @Override
    public AjaxResult getTerminalList(Long chargingStationId, Long userId, Integer type) {

        //获取登陆用户
        Long logUserId = SecurityUtils.getUserId();
        if(userId !=1){
            Map<String, Object> landUser = xhpcHistoryOrderService.getLandUser(logUserId);
            if(landUser !=null){
                if(landUser.get("userType") !=null){
                    if("01".equals(landUser.get("userType").toString())){
                        Long logOperatorId = Long.valueOf(landUser.get("operatorId").toString());
                        //运营商看自己的场站
                        return AjaxResult.success(xhpcStatisticsServiceMapper.getTerminalList(chargingStationId, logOperatorId, 1));
                    }else{
                        //查询赋值的场站
                        return AjaxResult.success(xhpcStatisticsServiceMapper.getTerminalList(chargingStationId, logUserId, 2));
                    }
                }
            }
        }else{
            return AjaxResult.success(xhpcStatisticsServiceMapper.getTerminalList(chargingStationId, userId, 0));
        }



        return AjaxResult.success();
    }

    @Override
    public void addStatisticsTime(XhpcStatisticsTimeInterval xhpcStatisticsTimeInterval) {
        xhpcStatisticsServiceMapper.addStatisticsTime(xhpcStatisticsTimeInterval);
    }

    @Override
    public void addStatisticsStation(XhpcStatisticsStation xhpcStatisticsStation) {
        xhpcStatisticsServiceMapper.addStatisticsStation(xhpcStatisticsStation);
    }

    private boolean getJudge(Long internetUserId, Long operatorId, Integer type) {

        if (type == null) {
            return true;
        }
        if (type == 2) {
            if (operatorId == null) {
                return true;
            }
        } else if (type == 3) {
            if (internetUserId == null) {
                return true;
            }
        } else if (type != 1) {
            return true;
        }
        return false;
    }
    private Map<String, Object> getStatistics(String time){
        Map<String, Object> map =new HashMap<>();
        map.put("chargingDegree",0.00);
        map.put("chargingTime",0.00);
        map.put("chargingNumber",0);
        map.put("powerPrice",0.00);
        map.put("servicePrice",0.00);
        map.put("totalPrice",0.00);
        map.put("promotionDiscount",0.00);
        map.put("actPrice",0.00);
        map.put("actPowerPrice",0.00);
        map.put("actServicePrice",0.00);
        map.put("internetCommission",0.00);
        map.put("internetSvcCommission",0.00);
        map.put("platformCommission",0.00);
        map.put("platformSvcCommisssion",0.00);
        map.put("operationCommission",0.00);
        map.put("operationSvcCommission",0.00);
        map.put("time",time);
        return map;
    }

    /**
     * 获取时间
     * type 1 开始时间  2结束时间
     */

    private String date(Integer type){
        if(type ==1){
            //一天的开始，结果：2017-03-01 00:00:00
            Date beginOfDay = DateUtil.beginOfDay(new Date());
            return beginOfDay.toString();
        }
        //一天的结束，结果：2017-03-01 23:59:59
        Date endOfDay = DateUtil.endOfDay(new Date());

        return endOfDay.toString();
    }

//    public static void main(String[] args) {
//        Date beginOfDay = DateUtil.beginOfDay(new Date());
//
//        System.out.println(">>>>>>>>>"+beginOfDay.toString());
//    }

    @Override
    public void exportByDay(ServletOutputStream out, String[] titles, String[] keys, List<Map<String, Object>> data) throws Exception {

        export(out, titles, keys, data);
    }

    @Override
    public void exportByDate(ServletOutputStream out, String[] titles, String[] keys, List<Map<String, Object>> data) throws Exception {
        export(out, titles, keys, data);
    }

    @Override
    public void export(ServletOutputStream out, String[] titles, String[] keys, List<Map<String, Object>> data) throws Exception {

//        try {
//            //Making the head.
//            HSSFWorkbook workbook = new HSSFWorkbook();
//            HSSFSheet hssfSheet = workbook.createSheet("sheet1");
//            HSSFRow row = hssfSheet.createRow(0);
//            HSSFCellStyle hssfCellStyle = workbook.createCellStyle();
//            hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//            hssfCellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
//            hssfCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//            HSSFCell hssfCell;
//            for (int i = 0; i < titles.length; i++) {
//                hssfCell = row.createCell(i);
//                hssfCell.setCellValue(titles[i]);
//                hssfCell.setCellStyle(hssfCellStyle);
//                hssfSheet.setColumnWidth(i, hssfCell.getStringCellValue().getBytes().length * 256 + 200);
//            }
//
//            //Loading data.
//            HSSFRow dataRow;
//            HSSFCell dataCell;
//            int valuesLength = keys.length;
//            for (int i = 1; i <= data.size(); i++) {
//                Map<String, Object> map = data.get(i - 1);
//                if (null != map) {
//                    dataRow = hssfSheet.createRow(i);
//                    for (int j = 0; j < valuesLength; j++) {
//                        if (null != map.get(keys[j])) {
//                            dataCell = dataRow.createCell(j);
//                            String s = String.valueOf(map.get(keys[j]));
//                            dataCell.setCellValue(s);
//                        }
//                    }
//                }
//            }
//            // Exporting to the client.
//            try {
//                workbook.write(out);
//                out.flush();
//                out.close();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new Exception("导出信息失败！");
//        }
    }


}
